terraform_inventory 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6513c4476079efcac4b5b632fbc8513e1a9f9a22
4
- data.tar.gz: 93610af144d41bd561652739891d96867fc07561
3
+ metadata.gz: 3acbc15219138ae1e49064e2474e45835aad33a9
4
+ data.tar.gz: 9f6be89b90e0d44030f82564c1a32484e0b07ecb
5
5
  SHA512:
6
- metadata.gz: cf5ba2d455565aedb88d44c51f025f3d6ecc1145cb94e3d34337fa06ce3902f24bbf8cb5b401add741597a19fde243ea725d8e9b8ea1f0a625adc51669d8eba1
7
- data.tar.gz: b9384ef22c9c57ddac87c8c7f9e0a15e76d4f43b66220c1a92466cb2366e221d5b023d2c2ade4d71e4b55191259b985fb20b7284fd84a30bb2789187b40b0b34
6
+ metadata.gz: 32163d05d1148ff67465f3380931e0b10ec82f6a31d6f3cb6d3be01e8bba3f4ca3227ce9235639ac4efd7cadc24ed9902982a359e7614bd37e5e0352fdd6a886
7
+ data.tar.gz: 4b1b32037fd2c7d54d58d446ddba6e53438b4c512598a64b57bf2f24a3ea3d9cfdd95d9695c1ff924f48d1882ba8fffab526d6872df1ea4ee3941290be8478a3
@@ -3,7 +3,7 @@ require "terraform_inventory/exception"
3
3
 
4
4
  module TerraformInventory
5
5
  class TerraformState
6
- # Find a resource given a resource selector.
6
+ # Find resources given a resource selector.
7
7
  #
8
8
  # A resource selector is composed of the following:
9
9
  # resource_type (ex. aws_instance)
@@ -14,10 +14,15 @@ module TerraformInventory
14
14
  # aws_instance.web.1 - selects a specific aws_instance resource named web
15
15
  # aws_instance.web - selects all aws_instance resources named web
16
16
  #
17
- def find_resource(resource_selector)
17
+ # Returns:
18
+ # [ resource_data ]
19
+ #
20
+ def find_resources(resource_selector)
18
21
  resource_type, resource_name, resource_number = parse_resource_selector(resource_selector)
19
22
 
20
- if resource_number
23
+ if @state[resource_type][resource_name].nil?
24
+ []
25
+ elsif resource_number
21
26
  [@state[resource_type][resource_name][resource_number]]
22
27
  else
23
28
  @state[resource_type][resource_name]
@@ -27,8 +32,13 @@ module TerraformInventory
27
32
  def group_by_host(resource_host_group_mapping)
28
33
  resource_host_group_mapping.reduce({}) do |data, (resource_selector, host_group)| # rubocop:disable Style/EachWithObject
29
34
  data[host_group.to_sym] ||= []
30
- data[host_group.to_sym].concat find_resource(resource_selector)
35
+ resources = find_resources(resource_selector)
36
+
37
+ if resources.nil? || resources.empty? || resources.first.nil?
38
+ raise "Resources matching resource_selector '#{resource_selector}' not found."
39
+ end
31
40
 
41
+ data[host_group.to_sym].concat resources
32
42
  data
33
43
  end
34
44
  end
@@ -1,3 +1,3 @@
1
1
  module TerraformInventory
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -49,7 +49,7 @@ describe TerraformInventory::TerraformState do
49
49
 
50
50
  describe "#find_resource" do
51
51
  context "using a resource_selector without resource_number" do
52
- let(:resources) { state.find_resource("aws_instance.web") }
52
+ let(:resources) { state.find_resources("aws_instance.web") }
53
53
 
54
54
  it "returns the correct resources" do
55
55
  expect(resources.size).to eq(2)
@@ -59,23 +59,45 @@ describe TerraformInventory::TerraformState do
59
59
  end
60
60
 
61
61
  context "using a resource_selector with a resource_number" do
62
- let(:resources) { state.find_resource("aws_instance.web.1") }
62
+ let(:resources) { state.find_resources("aws_instance.web.1") }
63
63
 
64
64
  it "returns the correct resources" do
65
65
  expect(resources.size).to eq(1)
66
66
  expect(resources.first["id"]).to eq("i-b69b2b9a")
67
67
  end
68
68
  end
69
+
70
+ context "using a resource_selector that won't be found" do
71
+ let(:resources) { state.find_resources("aws_instance.freeswitch") }
72
+
73
+ it "returns an empty array" do
74
+ expect(resources.size).to eq(0)
75
+ end
76
+ end
69
77
  end
70
78
 
71
79
  describe "#group_by_host" do
72
- let(:groups) { state.group_by_host Hash["aws_instance.web", "web"] }
80
+ context "when a resource_selector is found" do
81
+ let(:groups) { state.group_by_host Hash["aws_instance.web", "web"] }
73
82
 
74
- it "properly groups resources" do
75
- expect(groups.keys.size).to eq(1)
76
- expect(groups.keys.first).to eq(:web)
77
- expect(groups[:web].first["id"]).to eq("i-1299293e")
78
- expect(groups[:web][1]["id"]).to eq("i-b69b2b9a")
83
+ it "properly groups resources" do
84
+ expect(groups.keys.size).to eq(1)
85
+ expect(groups.keys.first).to eq(:web)
86
+ expect(groups[:web].first["id"]).to eq("i-1299293e")
87
+ expect(groups[:web][1]["id"]).to eq("i-b69b2b9a")
88
+ end
89
+ end
90
+
91
+ context "when a resource_selector isn't found" do
92
+ it "throws an exception" do
93
+ begin
94
+ state.group_by_host Hash["aws_instance.freeswitch", "freeswitch"]
95
+ rescue
96
+ next
97
+ else
98
+ raise "This should have raised an exception"
99
+ end
100
+ end
79
101
  end
80
102
  end
81
103
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: terraform_inventory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tyler Cross
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-14 00:00:00.000000000 Z
11
+ date: 2014-08-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor