|
| 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.log4j.Logger; |
| 20 | + |
| 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.ServerApiException; |
| 27 | +import org.apache.cloudstack.api.response.NicResponse; |
| 28 | +import org.apache.cloudstack.api.response.NicSecondaryIpResponse; |
| 29 | + |
| 30 | +import com.cloud.async.AsyncJob; |
| 31 | +import com.cloud.event.EventTypes; |
| 32 | +import com.cloud.exception.ConcurrentOperationException; |
| 33 | +import com.cloud.exception.InsufficientAddressCapacityException; |
| 34 | +import com.cloud.exception.InsufficientCapacityException; |
| 35 | +import com.cloud.exception.InvalidParameterValueException; |
| 36 | +import com.cloud.exception.ResourceAllocationException; |
| 37 | +import com.cloud.exception.ResourceUnavailableException; |
| 38 | +import com.cloud.network.Network; |
| 39 | +import com.cloud.user.Account; |
| 40 | +import com.cloud.user.UserContext; |
| 41 | +import com.cloud.utils.net.NetUtils; |
| 42 | +import com.cloud.vm.Nic; |
| 43 | + |
| 44 | +@APICommand(name = "addIpToNic", description = "Assigns secondary IP to NIC", responseObject = NicSecondaryIpResponse.class) |
| 45 | +public class AddIpToVmNicCmd extends BaseAsyncCmd { |
| 46 | + public static final Logger s_logger = Logger.getLogger(AddIpToVmNicCmd.class.getName()); |
| 47 | + private static final String s_name = "addiptovmnicresponse"; |
| 48 | + |
| 49 | + ///////////////////////////////////////////////////// |
| 50 | + //////////////// API parameters ///////////////////// |
| 51 | + ///////////////////////////////////////////////////// |
| 52 | + @Parameter(name=ApiConstants.NIC_ID, type=CommandType.UUID, entityType = NicResponse.class, required = true, |
| 53 | + description="the ID of the nic to which you want to assign private IP") |
| 54 | + private Long nicId; |
| 55 | + |
| 56 | + @Parameter(name = ApiConstants.IP_ADDRESS, type = CommandType.STRING, required = false, |
| 57 | + description = "Secondary IP Address") |
| 58 | + private String ipAddr; |
| 59 | + |
| 60 | + ///////////////////////////////////////////////////// |
| 61 | + /////////////////// Accessors /////////////////////// |
| 62 | + ///////////////////////////////////////////////////// |
| 63 | + |
| 64 | + public String getEntityTable() { |
| 65 | + return "nic_secondary_ips"; |
| 66 | + } |
| 67 | + |
| 68 | + public String getAccountName() { |
| 69 | + return UserContext.current().getCaller().getAccountName(); |
| 70 | + } |
| 71 | + |
| 72 | + public long getDomainId() { |
| 73 | + return UserContext.current().getCaller().getDomainId(); |
| 74 | + } |
| 75 | + |
| 76 | + private long getZoneId() { |
| 77 | + Network ntwk = _entityMgr.findById(Network.class, getNetworkId()); |
| 78 | + if (ntwk == null) { |
| 79 | + throw new InvalidParameterValueException("Can't find zone id for specified"); |
| 80 | + } |
| 81 | + return ntwk.getDataCenterId(); |
| 82 | + } |
| 83 | + |
| 84 | + public Long getNetworkId() { |
| 85 | + Nic nic = _entityMgr.findById(Nic.class, nicId); |
| 86 | + Long networkId = nic.getNetworkId(); |
| 87 | + return networkId; |
| 88 | + } |
| 89 | + |
| 90 | + public Long getNicId() { |
| 91 | + return nicId; |
| 92 | + } |
| 93 | + |
| 94 | + public String getIpaddress () { |
| 95 | + if (ipAddr != null) { |
| 96 | + return ipAddr; |
| 97 | + } else { |
| 98 | + return null; |
| 99 | + } |
| 100 | + } |
| 101 | + @Override |
| 102 | + public long getEntityOwnerId() { |
| 103 | + Account caller = UserContext.current().getCaller(); |
| 104 | + return caller.getAccountId(); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public String getEventType() { |
| 109 | + return EventTypes.EVENT_NET_IP_ASSIGN; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public String getEventDescription() { |
| 114 | + return "associating ip to nic id: " + getNetworkId() + " in zone " + getZoneId(); |
| 115 | + } |
| 116 | + |
| 117 | + ///////////////////////////////////////////////////// |
| 118 | + /////////////// API Implementation/////////////////// |
| 119 | + ///////////////////////////////////////////////////// |
| 120 | + |
| 121 | + |
| 122 | + @Override |
| 123 | + public String getCommandName() { |
| 124 | + return s_name; |
| 125 | + } |
| 126 | + |
| 127 | + public static String getResultObjectName() { |
| 128 | + return "addressinfo"; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public void execute() throws ResourceUnavailableException, ResourceAllocationException, |
| 133 | + ConcurrentOperationException, InsufficientCapacityException { |
| 134 | + |
| 135 | + UserContext.current().setEventDetails("Nic Id: " + getNicId() ); |
| 136 | + String ip; |
| 137 | + String SecondaryIp = null; |
| 138 | + if ((ip = getIpaddress()) != null) { |
| 139 | + if (!NetUtils.isValidIp(ip)) { |
| 140 | + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Invalid ip address " + ip); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + try { |
| 145 | + SecondaryIp = _networkService.allocateSecondaryGuestIP(_accountService.getAccount(getEntityOwnerId()), getZoneId(), getNicId(), getNetworkId(), getIpaddress()); |
| 146 | + } catch (InsufficientAddressCapacityException e) { |
| 147 | + throw new InvalidParameterValueException("Allocating guest ip for nic failed"); |
| 148 | + } |
| 149 | + |
| 150 | + if (SecondaryIp != null) { |
| 151 | + s_logger.info("Associated ip address to NIC : " + SecondaryIp); |
| 152 | + NicSecondaryIpResponse response = new NicSecondaryIpResponse(); |
| 153 | + response = _responseGenerator.createSecondaryIPToNicResponse(ip, getNicId(), getNetworkId()); |
| 154 | + response.setResponseName(getCommandName()); |
| 155 | + this.setResponseObject(response); |
| 156 | + } else { |
| 157 | + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to assign secondary ip to nic"); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public String getSyncObjType() { |
| 163 | + return BaseAsyncCmd.networkSyncObject; |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public Long getSyncObjId() { |
| 168 | + return getNetworkId(); |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public AsyncJob.Type getInstanceType() { |
| 173 | + return AsyncJob.Type.IpAddress; |
| 174 | + } |
| 175 | + |
| 176 | +} |
0 commit comments