-
Notifications
You must be signed in to change notification settings - Fork 1.4k
api: Introducing a new diagnostics API command for system VMs for CloudStack admins #2721
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
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
102dce2
This is a new feature for CS that allows Admin users improved
b9222e9
Add whitespace between operators
40ddc85
Add support for Simulator hypervisor
d2d0698
Format file against PEP-8
388dc36
Moving test file to already existing section, and removing redundant …
a84bf42
Moving test to the correct section following name grouping
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
136 changes: 136 additions & 0 deletions
136
api/src/main/java/org/apache/cloudstack/api/command/admin/diagnostics/RunDiagnosticsCmd.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,136 @@ | ||
| // 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.admin.diagnostics; | ||
|
|
||
| import com.cloud.exception.InsufficientCapacityException; | ||
| import com.cloud.exception.ResourceUnavailableException; | ||
| import com.cloud.user.Account; | ||
| import com.cloud.vm.VirtualMachine; | ||
| import org.apache.cloudstack.acl.RoleType; | ||
| import org.apache.cloudstack.api.APICommand; | ||
| import org.apache.cloudstack.api.ApiArgValidator; | ||
| import org.apache.cloudstack.api.ApiConstants; | ||
| import org.apache.cloudstack.api.ApiErrorCode; | ||
| import org.apache.cloudstack.api.BaseCmd; | ||
| import org.apache.cloudstack.api.Parameter; | ||
| import org.apache.cloudstack.api.ServerApiException; | ||
| import org.apache.cloudstack.api.response.RunDiagnosticsResponse; | ||
| import org.apache.cloudstack.api.response.SystemVmResponse; | ||
| import org.apache.cloudstack.context.CallContext; | ||
| import org.apache.cloudstack.diagnostics.DiagnosticsService; | ||
| import org.apache.cloudstack.diagnostics.DiagnosticsType; | ||
| import org.apache.commons.collections.CollectionUtils; | ||
| import org.apache.log4j.Logger; | ||
|
|
||
| import javax.inject.Inject; | ||
| import java.util.Collections; | ||
| import java.util.Map; | ||
|
|
||
| @APICommand(name = RunDiagnosticsCmd.APINAME, responseObject = RunDiagnosticsResponse.class, entityType = {VirtualMachine.class}, | ||
| responseHasSensitiveInfo = false, | ||
| requestHasSensitiveInfo = false, | ||
| description = "Execute network-utility command (ping/arping/tracert) on system VMs remotely", | ||
| authorized = {RoleType.Admin}, | ||
| since = "4.12.0.0") | ||
| public class RunDiagnosticsCmd extends BaseCmd { | ||
| private static final Logger LOGGER = Logger.getLogger(RunDiagnosticsCmd.class); | ||
| public static final String APINAME = "runDiagnostics"; | ||
|
|
||
| @Inject | ||
| private DiagnosticsService diagnosticsService; | ||
|
|
||
| ///////////////////////////////////////////////////// | ||
| //////////////// API parameters ///////////////////// | ||
| ///////////////////////////////////////////////////// | ||
| @Parameter(name = ApiConstants.TARGET_ID, type = CommandType.UUID, required = true, entityType = SystemVmResponse.class, | ||
| validations = {ApiArgValidator.PositiveNumber}, | ||
| description = "The ID of the system VM instance to diagnose") | ||
| private Long id; | ||
|
|
||
| @Parameter(name = ApiConstants.IP_ADDRESS, type = CommandType.STRING, required = true, | ||
| validations = {ApiArgValidator.NotNullOrEmpty}, | ||
| description = "The IP/Domain address to test connection to") | ||
| private String address; | ||
|
|
||
| @Parameter(name = ApiConstants.TYPE, type = CommandType.STRING, required = true, | ||
| validations = {ApiArgValidator.NotNullOrEmpty}, | ||
| description = "The system VM diagnostics type valid options are: ping, traceroute, arping") | ||
| private String type; | ||
|
|
||
| @Parameter(name = ApiConstants.PARAMS, type = CommandType.STRING, | ||
| authorized = {RoleType.Admin}, | ||
| description = "Additional command line options that apply for each command") | ||
| private String optionalArguments; | ||
|
|
||
| ///////////////////////////////////////////////////// | ||
| /////////////////// Accessors /////////////////////// | ||
| ///////////////////////////////////////////////////// | ||
| public Long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public String getAddress() { | ||
| return address; | ||
| } | ||
|
|
||
| public DiagnosticsType getType() { | ||
| DiagnosticsType diagnosticsType = DiagnosticsType.getCommand(type); | ||
| if (diagnosticsType == null) { | ||
| throw new IllegalArgumentException(type + " Is not a valid diagnostics command type. "); | ||
| } | ||
| return diagnosticsType; | ||
| } | ||
|
|
||
| public String getOptionalArguments() { | ||
| return optionalArguments; | ||
| } | ||
|
|
||
| ///////////////////////////////////////////////////// | ||
| /////////////////// Implementation ////////////////// | ||
| ///////////////////////////////////////////////////// | ||
| @Override | ||
| public String getCommandName() { | ||
| return APINAME.toLowerCase() + BaseCmd.RESPONSE_SUFFIX; | ||
| } | ||
|
|
||
| @Override | ||
| public long getEntityOwnerId() { | ||
| Account account = CallContext.current().getCallingAccount(); | ||
| if (account != null) { | ||
| return account.getId(); | ||
| } | ||
| return Account.ACCOUNT_ID_SYSTEM; | ||
| } | ||
|
|
||
| @Override | ||
| public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException { | ||
| RunDiagnosticsResponse response = new RunDiagnosticsResponse(); | ||
| try { | ||
| final Map<String, String> answerMap = diagnosticsService.runDiagnosticsCommand(this); | ||
| if (CollectionUtils.isNotEmpty(Collections.singleton(answerMap))) { | ||
| response.setStdout(answerMap.get(ApiConstants.STDOUT)); | ||
| response.setStderr(answerMap.get(ApiConstants.STDERR)); | ||
| response.setExitCode(answerMap.get(ApiConstants.EXITCODE)); | ||
| response.setObjectName("diagnostics"); | ||
| response.setResponseName(getCommandName()); | ||
| this.setResponseObject(response); | ||
| } | ||
| } catch (final ServerApiException e) { | ||
| throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.getMessage()); | ||
| } | ||
| } | ||
| } |
67 changes: 67 additions & 0 deletions
67
api/src/main/java/org/apache/cloudstack/api/response/RunDiagnosticsResponse.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,67 @@ | ||
| // | ||
| // 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.response; | ||
|
|
||
| import com.cloud.serializer.Param; | ||
| import com.cloud.vm.VirtualMachine; | ||
| import com.google.gson.annotations.SerializedName; | ||
| import org.apache.cloudstack.api.ApiConstants; | ||
| import org.apache.cloudstack.api.BaseResponse; | ||
| import org.apache.cloudstack.api.EntityReference; | ||
|
|
||
| @EntityReference(value = VirtualMachine.class) | ||
| public class RunDiagnosticsResponse extends BaseResponse { | ||
| @SerializedName(ApiConstants.STDOUT) | ||
| @Param(description = "the standard output from the command execution") | ||
| private String stdout; | ||
|
|
||
| @SerializedName(ApiConstants.STDERR) | ||
| @Param(description = "the standard error output from the command execution") | ||
| private String stderr; | ||
|
|
||
| @SerializedName(ApiConstants.EXITCODE) | ||
| @Param(description = "the command execution return code") | ||
| private String exitCode; | ||
|
|
||
| public String getStdout() { | ||
| return stdout; | ||
| } | ||
|
|
||
| public void setStdout(String stdout) { | ||
| this.stdout = stdout; | ||
| } | ||
|
|
||
| public String getStderr() { | ||
| return stderr; | ||
| } | ||
|
|
||
| public void setStderr(String stderr) { | ||
| this.stderr = stderr; | ||
| } | ||
|
|
||
| public String getExitCode() { | ||
| return exitCode; | ||
| } | ||
|
|
||
| public void setExitCode(String exitCode) { | ||
| this.exitCode = exitCode; | ||
| } | ||
|
|
||
| } |
29 changes: 29 additions & 0 deletions
29
api/src/main/java/org/apache/cloudstack/diagnostics/DiagnosticsService.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,29 @@ | ||
| // | ||
| // 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.diagnostics; | ||
|
|
||
| import org.apache.cloudstack.api.command.admin.diagnostics.RunDiagnosticsCmd; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| public interface DiagnosticsService { | ||
|
|
||
| Map<String, String> runDiagnosticsCommand(RunDiagnosticsCmd cmd); | ||
|
|
||
| } |
42 changes: 42 additions & 0 deletions
42
api/src/main/java/org/apache/cloudstack/diagnostics/DiagnosticsType.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,42 @@ | ||
| // | ||
| // 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.diagnostics; | ||
|
|
||
| public enum DiagnosticsType { | ||
| PING("ping"), TRACEROUTE("traceroute"), ARPING("arping"); | ||
|
|
||
| private String value; | ||
|
|
||
| DiagnosticsType(String value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| public String getValue() { | ||
| return value; | ||
| } | ||
|
|
||
| public static DiagnosticsType getCommand(String cmd) { | ||
| for (DiagnosticsType type : DiagnosticsType.values()) { | ||
| if (type.value.equalsIgnoreCase(cmd)) { | ||
| return type; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please keep the name sorted, include your file under an existing test section/block, don't add a new job/runner.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Included test to the section with routers and ssvm tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, you should keep it sorted by name as the file suggests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.