kitchen-terraform 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.
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2016 New Context Services, Inc.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require_relative 'group'
18
+
19
+ module Terraform
20
+ # Behaviour for the [:groups] config option
21
+ module GroupsConfig
22
+ def self.included(configurable_class)
23
+ configurable_class.required_config :groups do |_, value, configurable|
24
+ configurable.coerce_groups value: value
25
+ end
26
+ configurable_class.default_config :groups, []
27
+ end
28
+
29
+ def coerce_attributes(group:)
30
+ group[:attributes] = Hash group[:attributes]
31
+ rescue TypeError
32
+ config_error attribute: "groups][#{group}][:attributes",
33
+ expected: 'a mapping of Inspec attribute names to ' \
34
+ 'Terraform output variable names'
35
+ end
36
+
37
+ def coerce_controls(group:)
38
+ group[:controls] = Array group[:controls]
39
+ end
40
+
41
+ def coerce_groups(value:)
42
+ config[:groups] = Array(value).map do |group|
43
+ Group.new data: coerced_group(value: group)
44
+ end
45
+ end
46
+
47
+ def coerce_hostnames(group:)
48
+ group[:hostnames] = String group[:hostnames]
49
+ end
50
+
51
+ def coerce_name(group:)
52
+ group[:name] = String group[:name]
53
+ end
54
+
55
+ def coerce_port(group:)
56
+ group[:port] = Integer group.fetch(:port) { transport[:port] }
57
+ rescue ArgumentError, TypeError
58
+ config_error attribute: "groups][#{group}][:port", expected: 'an integer'
59
+ end
60
+
61
+ def coerce_username(group:)
62
+ group[:username] = String group.fetch(:username) { transport[:username] }
63
+ end
64
+
65
+ def coerced_group(value:)
66
+ Hash(value).tap do |group|
67
+ coerce_attributes group: group
68
+ coerce_controls group: group
69
+ coerce_hostnames group: group
70
+ coerce_name group: group
71
+ coerce_port group: group
72
+ coerce_username group: group
73
+ end
74
+ rescue TypeError
75
+ config_error attribute: "groups][#{value}", expected: 'a group mapping'
76
+ end
77
+ end
78
+ end
@@ -15,6 +15,7 @@
15
15
  # limitations under the License.
16
16
 
17
17
  require 'inspec'
18
+ require 'kitchen'
18
19
 
19
20
  module Terraform
20
21
  # Inspec::Runner with convenience methods for use by
@@ -22,26 +23,15 @@ module Terraform
22
23
  class InspecRunner < Inspec::Runner
23
24
  attr_reader :conf
24
25
 
25
- def self.run_and_verify(group:, options:, verifier:)
26
- new(options).tap do |runner|
27
- group.populate runner: runner
28
- verifier.populate runner: runner
29
- verifier.evaluate exit_code: runner.run
30
- end
31
- end
32
-
33
- def add(target:)
34
- add_target target, conf
35
- end
36
-
37
- def set_attribute(key:, value:)
38
- conf[:attributes].store key.to_s, value
26
+ def evaluate(verifier:)
27
+ verifier.add_targets runner: self
28
+ verifier.verify exit_code: run
39
29
  end
40
30
 
41
31
  private
42
32
 
43
33
  def initialize(conf = {})
44
- conf.store :attributes, conf.fetch(:attributes, {}).clone
34
+ conf[:attributes] = Kitchen::Util.stringified_hash conf[:attributes]
45
35
  super
46
36
  end
47
37
  end
@@ -15,24 +15,31 @@
15
15
  # limitations under the License.
16
16
 
17
17
  require_relative 'command'
18
+ require_relative 'command_extender'
19
+ require_relative 'zero_six_output'
20
+ require_relative 'zero_seven_output'
18
21
 
19
22
  module Terraform
20
23
  # Command to extract values of output variables
21
24
  class OutputCommand < Command
25
+ include CommandExtender
26
+
22
27
  def name
23
28
  'output'
24
29
  end
25
30
 
26
- def options
27
- "-state=#{state}"
28
- end
29
-
30
31
  private
31
32
 
32
- attr_accessor :state
33
+ attr_accessor :list, :state
33
34
 
34
- def initialize_attributes(state:)
35
+ def initialize_attributes(list:, version:, state:)
36
+ extend_behaviour version: version
37
+ self.list = list
35
38
  self.state = state
36
39
  end
40
+
41
+ def version_behaviours
42
+ { /v0.7/ => ZeroSevenOutput, /v0.6/ => ZeroSixOutput }
43
+ end
37
44
  end
38
45
  end
@@ -27,14 +27,13 @@ module Terraform
27
27
  end
28
28
 
29
29
  def options
30
- "-destroy=#{destroy} -input=false -out=#{out} -state=#{state}" \
31
- "#{color_switch}" \
32
- "#{processed_variables}#{processed_variable_files}"
30
+ "-destroy=#{destroy} -input=false -out=#{out} -state=#{state} " \
31
+ "#{color_switch}#{processed_variables}#{processed_variable_files}"
33
32
  end
34
33
 
35
34
  private
36
35
 
37
- attr_accessor :color, :destroy, :out, :state, :variables, :variable_files
36
+ attr_accessor :destroy, :out, :state, :variables, :variable_files
38
37
 
39
38
  def initialize_attributes(
40
39
  color:, destroy:, out:, state:, variables:, variable_files:
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2016 New Context Services, Inc.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ module Terraform
18
+ # Behaviour for the [:plan] config option
19
+ module PlanConfig
20
+ def self.included(configurable_class)
21
+ configurable_class.default_config :plan do |configurable|
22
+ configurable.instance_pathname filename: 'terraform.tfplan'
23
+ end
24
+ configurable_class.expand_path_for :plan
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2016 New Context Services, Inc.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require_relative 'command'
18
+ require_relative 'color_switch'
19
+
20
+ module Terraform
21
+ # Command to show a state or plan
22
+ class ShowCommand < Command
23
+ include ColorSwitch
24
+
25
+ def name
26
+ 'show'
27
+ end
28
+
29
+ def options
30
+ color_switch
31
+ end
32
+
33
+ private
34
+
35
+ def initialize_attributes(color:)
36
+ self.color = color
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2016 New Context Services, Inc.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ module Terraform
18
+ # Behaviour for the [:state] config option
19
+ module StateConfig
20
+ def self.included(configurable_class)
21
+ configurable_class.default_config :state do |configurable|
22
+ configurable.instance_pathname filename: 'terraform.tfstate'
23
+ end
24
+ configurable_class.expand_path_for :state
25
+ end
26
+ end
27
+ end
@@ -22,5 +22,9 @@ module Terraform
22
22
  def name
23
23
  'validate'
24
24
  end
25
+
26
+ def options
27
+ ''
28
+ end
25
29
  end
26
30
  end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2016 New Context Services, Inc.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ module Terraform
18
+ # Behaviour for the [:variable_files] config option
19
+ module VariableFilesConfig
20
+ def self.included(configurable_class)
21
+ configurable_class
22
+ .required_config :variable_files do |_, value, configurable|
23
+ configurable.coerce_variable_files value: value
24
+ end
25
+ configurable_class.default_config :variable_files, []
26
+ configurable_class.expand_path_for :variable_files
27
+ end
28
+
29
+ def coerce_variable_files(value:)
30
+ config[:variable_files] = Array value
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2016 New Context Services, Inc.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ module Terraform
18
+ # Behaviour for the [:variables] config option
19
+ module VariablesConfig
20
+ def self.included(configurable_class)
21
+ configurable_class.required_config :variables do |_, value, configurable|
22
+ configurable.coerce_variables value: value
23
+ end
24
+ configurable_class.default_config :variables, {}
25
+ end
26
+
27
+ def coerce_variables(value:)
28
+ config[:variables] =
29
+ if value.is_a?(Array) || value.is_a?(String)
30
+ deprecated_variables_format value: value
31
+ else
32
+ Hash value
33
+ end
34
+ rescue ArgumentError, TypeError
35
+ config_error attribute: 'variables',
36
+ expected: 'a mapping of Terraform variable assignments'
37
+ end
38
+
39
+ private
40
+
41
+ def deprecated_variables_format(value:)
42
+ config_deprecated attribute: 'variables', remediation: 'Use a mapping',
43
+ type: 'a list or string', version: '1.0'
44
+ Hash[Array(value).map { |string| string.split '=' }]
45
+ end
46
+ end
47
+ end
@@ -15,5 +15,5 @@
15
15
  # limitations under the License.
16
16
 
17
17
  module Terraform
18
- VERSION = '0.2.0'
18
+ VERSION = '0.3.0'
19
19
  end
@@ -22,5 +22,9 @@ module Terraform
22
22
  def name
23
23
  'version'
24
24
  end
25
+
26
+ def options
27
+ ''
28
+ end
25
29
  end
26
30
  end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2016 New Context Services, Inc.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require 'json'
18
+
19
+ module Terraform
20
+ # Behaviour for OutputCommand with Terraform 0.7
21
+ module ZeroSevenOutput
22
+ def options
23
+ "-json=true -state=#{state}"
24
+ end
25
+
26
+ private
27
+
28
+ def processed_output(raw_output:)
29
+ JSON.parse(raw_output).fetch('value')
30
+ .tap { |value| return list ? Array(value) : value }
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2016 New Context Services, Inc.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ module Terraform
18
+ # Behaviour for OutputCommand with Terraform 0.6
19
+ module ZeroSixOutput
20
+ def options
21
+ "-state=#{state}"
22
+ end
23
+
24
+ private
25
+
26
+ def processed_output(raw_output:)
27
+ raw_output.chomp.tap { |value| return list ? value.split(',') : value }
28
+ end
29
+ end
30
+ end