|
| 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.vm; |
| 18 | + |
| 19 | +import org.apache.cloudstack.acl.RoleType; |
| 20 | +import org.apache.cloudstack.api.ACL; |
| 21 | +import org.apache.cloudstack.api.APICommand; |
| 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.Parameter; |
| 26 | +import org.apache.cloudstack.api.ResponseObject; |
| 27 | +import org.apache.cloudstack.api.ServerApiException; |
| 28 | +import org.apache.cloudstack.api.response.NicResponse; |
| 29 | +import org.apache.cloudstack.api.response.UserVmResponse; |
| 30 | +import org.apache.cloudstack.context.CallContext; |
| 31 | + |
| 32 | +import com.cloud.event.EventTypes; |
| 33 | +import com.cloud.user.Account; |
| 34 | +import com.cloud.uservm.UserVm; |
| 35 | + |
| 36 | +import java.util.ArrayList; |
| 37 | +import java.util.EnumSet; |
| 38 | + |
| 39 | +@APICommand(name = "updateVmNic", description = "Updates the specified VM NIC", responseObject = NicResponse.class, requestHasSensitiveInfo = false, responseHasSensitiveInfo = false, |
| 40 | + authorized = { RoleType.Admin, RoleType.ResourceAdmin, RoleType.DomainAdmin, RoleType.User }) |
| 41 | +public class UpdateVmNicCmd extends BaseAsyncCmd { |
| 42 | + |
| 43 | + @ACL |
| 44 | + @Parameter(name = ApiConstants.NIC_ID, type = CommandType.UUID, entityType = NicResponse.class, required = true, description = "NIC ID") |
| 45 | + private Long nicId; |
| 46 | + |
| 47 | + @Parameter(name = ApiConstants.ENABLED, type = CommandType.BOOLEAN, description = "If true, sets the NIC state to UP; otherwise, sets the NIC state to DOWN") |
| 48 | + private Boolean enabled; |
| 49 | + |
| 50 | + public Long getNicId() { |
| 51 | + return nicId; |
| 52 | + } |
| 53 | + |
| 54 | + public Boolean isEnabled() { |
| 55 | + return enabled; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public String getEventType() { |
| 60 | + return EventTypes.EVENT_NIC_UPDATE; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public String getEventDescription() { |
| 65 | + return String.format("Updating NIC %s.", getResourceUuid(ApiConstants.NIC_ID)); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public long getEntityOwnerId() { |
| 70 | + UserVm vm = _responseGenerator.findUserVmByNicId(nicId); |
| 71 | + if (vm == null) { |
| 72 | + return Account.ACCOUNT_ID_SYSTEM; |
| 73 | + } |
| 74 | + return vm.getAccountId(); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public void execute() { |
| 79 | + CallContext.current().setEventDetails(String.format("NIC ID: %s", getResourceUuid(ApiConstants.NIC_ID))); |
| 80 | + |
| 81 | + UserVm result = _userVmService.updateVirtualMachineNic(this); |
| 82 | + |
| 83 | + ArrayList<ApiConstants.VMDetails> dc = new ArrayList<>(); |
| 84 | + dc.add(ApiConstants.VMDetails.valueOf("nics")); |
| 85 | + EnumSet<ApiConstants.VMDetails> details = EnumSet.copyOf(dc); |
| 86 | + |
| 87 | + if (result != null){ |
| 88 | + UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseObject.ResponseView.Restricted, "virtualmachine", details, result).get(0); |
| 89 | + response.setResponseName(getCommandName()); |
| 90 | + this.setResponseObject(response); |
| 91 | + } else { |
| 92 | + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update NIC from VM."); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments