Visitar URL original
Summary: add/remove/update default nics feature · gitqueue/cloudstack@15906c0 · GitHub
Skip to content

Commit 15906c0

Browse files
author
Marcus Sorensen
committed
Summary: add/remove/update default nics feature
Description: Adds API calls updateDefaultNicForVirtualMachine, addNicToVirtualMachine, and removeNicFromVirtualMachine. These are intended to allow a user to modify a VM's configuration post deployment, to adjust the networks to which the VM belongs. BUG-ID: CLOUDSTACK-645 Submitted-by: Brian Angus <blangus@betterservers.com> Submitted-by: Ryan Dietrich <ryan@betterservers.com> Signed-off-by: Marcus Sorensen <marcus@betterservers.com> 1359494800 -0700
1 parent 99556a4 commit 15906c0

21 files changed

Lines changed: 1163 additions & 25 deletions

‎api/src/com/cloud/event/EventTypes.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ public class EventTypes {
6363
public static final String EVENT_FIREWALL_OPEN = "FIREWALL.OPEN";
6464
public static final String EVENT_FIREWALL_CLOSE = "FIREWALL.CLOSE";
6565

66+
//NIC Events
67+
public static final String EVENT_NIC_CREATE = "NIC.CREATE";
68+
public static final String EVENT_NIC_DELETE = "NIC.DELETE";
69+
public static final String EVENT_NIC_UPDATE = "NIC.UPDATE";
70+
6671
// Load Balancers
6772
public static final String EVENT_ASSIGN_TO_LOAD_BALANCER_RULE = "LB.ASSIGN.TO.RULE";
6873
public static final String EVENT_REMOVE_FROM_LOAD_BALANCER_RULE = "LB.REMOVE.FROM.RULE";

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,27 @@ UserVm startVirtualMachine(StartVMCmd cmd) throws StorageUnavailableException, E
113113

114114
UserVm updateVirtualMachine(UpdateVMCmd cmd) throws ResourceUnavailableException, InsufficientCapacityException;
115115

116+
/**
117+
* Adds a NIC on the given network to the virtual machine
118+
* @param cmd the command object that defines the vm and the given network
119+
* @return the vm object if successful, null otherwise
120+
*/
121+
UserVm addNicToVirtualMachine(AddNicToVMCmd cmd);
122+
123+
/**
124+
* Removes a NIC on the given network from the virtual machine
125+
* @param cmd the command object that defines the vm and the given network
126+
* @return the vm object if successful, null otherwise
127+
*/
128+
UserVm removeNicFromVirtualMachine(RemoveNicFromVMCmd cmd);
129+
130+
/**
131+
* Updates default Nic to the given network for given virtual machine
132+
* @param cmd the command object that defines the vm and the given network
133+
* @return the vm object if successful, null otherwise
134+
*/
135+
UserVm updateDefaultNicForVirtualMachine(UpdateDefaultNicForVMCmd cmd);
136+
116137
UserVm recoverVirtualMachine(RecoverVMCmd cmd) throws ResourceAllocationException;
117138

118139
/**

‎api/src/org/apache/cloudstack/api/ApiConstants.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ public class ApiConstants {
224224
public static final String NETWORK_OFFERING_ID = "networkofferingid";
225225
public static final String NETWORK_IDS = "networkids";
226226
public static final String NETWORK_ID = "networkid";
227+
public static final String NIC_ID = "nicid";
227228
public static final String SPECIFY_VLAN = "specifyvlan";
228229
public static final String IS_DEFAULT = "isdefault";
229230
public static final String IS_SYSTEM = "issystem";
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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.*;
25+
import org.apache.cloudstack.api.ApiConstants.VMDetails;
26+
import org.apache.cloudstack.api.response.UserVmResponse;
27+
import org.apache.cloudstack.api.response.NetworkResponse;
28+
import com.cloud.event.EventTypes;
29+
import com.cloud.exception.ConcurrentOperationException;
30+
import com.cloud.exception.InsufficientCapacityException;
31+
import com.cloud.exception.ResourceAllocationException;
32+
import com.cloud.exception.ResourceUnavailableException;
33+
import com.cloud.user.Account;
34+
import com.cloud.user.UserContext;
35+
import com.cloud.uservm.UserVm;
36+
37+
@APICommand(name = "addNicToVirtualMachine", description="Adds VM to specified network by creating a NIC", responseObject=UserVmResponse.class)
38+
39+
public class AddNicToVMCmd extends BaseAsyncCmd {
40+
public static final Logger s_logger = Logger.getLogger(AddNicToVMCmd.class);
41+
private static final String s_name = "addnictovirtualmachineresponse";
42+
43+
/////////////////////////////////////////////////////
44+
//////////////// API parameters /////////////////////
45+
/////////////////////////////////////////////////////
46+
47+
@Parameter(name=ApiConstants.VIRTUAL_MACHINE_ID, type=CommandType.UUID, entityType=UserVmResponse.class,
48+
required=true, description="Virtual Machine ID")
49+
private Long vmId;
50+
51+
@Parameter(name=ApiConstants.NETWORK_ID, type=CommandType.UUID, entityType=NetworkResponse.class,
52+
required=true, description="Network ID")
53+
private Long netId;
54+
55+
@Parameter(name=ApiConstants.IP_ADDRESS, type=CommandType.STRING, description="IP Address for the new network")
56+
private String ipaddr;
57+
58+
/////////////////////////////////////////////////////
59+
/////////////////// Accessors ///////////////////////
60+
/////////////////////////////////////////////////////
61+
62+
public Long getVmId() {
63+
return vmId;
64+
}
65+
66+
public Long getNetworkId() {
67+
return netId;
68+
}
69+
70+
public String getIpAddress() {
71+
return ipaddr;
72+
}
73+
74+
/////////////////////////////////////////////////////
75+
/////////////// API Implementation///////////////////
76+
/////////////////////////////////////////////////////
77+
78+
@Override
79+
public String getCommandName() {
80+
return s_name;
81+
}
82+
83+
public static String getResultObjectName() {
84+
return "virtualmachine";
85+
}
86+
87+
@Override
88+
public String getEventType() {
89+
return EventTypes.EVENT_NIC_CREATE;
90+
}
91+
92+
@Override
93+
public String getEventDescription() {
94+
return "Adding network " + getNetworkId() + " to user vm: " + getVmId();
95+
}
96+
97+
@Override
98+
public long getEntityOwnerId() {
99+
UserVm vm = _responseGenerator.findUserVmById(getVmId());
100+
if (vm == null) {
101+
return Account.ACCOUNT_ID_SYSTEM; // bad id given, parent this command to SYSTEM so ERROR events are tracked
102+
}
103+
return vm.getAccountId();
104+
}
105+
106+
@Override
107+
public void execute(){
108+
UserContext.current().setEventDetails("Vm Id: " + getVmId() + " Network Id: " + getNetworkId());
109+
UserVm result = _userVmService.addNicToVirtualMachine(this);
110+
ArrayList<VMDetails> dc = new ArrayList<VMDetails>();
111+
dc.add(VMDetails.valueOf("nics"));
112+
EnumSet<VMDetails> details = EnumSet.copyOf(dc);
113+
if (result != null){
114+
UserVmResponse response = _responseGenerator.createUserVmResponse("virtualmachine", details, result).get(0);
115+
response.setResponseName(getCommandName());
116+
this.setResponseObject(response);
117+
} else {
118+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to add NIC to vm. Refer to server logs for details.");
119+
}
120+
}
121+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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.*;
25+
import org.apache.cloudstack.api.ApiConstants.VMDetails;
26+
import org.apache.cloudstack.api.response.UserVmResponse;
27+
import org.apache.cloudstack.api.response.NicResponse;
28+
import com.cloud.event.EventTypes;
29+
import com.cloud.exception.ConcurrentOperationException;
30+
import com.cloud.exception.InsufficientCapacityException;
31+
import com.cloud.exception.ResourceAllocationException;
32+
import com.cloud.exception.ResourceUnavailableException;
33+
import com.cloud.user.Account;
34+
import com.cloud.user.UserContext;
35+
import com.cloud.uservm.UserVm;
36+
37+
@APICommand(name = "removeNicFromVirtualMachine", description="Removes VM from specified network by deleting a NIC", responseObject=UserVmResponse.class)
38+
39+
public class RemoveNicFromVMCmd extends BaseAsyncCmd {
40+
public static final Logger s_logger = Logger.getLogger(RemoveNicFromVMCmd.class);
41+
private static final String s_name = "removenicfromvirtualmachineresponse";
42+
43+
/////////////////////////////////////////////////////
44+
//////////////// API parameters /////////////////////
45+
/////////////////////////////////////////////////////
46+
47+
@Parameter(name=ApiConstants.VIRTUAL_MACHINE_ID, type=CommandType.UUID, entityType=UserVmResponse.class,
48+
required=true, description="Virtual Machine ID")
49+
private Long vmId;
50+
51+
@Parameter(name=ApiConstants.NIC_ID, type=CommandType.UUID, entityType=NicResponse.class,
52+
required=true, description="NIC ID")
53+
private Long nicId;
54+
55+
/////////////////////////////////////////////////////
56+
/////////////////// Accessors ///////////////////////
57+
/////////////////////////////////////////////////////
58+
59+
public Long getVmId() {
60+
return vmId;
61+
}
62+
63+
public Long getNicId() {
64+
return nicId;
65+
}
66+
67+
/////////////////////////////////////////////////////
68+
/////////////// API Implementation///////////////////
69+
/////////////////////////////////////////////////////
70+
71+
@Override
72+
public String getCommandName() {
73+
return s_name;
74+
}
75+
76+
public static String getResultObjectName() {
77+
return "virtualmachine";
78+
}
79+
80+
@Override
81+
public String getEventType() {
82+
return EventTypes.EVENT_NIC_DELETE;
83+
}
84+
85+
@Override
86+
public String getEventDescription() {
87+
return "Removing NIC " + getNicId() + " from user vm: " + getVmId();
88+
}
89+
90+
91+
@Override
92+
public long getEntityOwnerId() {
93+
UserVm vm = _responseGenerator.findUserVmById(getVmId());
94+
if (vm == null) {
95+
return Account.ACCOUNT_ID_SYSTEM; // bad id given, parent this command to SYSTEM so ERROR events are tracked
96+
}
97+
return vm.getAccountId();
98+
}
99+
100+
@Override
101+
public void execute(){
102+
UserContext.current().setEventDetails("Vm Id: "+getVmId() + " Nic Id: " + getNicId());
103+
UserVm result = _userVmService.removeNicFromVirtualMachine(this);
104+
ArrayList<VMDetails> dc = new ArrayList<VMDetails>();
105+
dc.add(VMDetails.valueOf("nics"));
106+
EnumSet<VMDetails> details = EnumSet.copyOf(dc);
107+
if (result != null){
108+
UserVmResponse response = _responseGenerator.createUserVmResponse("virtualmachine", details, result).get(0);
109+
response.setResponseName(getCommandName());
110+
this.setResponseObject(response);
111+
} else {
112+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to remove NIC from vm, see error log for details");
113+
}
114+
}
115+
}

0 commit comments

Comments
 (0)