Skip to content

Commit b79d338

Browse files
committed
CLOUDSTACK-9051: update nic IP address of stopped vm
1 parent 20dcc25 commit b79d338

12 files changed

Lines changed: 513 additions & 116 deletions

File tree

api/src/com/cloud/vm/UserVmService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.apache.cloudstack.api.command.user.vm.StartVMCmd;
3535
import org.apache.cloudstack.api.command.user.vm.UpdateDefaultNicForVMCmd;
3636
import org.apache.cloudstack.api.command.user.vm.UpdateVMCmd;
37+
import org.apache.cloudstack.api.command.user.vm.UpdateVmNicIpCmd;
3738
import org.apache.cloudstack.api.command.user.vm.UpgradeVMCmd;
3839
import org.apache.cloudstack.api.command.user.vmgroup.CreateVMGroupCmd;
3940
import org.apache.cloudstack.api.command.user.vmgroup.DeleteVMGroupCmd;
@@ -129,6 +130,13 @@ UserVm startVirtualMachine(StartVMCmd cmd) throws StorageUnavailableException, E
129130
*/
130131
UserVm updateDefaultNicForVirtualMachine(UpdateDefaultNicForVMCmd cmd);
131132

133+
/**
134+
* Updated the ip address on the given NIC to the virtual machine
135+
* @param cmd the command object that defines the ip address and the given nic
136+
* @return the vm object if successful, null otherwise
137+
*/
138+
UserVm updateNicIpForVirtualMachine(UpdateVmNicIpCmd cmd);
139+
132140
UserVm recoverVirtualMachine(RecoverVMCmd cmd) throws ResourceAllocationException;
133141

134142
/**
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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 java.util.ArrayList;
20+
import java.util.EnumSet;
21+
22+
import org.apache.log4j.Logger;
23+
24+
import org.apache.cloudstack.api.APICommand;
25+
import org.apache.cloudstack.api.ApiCommandJobType;
26+
import org.apache.cloudstack.api.ApiConstants;
27+
import org.apache.cloudstack.api.ApiErrorCode;
28+
import org.apache.cloudstack.api.BaseAsyncCmd;
29+
import org.apache.cloudstack.api.Parameter;
30+
import org.apache.cloudstack.api.ServerApiException;
31+
import org.apache.cloudstack.api.ApiConstants.VMDetails;
32+
import org.apache.cloudstack.api.ResponseObject.ResponseView;
33+
import org.apache.cloudstack.api.response.NicResponse;
34+
import org.apache.cloudstack.api.response.UserVmResponse;
35+
import org.apache.cloudstack.context.CallContext;
36+
37+
import com.cloud.dc.DataCenter;
38+
import com.cloud.dc.DataCenter.NetworkType;
39+
import com.cloud.event.EventTypes;
40+
import com.cloud.exception.ConcurrentOperationException;
41+
import com.cloud.exception.InsufficientCapacityException;
42+
import com.cloud.exception.InvalidParameterValueException;
43+
import com.cloud.exception.ResourceAllocationException;
44+
import com.cloud.exception.ResourceUnavailableException;
45+
import com.cloud.network.Network;
46+
import com.cloud.uservm.UserVm;
47+
import com.cloud.utils.net.NetUtils;
48+
import com.cloud.vm.Nic;
49+
50+
@APICommand(name = "updateVmNicIp", description = "Update the default Ip of a VM Nic", responseObject = UserVmResponse.class)
51+
public class UpdateVmNicIpCmd extends BaseAsyncCmd {
52+
public static final Logger s_logger = Logger.getLogger(AddIpToVmNicCmd.class.getName());
53+
private static final String s_name = "updatevmnicipresponse";
54+
55+
/////////////////////////////////////////////////////
56+
//////////////// API parameters /////////////////////
57+
/////////////////////////////////////////////////////
58+
@Parameter(name=ApiConstants.NIC_ID, type=CommandType.UUID, entityType = NicResponse.class, required = true,
59+
description="the ID of the nic to which you want to assign private IP")
60+
private Long nicId;
61+
62+
@Parameter(name = ApiConstants.IP_ADDRESS, type = CommandType.STRING, required = false,
63+
description = "Secondary IP Address")
64+
private String ipAddr;
65+
66+
/////////////////////////////////////////////////////
67+
/////////////////// Accessors ///////////////////////
68+
/////////////////////////////////////////////////////
69+
70+
public String getEntityTable() {
71+
return "nic_secondary_ips";
72+
}
73+
74+
public String getAccountName() {
75+
return CallContext.current().getCallingAccount().getAccountName();
76+
}
77+
78+
public long getDomainId() {
79+
return CallContext.current().getCallingAccount().getDomainId();
80+
}
81+
82+
private long getZoneId() {
83+
Network ntwk = _entityMgr.findById(Network.class, getNetworkId());
84+
if (ntwk == null) {
85+
throw new InvalidParameterValueException("Can't find zone id for specified");
86+
}
87+
return ntwk.getDataCenterId();
88+
}
89+
90+
public Long getNetworkId() {
91+
Nic nic = _entityMgr.findById(Nic.class, nicId);
92+
if (nic == null) {
93+
throw new InvalidParameterValueException("Can't find network id for specified nic");
94+
}
95+
Long networkId = nic.getNetworkId();
96+
return networkId;
97+
}
98+
99+
public Long getNicId() {
100+
return nicId;
101+
}
102+
103+
public String getIpaddress () {
104+
if (ipAddr != null) {
105+
return ipAddr;
106+
} else {
107+
return null;
108+
}
109+
}
110+
111+
public NetworkType getNetworkType() {
112+
Network ntwk = _entityMgr.findById(Network.class, getNetworkId());
113+
DataCenter dc = _entityMgr.findById(DataCenter.class, ntwk.getDataCenterId());
114+
return dc.getNetworkType();
115+
}
116+
117+
@Override
118+
public long getEntityOwnerId() {
119+
return CallContext.current().getCallingAccountId();
120+
}
121+
122+
@Override
123+
public String getEventType() {
124+
return EventTypes.EVENT_NET_IP_ASSIGN;
125+
}
126+
127+
@Override
128+
public String getEventDescription() {
129+
return "associating ip to nic id: " + getNetworkId() + " in zone " + getZoneId();
130+
}
131+
132+
/////////////////////////////////////////////////////
133+
/////////////// API Implementation///////////////////
134+
/////////////////////////////////////////////////////
135+
136+
137+
@Override
138+
public String getCommandName() {
139+
return s_name;
140+
}
141+
142+
public static String getResultObjectName() {
143+
return "addressinfo";
144+
}
145+
146+
@Override
147+
public void execute() throws ResourceUnavailableException, ResourceAllocationException,
148+
ConcurrentOperationException, InsufficientCapacityException {
149+
150+
CallContext.current().setEventDetails("Nic Id: " + getNicId() );
151+
String ip;
152+
if ((ip = getIpaddress()) != null) {
153+
if (!NetUtils.isValidIp(ip)) {
154+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Invalid ip address " + ip);
155+
}
156+
}
157+
158+
UserVm vm = _userVmService.updateNicIpForVirtualMachine(this);
159+
ArrayList<VMDetails> dc = new ArrayList<VMDetails>();
160+
dc.add(VMDetails.valueOf("nics"));
161+
EnumSet<VMDetails> details = EnumSet.copyOf(dc);
162+
if (vm != null){
163+
UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Restricted, "virtualmachine", details, vm).get(0);
164+
response.setResponseName(getCommandName());
165+
this.setResponseObject(response);
166+
} else {
167+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update ip address on vm NIC. Refer to server logs for details.");
168+
}
169+
}
170+
171+
@Override
172+
public String getSyncObjType() {
173+
return BaseAsyncCmd.networkSyncObject;
174+
}
175+
176+
@Override
177+
public Long getSyncObjId() {
178+
return getNetworkId();
179+
}
180+
181+
@Override
182+
public ApiCommandJobType getInstanceType() {
183+
return ApiCommandJobType.IpAddress;
184+
}
185+
186+
}

client/WEB-INF/classes/resources/messages.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ label.capacity=Capacity
441441
label.capacity.bytes=Capacity Bytes
442442
label.capacity.iops=Capacity IOPS
443443
label.certificate=Server certificate
444+
label.change.ipaddress=Change IP address for NIC
444445
label.change.service.offering=Change service offering
445446
label.change.value=Change value
446447
label.character=Character
@@ -1783,6 +1784,7 @@ message.apply.snapshot.policy=You have successfully updated your current snapsho
17831784
message.attach.iso.confirm=Please confirm that you want to attach the ISO to this virtual instance.
17841785
message.attach.volume=Please fill in the following data to attach a new volume. If you are attaching a disk volume to a Windows based virtual machine, you will need to reboot the instance to see the attached disk.
17851786
message.basic.mode.desc=Choose this network model if you do <b>*<u>not</u>*</b> want to enable any VLAN support. All virtual instances created under this network model will be assigned an IP directly from the network and security groups are used to provide security and segregation.
1787+
message.change.ipaddress=Please confirm that you would like to change the IP address for this NIC on VM.
17861788
message.change.offering.confirm=Please confirm that you wish to change the service offering of this virtual instance.
17871789
message.change.password=Please change your password.
17881790
message.configure.all.traffic.types=You have multiple physical networks; please configure labels for each traffic type by clicking on the Edit button.

client/tomcatconf/commands.properties.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ updateDefaultNicForVirtualMachine=15
402402
####
403403
addIpToNic=15
404404
removeIpFromNic=15
405+
updateVmNicIp=15
405406
listNics=15
406407

407408
#### SSH key pair commands

0 commit comments

Comments
 (0)