terraform-wrapper 1.1.0 → 1.2.5

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: 757e953e25e4899b35a17e556123581ab077a84769a144f51db286a6b56f8608
4
- data.tar.gz: 25d70f9f0132157ccc738bf0c459f35d1e777f5760bae6177bc202e92bc1c95c
3
+ metadata.gz: 61563bbc4ee42f89c431029a6ef933e3327e785fbc3bde3091b804e60d23c95d
4
+ data.tar.gz: 17b0def9e9c08f8c1d80952297eab0012117181f6f21487128e224ef6938c273
5
5
  SHA512:
6
- metadata.gz: a0399939f5522c2ffb3fcee4e0dded13c09b7731e0025de5d92bdab8afe63e9b3506eba6bf99d345323ea4f77f7a9096e18203f975127eb29729b60379036144
7
- data.tar.gz: 9a8ef1ea51a121cd85a5a4334cda520f8a4246c2125510054c047f7d29e004cae8bf30a4eb200aae71321d0ccf1748b385449735d5e1bc00b65cf67b3f624c15
6
+ metadata.gz: f28121fc946492d46d11c28e96f5f7b95eb4b55731faa2c9e392af81b5e2ab9ad52c4648e645dac7e78819cedf3516b26cf41d8fd532c68ada8903b321e83a3c
7
+ data.tar.gz: 47b7f9c44fa88c75276f5497216b9480682c0e3590b31704d46a32d7b0b0e498b2d3aa575c04f07d452e5df5605ca2a53269a8bbefd26a0146bd4279ecddf20b
@@ -71,7 +71,7 @@ module TerraformWrapper
71
71
  ###############################################################################
72
72
 
73
73
  def cli()
74
- output = logger.colour ? "jsonc" : "json"
74
+ output = logger.colour ? "yamlc" : "yaml"
75
75
  cmdline = "\"#{@@az}\" version --output \"#{output}\""
76
76
  return(system(cmdline) || false)
77
77
  end
@@ -22,12 +22,13 @@ module TerraformWrapper
22
22
 
23
23
  attr_reader :base
24
24
  attr_reader :directory
25
+ attr_reader :provider
25
26
  attr_reader :platform
26
27
  attr_reader :version
27
28
 
28
29
  ###############################################################################
29
30
 
30
- def initialize(options:)
31
+ def initialize(options:, provider:)
31
32
  logger.fatal("Binary base path must be a string!") unless options["base"].kind_of?(String)
32
33
  logger.fatal("Binary base path must not be blank!") if options["base"].strip.empty?
33
34
 
@@ -39,8 +40,8 @@ module TerraformWrapper
39
40
  @version = options["version"]
40
41
 
41
42
  @platform = platform_detect
42
-
43
43
  @directory = File.join(@base, @version, @platform)
44
+ @provider = provider
44
45
  @name = "terraform"
45
46
  end
46
47
 
@@ -41,16 +41,16 @@ module TerraformWrapper
41
41
 
42
42
  @base = options["base"]
43
43
 
44
- logger.fatal("Configuration service name must be a string!") unless options["service"].kind_of?(String)
45
- logger.fatal("Configuration service name must not be blank!") if options["service"].strip.empty?
46
-
47
- @service = options["service"]
48
-
49
44
  logger.fatal("Configuration name must be a string!") unless options["name"].kind_of?(String)
50
45
  logger.fatal("Configuration name must not be blank!") if options["name"].strip.empty?
51
46
 
52
47
  @name = options["name"]
53
48
 
49
+ logger.fatal("Configuration service name must be a string!") unless options["service"].kind_of?(String)
50
+ logger.fatal("Configuration service name must not be blank!") if options["service"].strip.empty?
51
+
52
+ @service = options["service"]
53
+
54
54
  logger.fatal("Configuration authenticator for Azure enabled must be a Boolean!") unless [ true, false ].include?(options["auth-azure"])
55
55
 
56
56
  auth_azure = options["auth-azure"]
@@ -0,0 +1,41 @@
1
+ ###############################################################################
2
+
3
+ module TerraformWrapper
4
+
5
+ ###############################################################################
6
+
7
+ module Shared
8
+
9
+ ###############################################################################
10
+
11
+ class Provider
12
+
13
+ ###############################################################################
14
+
15
+ include TerraformWrapper::Shared::Logging
16
+
17
+ ###############################################################################
18
+
19
+ attr_reader :platforms
20
+
21
+ ###############################################################################
22
+
23
+ def initialize(options:)
24
+ logger.fatal("Provider platforms to prefetch hashs for must be an array!") unless options["platforms"].kind_of?(Array)
25
+
26
+ @platforms = options["platforms"]
27
+ end
28
+
29
+ ###############################################################################
30
+
31
+ end
32
+
33
+ ###############################################################################
34
+
35
+ end
36
+
37
+ ###############################################################################
38
+
39
+ end
40
+
41
+ ###############################################################################
@@ -14,6 +14,10 @@ module TerraformWrapper
14
14
 
15
15
  include TerraformWrapper::Shared::Logging
16
16
 
17
+ ###############################################################################
18
+
19
+ @@lockfile_name = ".terraform.lock.hcl"
20
+
17
21
  ###############################################################################
18
22
 
19
23
  attr_reader :binary
@@ -35,35 +39,13 @@ module TerraformWrapper
35
39
 
36
40
  ###############################################################################
37
41
 
38
- def download(upgrade: false)
42
+ def download
39
43
  parameters = Array.new
40
44
  parameters.append("-backend=false")
41
- parameters.append("-upgrade") if upgrade
42
-
43
- @downloaded = run(action: "init", parameters: parameters)
44
- logger.fatal("Failed to download Terraform modules.") unless @downloaded
45
- end
46
-
47
- ###############################################################################
48
-
49
- def import(address: nil, id: nil)
50
- logger.fatal("Cannot Terraform import before initialising backend!") unless initialised
51
-
52
- logger.fatal("Terraform state address for import must be a string!") unless address.kind_of?(String)
53
- logger.fatal("Terraform state address for import must be a string!") unless address.kind_of?(String)
54
- logger.fatal("Terraform state address for import must not be blank!") if address.strip.empty?
55
45
 
56
- logger.fatal("Identification for infrastructure to import must be a string!") unless id.kind_of?(String)
57
- logger.fatal("Identification for infrastructure to import must not be blank!") if id.strip.empty?
46
+ logger.fatal("Failed to download Terraform modules.") unless run(action: "init", parameters: parameters)
58
47
 
59
- parameters = Array.new
60
- parameters.concat(variable_files)
61
- parameters.concat(variable_strings)
62
-
63
- parameters.append("'#{address}'")
64
- parameters.append("'#{id}'")
65
-
66
- logger.fatal("Terraform import failed!") unless run(action: "import", parameters: parameters)
48
+ @downloaded = true
67
49
  end
68
50
 
69
51
  ###############################################################################
@@ -78,28 +60,36 @@ module TerraformWrapper
78
60
 
79
61
  config.auths.map(&:auth)
80
62
 
63
+ logger.fatal("Failed to initialise Terraform with backend.") unless run(action: "init", parameters: parameters)
64
+
81
65
  @config = config
82
- @initialised = run(action: "init", parameters: parameters)
83
- logger.fatal("Failed to initialise Terraform with backend.") unless @initialised
66
+ @initialised = true
84
67
  end
85
68
 
86
69
  ###############################################################################
87
70
 
88
- def plan(destroy: false, file: nil)
89
- logger.fatal("Cannot Terraform plan before initialising backend!") unless initialised
71
+ def upgrade
72
+ lockfile_path = File.join(@code.path, @@lockfile_name)
73
+
74
+ if File.file?(lockfile_path)
75
+ logger.info("Removing lock file: #{lockfile_path}")
76
+ File.delete(lockfile_path)
77
+ end
78
+
79
+ logger.fatal("Lock file removal failed!") if File.file?(lockfile_path)
90
80
 
91
81
  parameters = Array.new
92
- parameters.concat(variable_files)
93
- parameters.concat(variable_strings)
82
+ parameters.append("-update")
83
+ logger.fatal("Failed to upgrade Terraform modules!") unless run(action: "get", parameters: parameters)
94
84
 
95
- if not file.nil? and file.kind_of?(String) and not file.strip.empty? then
96
- logger.fatal("Failed to create plan directory: #{directory}") unless ::TerraformWrapper.create_directory(directory: File.dirname(file), purpose: "plan")
97
- parameters.append("-out=\"#{file}\"")
98
- end
85
+ parameters = Array.new
86
+ parameters.append("lock")
99
87
 
100
- parameters.append("-destroy") if destroy
88
+ @binary.provider.platforms.each do |platform|
89
+ parameters.append("-platform=\"#{platform}\"")
90
+ end
101
91
 
102
- logger.fatal("Terraform plan failed!") unless run(action: "plan", parameters: parameters)
92
+ logger.fatal("Failed to upgrade Terraform providers!") unless run(action: "providers", parameters: parameters)
103
93
  end
104
94
 
105
95
  ###############################################################################
@@ -122,6 +112,25 @@ module TerraformWrapper
122
112
  logger.fatal("Terraform apply failed!") unless run(action: "apply", parameters: parameters)
123
113
  end
124
114
 
115
+ ###############################################################################
116
+
117
+ def plan(destroy: false, file: nil)
118
+ logger.fatal("Cannot Terraform plan before initialising backend!") unless initialised
119
+
120
+ parameters = Array.new
121
+ parameters.concat(variable_files)
122
+ parameters.concat(variable_strings)
123
+
124
+ if not file.nil? and file.kind_of?(String) and not file.strip.empty? then
125
+ logger.fatal("Failed to create plan directory: #{directory}") unless ::TerraformWrapper.create_directory(directory: File.dirname(file), purpose: "plan")
126
+ parameters.append("-out=\"#{file}\"")
127
+ end
128
+
129
+ parameters.append("-destroy") if destroy
130
+
131
+ logger.fatal("Terraform plan failed!") unless run(action: "plan", parameters: parameters)
132
+ end
133
+
125
134
  ###############################################################################
126
135
 
127
136
  def destroy
@@ -135,6 +144,28 @@ module TerraformWrapper
135
144
  logger.fatal("Terraform destroy failed!") unless run(action: "destroy", parameters: parameters)
136
145
  end
137
146
 
147
+ ###############################################################################
148
+
149
+ def import(address: nil, id: nil)
150
+ logger.fatal("Cannot Terraform import before initialising backend!") unless initialised
151
+
152
+ logger.fatal("Terraform state address for import must be a string!") unless address.kind_of?(String)
153
+ logger.fatal("Terraform state address for import must be a string!") unless address.kind_of?(String)
154
+ logger.fatal("Terraform state address for import must not be blank!") if address.strip.empty?
155
+
156
+ logger.fatal("Identification for infrastructure to import must be a string!") unless id.kind_of?(String)
157
+ logger.fatal("Identification for infrastructure to import must not be blank!") if id.strip.empty?
158
+
159
+ parameters = Array.new
160
+ parameters.concat(variable_files)
161
+ parameters.concat(variable_strings)
162
+
163
+ parameters.append("'#{address}'")
164
+ parameters.append("'#{id}'")
165
+
166
+ logger.fatal("Terraform import failed!") unless run(action: "import", parameters: parameters)
167
+ end
168
+
138
169
  ###############################################################################
139
170
 
140
171
  def validate
@@ -146,6 +177,27 @@ module TerraformWrapper
146
177
 
147
178
  private
148
179
 
180
+ ###############################################################################
181
+
182
+ def run(action:, parameters: Array.new)
183
+ result = false
184
+
185
+ parameters.reject! { |item| not item.kind_of?(String) or item.strip.empty? }
186
+
187
+ cmdline = [ "\"#{@binary.path}\"", action ].concat(parameters).join(" ")
188
+
189
+ logger.info("Starting Terraform, action: #{action}")
190
+
191
+ puts("\n" + ('#' * 80) + "\n\n")
192
+
193
+ Dir.chdir(@code.path)
194
+ result = system(cmdline) || false
195
+
196
+ puts("\n" + ('#' * 80) + "\n\n")
197
+
198
+ return result
199
+ end
200
+
149
201
  ###############################################################################
150
202
 
151
203
  def variable_files
@@ -174,27 +226,6 @@ module TerraformWrapper
174
226
  return result
175
227
  end
176
228
 
177
- ###############################################################################
178
-
179
- def run(action:, parameters: Array.new)
180
- result = false
181
-
182
- parameters.reject! { |item| not item.kind_of?(String) or item.strip.empty? }
183
-
184
- cmdline = [ "\"#{@binary.path}\"", action ].concat(parameters).join(" ")
185
-
186
- logger.info("Starting Terraform, action: #{action}")
187
-
188
- puts("\n" + ('#' * 80) + "\n\n")
189
-
190
- Dir.chdir(@code.path)
191
- result = system(cmdline) || false
192
-
193
- puts("\n" + ('#' * 80) + "\n\n")
194
-
195
- return result
196
- end
197
-
198
229
  ###############################################################################
199
230
 
200
231
  end
@@ -36,11 +36,11 @@ module TerraformWrapper
36
36
  @core[:config] = config
37
37
  @core[:service] = service
38
38
 
39
- user = cleanse(variables: identifiers, reserved: @core.keys)
39
+ user = cleanse(variables: identifiers, reserved: @core.keys, downcase: true)
40
40
  merged = @core.merge(user)
41
41
 
42
42
  @identifiers = sort ? merged.sort.to_h : merged
43
- @values = @identifiers
43
+ @values = Hash.new
44
44
  @files = Array.new
45
45
  end
46
46
 
@@ -89,7 +89,7 @@ module TerraformWrapper
89
89
  ###############################################################################
90
90
 
91
91
  def clear_variables()
92
- @values = @identifers
92
+ @values = Hash.new
93
93
  end
94
94
 
95
95
  ###############################################################################
@@ -98,17 +98,20 @@ module TerraformWrapper
98
98
 
99
99
  ###############################################################################
100
100
 
101
- def cleanse(variables:, reserved:)
101
+ def cleanse(variables:, reserved:, downcase: false)
102
102
  result = Hash.new
103
103
 
104
104
  variables.keys.each do |key|
105
105
  logger.fatal("Could not clean variables hash. All keys MUST be strings!") unless key.kind_of?(String)
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)
107
- logger.fatal("Could not clean variables hash, duplicate key found: #{key.downcase}!") if result.key?(key.downcase.to_sym)
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?
110
106
 
111
- result[key.downcase.to_sym] = variables[key].strip
107
+ sym = downcase ? key.downcase.to_sym : key.to_sym
108
+
109
+ logger.fatal("Could not clean variables hash, key: #{sym.to_s} is reserved or already in use and cannot be used!") if reserved.include?(sym)
110
+ logger.fatal("Could not clean variables hash, duplicate key found: #{sym.to_s}!") if result.key?(sym)
111
+ logger.fatal("Could not clean variables hash, value for: #{sym.to_s} is not a string!") unless variables[key].kind_of?(String)
112
+ logger.fatal("Could not clean variables hash, value for: #{sym.to_s} is empty!") if variables[key].strip.empty?
113
+
114
+ result[sym] = variables[key].strip
112
115
  end
113
116
 
114
117
  return result
@@ -11,6 +11,7 @@ require_relative 'shared/binary'
11
11
  require_relative 'shared/code'
12
12
  require_relative 'shared/config'
13
13
  require_relative 'shared/latest'
14
+ require_relative 'shared/provider'
14
15
  require_relative 'shared/runner'
15
16
  require_relative 'shared/variables'
16
17
 
@@ -34,12 +34,12 @@ module TerraformWrapper
34
34
 
35
35
  def upgrade_task
36
36
  desc "Upgrades the Terraform infrastructure component modules, providers and lock file."
37
- task :upgrade => [:binary, :clean] do |t, args|
37
+ task :upgrade => [:binary] do |t, args|
38
38
  runner = TerraformWrapper::Shared::Runner.new(binary: @binary, code: @code)
39
39
 
40
40
  logger.info("Upgrading Terraform component: #{@code.name}...")
41
41
 
42
- runner.download(upgrade: true)
42
+ runner.upgrade
43
43
  end
44
44
  end
45
45
 
@@ -4,7 +4,7 @@ module TerraformWrapper
4
4
 
5
5
  ###############################################################################
6
6
 
7
- VERSION = "1.1.0"
7
+ VERSION = "1.2.5"
8
8
 
9
9
  ###############################################################################
10
10
 
@@ -43,8 +43,12 @@ module TerraformWrapper
43
43
  config_options["backend-options"] = options.key?("config-backend-options") ? options["config-backend-options"] : Hash.new
44
44
  config_options["service"] = service
45
45
 
46
- binary = TerraformWrapper::Shared::Binary.new(options: binary_options)
47
- code = TerraformWrapper::Shared::Code.new(options: code_options)
46
+ provider_options = Hash.new
47
+ provider_options["platforms"] = options.key?("provider-platforms") ? options["provider-platforms"] : Array.new
48
+
49
+ provider = TerraformWrapper::Shared::Provider.new(options: provider_options)
50
+ binary = TerraformWrapper::Shared::Binary.new(options: binary_options, provider: provider)
51
+ code = TerraformWrapper::Shared::Code.new(options: code_options)
48
52
 
49
53
  tasks = Array.new
50
54
  tasks << TerraformWrapper::Tasks::Apply.new(binary: binary, code: code, options: config_options)
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: 1.1.0
4
+ version: 1.2.5
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-10 00:00:00.000000000 Z
11
+ date: 2021-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -71,6 +71,7 @@ files:
71
71
  - lib/terraform-wrapper/shared/latest.rb
72
72
  - lib/terraform-wrapper/shared/logger.rb
73
73
  - lib/terraform-wrapper/shared/logging.rb
74
+ - lib/terraform-wrapper/shared/provider.rb
74
75
  - lib/terraform-wrapper/shared/runner.rb
75
76
  - lib/terraform-wrapper/shared/variables.rb
76
77
  - lib/terraform-wrapper/tasks.rb
@@ -107,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
108
  - !ruby/object:Gem::Version
108
109
  version: '0'
109
110
  requirements: []
110
- rubygems_version: 3.2.3
111
+ rubygems_version: 3.2.22
111
112
  signing_key:
112
113
  specification_version: 4
113
114
  summary: A ruby wrapper for managing Terraform binaries and remote state.