Skip to content

Commit 7eea9ed

Browse files
authored
Add API to enable/disable NICs for KVM (#12819)
1 parent 18075ae commit 7eea9ed

File tree

35 files changed

+693
-4
lines changed

35 files changed

+693
-4
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class NicTO extends NetworkTO {
3333
boolean dpdkEnabled;
3434
Integer mtu;
3535
Long networkId;
36+
boolean enabled;
3637

3738
String networkSegmentName;
3839

@@ -154,4 +155,12 @@ public String getNetworkSegmentName() {
154155
public void setNetworkSegmentName(String networkSegmentName) {
155156
this.networkSegmentName = networkSegmentName;
156157
}
158+
159+
public boolean isEnabled() {
160+
return enabled;
161+
}
162+
163+
public void setEnabled(boolean enabled) {
164+
this.enabled = enabled;
165+
}
157166
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,6 @@ public enum ReservationStrategy {
162162
String getIPv6Address();
163163

164164
Integer getMtu();
165+
166+
boolean isEnabled();
165167
}

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+
boolean enabled;
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+
enabled = nic.isEnabled();
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 boolean isEnabled() {
420+
return enabled;
421+
}
422+
423+
public void setEnabled(boolean enabled) {
424+
this.enabled = enabled;
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: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ public class NicResponse extends BaseResponse {
146146
@Param(description = "Public IP address associated with this NIC via Static NAT rule")
147147
private String publicIp;
148148

149+
@SerializedName(ApiConstants.ENABLED)
150+
@Param(description = "whether the NIC is enabled or not")
151+
private Boolean isEnabled;
152+
149153
public void setVmId(String vmId) {
150154
this.vmId = vmId;
151155
}
@@ -416,4 +420,12 @@ public void setPublicIpId(String publicIpId) {
416420
public void setPublicIp(String publicIp) {
417421
this.publicIp = publicIp;
418422
}
423+
424+
public Boolean getEnabled() {
425+
return isEnabled;
426+
}
427+
428+
public void setEnabled(Boolean enabled) {
429+
isEnabled = enabled;
430+
}
419431
}
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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 UpdateVmNicCommand extends Command {
21+
22+
String nicMacAddress;
23+
String instanceName;
24+
Boolean enabled;
25+
26+
@Override
27+
public boolean executeInSequence() {
28+
return true;
29+
}
30+
31+
protected UpdateVmNicCommand() {
32+
}
33+
34+
public UpdateVmNicCommand(String nicMacAdderss, String instanceName, Boolean enabled) {
35+
this.nicMacAddress = nicMacAdderss;
36+
this.instanceName = instanceName;
37+
this.enabled = enabled;
38+
}
39+
40+
public String getNicMacAddress() {
41+
return nicMacAddress;
42+
}
43+
44+
public String getVmName() {
45+
return instanceName;
46+
}
47+
48+
public Boolean isEnabled() {
49+
return enabled;
50+
}
51+
}

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, Boolean enabled) throws ResourceUnavailableException;
234+
233235
/**
234236
* @param vm
235237
* @param network

0 commit comments

Comments
 (0)