验证并获取ruby中azure的运行实例计数

时间:2017-05-12 17:46:08

标签: ruby azure

如何在ruby中获取Azure的运行实例数,我在AWS中看到的内容与How to enumerate running ec2 instances and load them into a database using ruby?相当。

由于

这是我做的:

LN300

回应:

azureAuth.rb:35:subscription_id= 'xyz' provider = MsRestAzure::ApplicationTokenProvider.new(tenant_id, client_id, secret) credentials = MsRest::TokenCredentials.new(provider) client = Azure::ARM::Resources::ResourceManagementClient.new(credentials) client.subscription_id = subscription_id resource_group_params = Azure::ARM::Resources::Models::ResourceGroup.new() resource_group_params.location = 'westus' promise = client.resource_groups.create_or_update('new_test_resource_group',resource_group_params) result = promise.value! resource_group_params = result.body p resource_group_params.name p resource_group_params.id 值!' #(NoMethodError)

1 个答案:

答案 0 :(得分:0)

从描述中,很难知道这个"运行实例"。您是指Azure Web应用程序还是Azure云服务实例?根据您的代码段,您似乎想要创建Azure资源组并获取一些相关信息。对于这种情况,我建议您按照Azure官方网站中的示例代码进行操作:Manage Azure resources and resource groups with Ruby。希望它可以给你一些提示。

[更新]

我很抱歉,我没有测试示例代码并为您提供解决方案。

  

仍然是抛出错误" nil:NilClass(NoMethodError)

根据我的测试,我认为你没有创建azure资源组,所以promise对象是nil。请尝试列出资源组操作client.resource_groups.list.each{ |group| print_item(group) }我认为你也会得到零问题。当我深入研究ms_rest_azure时,我甚至无法获得令牌。当我更改下面的代码(application_token_provider.rb)时,我可以获得令牌

1)request_body['{resource_uri}'] = "https%3A%2F%2Fmanagement.azure.com%2F" #ERB::Util.url_encode(@settings.token_audience)。使用management.azure.com而不是management.core.windows.net

2)response = connection.post do |request|将方法更改为post方法。

3):ssl => MsRest.ssl_options更改为:ssl => {:verify => false}

我建议你使用rest API来达到同样的目的。下面是我基于rest API的Ruby代码。希望它有所帮助。

  connection = Faraday.new(:url => 'https://login.windows.net/<telnet id>/oauth2/token',:ssl => {:verify => false}) do |builder|
    builder.adapter Faraday.default_adapter
 end
 response = connection.post do |request|
    request.headers['Content-Type'] = 'application/x-www-form-urlencoded'
    request.body = 'resource=https%3A%2F%2Fmanagement.azure.com%2F&client_id=<your client id>&client_secret=<your client secret>&grant_type=client_credentials'
 end 
  response_body = JSON.load(response.body)
  @token = response_body['access_token']
  getResrouceCon = Faraday.new(:url => 'https://management.azure.com/subscriptions/<subscriotion id>/resourcegroups?api-version=2015-01-01',:ssl => {:verify => false}) do |builder|     
    builder.adapter Faraday.default_adapter
 end
 response_resource = getResrouceCon.get do |req|
     req.headers["Authorization"] = "bearer #{@token}"
 end
 puts "response #{response_resource.body}"

注意:我使用的是最新版本azure_mgmt_resource

[更新]

我们可以从Azure resource portal找到Azure经典云服务。我们可以从配置中获取Azure云服务的大量有用信息。这是截图:

enter image description here

如果您想获得角色和角色实例。我们可以在Azure资源门户https://management.azure.com/subscriptions/<subscription id>/resourceGroups/<group name>/providers/Microsoft.ClassicCompute/domainNames/<cloud service name>/slots/Production?api-version=2016-04-01

中使用此rest API

我们只需要修改我之前的答案。以下是可以获得云服务配置的代码:

 getResrouceCon = Faraday.new(:url => 'https://management.azure.com/subscriptions/<subscription id>/resourceGroups/<group name>/providers/Microsoft.ClassicCompute/domainNames/<cloud service name>/slots/Production?api-version=2016-04-01',:ssl => {:verify => false}) do |builder|     
    builder.adapter Faraday.default_adapter
 end
 response_resource = getResrouceCon.get do |req|
     req.headers["Authorization"] = "bearer #{@token}"
 end
 puts "response #{response_resource.body}"