listSSHKeyPairs: list by account/domainid. Also added account/domainId info to sshKeyPair response

This commit is contained in:
Alena Prokharchyk 2012-04-16 14:19:10 -07:00
parent 0b45544e49
commit 8e715856df
6 changed files with 72 additions and 16 deletions

View File

@ -46,6 +46,7 @@ import com.cloud.api.response.PodResponse;
import com.cloud.api.response.RemoteAccessVpnResponse;
import com.cloud.api.response.ResourceCountResponse;
import com.cloud.api.response.ResourceLimitResponse;
import com.cloud.api.response.SSHKeyPairResponse;
import com.cloud.api.response.SecurityGroupResponse;
import com.cloud.api.response.ServiceOfferingResponse;
import com.cloud.api.response.SnapshotPolicyResponse;
@ -94,6 +95,7 @@ import com.cloud.storage.Volume;
import com.cloud.storage.snapshot.SnapshotPolicy;
import com.cloud.template.VirtualMachineTemplate;
import com.cloud.user.Account;
import com.cloud.user.SSHKeyPair;
import com.cloud.user.User;
import com.cloud.user.UserAccount;
import com.cloud.uservm.UserVm;
@ -216,4 +218,10 @@ public interface ResponseGenerator {
@Deprecated // This method is only a temporary solution. Do not use.
HostResponse createHostResponseTemporary(Host host, int details);
/**
* @param result
* @return
*/
SSHKeyPairResponse createSshKeyPairResponse(SSHKeyPair result);
}

View File

@ -46,6 +46,14 @@ public class ListSSHKeyPairsCmd extends BaseListCmd {
@Parameter(name="fingerprint", type=CommandType.STRING, description="A public key fingerprint to look for")
private String fingerprint;
@Parameter(name=ApiConstants.ACCOUNT, type=CommandType.STRING, description="List ssh keys for specific account. " +
"Must be used with the domainId parameter.")
private String accountName;
@Parameter(name=ApiConstants.DOMAIN_ID, type=CommandType.LONG, description="List ssh keys for specific domain." +
" If used with the account parameter, lists ssh keys for the specified account in this domain.")
private Long domainId;
/////////////////////////////////////////////////////
@ -59,6 +67,14 @@ public class ListSSHKeyPairsCmd extends BaseListCmd {
public String getFingerprint() {
return fingerprint;
}
public String getAccountName() {
return accountName;
}
public Long getDomainId() {
return domainId;
}
/////////////////////////////////////////////////////
@ -70,8 +86,7 @@ public class ListSSHKeyPairsCmd extends BaseListCmd {
List<? extends SSHKeyPair> resultList = _mgr.listSSHKeyPairs(this);
List<SSHKeyPairResponse> responses = new ArrayList<SSHKeyPairResponse>();
for (SSHKeyPair result : resultList) {
SSHKeyPairResponse r = new SSHKeyPairResponse(result.getName(), result.getFingerprint());
r.setObjectName("sshkeypair");
SSHKeyPairResponse r = _responseGenerator.createSshKeyPairResponse(result);
responses.add(r);
}

View File

@ -24,7 +24,6 @@ import com.cloud.api.ApiConstants;
import com.cloud.api.BaseCmd;
import com.cloud.api.Implementation;
import com.cloud.api.Parameter;
import com.cloud.api.BaseCmd.CommandType;
import com.cloud.api.response.SSHKeyPairResponse;
import com.cloud.user.Account;
import com.cloud.user.SSHKeyPair;
@ -91,7 +90,7 @@ public class RegisterSSHKeyPairCmd extends BaseCmd {
@Override
public void execute() {
SSHKeyPair result = _mgr.registerSSHKeyPair(this);
SSHKeyPairResponse response = new SSHKeyPairResponse(result.getName(), result.getFingerprint());
SSHKeyPairResponse response = _responseGenerator.createSshKeyPairResponse(result);
response.setResponseName(getCommandName());
response.setObjectName("keypair");
this.setResponseObject(response);

View File

@ -32,6 +32,12 @@ public class SSHKeyPairResponse extends BaseResponse {
@SerializedName("privatekey") @Param(description="Private key")
private String privateKey;
@SerializedName(ApiConstants.ACCOUNT) @Param(description="the account associated with the ssh key")
private String accountName;
@SerializedName(ApiConstants.DOMAIN_ID) @Param(description="the ID of the domain in which ssh key exists")
private Long domainId;
public SSHKeyPairResponse() {}
@ -45,28 +51,25 @@ public class SSHKeyPairResponse extends BaseResponse {
this.privateKey = privateKey;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFingerprint() {
return fingerprint;
}
public void setFingerprint(String fingerprint) {
this.fingerprint = fingerprint;
}
public String getPrivateKey() {
return privateKey;
}
public void setPrivateKey(String privateKey) {
this.privateKey = privateKey;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public void setDomainId(Long domainId) {
this.domainId = domainId;
}
}

View File

@ -62,6 +62,7 @@ import com.cloud.api.response.PodResponse;
import com.cloud.api.response.RemoteAccessVpnResponse;
import com.cloud.api.response.ResourceCountResponse;
import com.cloud.api.response.ResourceLimitResponse;
import com.cloud.api.response.SSHKeyPairResponse;
import com.cloud.api.response.SecurityGroupResponse;
import com.cloud.api.response.SecurityGroupResultObject;
import com.cloud.api.response.ServiceOfferingResponse;
@ -144,6 +145,7 @@ import com.cloud.storage.snapshot.SnapshotPolicy;
import com.cloud.template.VirtualMachineTemplate;
import com.cloud.test.PodZoneConfig;
import com.cloud.user.Account;
import com.cloud.user.SSHKeyPair;
import com.cloud.user.User;
import com.cloud.user.UserAccount;
import com.cloud.user.UserContext;
@ -2448,5 +2450,19 @@ public class ApiResponseHelper implements ResponseGenerator {
vmResponse.setObjectName("systemvminstance");
return vmResponse;
}
@Override
public SSHKeyPairResponse createSshKeyPairResponse(SSHKeyPair result) {
SSHKeyPairResponse r = new SSHKeyPairResponse(result.getName(), result.getFingerprint());
r.setDomainId(result.getDomainId());
//set account information
long accountId = result.getAccountId();
Account account = ApiDBUtils.findAccountById(accountId);
if (account != null) {
r.setAccountName(account.getAccountName());
}
r.setObjectName("sshkeypair");
return r;
}
}

View File

@ -4667,8 +4667,23 @@ public class ManagementServerImpl implements ManagementServer {
String name = cmd.getName();
String fingerPrint = cmd.getFingerprint();
Long accountId = null;
Long domainId = null;
Long domainId = cmd.getDomainId();
String accountName = cmd.getAccountName();
String path = null;
if (domainId != null) {
if (_domainDao.findById(domainId) == null) {
throw new InvalidParameterValueException("Unable to find domain id=" + domainId);
}
if (accountName != null) {
Account account= _accountDao.findAccount(accountName, domainId);
if (account == null) {
throw new InvalidParameterValueException("Unable to find domain id=" + domainId);
}
accountId = account.getId();
}
}
if (caller.getType() == Account.ACCOUNT_TYPE_NORMAL) {
accountId = caller.getId();