-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add API to enable/disable NICs for KVM #12819
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
DaanHoogland
merged 3 commits into
apache:main
from
scclouds:api-to-enable-disable-nics
Mar 31, 2026
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
api/src/main/java/org/apache/cloudstack/api/command/user/vm/UpdateVmNicCmd.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
| package org.apache.cloudstack.api.command.user.vm; | ||
|
|
||
| import com.cloud.exception.InvalidParameterValueException; | ||
| import com.cloud.vm.Nic; | ||
|
|
||
| import org.apache.cloudstack.acl.RoleType; | ||
| import org.apache.cloudstack.api.APICommand; | ||
| import org.apache.cloudstack.api.ApiConstants; | ||
| import org.apache.cloudstack.api.ApiErrorCode; | ||
| import org.apache.cloudstack.api.BaseAsyncCmd; | ||
| import org.apache.cloudstack.api.Parameter; | ||
| import org.apache.cloudstack.api.ResponseObject; | ||
| import org.apache.cloudstack.api.ServerApiException; | ||
| import org.apache.cloudstack.api.response.NicResponse; | ||
| import org.apache.cloudstack.api.response.UserVmResponse; | ||
| import org.apache.cloudstack.context.CallContext; | ||
| import org.apache.commons.lang3.StringUtils; | ||
|
|
||
| import com.cloud.event.EventTypes; | ||
| import com.cloud.user.Account; | ||
| import com.cloud.uservm.UserVm; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.EnumSet; | ||
|
|
||
| @APICommand(name = "updateVmNic", description = "Updates the specified VM NIC", responseObject = NicResponse.class, requestHasSensitiveInfo = false, responseHasSensitiveInfo = false, | ||
| authorized = { RoleType.Admin, RoleType.ResourceAdmin, RoleType.DomainAdmin, RoleType.User }) | ||
| public class UpdateVmNicCmd extends BaseAsyncCmd { | ||
|
|
||
| @Parameter(name = ApiConstants.NIC_ID, type = CommandType.UUID, entityType = NicResponse.class, required = true, description = "NIC ID") | ||
| private Long nicId; | ||
|
|
||
| @Parameter(name = ApiConstants.STATE, type = CommandType.STRING, description = "Whether the NIC link state is enabled or disabled") | ||
| private String linkState; | ||
|
|
||
| public Long getNicId() { | ||
| return nicId; | ||
| } | ||
|
|
||
| public Nic.LinkState getLinkState() { | ||
| if (linkState == null) { | ||
| return null; | ||
| } | ||
|
|
||
| try { | ||
| return Nic.LinkState.valueOf(StringUtils.capitalize(StringUtils.lowerCase(linkState))); | ||
| } catch (IllegalArgumentException ex) { | ||
| throw new InvalidParameterValueException(String.format("Unable to resolve link state [%s] to a supported value (Enabled or Disabled).", linkState)); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public String getEventType() { | ||
| return EventTypes.EVENT_NIC_UPDATE; | ||
| } | ||
|
|
||
| @Override | ||
| public String getEventDescription() { | ||
| return String.format("Updating NIC %s.", getResourceUuid(ApiConstants.NIC_ID)); | ||
| } | ||
|
|
||
| @Override | ||
| public long getEntityOwnerId() { | ||
| UserVm vm = _responseGenerator.findUserVmByNicId(nicId); | ||
| if (vm == null) { | ||
| return Account.ACCOUNT_ID_SYSTEM; | ||
| } | ||
| return vm.getAccountId(); | ||
| } | ||
|
|
||
| @Override | ||
| public void execute() { | ||
| CallContext.current().setEventDetails(String.format("NIC ID: %s", getResourceUuid(ApiConstants.NIC_ID))); | ||
| UserVm result = _userVmService.updateVirtualMachineNic(this); | ||
| ArrayList<ApiConstants.VMDetails> dc = new ArrayList<ApiConstants.VMDetails>(); | ||
| dc.add(ApiConstants.VMDetails.valueOf("nics")); | ||
| EnumSet<ApiConstants.VMDetails> details = EnumSet.copyOf(dc); | ||
| if (result != null){ | ||
| UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseObject.ResponseView.Restricted, "virtualmachine", details, result).get(0); | ||
| response.setResponseName(getCommandName()); | ||
| this.setResponseObject(response); | ||
| } else { | ||
| throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update NIC from VM."); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
core/src/main/java/com/cloud/agent/api/UpdateVmNicAnswer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
| // | ||
| package com.cloud.agent.api; | ||
|
|
||
| public class UpdateVmNicAnswer extends Answer { | ||
| public UpdateVmNicAnswer() { | ||
| } | ||
|
|
||
| public UpdateVmNicAnswer(UpdateVmNicCommand cmd, boolean success, String result) { | ||
| super(cmd, success, result); | ||
| } | ||
| } |
53 changes: 53 additions & 0 deletions
53
core/src/main/java/com/cloud/agent/api/UpdateVmNicCommand.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
| // | ||
| package com.cloud.agent.api; | ||
|
|
||
| import com.cloud.vm.Nic; | ||
|
|
||
| public class UpdateVmNicCommand extends Command { | ||
|
|
||
| String nicMacAddress; | ||
| String instanceName; | ||
| Nic.LinkState linkState; | ||
|
|
||
| @Override | ||
| public boolean executeInSequence() { | ||
| return true; | ||
| } | ||
|
|
||
| protected UpdateVmNicCommand() { | ||
| } | ||
|
|
||
| public UpdateVmNicCommand(String nicMacAdderss, String instanceName, Nic.LinkState linkState) { | ||
| this.nicMacAddress = nicMacAdderss; | ||
| this.instanceName = instanceName; | ||
| this.linkState = linkState; | ||
| } | ||
|
|
||
| public String getNicMacAddress() { | ||
| return nicMacAddress; | ||
| } | ||
|
|
||
| public String getVmName() { | ||
| return instanceName; | ||
| } | ||
|
|
||
| public Nic.LinkState getLinkState() { | ||
| return linkState; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.