carinadigital-hiera-cloudformation 0.0.2.0 → 0.0.2.1

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/Rakefile CHANGED
@@ -20,7 +20,7 @@ require 'rake/testtask'
20
20
 
21
21
  spec = Gem::Specification.new do |gem|
22
22
  gem.name = "carinadigital-hiera-cloudformation"
23
- gem.version = '0.0.2.0'
23
+ gem.version = '0.0.2.1'
24
24
  gem.authors = ["carinadigital"]
25
25
  gem.email = ["carinadigital@gmail.com"]
26
26
  gem.summary = %q{CloudFormation backend for Hiera}
@@ -33,7 +33,7 @@ class Hiera
33
33
 
34
34
  if Config.include?(:cloudformation) && !Config[:cloudformation].nil? then
35
35
  if Config[:cloudformation].fetch(:parse_metadata, false) then
36
- Hiera.debug("Will convert CloudFormation stringified metadata back to numbers or booleans.")
36
+ debug("Will convert CloudFormation stringified metadata back to numbers or booleans.")
37
37
  @parse_metadata = true
38
38
  else
39
39
  @parse_metadata = false
@@ -41,24 +41,24 @@ class Hiera
41
41
 
42
42
  @aws_config = {}
43
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")
44
+ debug("Found AWS access key #{Config[:cloudformation][:access_key_id]} from configuration")
45
45
  @aws_config[:access_key_id] = Config[:cloudformation][:access_key_id]
46
46
  @aws_config[:secret_access_key] = Config[:cloudformation][:secret_access_key]
47
47
  end
48
48
  if Config[:cloudformation].include?(:region) then
49
- Hiera.debug("Found AWS region #{Config[:cloudformation][:region]} from configuration")
49
+ debug("Found AWS region #{Config[:cloudformation][:region]} from configuration")
50
50
  @aws_config[:region] = Config[:cloudformation][:region]
51
51
  end
52
52
 
53
53
  else
54
- Hiera.debug("No configuration found, will fall back to env variables or IAM role")
54
+ debug("No configuration found, will fall back to env variables or IAM role")
55
55
  @cf = AWS::CloudFormation.new
56
56
  end
57
57
 
58
58
  @output_cache = TimedCache.new
59
59
  @resource_cache = TimedCache.new
60
60
 
61
- Hiera.debug("Hiera cloudformation backend loaded")
61
+ debug("Hiera cloudformation backend loaded")
62
62
  end
63
63
 
64
64
 
@@ -72,13 +72,30 @@ class Hiera
72
72
  # Interpolate the value from hiera.yaml
73
73
  if @aws_config.include?(:region)
74
74
  @aws_config[:region] = Backend.parse_answer(@aws_config[:region], scope)
75
- Hiera.debug("Using lookups from region #{@aws_config[:region]} for this run.")
75
+
76
+ # Make an array of valid AWS regions.
77
+ aws_region_names = []
78
+ AWS.regions.each do |region|
79
+ aws_region_names.push(region.name)
80
+ end
81
+
82
+ # Check this is a valid aws region.
83
+ if not aws_region_names.include? @aws_config[:region]
84
+ # If we don't have a region specified then the cloudformation endpoint will be malformed
85
+ # resulting in networking errors.
86
+ # Fail now with a proper error mesage.
87
+ error_message = "[cloudformation_backend]: AWS Region #{@aws_config[:region]} is invalid."
88
+ Hiera.warn(error_message)
89
+ raise Exception, error_message
90
+ end
91
+
92
+ debug("Using lookups from region #{@aws_config[:region]} for this run.")
76
93
  end
77
94
 
78
95
  if @aws_config.length != 0 then
79
96
  @cf = AWS::CloudFormation.new(@aws_config)
80
97
  else
81
- Hiera.debug("No AWS configuration found, will fall back to env variables or IAM role")
98
+ debug("No AWS configuration found, will fall back to env variables or IAM role")
82
99
  @cf = AWS::CloudFormation.new
83
100
  end
84
101
  end
@@ -93,13 +110,13 @@ class Hiera
93
110
  Backend.datasources(scope, order_override) do |elem|
94
111
  case elem
95
112
  when /cfstack\/([^\/]+)\/outputs/
96
- Hiera.debug("Looking up #{key} as an output of stack #{$1}")
113
+ debug("Looking up #{key} as an output of stack #{$1}")
97
114
  raw_answer = stack_output_query($1, key)
98
115
  when /cfstack\/([^\/]+)\/resources\/([^\/]+)/
99
- Hiera.debug("Looking up #{key} in metadata of stack #{$1} resource #{$2}")
116
+ debug("Looking up #{key} in metadata of stack #{$1} resource #{$2}")
100
117
  raw_answer = stack_resource_query($1, $2, key)
101
118
  else
102
- Hiera.debug("#{elem} doesn't seem to be a CloudFormation hierarchy element")
119
+ debug("#{elem} doesn't seem to be a CloudFormation hierarchy element")
103
120
  next
104
121
  end
105
122
 
@@ -133,11 +150,11 @@ class Hiera
133
150
  outputs = @output_cache.get(stack_name)
134
151
 
135
152
  if outputs.nil? then
136
- Hiera.debug("#{stack_name} outputs not cached, fetching...")
153
+ debug("#{stack_name} outputs not cached, fetching...")
137
154
  begin
138
155
  outputs = @cf.stacks[stack_name].outputs
139
156
  rescue AWS::CloudFormation::Errors::ValidationError
140
- Hiera.debug("Stack #{stack_name} outputs can't be retrieved")
157
+ debug("Stack #{stack_name} outputs can't be retrieved")
141
158
  outputs = [] # this is just a non-nil value to serve as marker in cache
142
159
  end
143
160
  @output_cache.put(stack_name, outputs, TIMEOUT)
@@ -152,12 +169,12 @@ class Hiera
152
169
  metadata = @resource_cache.get({:stack => stack_name, :resource => resource_id})
153
170
 
154
171
  if metadata.nil? then
155
- Hiera.debug("#{stack_name} #{resource_id} metadata not cached, fetching")
172
+ debug("#{stack_name} #{resource_id} metadata not cached, fetching")
156
173
  begin
157
174
  metadata = @cf.stacks[stack_name].resources[resource_id].metadata
158
175
  rescue AWS::CloudFormation::Errors::ValidationError
159
176
  # Stack or resource doesn't exist
160
- Hiera.debug("Stack #{stack_name} resource #{resource_id} can't be retrieved")
177
+ debug("Stack #{stack_name} resource #{resource_id} can't be retrieved")
161
178
  metadata = "{}" # This is just a non-nil value to serve as marker in cache
162
179
  end
163
180
  @resource_cache.put({:stack => stack_name, :resource => resource_id}, metadata, TIMEOUT)
@@ -203,6 +220,12 @@ class Hiera
203
220
  return json_object
204
221
  end
205
222
  end
223
+
224
+ # Custom function to wrap our debug messages to make them easier to find in the output.
225
+ def debug(message)
226
+ Hiera.debug("[cloudformation_backend]: #{message}")
227
+ end
228
+
206
229
  end
207
230
  end
208
231
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carinadigital-hiera-cloudformation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2.0
4
+ version: 0.0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-07-08 00:00:00.000000000 Z
12
+ date: 2014-09-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake