我需要以编程方式查找当前角色正在运行的当前区域(例如“West US”或“East US”)。有没有API可以找到它?
答案 0 :(得分:2)
如果您使用Management Api,则只能获取该信息。
通过REST或您可以使用c#Windows Azure管理库(在nuget上预先发布)。
但请注意,您需要设置管理证书才能获取信息。
更简单的替代方法是在云服务中创建设置,并在创建部署配置时设置值。我这样做,并为我目标的区域进行部署配置。
using( var azure = CloudContext.Clients.CreateComputeManagementClient(...))
{
var service = await azure.HostedServices.GetDetailedAsync("servicename");
// service.Properties.Location
// service.Properties.AffinityGroup;
}
using(var azure = CloudContext.Clients.CreateManagementClient(...))
{
var affinityGroup = await azure.AffinityGroups.GetAsync("name",new CancellationToken());
// affinityGroup.Location
}
这里......是凭证,管理证书或WAAD oauth令牌。 (ADAL:Active Directory身份验证库)可用于令牌。
这是从证书中获取凭据的代码:
public static CertificateCloudCredentials GetCertificateCloudCredentials(
string certificateThumbprint, string subscriptionId)
{
var certificate = CertificateHelper.LoadCertificate(
StoreName.My,
StoreLocation.LocalMachine,
certificateThumbprint);
if (certificate == null)
throw new Exception(
string.Format("Certificate with thumbprint '{0}' not found",
certificateThumbprint));
var cred = new CertificateCloudCredentials(
subscriptionId,
certificate
);
return cred;
}
答案 1 :(得分:2)
考虑在服务管理API中使用Get Cloud Service。当您提供角色所属的服务时,您可以检索类似于以下内容的响应。请注意我已加星标的位置字段。
<?xml version="1.0" encoding="utf-8"?>
<HostedService xmlns="http://schemas.microsoft.com/windowsazure">
<Url>hosted-service-url</Url>
<ServiceName>hosted-service-name</ServiceName>
<HostedServiceProperties>
<Description>description</Description>
<AffinityGroup>name-of-affinity-group</AffinityGroup>
**<Location>location-of-service</Location >**
<Label>base-64-encoded-name-of-service</Label>
<Status>current-status-of-service</Status>
<DateCreated>creation-date-of-service</DateCreated>
<DateLastModified>last-modification-date-of-service</DateLastModified>
<ExtendedProperties>
<ExtendedProperty>
<Name>name-of-property</Name>
<Value>value-of-property</Value>
</ExtendedProperty>
</ExtendedProperties>
<GuestAgentType>type-of-guest-agent</GuestAgentType>
</HostedServiceProperties>
<DefaultWinRmCertificateThumbprint>thumbprint-of-winrm-certificate</DefaultWinRmCertificateThumbprint>
</HostedService>
答案 2 :(得分:0)
该信息可从Azure Instance Metadata Service (IMDS)获得。 Azure公共云中运行的任何VM的REST终结点为http://169.254.169.254/metadata/instance?api-version=2017-04-02
。元数据对象包含两个子对象,一个用于“计算”,一个用于“网络”。区域名称显示在“计算”对象的“位置”成员中。
Microsoft/azureimds repo on github提供了用于访问IMDS数据的各种元素的多种语言的示例代码。通过IMDS API的2018-10-01版本可以获得比我在此显示的更多的信息;有关详细信息,请参见IMDS docs。
$ curl -s -H Metadata:True "http://169.254.169.254/metadata/instance?api-version=2017-04-02&format=json" | jq .
{
"compute": {
"location": "westus2",
"name": "samplevm",
"offer": "UbuntuServer",
"osType": "Linux",
"platformFaultDomain": "0",
"platformUpdateDomain": "0",
"publisher": "Canonical",
"sku": "18.04-LTS",
"version": "18.04.201904020",
"vmId": "(redacted)",
"vmSize": "Standard_D2s_v3"
},
"network": {
"interface": [
{
"ipv4": {
"ipAddress": [
{
"privateIpAddress": "10.0.0.7",
"publicIpAddress": ""
}
],
"subnet": [
{
"address": "10.0.0.0",
"prefix": "24"
}
]
},
"ipv6": {
"ipAddress": []
},
"macAddress": "(redacted)"
}
]
}
}