terraform-wrapper 0.2.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 66bb2630f4f5580fdf738902d4807b9584572ef131a01d685eecbe2a067f023c
4
- data.tar.gz: efcd70b4c39b0870f2cce7461c03861425262347d565c80d3da90841f4eb6bf2
3
+ metadata.gz: cec70134c0df1674effb0b5e07ffaa3ff7d1a2ca034dd6452563f616e67e09c1
4
+ data.tar.gz: b4c6416feea1f68b3db4f593f7e2692a7553869977dad45aa0da045477841add
5
5
  SHA512:
6
- metadata.gz: fede831e98a731bf9145c90bd54d51cbabd51a76337d7360cec08fe59c3bc464d526584b0ea1dbaa34a1daab63137b8c7389c8c2d043ebb297a9bc47dadb4433
7
- data.tar.gz: c6852721632b1fe7384127baecabd11d32cab7966f7ab40b804761211f18902118a7f966bdbdf78d0dc24c53374fa504d0f3bd77f8a63b105242dac51f14b878
6
+ metadata.gz: 0cb74ef36d96167259fec72539932d7614a25ad2f442fd75940bdaff3fe63b17aa7a793f17f90368c930112d7ab433950bf0c900ed46694d4dfd8e1cba3d311b
7
+ data.tar.gz: '090b646a9d688e4e12149752cffa4f71414a47c70b4ec619a7a142e342e9ece421e929baf42045b767c896666406317f0aff602b12d35d123b767bf87793b303'
@@ -4,7 +4,7 @@ module TerraformWrapper
4
4
 
5
5
  ###############################################################################
6
6
 
7
- def self.create_directory(directory:, purpose: nil, exists: true)
7
+ def self.create_directory(directory:, exists: true, purpose: nil)
8
8
  return exists if File.directory?(directory)
9
9
 
10
10
  result = false
@@ -20,6 +20,17 @@ module TerraformWrapper
20
20
  result = File.directory?(directory)
21
21
  end
22
22
 
23
+ ###############################################################################
24
+
25
+ def self.find(base:, name:, exts:, description: "File")
26
+ exts.each do |ext|
27
+ path = File.join(base, name + ext)
28
+ return path if File.file?(path)
29
+ end
30
+
31
+ logger.fatal("#{description} name: #{name} not found in location: #{base}!")
32
+ end
33
+
23
34
  ###############################################################################
24
35
 
25
36
  end
@@ -35,8 +35,8 @@ module TerraformWrapper
35
35
 
36
36
  ###############################################################################
37
37
 
38
- def initialize(code:, config:, options:, service:, variables:)
39
- construct(code: code, config: config, options: options, service: service, variables: variables)
38
+ def initialize(options:, variables:)
39
+ construct(options: options, variables: variables)
40
40
  end
41
41
 
42
42
  ###############################################################################
@@ -151,12 +151,12 @@ module TerraformWrapper
151
151
  end
152
152
 
153
153
  begin
154
- subscription = subscription % @variables
155
- keyvault = keyvault % @variables unless keyvault.nil?
156
- username = username % @variables unless keyvault.nil?
157
- password = password % @variables unless keyvault.nil?
154
+ subscription = subscription % @variables.identifiers
155
+ keyvault = keyvault % @variables.identifiers unless keyvault.nil?
156
+ username = username % @variables.identifiers unless keyvault.nil?
157
+ password = password % @variables.identifiers unless keyvault.nil?
158
158
  rescue
159
- logger.fatal("Azure authenticator options contain variables that are not included in the configuration file!")
159
+ logger.fatal("Azure authenticator options contain identifiers that are not included in the configuration file!")
160
160
  end
161
161
 
162
162
  details = subscription_details(subscription: subscription)
@@ -29,7 +29,7 @@ module TerraformWrapper
29
29
 
30
30
  ###############################################################################
31
31
 
32
- def initialize(code:, config:, options:, service:, variables:)
32
+ def initialize(options:, variables:)
33
33
  logger.fatal("This class should not be used directly! Please create an authenticator-specific class instead!")
34
34
  end
35
35
 
@@ -59,13 +59,9 @@ module TerraformWrapper
59
59
 
60
60
  ###############################################################################
61
61
 
62
- def construct(code:, config:, options:, service:, variables:)
62
+ def construct(options:, variables:)
63
63
  @options = options
64
- @variables = variables.values.merge({
65
- component: code.name,
66
- config: config,
67
- service: service
68
- })
64
+ @variables = variables
69
65
 
70
66
  specific
71
67
  end
@@ -35,8 +35,8 @@ module TerraformWrapper
35
35
 
36
36
  ###############################################################################
37
37
 
38
- def initialize(code:, config:, options:, service:, variables:)
39
- construct(code: code, config: config, options: options, service: service, variables: variables)
38
+ def initialize(options:, variables:)
39
+ construct(options: options, variables: variables)
40
40
  end
41
41
 
42
42
  ###############################################################################
@@ -101,18 +101,18 @@ module TerraformWrapper
101
101
  logger.fatal("AWS backend role to assume ARN must not be blank if specified!") if role.strip.empty?
102
102
  end
103
103
 
104
- logger.fatal("AWS backend S3 bucket name or key must include %{service}.") unless (bucket.include?("%{service}") or key.include?("%{service}"))
105
- logger.fatal("AWS backend S3 bucket name or key must include %{config}.") unless (bucket.include?("%{config}") or key.include?("%{config}"))
106
- logger.fatal("AWS backend S3 bucket name or key must include %{component}.") unless (bucket.include?("%{component}") or key.include?("%{component}"))
104
+ @variables.core.keys.map{ |sym| sym.to_s }.each do |core|
105
+ logger.fatal("AWS backend S3 bucket name or key must include %{#{core}}.") unless (bucket.include?("%{#{core}}") or key.include?("%{#{core}}"))
106
+ end
107
107
 
108
108
  begin
109
- bucket = bucket % @variables
110
- region = region % @variables
111
- key = key % @variables
112
- kms = kms % @variables unless kms.nil?
113
- role = role % @variables unless role.nil?
109
+ bucket = bucket % @variables.identifiers
110
+ region = region % @variables.identifiers
111
+ key = key % @variables.identifiers
112
+ kms = kms % @variables.identifiers unless kms.nil?
113
+ role = role % @variables.identifiers unless role.nil?
114
114
  rescue
115
- logger.fatal("AWS backend options contain variables that are not included in the configuration file!")
115
+ logger.fatal("AWS backend options contain identifiers that are not included in the configuration file!")
116
116
  end
117
117
 
118
118
  logger.fatal("Key: #{key} is too long for backend of type: #{@@type}") if key.length > 1024
@@ -31,8 +31,8 @@ module TerraformWrapper
31
31
 
32
32
  ###############################################################################
33
33
 
34
- def initialize(code:, config:, options:, service:, variables:)
35
- construct(code: code, config: config, options: options, service: service, variables: variables)
34
+ def initialize(options:, variables:)
35
+ construct(options: options, variables: variables)
36
36
  end
37
37
 
38
38
  ###############################################################################
@@ -75,17 +75,17 @@ module TerraformWrapper
75
75
  logger.fatal("Azure backend storage account key must be a string!") unless key.kind_of?(String)
76
76
  logger.fatal("Azure backend storage account key must not be blank!") if key.strip.empty?
77
77
 
78
- logger.fatal("Azure backend container or key must include %{service}.") unless (container.include?("%{service}") or key.include?("%{service}"))
79
- logger.fatal("Azure backend container or key must include %{config}.") unless (container.include?("%{config}") or key.include?("%{config}"))
80
- logger.fatal("Azure backend container or key must include %{component}.") unless (container.include?("%{component}") or key.include?("%{component}"))
78
+ @variables.core.keys.map{ |sym| sym.to_s }.each do |core|
79
+ logger.fatal("Azure backend container or key must include %{#{core}}.") unless (container.include?("%{#{core}}") or key.include?("%{#{core}}"))
80
+ end
81
81
 
82
82
  begin
83
- group = group % @variables
84
- account = account % @variables
85
- container = container % @variables
86
- key = key % @variables
83
+ group = group % @variables.identifiers
84
+ account = account % @variables.identifiers
85
+ container = container % @variables.identifiers
86
+ key = key % @variables.identifiers
87
87
  rescue
88
- logger.fatal("Azure backend options contain variables that are not included in the configuration file!")
88
+ logger.fatal("Azure backend options contain identifiers that are not included in the configuration file!")
89
89
  end
90
90
 
91
91
  if key.length > 1024 then
@@ -30,7 +30,7 @@ module TerraformWrapper
30
30
 
31
31
  ###############################################################################
32
32
 
33
- def initialize(code:, config:, options:, service:, variables:)
33
+ def initialize(options:, variables:)
34
34
  logger.fatal("This class should not be used directly! Please create a backend-specific class instead!")
35
35
  end
36
36
 
@@ -54,13 +54,9 @@ module TerraformWrapper
54
54
 
55
55
  ###############################################################################
56
56
 
57
- def construct(code:, config:, options:, service:, variables:)
57
+ def construct(options:, variables:)
58
58
  @options = options
59
- @variables = variables.values.merge({
60
- component: code.name,
61
- config: config,
62
- service: service
63
- })
59
+ @variables = variables
64
60
 
65
61
  specific
66
62
  end
@@ -28,8 +28,8 @@ module TerraformWrapper
28
28
 
29
29
  ###############################################################################
30
30
 
31
- def initialize(code:, config:, options:, service:, variables:)
32
- construct(code: code, config: config, options: options, service: service, variables: variables)
31
+ def initialize(options:, variables:)
32
+ construct(options: options, variables: variables)
33
33
  end
34
34
 
35
35
  ###############################################################################
@@ -52,14 +52,15 @@ module TerraformWrapper
52
52
  logger.fatal("Local backend path must be a string!") unless path.kind_of?(String)
53
53
  logger.fatal("Local backend path must not be blank!") if path.strip.empty?
54
54
 
55
- logger.fatal("Local backend path must include %{service} or the path to this repository.") unless (path.include?("%{service}") or path.include?(Dir.pwd))
56
- logger.fatal("Local backend path must include %{config}") unless path.include?("%{config}")
57
- logger.fatal("Local backend path must include %{component}") unless path.include?("%{component}")
55
+ @variables.core.keys.map{ |sym| sym.to_s }.each do |core|
56
+ next if (core == "service") and (path.include?(Dir.pwd))
57
+ logger.fatal("Local backend path must include %{#{core}}.") unless path.include?("%{#{core}}")
58
+ end
58
59
 
59
60
  begin
60
- path = path % @variables
61
+ path = path % @variables.identifiers
61
62
  rescue
62
- logger.fatal("Local backend options contain variables that are not included in the configuration file!")
63
+ logger.fatal("Local backend options contain identifiers that are not included in the configuration file!")
63
64
  end
64
65
 
65
66
  directory = File.dirname(path)
@@ -22,11 +22,6 @@ module TerraformWrapper
22
22
 
23
23
  @@config_exts = [ "", ".yaml", ".yml" ]
24
24
 
25
- ###############################################################################
26
-
27
- @@variable_files_name = "tfvars"
28
- @@variable_files_exts = [ ".tfvars" ]
29
-
30
25
  ###############################################################################
31
26
 
32
27
  attr_reader :auths
@@ -36,7 +31,6 @@ module TerraformWrapper
36
31
  attr_reader :name
37
32
  attr_reader :path
38
33
  attr_reader :service
39
- attr_reader :variable_files
40
34
  attr_reader :variables
41
35
 
42
36
  ###############################################################################
@@ -75,65 +69,37 @@ module TerraformWrapper
75
69
  backend_options = options["backend-options"]
76
70
 
77
71
  @code = code
78
- @path = find(base: @base, name: @name, exts: @@config_exts, description: "Configuration")
72
+ @path = ::TerraformWrapper.find(base: @base, name: @name, exts: @@config_exts, description: "Configuration")
79
73
 
80
74
  yaml = YAML.load(File.read(@path))
81
75
  logger.fatal("Invalid YAML in configuration file: #{@path}") unless yaml.kind_of?(Hash)
82
76
 
83
- if yaml.key?("variables") then
84
- logger.fatal("Key 'variables' is not a hash in configuration file: #{@path}") unless yaml["variables"].kind_of?(Hash)
85
- @variables = TerraformWrapper::Shared::Variables.new(values: yaml["variables"])
86
- else
87
- @variables = TerraformWrapper::Shared::Variables.new()
88
- end
77
+ identifiers = yaml.key?("identifiers") ? yaml["identifiers"] : Hash.new
78
+ @variables = TerraformWrapper::Shared::Variables.new(config: @name, component: @code.name, service: @service, identifiers: identifiers)
79
+ @variables.add_variables(variables: yaml["globals"]) if yaml.key?("globals")
80
+
81
+ if yaml.key?("terraform") then
82
+ logger.fatal("Key 'terraform' is not a hash in configuration file: #{@path}") unless yaml["terraform"].kind_of?(Hash)
83
+ terraform = yaml["terraform"]
89
84
 
90
- @variable_files = yaml.key?("terraform") ? validate(variable_files: yaml["terraform"]) : Array.new
85
+ @variables.add_variables(variables: terraform["variables"]) if terraform.key?("variables")
86
+ @variables.add_files(base: @base, files: terraform["files"]) if terraform.key?("files")
87
+ end
91
88
 
92
89
  @auths = Array.new
93
- @auths.append(TerraformWrapper::Shared::Auths::Azure.new(code: @code, config: @name, options: auth_azure_options, service: @service, variables: @variables)) if auth_azure
90
+ @auths.append(TerraformWrapper::Shared::Auths::Azure.new(options: auth_azure_options, variables: @variables)) if auth_azure
94
91
 
95
92
  if backend == "local" then
96
- @backend = TerraformWrapper::Shared::Backends::Local.new(code: @code, config: @name, options: backend_options, service: @service, variables: @variables)
93
+ @backend = TerraformWrapper::Shared::Backends::Local.new(options: backend_options, variables: @variables)
97
94
  elsif backend == "aws" then
98
- @backend = TerraformWrapper::Shared::Backends::AWS.new(code: @code, config: @name, options: backend_options, service: @service, variables: @variables)
95
+ @backend = TerraformWrapper::Shared::Backends::AWS.new(options: backend_options, variables: @variables)
99
96
  elsif backend == "azure" then
100
- @backend = TerraformWrapper::Shared::Backends::Azure.new(code: @code, config: @name, options: backend_options, service: @service, variables: @variables)
97
+ @backend = TerraformWrapper::Shared::Backends::Azure.new(options: backend_options, variables: @variables)
101
98
  else
102
99
  logger.fatal("Backend: #{backend} is not valid!")
103
100
  end
104
101
  end
105
102
 
106
- ###############################################################################
107
-
108
- private
109
-
110
- ###############################################################################
111
-
112
- def find(base:, name:, exts:, description: "File")
113
- exts.each do |ext|
114
- path = File.join(base, name + ext)
115
- return path if File.file?(path)
116
- end
117
-
118
- logger.fatal("#{description} name: #{@name} not found in location: #{@base}!")
119
- end
120
-
121
- ###############################################################################
122
-
123
- def validate(variable_files:)
124
- logger.fatal("Optional key 'variable_files' must be a list of strings!") unless variable_files.kind_of?(Array)
125
-
126
- result = Array.new
127
-
128
- variable_files.each do |variable_file|
129
- logger.fatal("All elements of 'terraform' must be strings!") unless variable_file.kind_of?(String)
130
- logger.fatal("All elements of 'terraform' must not be blank!") if variable_file.strip.empty?
131
- result.append(find(base: File.join(@base, @@variable_files_name), name: variable_file.strip, exts: @@variable_files_exts, description: "Terraform values file"))
132
- end
133
-
134
- return result
135
- end
136
-
137
103
  ###############################################################################
138
104
 
139
105
  end
@@ -151,8 +151,8 @@ module TerraformWrapper
151
151
 
152
152
  result = Array.new
153
153
 
154
- @config.variable_files.each do |variable_file|
155
- result.append("-var-file=\"#{variable_file}\"")
154
+ @config.variables.files.each do |file|
155
+ result.append("-var-file=\"#{file}\"")
156
156
  end
157
157
 
158
158
  return result
@@ -165,10 +165,6 @@ module TerraformWrapper
165
165
 
166
166
  result = Array.new
167
167
 
168
- result.append("-var=\"component=#{@code.name}\"")
169
- result.append("-var=\"config=#{@config.name}\"")
170
- result.append("-var=\"service=#{@config.service}\"")
171
-
172
168
  @config.variables.values.each do |key, value|
173
169
  result.append("-var=\"#{key.to_s}=#{value}\"")
174
170
  end
@@ -16,17 +16,80 @@ module TerraformWrapper
16
16
 
17
17
  ###############################################################################
18
18
 
19
- @@reserved = [ "component", "config", "service" ]
19
+ @@variable_files_name = "tfvars"
20
+ @@variable_files_exts = [ ".tfvars" ]
20
21
 
21
22
  ###############################################################################
22
23
 
24
+ attr_reader :core
25
+ attr_reader :files
26
+ attr_reader :identifiers
23
27
  attr_reader :values
24
28
 
25
29
  ###############################################################################
26
30
 
27
- def initialize(values: Hash.new, sort: true)
28
- cleansed = cleanse(values: values)
29
- @values = sort ? cleansed.sort.to_h : cleansed
31
+ def initialize(component:, config:, service:, identifiers: Hash.new, sort: false)
32
+ logger.fatal("Identifiers provided must be a hash!") unless identifiers.kind_of?(Hash)
33
+
34
+ @core = Hash.new()
35
+ @core[:component] = component
36
+ @core[:config] = config
37
+ @core[:service] = service
38
+
39
+ user = cleanse(variables: identifiers, reserved: @core.keys)
40
+ merged = @core.merge(user)
41
+
42
+ @identifiers = sort ? merged.sort.to_h : merged
43
+ @values = @identifiers
44
+ @files = Array.new
45
+ end
46
+
47
+ ###############################################################################
48
+
49
+ def add_files(base:, files:)
50
+ logger.fatal("Variable files provided must be an array!") unless files.kind_of?(Array)
51
+
52
+ files.each do |file|
53
+ logger.fatal("All provided variable file names must be strings!") unless file.kind_of?(String)
54
+ logger.fatal("All provided variable file names must not be blank!") if file.strip.empty?
55
+
56
+ path = ::TerraformWrapper.find(base: File.join(base, @@variable_files_name), name: file.strip, exts: @@variable_files_exts, description: "Terraform values file")
57
+
58
+ if @files.include?(path) then
59
+ logger.warn("Terraform variables file is included more than once: #{file.strip}")
60
+ else
61
+ @files.append(path)
62
+ end
63
+ end
64
+ end
65
+
66
+ ###############################################################################
67
+
68
+ def add_variables(variables:, sort: false)
69
+ logger.fatal("Variables provided must be a hash!") unless variables.kind_of?(Hash)
70
+
71
+ cleansed = cleanse(variables: variables, reserved: @values.keys)
72
+
73
+ begin
74
+ cleansed = cleansed.map{ |key, value| [ key, value % @identifiers ] }.to_h
75
+ rescue
76
+ logger.fatal("Variables contain identifiers that are not included in the configuration file!")
77
+ end
78
+
79
+ merged = @values.merge(cleansed)
80
+ @values = sort ? merged.sort.to_h : merged
81
+ end
82
+
83
+ ###############################################################################
84
+
85
+ def clear_files()
86
+ @files = Array.new
87
+ end
88
+
89
+ ###############################################################################
90
+
91
+ def clear_variables()
92
+ @values = @identifers
30
93
  end
31
94
 
32
95
  ###############################################################################
@@ -35,17 +98,17 @@ module TerraformWrapper
35
98
 
36
99
  ###############################################################################
37
100
 
38
- def cleanse(values:)
101
+ def cleanse(variables:, reserved:)
39
102
  result = Hash.new
40
103
 
41
- values.keys.each do |key|
104
+ variables.keys.each do |key|
42
105
  logger.fatal("Could not clean variables hash. All keys MUST be strings!") unless key.kind_of?(String)
43
- logger.fatal("Could not clean variables hash, key: #{key.downcase} is reserved and cannot be used!") if @@reserved.include?(key.downcase)
106
+ logger.fatal("Could not clean variables hash, key: #{key.downcase} is reserved or already in use and cannot be used!") if reserved.include?(key.downcase.to_sym)
44
107
  logger.fatal("Could not clean variables hash, duplicate key found: #{key.downcase}!") if result.key?(key.downcase.to_sym)
45
- logger.fatal("Could not clean variables hash, value for: #{key.downcase} is not a string!") unless values[key].kind_of?(String)
46
- logger.fatal("Could not clean variables hash, value for: #{key.downcase} is empty!") if values[key].strip.empty?
108
+ logger.fatal("Could not clean variables hash, value for: #{key.downcase} is not a string!") unless variables[key].kind_of?(String)
109
+ logger.fatal("Could not clean variables hash, value for: #{key.downcase} is empty!") if variables[key].strip.empty?
47
110
 
48
- result[key.downcase.to_sym] = values[key].strip
111
+ result[key.downcase.to_sym] = variables[key].strip
49
112
  end
50
113
 
51
114
  return result
@@ -4,7 +4,7 @@ module TerraformWrapper
4
4
 
5
5
  ###############################################################################
6
6
 
7
- VERSION = "0.2.2"
7
+ VERSION = "1.0.0"
8
8
 
9
9
  ###############################################################################
10
10
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: terraform-wrapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Lees
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-03-05 00:00:00.000000000 Z
11
+ date: 2021-03-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake