tfwrapper 0.2.0 → 0.3.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: 546f4a0d5cfe06c3e1606071f6c09c74752d6ef0
4
- data.tar.gz: 52178cde8c11eed2bed48df05c086ef9517d62c6
3
+ metadata.gz: 4f11f99f25e1c0f38ce5c79928b9b6434aee3445
4
+ data.tar.gz: b00feb3297fb625886ce712a76300b2251560e34
5
5
  SHA512:
6
- metadata.gz: 296ea36c9f2c02ebe4fed3b94bd69f3e47bb31f938980e8b96ff7d2528e1b2db63d9d9934394cb2a3fdb83bda0507cdaa238ec6425fe8830d4466c6097704fce
7
- data.tar.gz: 4d2ac178cdd25438debf5add32f007f76d1ca99b23d04dfa29e26d90ef1d819c8ceb6172e6f4c93e09893daa1ca02ccb4fa2ae2dd1e70d9cab09b79baa0281c1
6
+ metadata.gz: 2516206493b0e019dfeada871fef5e0f444c1eb128c7297daa73afea55ae261a1db4e28c9d213ba18e0d651c777d7ce35a481a8c3e9461a18a7a98de113e0863
7
+ data.tar.gz: f7e976dc8d51f1f0a68d9300d450626f424a6aa6a12c6af40229e8f187db1ccded5ebeb5f7884d4e7693eaf6f31446a5b0b31cec4c05c7e09b311b428e7e508b
data/.gitignore CHANGED
@@ -13,6 +13,7 @@
13
13
  /vendor/
14
14
  .terraform
15
15
  build.tfvars.json
16
+ **/terraform.tfstate.backup
16
17
 
17
18
  # Used by dotenv library to load environment variables.
18
19
  # .env
data/.rubocop.yml CHANGED
@@ -20,3 +20,7 @@ Style/MutableConstant:
20
20
  Exclude:
21
21
  - 'lib/tfwrapper/version.rb'
22
22
  - 'spec/acceptance/acceptance_spec.rb'
23
+
24
+ Layout/MultilineMethodCallIndentation:
25
+ Exclude:
26
+ - 'spec/unit/raketasks_spec.rb'
data/ChangeLog.md CHANGED
@@ -1,3 +1,13 @@
1
+ Version 0.3.0
2
+
3
+ - Add `tf:output` and `tf:output_json` Rake tasks
4
+ - Have TFWrapper::RakeTasks store the current terraform version as an instance variable;
5
+ also make this accessible as tf_version.
6
+ - Add '-auto-approve' to terraform apply command if running with tf >= 0.10.0
7
+ - Update acceptance tests to work with terraform 0.9.x and 0.10.x
8
+ - Acceptance test logic to find the latest terraform release version from GitHub API
9
+ - Run acceptance tests for multiple terraform versions (currently: 0.9.2, 0.9.7, 0.10.0, 0.10.2, latest)
10
+
1
11
  Version 0.2.0
2
12
 
3
13
  - initial release (migrated from previous internal/private gem)
data/README.md CHANGED
@@ -45,7 +45,7 @@ gem 'tfwrapper', '~> 0.2.0'
45
45
 
46
46
  ### Supported Terraform Versions
47
47
 
48
- tfwrapper only supports terraform 0.9+. It has been tested up to 0.9.2.
48
+ tfwrapper only supports terraform 0.9+. It is tested against multiple versions from 0.9.2 to 0.10.2 and the current release.
49
49
 
50
50
  ## Usage
51
51
 
data/circle.yml CHANGED
@@ -20,5 +20,9 @@ test:
20
20
  - 'rvm 2.1.8 exec bundle exec rake spec:unit'
21
21
  - 'rvm 2.2.5 exec bundle exec rake spec:unit'
22
22
  - 'rvm 2.3.1 exec bundle exec rake spec:unit'
23
- - 'rvm 2.3.1 exec bundle exec rake spec:acceptance'
23
+ - 'rvm 2.3.1 exec bundle exec rake spec:acceptance TF_VERSION=0.9.2'
24
+ - 'rvm 2.3.1 exec bundle exec rake spec:acceptance TF_VERSION=0.9.7'
25
+ - 'rvm 2.3.1 exec bundle exec rake spec:acceptance TF_VERSION=0.10.0'
26
+ - 'rvm 2.3.1 exec bundle exec rake spec:acceptance TF_VERSION=0.10.2'
27
+ - 'rvm 2.3.1 exec bundle exec rake spec:acceptance TF_VERSION=latest'
24
28
  - 'bundle exec rake yard:generate'
@@ -31,6 +31,8 @@ module TFWrapper
31
31
  Gem::Version.new('0.9.0')
32
32
  end
33
33
 
34
+ attr_reader :tf_version
35
+
34
36
  # Generate Rake tasks for working with Terraform at Manheim.
35
37
  #
36
38
  # @param tf_dir [String] Terraform config directory, relative to Rakefile.
@@ -67,6 +69,8 @@ module TFWrapper
67
69
  @tf_extra_vars = opts.fetch(:tf_extra_vars, {})
68
70
  @backend_config = opts.fetch(:backend_config, {})
69
71
  @consul_url = opts.fetch(:consul_url, nil)
72
+ # default to lowest possible version; this is set in the 'init' task
73
+ @tf_version = Gem::Version.new('0.0.0')
70
74
  # rubocop:disable Style/GuardClause
71
75
  if @consul_url.nil? && !@consul_env_vars_prefix.nil?
72
76
  raise StandardError, 'Cannot set env vars in Consul when consul_url ' \
@@ -92,6 +96,7 @@ module TFWrapper
92
96
  install_refresh
93
97
  install_destroy
94
98
  install_write_tf_vars
99
+ install_output
95
100
  end
96
101
 
97
102
  # add the 'tf:init' Rake task. This checks environment variables,
@@ -102,7 +107,7 @@ module TFWrapper
102
107
  desc 'Run terraform init with appropriate arguments'
103
108
  task :init do
104
109
  TFWrapper::Helpers.check_env_vars(@tf_vars_from_env.values)
105
- check_tf_version
110
+ @tf_version = check_tf_version
106
111
  cmd = [
107
112
  'terraform',
108
113
  'init',
@@ -146,8 +151,11 @@ module TFWrapper
146
151
  :"#{nsprefix}:write_tf_vars",
147
152
  :"#{nsprefix}:plan"
148
153
  ] do |_t, args|
154
+ cmd_arr = %w[terraform apply]
155
+ cmd_arr << '-auto-approve' if tf_version >= Gem::Version.new('0.10.0')
156
+ cmd_arr << "-var-file #{var_file_path}"
149
157
  cmd = cmd_with_targets(
150
- ['terraform', 'apply', "-var-file #{var_file_path}"],
158
+ cmd_arr,
151
159
  args[:target],
152
160
  args.extras
153
161
  )
@@ -176,6 +184,24 @@ module TFWrapper
176
184
  end
177
185
  end
178
186
 
187
+ # add the 'tf:output' Rake task
188
+ def install_output
189
+ namespace nsprefix do
190
+ task output: [
191
+ :"#{nsprefix}:init",
192
+ :"#{nsprefix}:refresh"
193
+ ] do
194
+ terraform_runner('terraform output')
195
+ end
196
+ task output_json: [
197
+ :"#{nsprefix}:init",
198
+ :"#{nsprefix}:refresh"
199
+ ] do
200
+ terraform_runner('terraform output -json')
201
+ end
202
+ end
203
+ end
204
+
179
205
  # add the 'tf:destroy' Rake task
180
206
  def install_destroy
181
207
  namespace nsprefix do
@@ -212,7 +238,7 @@ module TFWrapper
212
238
  tf_vars = terraform_vars
213
239
  puts 'Terraform vars:'
214
240
  tf_vars.sort.map do |k, v|
215
- if k == 'aws_access_key' || k == 'aws_secret_key'
241
+ if %w[aws_access_key aws_secret_key].include?(k)
216
242
  puts "#{k} => (redacted)"
217
243
  else
218
244
  puts "#{k} => #{v}"
@@ -308,6 +334,7 @@ module TFWrapper
308
334
  "binary reports itself as #{m[1]} (#{all_out_err})"
309
335
  end
310
336
  puts "Running with: #{all_out_err}"
337
+ tf_ver
311
338
  end
312
339
 
313
340
  # update stack status in Consul
@@ -315,7 +342,7 @@ module TFWrapper
315
342
  require 'diplomat'
316
343
  require 'json'
317
344
  data = {}
318
- @tf_vars_from_env.values.each { |k| data[k] = ENV[k] }
345
+ @tf_vars_from_env.each_value { |k| data[k] = ENV[k] }
319
346
 
320
347
  Diplomat.configure do |config|
321
348
  config.url = @consul_url
@@ -4,5 +4,5 @@ module TFWrapper
4
4
  # version of the Gem/module; used in the gemspec and in messages.
5
5
  # NOTE: When updating this, also update the version in the "Installation"
6
6
  # section of README.md
7
- VERSION = '0.2.0'
7
+ VERSION = '0.3.0'
8
8
  end
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'ffi'
4
+ require 'faraday'
5
+ require 'json'
4
6
 
5
7
  def cleanup_tf
6
8
  fixture_dir = File.absolute_path(
@@ -9,6 +11,20 @@ def cleanup_tf
9
11
  Dir.glob("#{fixture_dir}/**/.terraform").each do |d|
10
12
  FileUtils.rmtree(d) if File.directory?(d)
11
13
  end
14
+ Dir.glob("#{fixture_dir}/**/terraform.tfstate*").each do |f|
15
+ File.delete(f)
16
+ end
17
+ end
18
+
19
+ def desired_tf_version
20
+ if ENV.include?('TF_VERSION') && ENV['TF_VERSION'] != 'latest'
21
+ return ENV['TF_VERSION']
22
+ end
23
+ # else get the latest release from GitHub
24
+ resp = Faraday.get('https://api.github.com/repos/hashicorp/terraform/releases/latest')
25
+ rel = JSON.parse(resp.body)['tag_name'].sub(/^v/, '')
26
+ puts "Found latest terraform release on GitHub: #{rel}"
27
+ rel
12
28
  end
13
29
 
14
30
  class HashicorpFetcher
@@ -5,7 +5,14 @@ require_relative 'acceptance_helpers'
5
5
  require 'open3'
6
6
  require 'json'
7
7
 
8
- TF_VERSION = '0.9.2'
8
+ TF_VERSION = desired_tf_version
9
+ if Gem::Version.new(TF_VERSION) >= Gem::Version.new('0.10.0')
10
+ EXPECTED_SERIAL = 2
11
+ APPLY_CMD = 'terraform apply -auto-approve'
12
+ else
13
+ EXPECTED_SERIAL = 1
14
+ APPLY_CMD = 'terraform apply'
15
+ end
9
16
 
10
17
  Diplomat.configure do |config|
11
18
  config.url = 'http://127.0.0.1:8500'
@@ -21,7 +28,7 @@ describe 'tfwrapper' do
21
28
  @server.stop
22
29
  end
23
30
  after(:each) { cleanup_tf }
24
- context 'testOne - basic TF with remote state' do
31
+ context 'testOne - basic TF with remote state', order: :defined do
25
32
  before(:all) do
26
33
  @fixturepath = File.absolute_path(
27
34
  File.join(File.dirname(__FILE__), '..', 'fixtures')
@@ -84,7 +91,7 @@ describe 'tfwrapper' do
84
91
  end
85
92
  it 'runs apply correctly and succeeds' do
86
93
  expect(@out_err)
87
- .to include('terraform_runner command: \'terraform apply -var-file')
94
+ .to include("terraform_runner command: '#{APPLY_CMD} -var-file")
88
95
  expect(@out_err).to include('consul_keys.testOne: Creating...')
89
96
  expect(@out_err).to include(
90
97
  'Apply complete! Resources: 1 added, 0 changed, 0 destroyed.'
@@ -103,7 +110,7 @@ describe 'tfwrapper' do
103
110
  state = JSON.parse(Diplomat::Kv.get('terraform/testOne'))
104
111
  expect(state['version']).to eq(3)
105
112
  expect(state['terraform_version']).to eq(TF_VERSION)
106
- expect(state['serial']).to eq(1)
113
+ expect(state['serial']).to eq(EXPECTED_SERIAL)
107
114
  expect(state['modules'].length).to eq(1)
108
115
  expect(state['modules'][0]['outputs']['foo_variable']['value'])
109
116
  .to eq('bar')
@@ -112,8 +119,46 @@ describe 'tfwrapper' do
112
119
  expect(state['modules'][0]['resources'].length).to eq(1)
113
120
  end
114
121
  end
122
+ describe 'tf:output' do
123
+ before(:all) do
124
+ @out_err, @ecode = Open3.capture2e(
125
+ 'timeout -k 60 45 bundle exec rake tf:output 2>/dev/null',
126
+ chdir: @fixturepath
127
+ )
128
+ end
129
+ it 'does not time out' do
130
+ expect(@ecode.exitstatus).to_not eq(124)
131
+ expect(@ecode.exitstatus).to_not eq(137)
132
+ end
133
+ it 'exits zero' do
134
+ expect(@ecode.exitstatus).to eq(0)
135
+ end
136
+ it 'shows the outputs' do
137
+ expect(@out_err).to include('foo_variable = bar')
138
+ end
139
+ end
140
+ describe 'tf:output_json' do
141
+ before(:all) do
142
+ @out_err, @ecode = Open3.capture2e(
143
+ 'timeout -k 60 45 bundle exec rake tf:output_json 2>/dev/null',
144
+ chdir: @fixturepath
145
+ )
146
+ end
147
+ it 'does not time out' do
148
+ expect(@ecode.exitstatus).to_not eq(124)
149
+ expect(@ecode.exitstatus).to_not eq(137)
150
+ end
151
+ it 'exits zero' do
152
+ expect(@ecode.exitstatus).to eq(0)
153
+ end
154
+ it 'shows the outputs' do
155
+ expect(@out_err).to match(/.*{\s+"foo_variable":\s{\s+"sensitive":\s
156
+ false,\s+"type":\s"string",\s+"value":\s
157
+ "bar"\s+}\s+}.*/x)
158
+ end
159
+ end
115
160
  end
116
- context 'testTwo - TF with vars, remote state and consul env var update' do
161
+ context 'testTwo - TF w/ vars, rmt state & consul env var', order: :defined do
117
162
  before(:all) do
118
163
  @fixturepath = File.absolute_path(
119
164
  File.join(File.dirname(__FILE__), '..', 'fixtures', 'testTwo')
@@ -183,7 +228,7 @@ describe 'tfwrapper' do
183
228
  end
184
229
  it 'runs apply correctly and succeeds' do
185
230
  expect(@out_err)
186
- .to include('terraform_runner command: \'terraform apply -var-file')
231
+ .to include("terraform_runner command: '#{APPLY_CMD} -var-file")
187
232
  expect(@out_err).to include('consul_keys.testTwo: Creating...')
188
233
  expect(@out_err).to include(
189
234
  'Apply complete! Resources: 1 added, 0 changed, 0 destroyed.'
@@ -206,7 +251,7 @@ describe 'tfwrapper' do
206
251
  state = JSON.parse(Diplomat::Kv.get('terraform/testTwo/foo'))
207
252
  expect(state['version']).to eq(3)
208
253
  expect(state['terraform_version']).to eq(TF_VERSION)
209
- expect(state['serial']).to eq(1)
254
+ expect(state['serial']).to eq(EXPECTED_SERIAL)
210
255
  expect(state['modules'].length).to eq(1)
211
256
  expect(state['modules'][0]['outputs']['foo_variable']['value'])
212
257
  .to eq('fooval')
@@ -242,7 +287,7 @@ describe 'tfwrapper' do
242
287
  end
243
288
  it 'runs apply correctly and succeeds' do
244
289
  expect(@out_err)
245
- .to include('terraform_runner command: \'terraform apply -var-file')
290
+ .to include("terraform_runner command: '#{APPLY_CMD} -var-file")
246
291
  expect(@out_err).to include('consul_keys.testTwo: Creating...')
247
292
  expect(@out_err).to include(
248
293
  'Apply complete! Resources: 1 added, 0 changed, 0 destroyed.'
@@ -265,7 +310,7 @@ describe 'tfwrapper' do
265
310
  state = JSON.parse(Diplomat::Kv.get('terraform/testTwo/bar'))
266
311
  expect(state['version']).to eq(3)
267
312
  expect(state['terraform_version']).to eq(TF_VERSION)
268
- expect(state['serial']).to eq(1)
313
+ expect(state['serial']).to eq(EXPECTED_SERIAL)
269
314
  expect(state['modules'].length).to eq(1)
270
315
  expect(state['modules'][0]['outputs']['foo_variable']['value'])
271
316
  .to eq('fooval')
@@ -276,8 +321,29 @@ describe 'tfwrapper' do
276
321
  expect(state['modules'][0]['resources'].length).to eq(1)
277
322
  end
278
323
  end
324
+ describe 'tf:output' do
325
+ before(:all) do
326
+ ENV['TFSUFFIX'] = 'bar'
327
+ @out_err, @ecode = Open3.capture2e(
328
+ 'timeout -k 60 45 bundle exec rake tf:output 2>/dev/null',
329
+ chdir: @fixturepath
330
+ )
331
+ @varpath = File.join(@fixturepath, 'build.tfvars.json')
332
+ end
333
+ it 'does not time out' do
334
+ expect(@ecode.exitstatus).to_not eq(124)
335
+ expect(@ecode.exitstatus).to_not eq(137)
336
+ end
337
+ it 'exits zero' do
338
+ expect(@ecode.exitstatus).to eq(0)
339
+ end
340
+ it 'shows the outputs' do
341
+ expect(@out_err).to include('foo_variable = fooval')
342
+ expect(@out_err).to include('bar_variable = barval')
343
+ end
344
+ end
279
345
  end
280
- context 'testThree - TF with namespaces' do
346
+ context 'testThree - TF with namespaces', order: :defined do
281
347
  before(:all) do
282
348
  @fixturepath = File.absolute_path(
283
349
  File.join(File.dirname(__FILE__), '..', 'fixtures', 'testThree')
@@ -372,40 +438,63 @@ describe 'tfwrapper' do
372
438
  end
373
439
  it 'runs apply correctly and succeeds' do
374
440
  expect(@out_err)
375
- .to include('terraform_runner command: \'terraform apply -var-file')
441
+ .to include("terraform_runner command: '#{APPLY_CMD} -var-file")
376
442
  expect(@out_err).to include('consul_keys.testThreeFoo: Creating...')
377
443
  expect(@out_err).to include(
378
444
  'Apply complete! Resources: 1 added, 0 changed, 0 destroyed.'
379
445
  )
380
446
  expect(@out_err).to include(
381
- "Outputs:\n\nbar_variable = barval\nfoo_variable = fooval"
447
+ "Outputs:\n\nbar_variable = barONEval\nfoo_variable = fooval"
382
448
  )
383
449
  end
384
450
  it 'writes the vars file' do
385
451
  expect(File.file?(@varpath)).to be(true)
386
452
  c = File.open(@varpath, 'r').read
387
453
  expect(JSON.parse(c))
388
- .to eq('foo' => 'fooval', 'bar' => 'barval')
454
+ .to eq('foo' => 'fooval', 'bar' => 'barONEval')
389
455
  end
390
456
  it 'sets the consul keys' do
391
457
  expect(Diplomat::Kv.get('testThreeFoo/foo')).to eq('fooval')
392
- expect(Diplomat::Kv.get('testThreeFoo/bar')).to eq('barval')
458
+ expect(Diplomat::Kv.get('testThreeFoo/bar')).to eq('barONEval')
393
459
  end
394
460
  it 'writes remote state to consul' do
395
461
  state = JSON.parse(Diplomat::Kv.get('terraform/testThreeFoo'))
396
462
  expect(state['version']).to eq(3)
397
463
  expect(state['terraform_version']).to eq(TF_VERSION)
398
- expect(state['serial']).to eq(1)
464
+ expect(state['serial']).to eq(EXPECTED_SERIAL)
399
465
  expect(state['modules'].length).to eq(1)
400
466
  expect(state['modules'][0]['outputs']['foo_variable']['value'])
401
467
  .to eq('fooval')
402
468
  expect(state['modules'][0]['outputs']['bar_variable']['value'])
403
- .to eq('barval')
469
+ .to eq('barONEval')
404
470
  expect(state['modules'][0]['resources'])
405
471
  .to include('consul_keys.testThreeFoo')
406
472
  expect(state['modules'][0]['resources'].length).to eq(1)
407
473
  end
408
474
  end
475
+ describe 'tf:output' do
476
+ before(:all) do
477
+ @out_err, @ecode = Open3.capture2e(
478
+ 'timeout -k 60 45 bundle exec rake tf:output 2>/dev/null',
479
+ chdir: @fixturepath
480
+ )
481
+ @varpath = File.join(@fixturepath, 'build.tfvars.json')
482
+ end
483
+ after(:all) do
484
+ File.delete(@varpath) if File.file?(@varpath)
485
+ end
486
+ it 'does not time out' do
487
+ expect(@ecode.exitstatus).to_not eq(124)
488
+ expect(@ecode.exitstatus).to_not eq(137)
489
+ end
490
+ it 'exits zero' do
491
+ expect(@ecode.exitstatus).to eq(0)
492
+ end
493
+ it 'shows the outputs' do
494
+ expect(@out_err).to include('foo_variable = fooval')
495
+ expect(@out_err).to include('bar_variable = barONEval')
496
+ end
497
+ end
409
498
  describe 'bar_tf:apply' do
410
499
  before(:all) do
411
500
  @out_err, @ecode = Open3.capture2e(
@@ -429,40 +518,63 @@ describe 'tfwrapper' do
429
518
  end
430
519
  it 'runs apply correctly and succeeds' do
431
520
  expect(@out_err)
432
- .to include('terraform_runner command: \'terraform apply -var-file')
521
+ .to include("terraform_runner command: '#{APPLY_CMD} -var-file")
433
522
  expect(@out_err).to include('consul_keys.testThreeBar: Creating...')
434
523
  expect(@out_err).to include(
435
524
  'Apply complete! Resources: 1 added, 0 changed, 0 destroyed.'
436
525
  )
437
526
  expect(@out_err).to include(
438
- "Outputs:\n\nbar_variable = barval\nfoo_variable = fooval"
527
+ "Outputs:\n\nbar_variable = barTWOval\nfoo_variable = fooval"
439
528
  )
440
529
  end
441
530
  it 'writes the vars file' do
442
531
  expect(File.file?(@varpath)).to be(true)
443
532
  c = File.open(@varpath, 'r').read
444
533
  expect(JSON.parse(c))
445
- .to eq('foo' => 'fooval', 'bar' => 'barval')
534
+ .to eq('foo' => 'fooval', 'bar' => 'barTWOval')
446
535
  end
447
536
  it 'sets the consul keys' do
448
537
  expect(Diplomat::Kv.get('testThreeBar/foo')).to eq('fooval')
449
- expect(Diplomat::Kv.get('testThreeBar/bar')).to eq('barval')
538
+ expect(Diplomat::Kv.get('testThreeBar/bar')).to eq('barTWOval')
450
539
  end
451
540
  it 'writes remote state to consul' do
452
541
  state = JSON.parse(Diplomat::Kv.get('terraform/testThreeBar'))
453
542
  expect(state['version']).to eq(3)
454
543
  expect(state['terraform_version']).to eq(TF_VERSION)
455
- expect(state['serial']).to eq(1)
544
+ expect(state['serial']).to eq(EXPECTED_SERIAL)
456
545
  expect(state['modules'].length).to eq(1)
457
546
  expect(state['modules'][0]['outputs']['foo_variable']['value'])
458
547
  .to eq('fooval')
459
548
  expect(state['modules'][0]['outputs']['bar_variable']['value'])
460
- .to eq('barval')
549
+ .to eq('barTWOval')
461
550
  expect(state['modules'][0]['resources'])
462
551
  .to include('consul_keys.testThreeBar')
463
552
  expect(state['modules'][0]['resources'].length).to eq(1)
464
553
  end
465
554
  end
555
+ describe 'bar_tf:output' do
556
+ before(:all) do
557
+ @out_err, @ecode = Open3.capture2e(
558
+ 'timeout -k 60 45 bundle exec rake bar_tf:output 2>/dev/null',
559
+ chdir: @fixturepath
560
+ )
561
+ @varpath = File.join(@fixturepath, 'bar_build.tfvars.json')
562
+ end
563
+ after(:all) do
564
+ File.delete(@varpath) if File.file?(@varpath)
565
+ end
566
+ it 'does not time out' do
567
+ expect(@ecode.exitstatus).to_not eq(124)
568
+ expect(@ecode.exitstatus).to_not eq(137)
569
+ end
570
+ it 'exits zero' do
571
+ expect(@ecode.exitstatus).to eq(0)
572
+ end
573
+ it 'shows the outputs' do
574
+ expect(@out_err).to include('foo_variable = fooval')
575
+ expect(@out_err).to include('bar_variable = barTWOval')
576
+ end
577
+ end
466
578
  describe 'baz_tf:apply' do
467
579
  before(:all) do
468
580
  @out_err, @ecode = Open3.capture2e(
@@ -486,7 +598,7 @@ describe 'tfwrapper' do
486
598
  end
487
599
  it 'runs apply correctly and succeeds' do
488
600
  expect(@out_err)
489
- .to include('terraform_runner command: \'terraform apply -var-file')
601
+ .to include("terraform_runner command: '#{APPLY_CMD} -var-file")
490
602
  expect(@out_err).to include('consul_keys.testThreeBaz: Creating...')
491
603
  expect(@out_err).to include(
492
604
  'Apply complete! Resources: 1 added, 0 changed, 0 destroyed.'
@@ -508,7 +620,7 @@ describe 'tfwrapper' do
508
620
  state = JSON.parse(Diplomat::Kv.get('terraform/testThreeBaz'))
509
621
  expect(state['version']).to eq(3)
510
622
  expect(state['terraform_version']).to eq(TF_VERSION)
511
- expect(state['serial']).to eq(1)
623
+ expect(state['serial']).to eq(EXPECTED_SERIAL)
512
624
  expect(state['modules'].length).to eq(1)
513
625
  expect(state['modules'][0]['outputs']['foo_variable']['value'])
514
626
  .to eq('fooval')
@@ -521,5 +633,28 @@ describe 'tfwrapper' do
521
633
  expect(cvars).to eq('FOO' => 'fooval')
522
634
  end
523
635
  end
636
+ describe 'baz_tf:output' do
637
+ before(:all) do
638
+ @out_err, @ecode = Open3.capture2e(
639
+ 'timeout -k 60 45 bundle exec rake baz_tf:output 2>/dev/null',
640
+ chdir: @fixturepath
641
+ )
642
+ @varpath = File.join(@fixturepath, 'baz_build.tfvars.json')
643
+ end
644
+ after(:all) do
645
+ File.delete(@varpath) if File.file?(@varpath)
646
+ end
647
+ it 'does not time out' do
648
+ expect(@ecode.exitstatus).to_not eq(124)
649
+ expect(@ecode.exitstatus).to_not eq(137)
650
+ end
651
+ it 'exits zero' do
652
+ expect(@ecode.exitstatus).to eq(0)
653
+ end
654
+ it 'shows the outputs' do
655
+ expect(@out_err).to include('foo_variable = fooval')
656
+ expect(@out_err).to_not include('bar_variable = ')
657
+ end
658
+ end
524
659
  end
525
660
  end
@@ -13,14 +13,14 @@ TFWrapper::RakeTasks.install_tasks(
13
13
  'foo',
14
14
  backend_config: { 'path' => 'terraform/testThreeFoo' },
15
15
  tf_vars_from_env: { 'foo' => 'FOO' },
16
- tf_extra_vars: { 'bar' => 'barval' }
16
+ tf_extra_vars: { 'bar' => 'barONEval' }
17
17
  )
18
18
 
19
19
  TFWrapper::RakeTasks.install_tasks(
20
20
  'bar',
21
21
  namespace_prefix: 'bar',
22
22
  tf_vars_from_env: { 'foo' => 'FOO' },
23
- tf_extra_vars: { 'bar' => 'barval' }
23
+ tf_extra_vars: { 'bar' => 'barTWOval' }
24
24
  )
25
25
 
26
26
  TFWrapper::RakeTasks.install_tasks(
@@ -60,6 +60,8 @@ describe TFWrapper::RakeTasks do
60
60
  expect(cls.instance_variable_get('@tf_extra_vars')).to eq({})
61
61
  expect(cls.instance_variable_get('@backend_config')).to eq({})
62
62
  expect(cls.instance_variable_get('@consul_url')).to eq(nil)
63
+ expect(cls.instance_variable_get('@tf_version'))
64
+ .to eq(Gem::Version.new('0.0.0'))
63
65
  end
64
66
  it 'sets options' do
65
67
  allow(ENV).to receive(:[])
@@ -212,12 +214,14 @@ describe TFWrapper::RakeTasks do
212
214
  allow(subject).to receive(:install_refresh)
213
215
  allow(subject).to receive(:install_destroy)
214
216
  allow(subject).to receive(:install_write_tf_vars)
217
+ allow(subject).to receive(:install_output)
215
218
  expect(subject).to receive(:install_init).once
216
219
  expect(subject).to receive(:install_plan).once
217
220
  expect(subject).to receive(:install_apply).once
218
221
  expect(subject).to receive(:install_refresh).once
219
222
  expect(subject).to receive(:install_destroy).once
220
223
  expect(subject).to receive(:install_write_tf_vars).once
224
+ expect(subject).to receive(:install_output).once
221
225
  subject.install
222
226
  end
223
227
  end
@@ -240,6 +244,8 @@ describe TFWrapper::RakeTasks do
240
244
  end
241
245
  it 'runs the init command with backend_config options' do
242
246
  Rake.application['tf:init'].clear_prerequisites
247
+ expect(subject.instance_variable_get(:@tf_version))
248
+ .to eq(Gem::Version.new('0.0.0'))
243
249
  vars = { foo: 'bar', baz: 'blam' }
244
250
  subject.instance_variable_set('@tf_vars_from_env', vars)
245
251
  allow(TFWrapper::Helpers).to receive(:check_env_vars)
@@ -252,6 +258,7 @@ describe TFWrapper::RakeTasks do
252
258
  )
253
259
  allow(subject).to receive(:terraform_runner)
254
260
  allow(subject).to receive(:check_tf_version)
261
+ .and_return(Gem::Version.new('0.9.5'))
255
262
  expect(TFWrapper::Helpers)
256
263
  .to receive(:check_env_vars).once.ordered.with(vars.values)
257
264
  expect(subject).to receive(:check_tf_version).once.ordered
@@ -261,6 +268,8 @@ describe TFWrapper::RakeTasks do
261
268
  ' -backend-config=\'path=consulprefix\''\
262
269
  ' -backend-config=\'foo=bar\'')
263
270
  Rake.application['tf:init'].invoke
271
+ expect(subject.instance_variable_get(:@tf_version))
272
+ .to eq(Gem::Version.new('0.9.5'))
264
273
  end
265
274
  it 'runs the init command without backend_config options' do
266
275
  Rake.application['tf:init'].clear_prerequisites
@@ -271,12 +280,15 @@ describe TFWrapper::RakeTasks do
271
280
  subject.instance_variable_set('@backend_config', {})
272
281
  allow(subject).to receive(:terraform_runner)
273
282
  allow(subject).to receive(:check_tf_version)
283
+ .and_return(Gem::Version.new('0.10.2'))
274
284
  expect(TFWrapper::Helpers)
275
285
  .to receive(:check_env_vars).once.ordered.with(vars.values)
276
286
  expect(subject).to receive(:check_tf_version).once.ordered
277
287
  expect(subject).to receive(:terraform_runner).once.ordered
278
288
  .with('terraform init -input=false')
279
289
  Rake.application['tf:init'].invoke
290
+ expect(subject.instance_variable_get(:@tf_version))
291
+ .to eq(Gem::Version.new('0.10.2'))
280
292
  end
281
293
  end
282
294
  describe '#install_plan' do
@@ -341,53 +353,105 @@ describe TFWrapper::RakeTasks do
341
353
  before do
342
354
  subject.install_apply
343
355
  end
344
-
345
356
  it 'adds the apply task' do
346
357
  expect(Rake.application['tf:apply']).to be_instance_of(Rake::Task)
347
358
  expect(Rake.application['tf:apply'].prerequisites)
348
359
  .to eq(%w[tf:init tf:write_tf_vars tf:plan])
349
360
  expect(Rake.application['tf:apply'].arg_names).to eq([:target])
350
361
  end
351
- it 'runs the apply command with no targets' do
352
- Rake.application['tf:apply'].clear_prerequisites
353
- allow(subject).to receive(:var_file_path).and_return('file.tfvars.json')
354
- allow(subject).to receive(:terraform_runner)
355
- allow(subject).to receive(:update_consul_stack_env_vars)
356
- expect(subject).to receive(:terraform_runner).once
357
- .with('terraform apply -var-file file.tfvars.json')
358
- expect(subject).to_not receive(:update_consul_stack_env_vars)
359
- Rake.application['tf:apply'].invoke
360
- end
361
- it 'runs the apply command with one target' do
362
- Rake.application['tf:apply'].clear_prerequisites
363
- allow(subject).to receive(:var_file_path).and_return('file.tfvars.json')
364
- allow(subject).to receive(:terraform_runner)
365
- expect(subject).to receive(:terraform_runner).once
366
- .with('terraform apply -var-file file.tfvars.json ' \
367
- '-target tar.get[1]')
368
- Rake.application['tf:apply'].invoke('tar.get[1]')
369
- end
370
- it 'runs the apply command with three targets' do
371
- Rake.application['tf:apply'].clear_prerequisites
372
- allow(subject).to receive(:var_file_path).and_return('file.tfvars.json')
373
- allow(subject).to receive(:terraform_runner)
374
- expect(subject).to receive(:terraform_runner).once
375
- .with('terraform apply -var-file file.tfvars.json ' \
376
- '-target tar.get[1] -target t.gt[2] -target my.target[3]')
377
- Rake.application['tf:apply'].invoke(
378
- 'tar.get[1]', 't.gt[2]', 'my.target[3]'
379
- )
362
+ context 'terraform version 0.9.5' do
363
+ before(:each) do
364
+ allow(subject).to receive(:tf_version)
365
+ .and_return(Gem::Version.new('0.9.5'))
366
+ end
367
+ it 'runs the apply command with no targets' do
368
+ Rake.application['tf:apply'].clear_prerequisites
369
+ allow(subject).to receive(:var_file_path).and_return('file.tfvars.json')
370
+ allow(subject).to receive(:terraform_runner)
371
+ allow(subject).to receive(:update_consul_stack_env_vars)
372
+ expect(subject).to receive(:terraform_runner).once
373
+ .with('terraform apply -var-file file.tfvars.json')
374
+ expect(subject).to_not receive(:update_consul_stack_env_vars)
375
+ Rake.application['tf:apply'].invoke
376
+ end
377
+ it 'runs the apply command with one target' do
378
+ Rake.application['tf:apply'].clear_prerequisites
379
+ allow(subject).to receive(:var_file_path).and_return('file.tfvars.json')
380
+ allow(subject).to receive(:terraform_runner)
381
+ expect(subject).to receive(:terraform_runner).once
382
+ .with('terraform apply -var-file file.tfvars.json ' \
383
+ '-target tar.get[1]')
384
+ Rake.application['tf:apply'].invoke('tar.get[1]')
385
+ end
386
+ it 'runs the apply command with three targets' do
387
+ Rake.application['tf:apply'].clear_prerequisites
388
+ allow(subject).to receive(:var_file_path).and_return('file.tfvars.json')
389
+ allow(subject).to receive(:terraform_runner)
390
+ expect(subject).to receive(:terraform_runner).once
391
+ .with('terraform apply -var-file file.tfvars.json ' \
392
+ '-target tar.get[1] -target t.gt[2] -target my.target[3]')
393
+ Rake.application['tf:apply'].invoke(
394
+ 'tar.get[1]', 't.gt[2]', 'my.target[3]'
395
+ )
396
+ end
397
+ it 'runs update_consul_stack_env_vars if consul_env_vars_prefix !nil' do
398
+ subject.instance_variable_set('@consul_env_vars_prefix', 'foo')
399
+ Rake.application['tf:apply'].clear_prerequisites
400
+ allow(subject).to receive(:var_file_path).and_return('file.tfvars.json')
401
+ allow(subject).to receive(:terraform_runner)
402
+ allow(subject).to receive(:update_consul_stack_env_vars)
403
+ expect(subject).to receive(:terraform_runner).once
404
+ .with('terraform apply -var-file file.tfvars.json')
405
+ expect(subject).to receive(:update_consul_stack_env_vars).once
406
+ Rake.application['tf:apply'].invoke
407
+ end
380
408
  end
381
- it 'runs update_consul_stack_env_vars if consul_env_vars_prefix not nil' do
382
- subject.instance_variable_set('@consul_env_vars_prefix', 'foo')
383
- Rake.application['tf:apply'].clear_prerequisites
384
- allow(subject).to receive(:var_file_path).and_return('file.tfvars.json')
385
- allow(subject).to receive(:terraform_runner)
386
- allow(subject).to receive(:update_consul_stack_env_vars)
387
- expect(subject).to receive(:terraform_runner).once
388
- .with('terraform apply -var-file file.tfvars.json')
389
- expect(subject).to receive(:update_consul_stack_env_vars).once
390
- Rake.application['tf:apply'].invoke
409
+ context 'terraform version 0.10.2' do
410
+ before(:each) do
411
+ allow(subject).to receive(:tf_version)
412
+ .and_return(Gem::Version.new('0.10.2'))
413
+ end
414
+ it 'runs the apply command with no targets' do
415
+ Rake.application['tf:apply'].clear_prerequisites
416
+ allow(subject).to receive(:var_file_path).and_return('file.tfvars.json')
417
+ allow(subject).to receive(:terraform_runner)
418
+ allow(subject).to receive(:update_consul_stack_env_vars)
419
+ expect(subject).to receive(:terraform_runner).once
420
+ .with('terraform apply -auto-approve -var-file file.tfvars.json')
421
+ expect(subject).to_not receive(:update_consul_stack_env_vars)
422
+ Rake.application['tf:apply'].invoke
423
+ end
424
+ it 'runs the apply command with one target' do
425
+ Rake.application['tf:apply'].clear_prerequisites
426
+ allow(subject).to receive(:var_file_path).and_return('file.tfvars.json')
427
+ allow(subject).to receive(:terraform_runner)
428
+ expect(subject).to receive(:terraform_runner).once
429
+ .with('terraform apply -auto-approve -var-file file.tfvars.json ' \
430
+ '-target tar.get[1]')
431
+ Rake.application['tf:apply'].invoke('tar.get[1]')
432
+ end
433
+ it 'runs the apply command with three targets' do
434
+ Rake.application['tf:apply'].clear_prerequisites
435
+ allow(subject).to receive(:var_file_path).and_return('file.tfvars.json')
436
+ allow(subject).to receive(:terraform_runner)
437
+ expect(subject).to receive(:terraform_runner).once
438
+ .with('terraform apply -auto-approve -var-file file.tfvars.json ' \
439
+ '-target tar.get[1] -target t.gt[2] -target my.target[3]')
440
+ Rake.application['tf:apply'].invoke(
441
+ 'tar.get[1]', 't.gt[2]', 'my.target[3]'
442
+ )
443
+ end
444
+ it 'runs update_consul_stack_env_vars if consul_env_vars_prefix !nil' do
445
+ subject.instance_variable_set('@consul_env_vars_prefix', 'foo')
446
+ Rake.application['tf:apply'].clear_prerequisites
447
+ allow(subject).to receive(:var_file_path).and_return('file.tfvars.json')
448
+ allow(subject).to receive(:terraform_runner)
449
+ allow(subject).to receive(:update_consul_stack_env_vars)
450
+ expect(subject).to receive(:terraform_runner).once
451
+ .with('terraform apply -auto-approve -var-file file.tfvars.json')
452
+ expect(subject).to receive(:update_consul_stack_env_vars).once
453
+ Rake.application['tf:apply'].invoke
454
+ end
391
455
  end
392
456
  end
393
457
  describe '#install_refresh' do
@@ -558,6 +622,47 @@ describe TFWrapper::RakeTasks do
558
622
  end
559
623
  end
560
624
  end
625
+ describe '#install_output' do
626
+ # these let/before/after come from bundler's gem_helper_spec.rb
627
+ let!(:rake_application) { Rake.application }
628
+ before(:each) do
629
+ Rake::Task.clear
630
+ Rake.application = Rake::Application.new
631
+ end
632
+ after(:each) do
633
+ Rake.application = rake_application
634
+ end
635
+ before do
636
+ subject.install_output
637
+ end
638
+
639
+ it 'adds the output task' do
640
+ expect(Rake.application['tf:output']).to be_instance_of(Rake::Task)
641
+ expect(Rake.application['tf:output'].prerequisites)
642
+ .to eq(%w[tf:init tf:refresh])
643
+ end
644
+ it 'runs the output command' do
645
+ Rake.application['tf:output'].clear_prerequisites
646
+ allow(subject).to receive(:var_file_path).and_return('file.tfvars.json')
647
+ allow(subject).to receive(:terraform_runner)
648
+ expect(subject).to receive(:terraform_runner).once
649
+ .with('terraform output')
650
+ Rake.application['tf:output'].invoke
651
+ end
652
+ it 'adds the output_json task' do
653
+ expect(Rake.application['tf:output_json']).to be_instance_of(Rake::Task)
654
+ expect(Rake.application['tf:output_json'].prerequisites)
655
+ .to eq(%w[tf:init tf:refresh])
656
+ end
657
+ it 'runs the output -json command' do
658
+ Rake.application['tf:output_json'].clear_prerequisites
659
+ allow(subject).to receive(:var_file_path).and_return('file.tfvars.json')
660
+ allow(subject).to receive(:terraform_runner)
661
+ expect(subject).to receive(:terraform_runner).once
662
+ .with('terraform output -json')
663
+ Rake.application['tf:output_json'].invoke
664
+ end
665
+ end
561
666
  describe '#terraform_vars' do
562
667
  it 'builds a hash and sets overrides' do
563
668
  v_from_e = { 'vfe1' => 'vfe1name', 'vfe2' => 'vfe2name' }
@@ -718,7 +823,7 @@ describe TFWrapper::RakeTasks do
718
823
  .and_return(['Terraform v3.4.5-dev (abcde1234+CHANGES)', 0])
719
824
  allow(Gem::Version).to receive(:new).and_return(ver)
720
825
  expect(Gem::Version).to receive(:new).once.with('3.4.5')
721
- subject.check_tf_version
826
+ expect(subject.check_tf_version).to eq(ver)
722
827
  end
723
828
  it 'fails if the version cannot be identified' do
724
829
  allow(TFWrapper::Helpers).to receive(:run_cmd_stream_output)
data/tfwrapper.gemspec CHANGED
@@ -1,4 +1,3 @@
1
- # -*- encoding: utf-8 -*-
2
1
  # frozen_string_literal: true
3
2
 
4
3
  require File.expand_path('../lib/tfwrapper/version', __FILE__)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tfwrapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - jantman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-06 00:00:00.000000000 Z
11
+ date: 2017-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: retries