diff --git a/api/src/com/cloud/api/ResponseGenerator.java b/api/src/com/cloud/api/ResponseGenerator.java index 4691e5ff67c..576dab46d82 100755 --- a/api/src/com/cloud/api/ResponseGenerator.java +++ b/api/src/com/cloud/api/ResponseGenerator.java @@ -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); + } diff --git a/api/src/com/cloud/api/commands/ListSSHKeyPairsCmd.java b/api/src/com/cloud/api/commands/ListSSHKeyPairsCmd.java index 0723a9e2e0e..5789df17efd 100644 --- a/api/src/com/cloud/api/commands/ListSSHKeyPairsCmd.java +++ b/api/src/com/cloud/api/commands/ListSSHKeyPairsCmd.java @@ -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 resultList = _mgr.listSSHKeyPairs(this); List responses = new ArrayList(); for (SSHKeyPair result : resultList) { - SSHKeyPairResponse r = new SSHKeyPairResponse(result.getName(), result.getFingerprint()); - r.setObjectName("sshkeypair"); + SSHKeyPairResponse r = _responseGenerator.createSshKeyPairResponse(result); responses.add(r); } diff --git a/api/src/com/cloud/api/commands/RegisterSSHKeyPairCmd.java b/api/src/com/cloud/api/commands/RegisterSSHKeyPairCmd.java index 15aa96f2f5b..fbd96d4dc2c 100644 --- a/api/src/com/cloud/api/commands/RegisterSSHKeyPairCmd.java +++ b/api/src/com/cloud/api/commands/RegisterSSHKeyPairCmd.java @@ -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); diff --git a/api/src/com/cloud/api/response/SSHKeyPairResponse.java b/api/src/com/cloud/api/response/SSHKeyPairResponse.java index 0134356d7b5..7121ae6e6f6 100644 --- a/api/src/com/cloud/api/response/SSHKeyPairResponse.java +++ b/api/src/com/cloud/api/response/SSHKeyPairResponse.java @@ -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; + } } diff --git a/server/src/com/cloud/api/ApiResponseHelper.java b/server/src/com/cloud/api/ApiResponseHelper.java index 4d3b45f8dac..27c247a4dd4 100755 --- a/server/src/com/cloud/api/ApiResponseHelper.java +++ b/server/src/com/cloud/api/ApiResponseHelper.java @@ -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; + } } diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index 7bafc01b036..029bdf418a1 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -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();