gpt4 book ai didi

c# - 如何通过Azure管理API打开/关闭Azure虚拟机(休息)

转载 作者:行者123 更新时间:2023-11-30 20:29:03 26 4
gpt4 key购买 nike

我想为自己创建一个启动/停止 Azure VM 机器人。我想做的是拥有一个 slack/telegram 机器人,它可以监听消息并通过命令/start/stop 启动/停止我的虚拟机。我应该使用什么 REST api 命令来执行此操作?

需要什么:

一些 C# 示例代码,调用 azure 管理 API 来启动已释放的虚拟机

一些引用,我可以在其中获取 API 方法参数的值(例如订阅 ID、资源 ID 等)。

还有

我已阅读this问题,但它并没有帮助我理解如何处理授权以及从哪里获取这些参数。

我正在使用 C# 语言创建该机器人。

最佳答案

calls azure management API to start deallocated virtual machine

Virtual Machines REST API列出虚拟机上的操作。要启动虚拟机,您可以尝试 this API :

POST https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.Compute/virtualMachines/{vm}/start?api-version={apiVersion}

where I can get values for API method parameters (e.g. subscription id, resource id, etc).

您可以在 Azure 门户上找到 {subscriptionId}{ resourceGroup}

enter image description here

how to deal with authorization

您可以查看this article开始使用 Azure REST 操作和请求身份验证。您可以引用以下代码获取访问 token 。

string tenantId = "{tenantId}";
string clientId = "{clientId}";
string clientSecret = "{secret}";
string subscriptionid = "{subscriptionid}";

string authContextURL = "https://login.windows.net/" + tenantId;
var authenticationContext = new AuthenticationContext(authContextURL);
var credential = new ClientCredential(clientId, clientSecret);
var result = await authenticationContext.AcquireTokenAsync(resource: "https://management.azure.com/", clientCredential: credential);

if (result == null)
{
throw new InvalidOperationException("Failed to obtain the JWT token");
}

string token = result.AccessToken;

此外,本文还解释了如何 create AD application and service principal that can access resources ,请引用。

关于c# - 如何通过Azure管理API打开/关闭Azure虚拟机(休息),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46608254/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com