|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.apache.cloudstack.api.command.user.vm; |
| 19 | + |
| 20 | +import org.apache.log4j.Logger; |
| 21 | + |
| 22 | +import org.apache.cloudstack.api.ApiConstants; |
| 23 | +import org.apache.cloudstack.api.ApiErrorCode; |
| 24 | +import org.apache.cloudstack.api.BaseAsyncCmd; |
| 25 | +import org.apache.cloudstack.api.BaseCmd; |
| 26 | +import org.apache.cloudstack.api.APICommand; |
| 27 | +import org.apache.cloudstack.api.Parameter; |
| 28 | +import org.apache.cloudstack.api.ServerApiException; |
| 29 | +import org.apache.cloudstack.api.response.UserVmResponse; |
| 30 | +import org.apache.cloudstack.api.response.DomainResponse; |
| 31 | +import org.apache.cloudstack.api.response.ProjectResponse; |
| 32 | +import com.cloud.async.AsyncJob; |
| 33 | +import com.cloud.user.Account; |
| 34 | +import com.cloud.user.UserContext; |
| 35 | +import com.cloud.uservm.UserVm; |
| 36 | +import com.cloud.event.EventTypes; |
| 37 | +import com.cloud.exception.InsufficientCapacityException; |
| 38 | +import com.cloud.exception.ResourceUnavailableException; |
| 39 | + |
| 40 | +@APICommand(name = "resetSSHKeyForVirtualMachine", responseObject = UserVmResponse.class, description = "Resets the SSH Key for virtual machine. " + |
| 41 | + "The virtual machine must be in a \"Stopped\" state. [async]") |
| 42 | +public class ResetVMSSHKeyCmd extends BaseAsyncCmd { |
| 43 | + |
| 44 | + public static final Logger s_logger = Logger.getLogger(ResetVMSSHKeyCmd.class.getName()); |
| 45 | + |
| 46 | + private static final String s_name = "resetSSHKeyforvirtualmachineresponse"; |
| 47 | + |
| 48 | + ///////////////////////////////////////////////////// |
| 49 | + //////////////// API parameters ///////////////////// |
| 50 | + ///////////////////////////////////////////////////// |
| 51 | + |
| 52 | + @Parameter(name = ApiConstants.ID, type = CommandType.UUID, entityType = UserVmResponse.class, required = true, description = "The ID of the virtual machine") |
| 53 | + private Long id; |
| 54 | + |
| 55 | + @Parameter(name = ApiConstants.SSH_KEYPAIR, type = CommandType.STRING, required = true, description = "name of the ssh key pair used to login to the virtual machine") |
| 56 | + private String name; |
| 57 | + |
| 58 | + |
| 59 | + //Owner information |
| 60 | + @Parameter(name = ApiConstants.ACCOUNT, type = CommandType.STRING, description = "an optional account for the ssh key. Must be used with domainId.") |
| 61 | + private String accountName; |
| 62 | + |
| 63 | + @Parameter(name = ApiConstants.DOMAIN_ID, type = CommandType.UUID, entityType = DomainResponse.class, description = "an optional domainId for the virtual machine. If the account parameter is used, domainId must also be used.") |
| 64 | + private Long domainId; |
| 65 | + |
| 66 | + @Parameter(name = ApiConstants.PROJECT_ID, type = CommandType.UUID, entityType = ProjectResponse.class, description = "an optional project for the ssh key") |
| 67 | + private Long projectId; |
| 68 | + |
| 69 | + |
| 70 | + ///////////////////////////////////////////////////// |
| 71 | + /////////////////// Accessors /////////////////////// |
| 72 | + ///////////////////////////////////////////////////// |
| 73 | + |
| 74 | + public String getName() { |
| 75 | + return name; |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + public Long getId() { |
| 80 | + return id; |
| 81 | + } |
| 82 | + |
| 83 | + public String getAccountName() { |
| 84 | + return accountName; |
| 85 | + } |
| 86 | + |
| 87 | + public Long getDomainId() { |
| 88 | + return domainId; |
| 89 | + } |
| 90 | + |
| 91 | + public Long getProjectId() { |
| 92 | + return projectId; |
| 93 | + } |
| 94 | + |
| 95 | + ///////////////////////////////////////////////////// |
| 96 | + /////////////// API Implementation/////////////////// |
| 97 | + ///////////////////////////////////////////////////// |
| 98 | + |
| 99 | + |
| 100 | + @Override |
| 101 | + public String getEventType() { |
| 102 | + return EventTypes.EVENT_VM_RESETSSHKEY; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public String getEventDescription() { |
| 107 | + return "resetting SSHKey for vm: " + getId(); |
| 108 | + } |
| 109 | + |
| 110 | + public AsyncJob.Type getInstanceType() { |
| 111 | + return AsyncJob.Type.VirtualMachine; |
| 112 | + } |
| 113 | + |
| 114 | + |
| 115 | + @Override |
| 116 | + public String getCommandName() { |
| 117 | + return s_name; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public long getEntityOwnerId() { |
| 122 | + UserVm vm = _responseGenerator.findUserVmById(getId()); |
| 123 | + if (vm != null) { |
| 124 | + return vm.getAccountId(); |
| 125 | + } |
| 126 | + |
| 127 | + return Account.ACCOUNT_ID_SYSTEM; // no account info given, parent this command to SYSTEM so ERROR events are tracked |
| 128 | + } |
| 129 | + |
| 130 | + public Long getInstanceId() { |
| 131 | + return getId(); |
| 132 | + } |
| 133 | + |
| 134 | + |
| 135 | + @Override |
| 136 | + public void execute() throws ResourceUnavailableException, |
| 137 | + InsufficientCapacityException { |
| 138 | + |
| 139 | + UserContext.current().setEventDetails("Vm Id: " + getId()); |
| 140 | + UserVm result = _userVmService.resetVMSSHKey(this); |
| 141 | + |
| 142 | + if (result != null) { |
| 143 | + UserVmResponse response = _responseGenerator.createUserVmResponse("virtualmachine", result).get(0); |
| 144 | + response.setResponseName(getCommandName()); |
| 145 | + this.setResponseObject(response); |
| 146 | + } else { |
| 147 | + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to reset vm SSHKey"); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | +} |
0 commit comments