快速入门:使用 Ruby 运行你的第一个 Resource Graph 查询Quickstart: Run your first Resource Graph query using Ruby

使用 Azure Resource Graph 的第一步是确保为 Ruby 安装了所需的 gem。The first step to using Azure Resource Graph is to check that the required gems for Ruby are installed. 本快速入门将指导你完成将该 gem 添加到 Ruby 安装的过程。This quickstart walks you through the process of adding the gems to your Ruby installation.

在此过程结束时,你已将该 gem 添加到 Ruby 安装中,并将运行你的第一个 Resource Graph 查询。At the end of this process, you'll have added the gems to your Ruby installation and run your first Resource Graph query.

先决条件Prerequisites

  • 如果没有 Azure 订阅,请在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.
  • Azure 服务主体,包括 clientId 和 clientSecret 。An Azure service principal, including the clientId and clientSecret.

创建 Resource Graph 项目Create the Resource Graph project

若要使 Ruby 能够查询 Azure Resource Graph,则必须将 gem 添加到 GemfileTo enable Ruby to query Azure Resource Graph, the gem must be added to the Gemfile. 此 gem 适用于可使用 Ruby 的任何地方,包括 Windows 10 上的 bash 或本地安装的 bash。This gem works wherever Ruby can be used, bash on Windows 10, or locally installed.

  1. 请确保安装最新的 Ruby(至少为 2.7.1)。Check that the latest Ruby is installed (at least 2.7.1 ). 如果尚未安装,请在 Ruby-Lang.org 下载。If it isn't yet installed, download it at Ruby-Lang.org.

  2. 在你选择的 Ruby 环境中,在新的项目文件夹中初始化一个捆绑包:In your Ruby environment of choice, initialize a bundle in a new project folder:

    # Initialize a bundle to create a new Gemfile
    bundle init
    
  3. 更新 Gemfile 和 Azure Resource Graph 所需的 gem。Update your Gemfile with the gems needed for Azure Resource Graph. 更新的文件应类似于以下示例:The updated file should look similar to this example:

    # frozen_string_literal: true
    
    source "https://rubygems.org"
    
    git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
    
    # gem "rails"
    gem 'azure_mgmt_resourcegraph', '~> 0.17.2'
    
  4. 从项目文件夹中,运行 bundle installFrom the project folder, run bundle install. 确认 gem 是否已通过 bundle list 安装。Confirm the gems were installed with bundle list.

  5. 在同一项目文件夹中,使用以下代码创建 argQuery.rb 并保存更新的文件:In the same project folder, create argQuery.rb with the following code and save the updated file:

    #!/usr/bin/env ruby
    
    require 'azure_mgmt_resourcegraph'
    ARG = Azure::ResourceGraph::Profiles::Latest::Mgmt
    
    # Get arguments and set options
    options = {
        tenant_id: ARGV[0],
        client_id: ARGV[1],
        client_secret: ARGV[2],
        subscription_id: ARGV[3]
    }
    
    # Create Resource Graph client from options
    argClient = ARG::Client.new(options)
    
    # Create Resource Graph QueryRequest for subscription with query
    query_request = ARGModels::QueryRequest.new.tap do |qr|
        qr.subscriptions = [ARGV[3]]
        qr.query = ARGV[4]
    end
    
    # Get the resources from Resource Graph
    response = argClient.resources(query_request)
    
    # Convert data to JSON and output
    puts response.data.to_json
    

运行首个 Resource Graph 查询Run your first Resource Graph query

保存好 Ruby 脚本并准备好使用后,可以尝试简单的 Resource Graph 查询。With the Ruby script saved and ready to use, it's time to try out a simple Resource Graph query. 该查询返回前五个 Azure 资源,以及每个资源的名称和资源类型 。The query returns the first five Azure resources with the Name and Resource Type of each resource.

在对 argQuery 的每次调用中,都包含替换为自己的值时需要使用的变量:In each call to argQuery, there are variables that are used that you need to replace with your own values:

  • {tenantId} - 替换为租户 ID{tenantId} - Replace with your tenant ID
  • {clientId} -替换为服务主体的客户端 ID{clientId} - Replace with the client ID of your service principal
  • {clientSecret} -替换为服务主体的客户端机密{clientSecret} - Replace with the client secret of your service principal
  • {subscriptionId} - 替换为订阅 ID{subscriptionId} - Replace with your subscription ID
  1. 将目录更改为你在其中创建 GemfileargClient.rb 文件的项目文件夹。Change directories to the project folder where you created the Gemfile and argClient.rb files.

  2. 使用 gem 和 resources 方法运行首个 Azure Resource Graph 查询:Run your first Azure Resource Graph query using the gems and the resources method:

    ruby argQuery.rb "{tenantId}" "{clientId}" "{clientSecret}" "{subscriptionId}" "Resources | project name, type | limit 5"
    

    备注

    由于此查询示例未提供排序修饰符(例如 order by),因此多次运行此查询可能会为每个请求生成一组不同的资源。As this query example does not provide a sort modifier such as order by, running this query multiple times is likely to yield a different set of resources per request.

  3. 将最后一个参数更改为 argQuery.rb,并将查询更改为按 Name 属性应用 order byChange the final parameter to argQuery.rb and change the query to order by the Name property:

    ruby argQuery.rb "{tenantId}" "{clientId}" "{clientSecret}" "{subscriptionId}" "Resources | project name, type | limit 5 | order by name asc"
    

    备注

    与第一个查询一样,多次运行此查询可能会为每个请求生成一组不同的资源。Just as with the first query, running this query multiple times is likely to yield a different set of resources per request. 查询命令的顺序非常重要。The order of the query commands is important. 在本例中,order by 位于 limit 之后。In this example, the order by comes after the limit. 命令按此顺序执行,首先会限制查询结果,然后对它们进行排序。This command order first limits the query results and then orders them.

  4. 将最后一个参数更改为 argQuery.rb,并将查询更改为先按 Name 属性应用 order by,然后对前五个结果应用 limitChange the final parameter to argQuery.rb and change the query to first order by the Name property and then limit to the top five results:

    ruby argQuery.rb "{tenantId}" "{clientId}" "{clientSecret}" "{subscriptionId}" "Resources | project name, type | order by name asc | limit 5"
    

假设环境中没有任何变化,则多次运行最后一个查询时,返回的结果将是一致的且按 Name 属性排序,但仍限制为前五个结果。When the final query is run several times, assuming that nothing in your environment is changing, the results returned are consistent and ordered by the Name property, but still limited to the top five results.

清理资源Clean up resources

如果希望从 Ruby 环境中删除已安装的 gem,可使用以下命令执行此操作:If you wish to remove the installed gems from your Ruby environment, you can do so by using the following command:

# Remove the installed gems from the Ruby environment
gem uninstall azure_mgmt_resourcegraph

备注

Gem azure_mgmt_resourcegraph 具有 ms_restms_rest_azure 等依赖项,根据你的环境,这些依赖项也可能已安装。The gem azure_mgmt_resourcegraph has dependencies such as ms_rest and ms_rest_azure that may have also been installed depending on your environment. 如果不再需要这些 gem,还可以将它们卸载。You may uninstall these gems also if no longer needed.

后续步骤Next steps

本快速入门介绍了如何将 Resource Graph gem 添加到 Ruby 环境并运行第一个查询。In this quickstart, you've added the Resource Graph gems to your Ruby environment and run your first query. 若要详细了解 Resource Graph 语言,请继续阅读查询语言详细信息页。To learn more about the Resource Graph language, continue to the query language details page.