Skip to content

Commit 9c6d9a0

Browse files
committed
Add API to enable/disable NICs for KVM
1 parent 93239e0 commit 9c6d9a0

File tree

35 files changed

+713
-4
lines changed

35 files changed

+713
-4
lines changed

api/src/main/java/com/cloud/agent/api/to/NicTO.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.cloud.agent.api.to;
1818

1919
import com.cloud.offering.NetworkOffering;
20+
import com.cloud.vm.Nic;
2021

2122
import java.util.List;
2223
import java.util.Map;
@@ -33,6 +34,7 @@ public class NicTO extends NetworkTO {
3334
boolean dpdkEnabled;
3435
Integer mtu;
3536
Long networkId;
37+
Nic.LinkState linkState;
3638

3739
String networkSegmentName;
3840

@@ -154,4 +156,12 @@ public String getNetworkSegmentName() {
154156
public void setNetworkSegmentName(String networkSegmentName) {
155157
this.networkSegmentName = networkSegmentName;
156158
}
159+
160+
public Nic.LinkState getLinkState() {
161+
return linkState;
162+
}
163+
164+
public void setLinkState(Nic.LinkState linkState) {
165+
this.linkState = linkState;
166+
}
157167
}

api/src/main/java/com/cloud/vm/Nic.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ enum Event {
3737
ReservationRequested, ReleaseRequested, CancelRequested, OperationCompleted, OperationFailed,
3838
}
3939

40+
enum LinkState {
41+
Enabled, Disabled
42+
}
43+
4044
public enum State implements FiniteState<State, Event> {
4145
Allocated("Resource is allocated but not reserved"), Reserving("Resource is being reserved right now"), Reserved("Resource has been reserved."), Releasing(
4246
"Resource is being released"), Deallocating("Resource is being deallocated");
@@ -162,4 +166,6 @@ public enum ReservationStrategy {
162166
String getIPv6Address();
163167

164168
Integer getMtu();
169+
170+
LinkState getLinkState();
165171
}

api/src/main/java/com/cloud/vm/NicProfile.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public class NicProfile implements InternalIdentity, Serializable {
5252
boolean defaultNic;
5353
Integer networkRate;
5454
boolean isSecurityGroupEnabled;
55+
Nic.LinkState linkState;
5556

5657
Integer orderIndex;
5758

@@ -87,6 +88,7 @@ public NicProfile(Nic nic, Network network, URI broadcastUri, URI isolationUri,
8788
broadcastType = network.getBroadcastDomainType();
8889
trafficType = network.getTrafficType();
8990
format = nic.getAddressFormat();
91+
linkState = nic.getLinkState();
9092

9193
iPv4Address = nic.getIPv4Address();
9294
iPv4Netmask = nic.getIPv4Netmask();
@@ -414,6 +416,14 @@ public void setIpv4AllocationRaceCheck(boolean ipv4AllocationRaceCheck) {
414416
this.ipv4AllocationRaceCheck = ipv4AllocationRaceCheck;
415417
}
416418

419+
public Nic.LinkState getLinkState() {
420+
return linkState;
421+
}
422+
423+
public void setLinkState(Nic.LinkState linkState) {
424+
this.linkState = linkState;
425+
}
426+
417427
//
418428
// OTHER METHODS
419429
//

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.apache.cloudstack.api.command.user.vm.StartVMCmd;
4141
import org.apache.cloudstack.api.command.user.vm.UpdateDefaultNicForVMCmd;
4242
import org.apache.cloudstack.api.command.user.vm.UpdateVMCmd;
43+
import org.apache.cloudstack.api.command.user.vm.UpdateVmNicCmd;
4344
import org.apache.cloudstack.api.command.user.vm.UpdateVmNicIpCmd;
4445
import org.apache.cloudstack.api.command.user.vm.UpgradeVMCmd;
4546
import org.apache.cloudstack.api.command.user.vmgroup.CreateVMGroupCmd;
@@ -152,6 +153,8 @@ void startVirtualMachineForHA(VirtualMachine vm, Map<VirtualMachineProfile.Param
152153
*/
153154
UserVm updateNicIpForVirtualMachine(UpdateVmNicIpCmd cmd);
154155

156+
UserVm updateVirtualMachineNic(UpdateVmNicCmd cmd);
157+
155158
UserVm recoverVirtualMachine(RecoverVMCmd cmd) throws ResourceAllocationException;
156159

157160
/**

api/src/main/java/org/apache/cloudstack/api/ResponseGenerator.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,8 @@ public interface ResponseGenerator {
343343

344344
UserVm findUserVmById(Long vmId);
345345

346+
UserVm findUserVmByNicId(Long nicId);
347+
346348
Volume findVolumeById(Long volumeId);
347349

348350
Account findAccountByNameDomain(String accountName, Long domainId);
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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 com.cloud.exception.InvalidParameterValueException;
20+
import com.cloud.vm.Nic;
21+
22+
import org.apache.cloudstack.acl.RoleType;
23+
import org.apache.cloudstack.api.APICommand;
24+
import org.apache.cloudstack.api.ApiConstants;
25+
import org.apache.cloudstack.api.ApiErrorCode;
26+
import org.apache.cloudstack.api.BaseAsyncCmd;
27+
import org.apache.cloudstack.api.Parameter;
28+
import org.apache.cloudstack.api.ResponseObject;
29+
import org.apache.cloudstack.api.ServerApiException;
30+
import org.apache.cloudstack.api.response.NicResponse;
31+
import org.apache.cloudstack.api.response.UserVmResponse;
32+
import org.apache.cloudstack.context.CallContext;
33+
import org.apache.commons.lang3.StringUtils;
34+
35+
import com.cloud.event.EventTypes;
36+
import com.cloud.user.Account;
37+
import com.cloud.uservm.UserVm;
38+
39+
import java.util.ArrayList;
40+
import java.util.EnumSet;
41+
42+
@APICommand(name = "updateVmNic", description = "Updates the specified VM NIC", responseObject = NicResponse.class, requestHasSensitiveInfo = false, responseHasSensitiveInfo = false,
43+
authorized = { RoleType.Admin, RoleType.ResourceAdmin, RoleType.DomainAdmin, RoleType.User })
44+
public class UpdateVmNicCmd extends BaseAsyncCmd {
45+
46+
@Parameter(name = ApiConstants.NIC_ID, type = CommandType.UUID, entityType = NicResponse.class, required = true, description = "NIC ID")
47+
private Long nicId;
48+
49+
@Parameter(name = ApiConstants.STATE, type = CommandType.STRING, description = "Whether the NIC link state is enabled or disabled")
50+
private String linkState;
51+
52+
public Long getNicId() {
53+
return nicId;
54+
}
55+
56+
public Nic.LinkState getLinkState() {
57+
if (linkState == null) {
58+
return null;
59+
}
60+
61+
try {
62+
return Nic.LinkState.valueOf(StringUtils.capitalize(StringUtils.lowerCase(linkState)));
63+
} catch (IllegalArgumentException ex) {
64+
throw new InvalidParameterValueException(String.format("Unable to resolve link state [%s] to a supported value (Enabled or Disabled).", linkState));
65+
}
66+
}
67+
68+
@Override
69+
public String getEventType() {
70+
return EventTypes.EVENT_NIC_UPDATE;
71+
}
72+
73+
@Override
74+
public String getEventDescription() {
75+
return String.format("Updating NIC %s.", getResourceUuid(ApiConstants.NIC_ID));
76+
}
77+
78+
@Override
79+
public long getEntityOwnerId() {
80+
UserVm vm = _responseGenerator.findUserVmByNicId(nicId);
81+
if (vm == null) {
82+
return Account.ACCOUNT_ID_SYSTEM;
83+
}
84+
return vm.getAccountId();
85+
}
86+
87+
@Override
88+
public void execute() {
89+
CallContext.current().setEventDetails(String.format("NIC ID: %s", getResourceUuid(ApiConstants.NIC_ID)));
90+
UserVm result = _userVmService.updateVirtualMachineNic(this);
91+
ArrayList<ApiConstants.VMDetails> dc = new ArrayList<ApiConstants.VMDetails>();
92+
dc.add(ApiConstants.VMDetails.valueOf("nics"));
93+
EnumSet<ApiConstants.VMDetails> details = EnumSet.copyOf(dc);
94+
if (result != null){
95+
UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseObject.ResponseView.Restricted, "virtualmachine", details, result).get(0);
96+
response.setResponseName(getCommandName());
97+
this.setResponseObject(response);
98+
} else {
99+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update NIC from VM.");
100+
}
101+
}
102+
}

api/src/main/java/org/apache/cloudstack/api/response/NicResponse.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.List;
2020

21+
import com.cloud.vm.Nic.LinkState;
2122
import org.apache.cloudstack.api.ApiConstants;
2223
import org.apache.cloudstack.api.BaseResponse;
2324
import org.apache.cloudstack.api.EntityReference;
@@ -146,6 +147,10 @@ public class NicResponse extends BaseResponse {
146147
@Param(description = "Public IP address associated with this NIC via Static NAT rule")
147148
private String publicIp;
148149

150+
@SerializedName(ApiConstants.STATE)
151+
@Param(description = "NIC's link state")
152+
private LinkState linkState;
153+
149154
public void setVmId(String vmId) {
150155
this.vmId = vmId;
151156
}
@@ -416,4 +421,12 @@ public void setPublicIpId(String publicIpId) {
416421
public void setPublicIp(String publicIp) {
417422
this.publicIp = publicIp;
418423
}
424+
425+
public LinkState getLinkState() {
426+
return linkState;
427+
}
428+
429+
public void setLinkState(LinkState linkState) {
430+
this.linkState = linkState;
431+
}
419432
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
//
18+
package com.cloud.agent.api;
19+
20+
public class UpdateVmNicAnswer extends Answer {
21+
public UpdateVmNicAnswer() {
22+
}
23+
24+
public UpdateVmNicAnswer(UpdateVmNicCommand cmd, boolean success, String result) {
25+
super(cmd, success, result);
26+
}
27+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
//
18+
package com.cloud.agent.api;
19+
20+
import com.cloud.vm.Nic;
21+
22+
public class UpdateVmNicCommand extends Command {
23+
24+
String nicMacAddress;
25+
String instanceName;
26+
Nic.LinkState linkState;
27+
28+
@Override
29+
public boolean executeInSequence() {
30+
return true;
31+
}
32+
33+
protected UpdateVmNicCommand() {
34+
}
35+
36+
public UpdateVmNicCommand(String nicMacAdderss, String instanceName, Nic.LinkState linkState) {
37+
this.nicMacAddress = nicMacAdderss;
38+
this.instanceName = instanceName;
39+
this.linkState = linkState;
40+
}
41+
42+
public String getNicMacAddress() {
43+
return nicMacAddress;
44+
}
45+
46+
public String getVmName() {
47+
return instanceName;
48+
}
49+
50+
public Nic.LinkState getLinkState() {
51+
return linkState;
52+
}
53+
}

engine/api/src/main/java/com/cloud/vm/VirtualMachineManager.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ NicProfile addVmToNetwork(VirtualMachine vm, Network network, NicProfile request
230230

231231
Boolean updateDefaultNicForVM(VirtualMachine vm, Nic nic, Nic defaultNic);
232232

233+
boolean updateVmNic(VirtualMachine vm, Nic nic, Nic.LinkState linkState) throws ResourceUnavailableException;
234+
233235
/**
234236
* @param vm
235237
* @param network

0 commit comments

Comments
 (0)