【问题标题】:How to turn on/off Azure virtual machine via Azure management api (rest)如何通过 Azure 管理 API 开启/关闭 Azure 虚拟机(休息)
【发布时间】:2017-10-12 16:55:57
【问题描述】:

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

需要什么:

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

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

还有

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

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

【问题讨论】:

  • 添加有关您想要实现的目标的更多详细信息。您尝试发布的链接无效。

标签: c# azure azure-management-api


【解决方案1】:

调用 azure 管理 API 以启动已释放的虚拟机

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

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

我可以在哪里获取 API 方法参数的值(例如订阅 ID、资源 ID 等)。

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

如何处理授权

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

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,请参考。

【讨论】:

  • 工作就像一个魅力!谢谢!
猜你喜欢
  • 2021-12-28
  • 2012-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-18
  • 1970-01-01
  • 2016-11-20
相关资源
最近更新 更多