|
| 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 | +package org.apache.cloudstack.api.command.user.affinitygroup; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import org.apache.cloudstack.affinity.AffinityGroupResponse; |
| 23 | +import org.apache.cloudstack.api.ACL; |
| 24 | +import org.apache.cloudstack.api.APICommand; |
| 25 | +import org.apache.cloudstack.api.ApiConstants; |
| 26 | +import org.apache.cloudstack.api.ApiErrorCode; |
| 27 | +import org.apache.cloudstack.api.BaseAsyncCmd; |
| 28 | +import org.apache.cloudstack.api.Parameter; |
| 29 | +import org.apache.cloudstack.api.ServerApiException; |
| 30 | +import org.apache.cloudstack.api.response.UserVmResponse; |
| 31 | +import org.apache.log4j.Logger; |
| 32 | + |
| 33 | +import com.cloud.async.AsyncJob; |
| 34 | +import com.cloud.event.EventTypes; |
| 35 | +import com.cloud.exception.InsufficientCapacityException; |
| 36 | +import com.cloud.exception.InvalidParameterValueException; |
| 37 | +import com.cloud.exception.ResourceUnavailableException; |
| 38 | +import com.cloud.user.Account; |
| 39 | +import com.cloud.user.UserContext; |
| 40 | +import com.cloud.uservm.UserVm; |
| 41 | + |
| 42 | + |
| 43 | +@APICommand(name = "updateVMAffinityGroup", description = "Updates the affinity/anti-affinity group associations of a virtual machine. The VM has to be stopped and restarted for the " |
| 44 | + + "new properties to take effect.", responseObject = UserVmResponse.class) |
| 45 | +public class UpdateVMAffinityGroupCmd extends BaseAsyncCmd { |
| 46 | + public static final Logger s_logger = Logger.getLogger(UpdateVMAffinityGroupCmd.class.getName()); |
| 47 | + private static final String s_name = "updatevirtualmachineresponse"; |
| 48 | + |
| 49 | + ///////////////////////////////////////////////////// |
| 50 | + //////////////// API parameters ///////////////////// |
| 51 | + ///////////////////////////////////////////////////// |
| 52 | + |
| 53 | + |
| 54 | + @ACL |
| 55 | + @Parameter(name=ApiConstants.ID, type=CommandType.UUID, entityType=UserVmResponse.class, |
| 56 | + required=true, description="The ID of the virtual machine") |
| 57 | + private Long id; |
| 58 | + |
| 59 | + @ACL |
| 60 | + @Parameter(name = ApiConstants.AFFINITY_GROUP_IDS, type = CommandType.LIST, collectionType = CommandType.UUID, entityType = AffinityGroupResponse.class, description = "comma separated list of affinity groups id that are going to be applied to the virtual machine. " |
| 61 | + + "Should be passed only when vm is created from a zone with Basic Network support." |
| 62 | + + " Mutually exclusive with securitygroupnames parameter") |
| 63 | + private List<Long> affinityGroupIdList; |
| 64 | + |
| 65 | + @ACL |
| 66 | + @Parameter(name = ApiConstants.AFFINITY_GROUP_NAMES, type = CommandType.LIST, collectionType = CommandType.STRING, entityType = AffinityGroupResponse.class, description = "comma separated list of affinity groups names that are going to be applied to the virtual machine." |
| 67 | + + " Should be passed only when vm is created from a zone with Basic Network support. " |
| 68 | + + "Mutually exclusive with securitygroupids parameter") |
| 69 | + private List<String> affinityGroupNameList; |
| 70 | + |
| 71 | + ///////////////////////////////////////////////////// |
| 72 | + /////////////////// Accessors /////////////////////// |
| 73 | + ///////////////////////////////////////////////////// |
| 74 | + |
| 75 | + |
| 76 | + public Long getId() { |
| 77 | + return id; |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + public List<Long> getAffinityGroupIdList() { |
| 82 | + if (affinityGroupNameList != null && affinityGroupIdList != null) { |
| 83 | + throw new InvalidParameterValueException( |
| 84 | + "affinitygroupids parameter is mutually exclusive with affinitygroupnames parameter"); |
| 85 | + } |
| 86 | + |
| 87 | + // transform group names to ids here |
| 88 | + if (affinityGroupNameList != null) { |
| 89 | + List<Long> affinityGroupIds = new ArrayList<Long>(); |
| 90 | + for (String groupName : affinityGroupNameList) { |
| 91 | + Long groupId = _responseGenerator.getAffinityGroupId(groupName, getEntityOwnerId()); |
| 92 | + if (groupId == null) { |
| 93 | + throw new InvalidParameterValueException("Unable to find group by name " + groupName |
| 94 | + + " for account " + getEntityOwnerId()); |
| 95 | + } else { |
| 96 | + affinityGroupIds.add(groupId); |
| 97 | + } |
| 98 | + } |
| 99 | + return affinityGroupIds; |
| 100 | + } else { |
| 101 | + return affinityGroupIdList; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + ///////////////////////////////////////////////////// |
| 106 | + /////////////// API Implementation/////////////////// |
| 107 | + ///////////////////////////////////////////////////// |
| 108 | + |
| 109 | + |
| 110 | + @Override |
| 111 | + public String getCommandName() { |
| 112 | + return s_name; |
| 113 | + } |
| 114 | + |
| 115 | + public static String getResultObjectName() { |
| 116 | + return "virtualmachine"; |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public long getEntityOwnerId() { |
| 121 | + UserVm userVm = _entityMgr.findById(UserVm.class, getId()); |
| 122 | + if (userVm != null) { |
| 123 | + return userVm.getAccountId(); |
| 124 | + } |
| 125 | + |
| 126 | + return Account.ACCOUNT_ID_SYSTEM; // no account info given, parent this command to SYSTEM so ERROR events are tracked |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public void execute() throws ResourceUnavailableException, |
| 131 | + InsufficientCapacityException, ServerApiException { |
| 132 | + UserContext.current().setEventDetails("Vm Id: "+getId()); |
| 133 | + UserVm result = _affinityGroupService.updateVMAffinityGroups(getId(), getAffinityGroupIdList()); |
| 134 | + if (result != null){ |
| 135 | + UserVmResponse response = _responseGenerator.createUserVmResponse("virtualmachine", result).get(0); |
| 136 | + response.setResponseName(getCommandName()); |
| 137 | + this.setResponseObject(response); |
| 138 | + } else { |
| 139 | + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update vm's affinity groups"); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public String getEventType() { |
| 145 | + return EventTypes.EVENT_VM_AFFINITY_GROUP_UPDATE; |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public String getEventDescription() { |
| 150 | + return "updating VM Affinity Group"; |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public AsyncJob.Type getInstanceType() { |
| 155 | + return AsyncJob.Type.AffinityGroup; |
| 156 | + } |
| 157 | + |
| 158 | +} |
0 commit comments