hiera-aws 0.0.8 → 0.1.0

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: 1562b0fa2d2ba741b2f5b12baff4e13567d72849
4
- data.tar.gz: f00a5df6b7c0d3c58ff7f9faff00c2e09aaeab49
3
+ metadata.gz: 6804168e6c111bd7e5edc20f782abbecb230ac19
4
+ data.tar.gz: 50a75291e7d1625444d21fa37d67cd3669edac7d
5
5
  SHA512:
6
- metadata.gz: 048087837fc6f406e5e8e3a70b1b1685114fdfbe80a79fb367437d31c55fe52f8476badd34e3d0e175886b313134f0689985438a43a1fb768359ad1a08cb27e9
7
- data.tar.gz: 2af386fad0a859c8b7d133a9a2a4c12bea04c7c38c5c4402243ecdfdeb63cb4db65713e703135da99f257db728172d36f26b77c845b1206ca55fff6b0085f9f1
6
+ metadata.gz: 4a0045d0fd95522afd2197e21aeccfdacd5e1cca54013aa7315de2cf214ed41fb8610abeec10d144ce74eb00289f360b78817a9ffe705a73ee564cb575d51d6b
7
+ data.tar.gz: 724067dda2420c650282104587d56c444ce776e2ffca8b90f1e7e4579fc2bd9fc839057dc773fb5fe6b0e6018a2ff87c1d7e7373a57afb0b3d93c1403288d17b
data/README.md CHANGED
@@ -97,6 +97,9 @@ cluster_nodes = hiera("memcached_cluster_nodes_for_cfn_stack")
97
97
 
98
98
  ### rds tag=value...
99
99
 
100
+ **Note: "rds" has been superseded by "rds_instances" (see below). It is still
101
+ supported for backward compatibility, but will be removed in a future version.**
102
+
100
103
  Returns an array of all RDS database instances that have one or more tags. The
101
104
  returned array has the format `["host1", "host2"]`.
102
105
 
@@ -113,6 +116,38 @@ rds_instances = hiera("rds environment=dev")
113
116
  rds_instances = hiera("rds environment=production role=mgmt-db")
114
117
  ```
115
118
 
119
+ ### rds_instances tag=value...
120
+
121
+ Returns an array of all RDS database instances that have one or more tags
122
+ assigned.
123
+
124
+ For each instance in the array the following hash is returned:
125
+
126
+ ```json
127
+ {
128
+ "db_instance_identifier" => "some-instance-identifier",
129
+ "endpoint" => {"address" => "some.rds.endpoint", "port" => 3306},
130
+ "engine" => "mysql"
131
+ }
132
+ ```
133
+
134
+ Usage:
135
+
136
+ ```
137
+ # Get all RDS instances
138
+ rds_instances = hiera("rds_instances")
139
+
140
+ # Get all RDS instances that have a tag named "environment" with the value "dev"
141
+ rds_instances = hiera("rds_instances environment=dev")
142
+
143
+ # Get all RDS instances that have two specific tags
144
+ rds_instances = hiera("rds_instances environment=production role=mgmt-db")
145
+
146
+ # Accessing specific properties of the first RDS instance
147
+ $instance_identifier = $rds_instances[0]['db_instance_identifier']
148
+ $endpoint_address = $rds_instances[0]['endpoint']['address']
149
+ ```
150
+
116
151
  ## License and Authors
117
152
 
118
153
  * Author:: Mathias Lafeldt (mathias.lafeldt@jimdo.com)
@@ -37,6 +37,22 @@ class Hiera
37
37
  return nil if fact == :undefined
38
38
  fact
39
39
  end
40
+
41
+ # Inspired by http://devblog.avdi.org/2009/07/14/recursively-symbolize-keys/
42
+ def stringify_keys(hash)
43
+ hash.reduce({}) do |result, (key, value)|
44
+ new_key = case key
45
+ when Symbol then key.to_s
46
+ else key
47
+ end
48
+ new_value = case value
49
+ when Hash then stringify_keys(value)
50
+ else value
51
+ end
52
+ result[new_key] = new_value
53
+ result
54
+ end
55
+ end
40
56
  end
41
57
  end
42
58
  end
@@ -11,21 +11,32 @@ class Hiera
11
11
  end
12
12
 
13
13
  # Override default key lookup to implement custom format. Examples:
14
- # - hiera("rds")
15
- # - hiera("rds environment=dev")
16
- # - hiera("rds role=mgmt-db")
17
- # - hiera("rds environment=production role=mgmt-db")
14
+ # - hiera("rds_instances")
15
+ # - hiera("rds_instances environment=dev")
16
+ # - hiera("rds_instances role=mgmt-db")
17
+ # - hiera("rds_instances environment=production role=mgmt-db")
18
18
  def lookup(key, scope)
19
19
  r = super(key, scope)
20
20
  return r if r
21
21
 
22
22
  args = key.split
23
- if args.shift == "rds"
23
+ subkey = args.shift
24
+
25
+ # TODO: "rds" has been superseded by "rds_instances" but is still
26
+ # supported for backward compatibility. Remove it in a future
27
+ # version.
28
+ if %w(rds rds_instances).include? subkey
24
29
  if args.length > 0
25
30
  tags = Hash[args.map { |t| t.split("=") }]
26
- db_instances_with_tags(tags).map { |i| i[:endpoint][:address] }
31
+ db_instances_with_tags(tags)
27
32
  else
28
- db_instances.map { |i| i[:endpoint][:address] }
33
+ db_instances
34
+ end.map do |i|
35
+ if subkey == "rds"
36
+ i[:endpoint][:address]
37
+ else
38
+ prepare_instance_data(i)
39
+ end
29
40
  end
30
41
  end
31
42
  end
@@ -38,7 +49,7 @@ class Hiera
38
49
 
39
50
  def db_instances_with_tags(tags)
40
51
  db_instances.select do |i|
41
- all_tags = db_instance_tags(i[:db_instance_identifier])
52
+ all_tags = db_instance_tags(i.fetch(:db_instance_identifier))
42
53
  tags.all? { |k, v| tags[k] == all_tags[k] }
43
54
  end
44
55
  end
@@ -51,6 +62,16 @@ class Hiera
51
62
  tags = @client.list_tags_for_resource(:resource_name => db_resource_name(db_instance_id))
52
63
  Hash[tags[:tag_list].map { |t| [t[:key], t[:value]] }]
53
64
  end
65
+
66
+ # Prepare RDS instance data for consumption by Puppet. For Puppet to
67
+ # work, all hash keys have to be converted from symbols to strings.
68
+ def prepare_instance_data(hash)
69
+ {
70
+ "db_instance_identifier" => hash.fetch(:db_instance_identifier),
71
+ "endpoint" => stringify_keys(hash.fetch(:endpoint)),
72
+ "engine" => hash.fetch(:engine)
73
+ }
74
+ end
54
75
  end
55
76
  end
56
77
  end
@@ -1,7 +1,7 @@
1
1
  class Hiera
2
2
  module Backend
3
3
  module Aws # rubocop:disable Documentation
4
- VERSION = "0.0.8"
4
+ VERSION = "0.1.0"
5
5
  end
6
6
  end
7
7
  end
@@ -21,7 +21,7 @@ class Hiera
21
21
  def lookup(key, scope, order_override, resolution_type)
22
22
  answer = nil
23
23
 
24
- Hiera.debug("Looking up #{key} in AWS backend")
24
+ Hiera.debug("Looking up '#{key}' in AWS backend")
25
25
 
26
26
  Backend.datasources(scope, order_override) do |source|
27
27
  Hiera.debug("Looking for data source #{source}")
data/spec/aws_rds_spec.rb CHANGED
@@ -9,15 +9,18 @@ class Hiera
9
9
  :db_instances => [
10
10
  {
11
11
  :db_instance_identifier => "db1",
12
- :endpoint => { :address => "db1.eu-west-1.rds.amazonaws.com" }
12
+ :endpoint => { :address => "db1.eu-west-1.rds.amazonaws.com" },
13
+ :engine => "mysql"
13
14
  },
14
15
  {
15
16
  :db_instance_identifier => "db2",
16
- :endpoint => { :address => "db2.eu-west-1.rds.amazonaws.com" }
17
+ :endpoint => { :address => "db2.eu-west-1.rds.amazonaws.com" },
18
+ :engine => "mysql"
17
19
  },
18
20
  {
19
21
  :db_instance_identifier => "db3",
20
- :endpoint => { :address => "db3.eu-west-1.rds.amazonaws.com" }
22
+ :endpoint => { :address => "db3.eu-west-1.rds.amazonaws.com" },
23
+ :engine => "mysql"
21
24
  }
22
25
  ]
23
26
  }
@@ -53,7 +56,10 @@ class Hiera
53
56
  end
54
57
  end
55
58
 
56
- describe "#lookup" do
59
+ # TODO: "rds" has been superseded by "rds_instances" but is still
60
+ # supported for backward compatibility. Remove it in a future
61
+ # version.
62
+ describe "rds lookup (deprecated)" do
57
63
  let(:scope) { { "aws_account_number" => "12345678" } }
58
64
 
59
65
  it "returns nil if Hiera key is unknown" do
@@ -78,6 +84,78 @@ class Hiera
78
84
  expect(rds.lookup("rds environment=staging", scope)).to eq []
79
85
  end
80
86
  end
87
+
88
+ describe "rds_instances lookup" do
89
+ let(:scope) { { "aws_account_number" => "12345678" } }
90
+
91
+ it "returns nil if Hiera key is unknown" do
92
+ expect(rds.lookup("doge", scope)).to be_nil
93
+ end
94
+
95
+ it "returns all database instances if no tags are provided" do
96
+ expect(rds.lookup("rds_instances", scope)).to eq [
97
+ {
98
+ "db_instance_identifier" => "db1",
99
+ "endpoint" => { "address" => "db1.eu-west-1.rds.amazonaws.com" },
100
+ "engine" => "mysql"
101
+ },
102
+ {
103
+ "db_instance_identifier" => "db2",
104
+ "endpoint" => { "address" => "db2.eu-west-1.rds.amazonaws.com" },
105
+ "engine" => "mysql"
106
+ },
107
+ {
108
+ "db_instance_identifier" => "db3",
109
+ "endpoint" => { "address" => "db3.eu-west-1.rds.amazonaws.com" },
110
+ "engine" => "mysql"
111
+ }
112
+ ]
113
+ end
114
+
115
+ it "returns database instances with role tag" do
116
+ expect(rds.lookup("rds_instances role=mgmt-db", scope)).to eq [
117
+ {
118
+ "db_instance_identifier" => "db2",
119
+ "endpoint" => { "address" => "db2.eu-west-1.rds.amazonaws.com" },
120
+ "engine" => "mysql"
121
+ },
122
+ {
123
+ "db_instance_identifier" => "db3",
124
+ "endpoint" => { "address" => "db3.eu-west-1.rds.amazonaws.com" },
125
+ "engine" => "mysql"
126
+ }
127
+ ]
128
+ end
129
+
130
+ it "returns database instances with environment tag" do
131
+ expect(rds.lookup("rds_instances environment=dev", scope)).to eq [
132
+ {
133
+ "db_instance_identifier" => "db1",
134
+ "endpoint" => { "address" => "db1.eu-west-1.rds.amazonaws.com" },
135
+ "engine" => "mysql"
136
+ },
137
+ {
138
+ "db_instance_identifier" => "db2",
139
+ "endpoint" => { "address" => "db2.eu-west-1.rds.amazonaws.com" },
140
+ "engine" => "mysql"
141
+ }
142
+ ]
143
+ end
144
+
145
+ it "returns database instances with environment and role tags" do
146
+ expect(rds.lookup("rds_instances environment=production role=mgmt-db", scope)).to eq [
147
+ {
148
+ "db_instance_identifier" => "db3",
149
+ "endpoint" => { "address" => "db3.eu-west-1.rds.amazonaws.com" },
150
+ "engine" => "mysql"
151
+ }
152
+ ]
153
+ end
154
+
155
+ it "returns empty array if no database instances can be found" do
156
+ expect(rds.lookup("rds_instances environment=staging", scope)).to eq []
157
+ end
158
+ end
81
159
  end
82
160
  end
83
161
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hiera-aws
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mathias Lafeldt
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-02-25 00:00:00.000000000 Z
12
+ date: 2014-03-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: aws-sdk