ruby - 遍历aws实例/卷并创建嵌套哈希结构

时间:2016-04-26 06:11:20

标签: ruby aws-sdk

使用aws-sdk,我需要发现instance_ids和所有附加的卷。然后我想迭代这些卷。

@instances包含实例ID列表。这是必要的,因为首先需要在客户端完成过滤。

@ec2 = Aws::EC2::Client.new

@instances.each do |instance_id|
    resp =  @ec2.describe_volumes({ 
        filters: [{
           name: 'attachment.instance-id',
           values: [instance_id]
        }]
    }).volumes
    resp.each do |volumes|
        (@volumes[instance_id] ||= []) << volumes.volume_id
    end
end

这很好用。我的输出类似于:

{
  "i-11111111": [
     "vol-xxxxxxxx",
     "vol-xxxxxxxx",
     "vol-xxxxxxxx"
  ],
  "i-22222222": [
     "vol-xxxxxxxx",
     "vol-xxxxxxxx",
     "vol-xxxxxxxx",
     "vol-xxxxxxxx",
     "vol-xxxxxxxx"
  ],
  "i-33333333": [
     "vol-xxxxxxxx"
  ],
  "i-44444444": [
     "vol-xxxxxxxx",
     "vol-xxxxxxxx"
  ]
}

问题是我想更深入,并将卷数组更改为具有该键的volume-id的哈希值。

https://docs.aws.amazon.com/sdkforruby/api/Aws/EC2/Client.html#describe_volumes-instance_method

看起来像

{
  "i-11111111": {
     "vol-xxxxxxxx": [
         device: "/dev/blah1",
         attach_time: "blah"
     ],
     "vol-xxxxxxxx": [
         device: "/dev/blah2",
         attach_time: "blah"
     ]
  },
  "i-22222222": {
     "vol-xxxxxxxx": [
         device: "/dev/blah1",
         attach_time: "blah"
     ],
     "vol-xxxxxxxx": [
         device: "/dev/blah2",
         attach_time: "blah"
     ],
     "vol-xxxxxxxx": [
         device: "/dev/blah3",
         attach_time: "blah"
     ]
   }
}

然后我可以在哈希中查询所需的信息(假设可能吗?)

感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

我认为你可以这样做:

{{#myComponent  param1="xyz" param2=true as |section|}}

这假定:

  • resp.each do |volumes| volume_ids = volumes.map(&:volume_id) descriptions = @ec2.describe_volumes(volume_ids: volume_ids) volume_hash = (@volumes[instance_id] ||= {}) descriptions.each { |desc| volume_hash[desc.volume_id] = desc } end 选项的格式是数组;我从文档中不清楚它应该是数组还是字符串

  • volume_ids API文档中列出的所有其他选项都不是必需的

答案 1 :(得分:0)

"~/test.conf"