carinadigital-hiera-cloudformation 0.0.1.4 → 0.0.2.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.
data/LICENSE.txt CHANGED
@@ -1,22 +1,13 @@
1
- Copyright (c) 2013 Hugh Cole-Baker
1
+ Copyright 2013-2014 FanDuel Ltd.
2
2
 
3
- MIT License
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
4
6
 
5
- Permission is hereby granted, free of charge, to any person obtaining
6
- a copy of this software and associated documentation files (the
7
- "Software"), to deal in the Software without restriction, including
8
- without limitation the rights to use, copy, modify, merge, publish,
9
- distribute, sublicense, and/or sell copies of the Software, and to
10
- permit persons to whom the Software is furnished to do so, subject to
11
- the following conditions:
7
+ http://www.apache.org/licenses/LICENSE-2.0
12
8
 
13
- The above copyright notice and this permission notice shall be
14
- included in all copies or substantial portions of the Software.
15
-
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
data/README.md CHANGED
@@ -29,14 +29,20 @@ If you do not add these keys to your configuration file, the access keys will be
29
29
  the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables, or from an IAM
30
30
  instance role (if you are running Hiera on an EC2 instance with an IAM role assigned).
31
31
 
32
- You can also add a region setting to the `:cloudformation` section in
33
- hiera.yaml:
32
+ The AWS region to use can also be configured in the `:cloudformation` section of hiera.yaml.
33
+ You can also tell the backend to convert string literals "true", "false", "3.14", etc to Boolean
34
+ or Number types with the `:parse_metadata` configuration option; this may be useful as
35
+ CloudFormation will convert Booleans and Numbers in the template JSON metadata into Strings when
36
+ retrieved from a stack resource:
34
37
 
35
38
  :cloudformation:
36
- :region: us-west-2
39
+ :region: 'us-west-1'
40
+ :parse_metadata: true
37
41
 
38
- If not specified, the region setting will be looked up from the `AWS_REGION`
39
- environment variable, or `us-west-2` if not set.
42
+ For use in multiple AWS regions, the region can be set by an interpolated variable.
43
+
44
+ :cloudformation:
45
+ :region: %{::examplefact_region}
40
46
 
41
47
  To use this backend you also need to add entries to your "hierarchy" in your hiera.yaml file.
42
48
  If you put an entry of this form in your hierarchy:
@@ -110,3 +116,12 @@ and you include an entry:
110
116
 
111
117
  in your hierarchy, you can query hiera for the key "class::access_key_id" or "class::secret_access_key"
112
118
  and retrieve the attributes of the "MyIAMKey" resource created by CloudFormation.
119
+
120
+
121
+ ## Run tests
122
+
123
+ Requires ruby 1.9+ for minitest
124
+
125
+ ```bash
126
+ rake test
127
+ ```
data/Rakefile CHANGED
@@ -1,5 +1,5 @@
1
1
  =begin
2
- Copyright 2013 FanDuel Ltd.
2
+ Copyright 2013-2014 FanDuel Ltd.
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -16,14 +16,15 @@ limitations under the License.
16
16
 
17
17
  require 'rubygems'
18
18
  require 'rubygems/package_task'
19
+ require 'rake/testtask'
19
20
 
20
21
  spec = Gem::Specification.new do |gem|
21
22
  gem.name = "carinadigital-hiera-cloudformation"
22
- gem.version = '0.0.1.4'
23
+ gem.version = '0.0.2.0'
23
24
  gem.authors = ["carinadigital"]
24
25
  gem.email = ["carinadigital@gmail.com"]
25
26
  gem.summary = %q{CloudFormation backend for Hiera}
26
- gem.description = %q{Forked from hiera-cloudformation. Queries CloudFormation stack outputs or resource metadata for Hiera data.}
27
+ gem.description = %q{Forked from hiera-cloudformation. Multiregion cloudFormation quieries of stack outputs or resource metadata for Hiera.}
27
28
  gem.homepage = "https://github.com/carinadigital/hiera-cloudformation"
28
29
  gem.license = "Apache License (2.0)"
29
30
 
@@ -33,13 +34,16 @@ spec = Gem::Specification.new do |gem|
33
34
  gem.require_paths = ["lib"]
34
35
 
35
36
  gem.add_development_dependency "rake"
36
- gem.add_runtime_dependency "nokogiri", "~> 1.5.11"
37
- gem.add_runtime_dependency "aws-sdk", "~> 1.33.0"
37
+ gem.add_runtime_dependency "aws-sdk", "~> 1.11.2"
38
38
  gem.add_runtime_dependency "timedcache", "~> 0.4.0"
39
39
  gem.add_runtime_dependency "json", "~> 1.8.0"
40
-
41
40
  end
42
41
 
43
42
  Gem::PackageTask.new(spec) do |pkg|
44
43
  pkg.need_tar = true
45
44
  end
45
+
46
+ Rake::TestTask.new do |t|
47
+ t.pattern = 'test/*_test.rb'
48
+ t.verbose = true
49
+ end
@@ -1,5 +1,5 @@
1
1
  =begin
2
- Copyright 2013 FanDuel Ltd.
2
+ Copyright 2013-2014 FanDuel Ltd.
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -31,36 +31,65 @@ class Hiera
31
31
  require 'json'
32
32
  end
33
33
 
34
- if Config.include?(:cloudformation) && !Config[:cloudformation].nil?
35
- aws_config = {}
36
- if Config[:cloudformation].include?(:access_key_id) && Config[:cloudformation].include?(:secret_access_key)
37
- Hiera.debug("Found AWS access key from configuration")
38
- aws_config[:access_key_id] = Config[:cloudformation][:access_key_id]
39
- aws_config[:secret_access_key] = Config[:cloudformation][:secret_access_key]
34
+ if Config.include?(:cloudformation) && !Config[:cloudformation].nil? then
35
+ if Config[:cloudformation].fetch(:parse_metadata, false) then
36
+ Hiera.debug("Will convert CloudFormation stringified metadata back to numbers or booleans.")
37
+ @parse_metadata = true
38
+ else
39
+ @parse_metadata = false
40
40
  end
41
- if Config[:cloudformation].include?(:region)
42
- Hiera.debug("Found AWS region from configuration")
43
- aws_config[:region] = Config[:cloudformation][:region]
41
+
42
+ @aws_config = {}
43
+ if Config[:cloudformation].include?(:access_key_id) && Config[:cloudformation].include?(:secret_access_key) then
44
+ Hiera.debug("Found AWS access key #{Config[:cloudformation][:access_key_id]} from configuration")
45
+ @aws_config[:access_key_id] = Config[:cloudformation][:access_key_id]
46
+ @aws_config[:secret_access_key] = Config[:cloudformation][:secret_access_key]
44
47
  end
45
- if aws_config.length == 0
46
- Hiera.debug("CloudFormation configuration is found, but none of the settings are recognized.")
47
- else
48
- AWS.config(aws_config)
48
+ if Config[:cloudformation].include?(:region) then
49
+ Hiera.debug("Found AWS region #{Config[:cloudformation][:region]} from configuration")
50
+ @aws_config[:region] = Config[:cloudformation][:region]
49
51
  end
52
+
50
53
  else
51
54
  Hiera.debug("No configuration found, will fall back to env variables or IAM role")
55
+ @cf = AWS::CloudFormation.new
52
56
  end
53
57
 
54
- @cf = AWS::CloudFormation.new
55
58
  @output_cache = TimedCache.new
56
59
  @resource_cache = TimedCache.new
57
60
 
58
61
  Hiera.debug("Hiera cloudformation backend loaded")
59
62
  end
60
63
 
64
+
65
+ def create_connection(scope)
66
+
67
+ # If we already have a connection object then return early.
68
+ if defined? @cf then
69
+ return
70
+ end
71
+
72
+ # Interpolate the value from hiera.yaml
73
+ if @aws_config.include?(:region)
74
+ @aws_config[:region] = Backend.parse_answer(@aws_config[:region], scope)
75
+ Hiera.debug("Using lookups from region #{@aws_config[:region]} for this run.")
76
+ end
77
+
78
+ if @aws_config.length != 0 then
79
+ @cf = AWS::CloudFormation.new(@aws_config)
80
+ else
81
+ Hiera.debug("No AWS configuration found, will fall back to env variables or IAM role")
82
+ @cf = AWS::CloudFormation.new
83
+ end
84
+ end
85
+
86
+
61
87
  def lookup(key, scope, order_override, resolution_type)
62
88
  answer = nil
63
89
 
90
+ # Idempotent connection creation.
91
+ create_connection(scope)
92
+
64
93
  Backend.datasources(scope, order_override) do |elem|
65
94
  case elem
66
95
  when /cfstack\/([^\/]+)\/outputs/
@@ -76,6 +105,10 @@ class Hiera
76
105
 
77
106
  next if raw_answer.nil?
78
107
 
108
+ if @parse_metadata then
109
+ raw_answer = convert_metadata(raw_answer)
110
+ end
111
+
79
112
  new_answer = Backend.parse_answer(raw_answer, scope)
80
113
 
81
114
  case resolution_type
@@ -140,6 +173,36 @@ class Hiera
140
173
 
141
174
  return nil
142
175
  end
176
+
177
+ def convert_metadata(json_object)
178
+ if json_object.is_a?(Hash) then
179
+ # convert each value of a Hash
180
+ converted_object = {}
181
+ json_object.each do |key, value|
182
+ converted_object[key] = convert_metadata(value)
183
+ end
184
+ return converted_object
185
+ elsif json_object.is_a?(Array) then
186
+ # convert each item in an Array
187
+ return json_object.map { |item| convert_metadata(item) }
188
+ elsif json_object == "true" then
189
+ # Boolean literals
190
+ return true
191
+ elsif json_object == "false" then
192
+ return false
193
+ elsif json_object == "null" then
194
+ return nil
195
+ elsif /^-?([1-9]\d*|0)(.\d+)?([eE][+-]?\d+)?$/.match(json_object) then
196
+ # Numeric literals
197
+ if json_object.include?('.') then
198
+ return json_object.to_f
199
+ else
200
+ return json_object.to_i
201
+ end
202
+ else
203
+ return json_object
204
+ end
205
+ end
143
206
  end
144
207
  end
145
208
  end
@@ -0,0 +1,51 @@
1
+ require 'minitest/autorun'
2
+ require_relative '../lib/hiera/backend/cloudformation_backend'
3
+
4
+ class ConvertMetadataTest < MiniTest::Unit::TestCase
5
+
6
+ class CloudformationBackendNoInit < Hiera::Backend::Cloudformation_backend
7
+ def initialize
8
+ # nope
9
+ end
10
+ end
11
+
12
+ def setup
13
+ @cfb = CloudformationBackendNoInit.new
14
+ end
15
+
16
+ def test_boolean
17
+ assert_equal true, @cfb.convert_metadata("true")
18
+ assert_equal false, @cfb.convert_metadata("false")
19
+ end
20
+
21
+ def test_nil
22
+ assert_equal nil, @cfb.convert_metadata("null")
23
+ end
24
+
25
+ def test_integer
26
+ assert_equal 1, @cfb.convert_metadata("1")
27
+ assert_equal -1, @cfb.convert_metadata("-1")
28
+ end
29
+
30
+ def test_float
31
+ assert_in_delta 1.0, @cfb.convert_metadata("1.0"), 0.001
32
+ assert_in_delta -1.0, @cfb.convert_metadata("-1.0"), 0.001
33
+ end
34
+
35
+ def test_string
36
+ assert_equal "monkey", @cfb.convert_metadata("monkey")
37
+ assert_equal "1.0.1", @cfb.convert_metadata("1.0.1")
38
+ assert_equal "True", @cfb.convert_metadata("True")
39
+ end
40
+
41
+ def test_array
42
+ expected = [1, 2, 3, [4, 5, 6]]
43
+ assert_equal expected, @cfb.convert_metadata(["1", "2", "3", ["4", "5", "6"]])
44
+ end
45
+
46
+ def test_hash
47
+ expected = {"monkey" => {"fez" => [1, 2, 3]}}
48
+ assert_equal expected, @cfb.convert_metadata({"monkey" => {"fez" => ["1", "2", "3"]}})
49
+ end
50
+
51
+ end
metadata CHANGED
@@ -1,60 +1,52 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carinadigital-hiera-cloudformation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.4
4
+ version: 0.0.2.0
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - carinadigital
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2014-04-04 00:00:00.000000000 Z
12
+ date: 2014-07-08 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: rake
15
16
  requirement: !ruby/object:Gem::Requirement
17
+ none: false
16
18
  requirements:
17
- - - '>='
19
+ - - ! '>='
18
20
  - !ruby/object:Gem::Version
19
21
  version: '0'
20
22
  type: :development
21
23
  prerelease: false
22
24
  version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
23
26
  requirements:
24
- - - '>='
27
+ - - ! '>='
25
28
  - !ruby/object:Gem::Version
26
29
  version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: nokogiri
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ~>
32
- - !ruby/object:Gem::Version
33
- version: 1.5.11
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ~>
39
- - !ruby/object:Gem::Version
40
- version: 1.5.11
41
30
  - !ruby/object:Gem::Dependency
42
31
  name: aws-sdk
43
32
  requirement: !ruby/object:Gem::Requirement
33
+ none: false
44
34
  requirements:
45
35
  - - ~>
46
36
  - !ruby/object:Gem::Version
47
- version: 1.33.0
37
+ version: 1.11.2
48
38
  type: :runtime
49
39
  prerelease: false
50
40
  version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
51
42
  requirements:
52
43
  - - ~>
53
44
  - !ruby/object:Gem::Version
54
- version: 1.33.0
45
+ version: 1.11.2
55
46
  - !ruby/object:Gem::Dependency
56
47
  name: timedcache
57
48
  requirement: !ruby/object:Gem::Requirement
49
+ none: false
58
50
  requirements:
59
51
  - - ~>
60
52
  - !ruby/object:Gem::Version
@@ -62,6 +54,7 @@ dependencies:
62
54
  type: :runtime
63
55
  prerelease: false
64
56
  version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
65
58
  requirements:
66
59
  - - ~>
67
60
  - !ruby/object:Gem::Version
@@ -69,6 +62,7 @@ dependencies:
69
62
  - !ruby/object:Gem::Dependency
70
63
  name: json
71
64
  requirement: !ruby/object:Gem::Requirement
65
+ none: false
72
66
  requirements:
73
67
  - - ~>
74
68
  - !ruby/object:Gem::Version
@@ -76,12 +70,13 @@ dependencies:
76
70
  type: :runtime
77
71
  prerelease: false
78
72
  version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
79
74
  requirements:
80
75
  - - ~>
81
76
  - !ruby/object:Gem::Version
82
77
  version: 1.8.0
83
- description: Forked from hiera-cloudformation. Queries CloudFormation stack outputs
84
- or resource metadata for Hiera data.
78
+ description: Forked from hiera-cloudformation. Multiregion cloudFormation quieries
79
+ of stack outputs or resource metadata for Hiera.
85
80
  email:
86
81
  - carinadigital@gmail.com
87
82
  executables: []
@@ -89,31 +84,34 @@ extensions: []
89
84
  extra_rdoc_files: []
90
85
  files:
91
86
  - lib/hiera/backend/cloudformation_backend.rb
87
+ - test/convert_metadata_test.rb
92
88
  - Rakefile
93
89
  - README.md
94
90
  - LICENSE.txt
95
91
  homepage: https://github.com/carinadigital/hiera-cloudformation
96
92
  licenses:
97
93
  - Apache License (2.0)
98
- metadata: {}
99
94
  post_install_message:
100
95
  rdoc_options: []
101
96
  require_paths:
102
97
  - lib
103
98
  required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
104
100
  requirements:
105
- - - '>='
101
+ - - ! '>='
106
102
  - !ruby/object:Gem::Version
107
103
  version: '0'
108
104
  required_rubygems_version: !ruby/object:Gem::Requirement
105
+ none: false
109
106
  requirements:
110
- - - '>='
107
+ - - ! '>='
111
108
  - !ruby/object:Gem::Version
112
109
  version: '0'
113
110
  requirements: []
114
111
  rubyforge_project:
115
- rubygems_version: 2.0.7
112
+ rubygems_version: 1.8.23
116
113
  signing_key:
117
- specification_version: 4
114
+ specification_version: 3
118
115
  summary: CloudFormation backend for Hiera
119
- test_files: []
116
+ test_files:
117
+ - test/convert_metadata_test.rb
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: 0c4e11fe875d3211e816ad3b21441e5e3903ddd3
4
- data.tar.gz: 8b04f982419a7a5fb2e9c3f8796117ff3c572d95
5
- SHA512:
6
- metadata.gz: 14cf1e677ca9a55547dd6bbabc40228640aedbcab4db542daa892a7d1f34e77f84c154a63bcd28bb96c9975a8ae4304ecddb8a952b64871a61a07cb60a6223a2
7
- data.tar.gz: ca8d9ccef14715cb11821d831c296687ec1a179418bf5a65044317bca73b972914f45d166eba720e5fd2d85431c6d68ac23fb0a5c7df4e02bbd839692ef3fc04