terraform-wrapper 0.1.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: 42a8d62aa53845eaf88d6eab555ccd6f5185197691f91695ad582c2eb6deffe5
4
- data.tar.gz: bf3c949c33705cbab386b9c1cdc1cd5e408a082bcb5f3a636e2897679011310b
3
+ metadata.gz: cec70134c0df1674effb0b5e07ffaa3ff7d1a2ca034dd6452563f616e67e09c1
4
+ data.tar.gz: b4c6416feea1f68b3db4f593f7e2692a7553869977dad45aa0da045477841add
5
5
  SHA512:
6
- metadata.gz: 32d29ab063a2a9a211de73e9ad416c1bbca85d62f68fbae07a324806260b674187968aba8a68ecd4740879e0d05373f7eeb48c4538185dc1e2e2aca82983f896
7
- data.tar.gz: 34025e0835bb7e806d659679226f32ed850dfa6fe3b35181e83fca9901037cde9831a31be47172c5c8af10c5aa14aa5f49725458da3c63cfd6867342fb3610ed
6
+ metadata.gz: 0cb74ef36d96167259fec72539932d7614a25ad2f442fd75940bdaff3fe63b17aa7a793f17f90368c930112d7ab433950bf0c900ed46694d4dfd8e1cba3d311b
7
+ data.tar.gz: '090b646a9d688e4e12149752cffa4f71414a47c70b4ec619a7a142e342e9ece421e929baf42045b767c896666406317f0aff602b12d35d123b767bf87793b303'
data/.gitlab-ci.yml CHANGED
@@ -32,6 +32,6 @@ deploy:
32
32
  - echo "-- Publishing Gem --"
33
33
  - rake release
34
34
  - echo "-- Listing Sums --"
35
- - sha256sum *.gem
35
+ - sha256sum "pkg"/*.gem
36
36
 
37
37
  ###############################################################################
data/Gemfile CHANGED
@@ -11,7 +11,3 @@ source "https://rubygems.org"
11
11
  gemspec
12
12
 
13
13
  ###############################################################################
14
-
15
- gem "rake", "~> 13.0"
16
-
17
- ###############################################################################
@@ -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
  ###############################################################################
@@ -78,7 +78,7 @@ module TerraformWrapper
78
78
  stdout = `#{cmdline}`
79
79
  code = $?.exitstatus
80
80
 
81
- logger.fatal("Failed to get secret: #{name} from key vault: #{vault}!") if (code != 0 and stdout.strip.empty?)
81
+ logger.fatal("Failed to get secret: #{name} from key vault: #{vault}!") if (code != 0 or stdout.strip.empty?)
82
82
 
83
83
  return(stdout.strip)
84
84
  end
@@ -97,11 +97,11 @@ module TerraformWrapper
97
97
  details = YAML.load(stdout.strip)
98
98
 
99
99
  logger.fatal("Returned details did not include the subscription ID!") unless details.key?("id")
100
- logger.fatal("Returned subscription ID is not a String!") unless details["id"].kind_of?(String)
100
+ logger.fatal("Returned subscription ID is not a string!") unless details["id"].kind_of?(String)
101
101
  logger.fatal("Returned subscription ID is empty!") if details["id"].strip.empty?
102
102
 
103
103
  logger.fatal("Returned details did not include the tenant ID!") unless details.key?("tenant")
104
- logger.fatal("Returned tenant ID is not a String!") unless details["tenant"].kind_of?(String)
104
+ logger.fatal("Returned tenant ID is not a string!") unless details["tenant"].kind_of?(String)
105
105
  logger.fatal("Returned tenant ID is empty!") if details["tenant"].strip.empty?
106
106
 
107
107
  details.transform_values! { |value| value.strip }
@@ -120,36 +120,43 @@ module TerraformWrapper
120
120
  logger.fatal("Azure CLI must be installed and accessible to use the Azure authenticator.") unless cli
121
121
 
122
122
  logger.fatal("Azure authenticator mandatory option 'subscription' has not been set!") unless @options.key?("subscription")
123
+ logger.fatal("Azure authenticator subscription must be a string!") unless @options["subscription"].kind_of?(String)
124
+ logger.fatal("Azure authenticator subscription must not be blank!") if @options["subscription"].strip.empty?
123
125
 
124
126
  subscription = @options["subscription"]
125
127
 
126
- logger.fatal("Azure authenticator subscription must be a String!") unless subscription.kind_of?(String)
127
- logger.fatal("Azure authenticator subscription must not be blank!") if subscription.strip.empty?
128
-
129
128
  if @options.key?("keyvault") then
130
- keyvault = @options["keyvault"]
129
+ logger.fatal("Azure authenticator keyvault name must be a string if specified!") unless @options["keyvault"].kind_of?(String)
130
+ logger.fatal("Azure authenticator keyvault name must not be blank if specified!") if @options["keyvault"].strip.empty?
131
131
 
132
- logger.fatal("Azure authenticator keyvault name must be a String if specified!") unless keyvault.kind_of?(String)
133
- logger.fatal("Azure authenticator keyvault name must not be blank if specified!") if keyvault.strip.empty?
132
+ keyvault = @options["keyvault"]
134
133
 
135
- username = @options.key?("username-secret") ? @options["username-secret"] : "terraform-username"
134
+ if @options.key?("username-secret") then
135
+ logger.fatal("Azure authenticator keyvault secret for username must be a string if keyvault name is specified!") unless @options["username-secret"].kind_of?(String)
136
+ logger.fatal("Azure authenticator keyvault secret for username must not be blank if keyvault name is specified!") if @options["username-secret"].strip.empty?
136
137
 
137
- logger.fatal("Azure authenticator keyvault secret for username must be a String if keyvault name is specified!") unless username.kind_of?(String)
138
- logger.fatal("Azure authenticator keyvault secret for username must not be blank if keyvault name is specified!") if username.strip.empty?
138
+ username = @options["username-secret"]
139
+ else
140
+ username = "terraform-username"
141
+ end
139
142
 
140
- password = @options.key?("username-password") ? @options["username-password"] : "terraform-password"
143
+ if @options.key?("password-secret") then
144
+ logger.fatal("Azure authenticator keyvault secret for password must be a string if keyvault name is specified!") unless @options["password-secret"].kind_of?(String)
145
+ logger.fatal("Azure authenticator keyvault secret for password must not be blank if keyvault name is specified!") if @options["password-secret"].strip.empty?
141
146
 
142
- logger.fatal("Azure authenticator keyvault secret for password must be a String if keyvault name is specified!") unless password.kind_of?(String)
143
- logger.fatal("Azure authenticator keyvault secret for password must not be blank if keyvault name is specified!") if password.strip.empty?
147
+ password = @options["password-secret"]
148
+ else
149
+ password = "terraform-password"
150
+ end
144
151
  end
145
152
 
146
153
  begin
147
- subscription = subscription % @variables
148
- keyvault = keyvault % @variables unless keyvault.nil?
149
- username = username % @variables unless keyvault.nil?
150
- 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?
151
158
  rescue
152
- 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!")
153
160
  end
154
161
 
155
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
  ###############################################################################
@@ -70,17 +70,17 @@ module TerraformWrapper
70
70
 
71
71
  bucket = @options["bucket"]
72
72
 
73
- logger.fatal("AWS backend S3 bucket name must be a String!") unless bucket.kind_of?(String)
73
+ logger.fatal("AWS backend S3 bucket name must be a string!") unless bucket.kind_of?(String)
74
74
  logger.fatal("AWS backend S3 bucket name must not be blank!") if bucket.strip.empty?
75
75
 
76
76
  region = @options["region"]
77
77
 
78
- logger.fatal("AWS backend S3 bucket region must be a String!") unless region.kind_of?(String)
78
+ logger.fatal("AWS backend S3 bucket region must be a string!") unless region.kind_of?(String)
79
79
  logger.fatal("AWS backend S3 bucket region must not be blank!") if region.strip.empty?
80
80
 
81
81
  key = @options.key?("key") ? @options["key"] : File.join("%{service}", "%{config}", "%{component}" + @@ext)
82
82
 
83
- logger.fatal("AWS backend S3 bucket key must be a String!") unless key.kind_of?(String)
83
+ logger.fatal("AWS backend S3 bucket key must be a string!") unless key.kind_of?(String)
84
84
  logger.fatal("AWS backend S3 bucket key must not be blank!") if key.strip.empty?
85
85
 
86
86
  encrypt = @options.key?("encrypt") ? @options["encrypt"] : true
@@ -90,29 +90,29 @@ module TerraformWrapper
90
90
  if @options.key?("kms") then
91
91
  kms = @options["kms"]
92
92
 
93
- logger.fatal("AWS backend S3 bucket encryption KMS key ARN must be a String if specified!") unless kms.kind_of?(String)
93
+ logger.fatal("AWS backend S3 bucket encryption KMS key ARN must be a string if specified!") unless kms.kind_of?(String)
94
94
  logger.fatal("AWS backend S3 bucket encryption KMS key ARN must not be blank if specified!") if kms.strip.empty?
95
95
  end
96
96
 
97
97
  if @options.key?("role") then
98
98
  role = @options["role"]
99
99
 
100
- logger.fatal("AWS backend role to assume ARN must be a String if specified!") unless role.kind_of?(String)
100
+ logger.fatal("AWS backend role to assume ARN must be a string if specified!") unless role.kind_of?(String)
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
  ###############################################################################
@@ -57,35 +57,35 @@ module TerraformWrapper
57
57
 
58
58
  group = @options["group"]
59
59
 
60
- logger.fatal("Azure backend group must be a String!") unless group.kind_of?(String)
60
+ logger.fatal("Azure backend group must be a string!") unless group.kind_of?(String)
61
61
  logger.fatal("Azure backend group must not be blank!") if group.strip.empty?
62
62
 
63
63
  account = @options.key?("account") ? @options["account"] : group + "tf"
64
64
 
65
- logger.fatal("Azure backend storage account must be a String!") unless account.kind_of?(String)
65
+ logger.fatal("Azure backend storage account must be a string!") unless account.kind_of?(String)
66
66
  logger.fatal("Azure backend storage account must not be blank!") if account.strip.empty?
67
67
 
68
68
  container = @options.key?("container") ? @options["container"] : "default"
69
69
 
70
- logger.fatal("Azure backend storage account container must be a String!") unless container.kind_of?(String)
70
+ logger.fatal("Azure backend storage account container must be a string!") unless container.kind_of?(String)
71
71
  logger.fatal("Azure backend storage account container must not be blank!") if container.strip.empty?
72
72
 
73
73
  key = @options.key?("key") ? @options["key"] : File.join("%{service}", "%{config}", "%{component}" + @@ext)
74
74
 
75
- logger.fatal("Azure backend storage account key must be a String!") unless key.kind_of?(String)
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
  ###############################################################################
@@ -49,17 +49,18 @@ module TerraformWrapper
49
49
  def specific()
50
50
  path = @options.key?("path") ? @options["path"] : File.join(Dir.pwd, "state", "terraform", "%{config}", "%{component}" + @@ext)
51
51
 
52
- logger.fatal("Local backend path must be a String!") unless path.kind_of?(String)
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)
@@ -28,12 +28,12 @@ module TerraformWrapper
28
28
  ###############################################################################
29
29
 
30
30
  def initialize(options:)
31
- logger.fatal("Binary base path must be a String!") unless options["base"].kind_of?(String)
31
+ logger.fatal("Binary base path must be a string!") unless options["base"].kind_of?(String)
32
32
  logger.fatal("Binary base path must not be blank!") if options["base"].strip.empty?
33
33
 
34
34
  @base = options["base"]
35
35
 
36
- logger.fatal("Binary version must be a String!") unless options["version"].kind_of?(String)
36
+ logger.fatal("Binary version must be a string!") unless options["version"].kind_of?(String)
37
37
  logger.fatal("Binary version must not be blank!") if options["version"].strip.empty?
38
38
 
39
39
  @version = options["version"]
@@ -23,12 +23,12 @@ module TerraformWrapper
23
23
  ###############################################################################
24
24
 
25
25
  def initialize(options:)
26
- logger.fatal("Code base path must be a String!") unless options["base"].kind_of?(String)
26
+ logger.fatal("Code base path must be a string!") unless options["base"].kind_of?(String)
27
27
  logger.fatal("Code base path must not be blank!") if options["base"].strip.empty?
28
28
 
29
29
  @base = options["base"]
30
30
 
31
- logger.fatal("Code component name must be a String!") unless options["name"].kind_of?(String)
31
+ logger.fatal("Code component name must be a string!") unless options["name"].kind_of?(String)
32
32
  logger.fatal("Code component name must not be blank!") if options["name"].strip.empty?
33
33
 
34
34
  @name = options["name"]
@@ -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_ext = ".tfvars"
29
-
30
25
  ###############################################################################
31
26
 
32
27
  attr_reader :auths
@@ -36,23 +31,22 @@ 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
  ###############################################################################
43
37
 
44
38
  def initialize(code:, options:)
45
- logger.fatal("Configuration base path must be a String!") unless options["base"].kind_of?(String)
39
+ logger.fatal("Configuration base path must be a string!") unless options["base"].kind_of?(String)
46
40
  logger.fatal("Configuration base path must not be blank!") if options["base"].strip.empty?
47
41
 
48
42
  @base = options["base"]
49
43
 
50
- logger.fatal("Configuration service name must be a String!") unless options["service"].kind_of?(String)
44
+ logger.fatal("Configuration service name must be a string!") unless options["service"].kind_of?(String)
51
45
  logger.fatal("Configuration service name must not be blank!") if options["service"].strip.empty?
52
46
 
53
47
  @service = options["service"]
54
48
 
55
- logger.fatal("Configuration name must be a String!") unless options["name"].kind_of?(String)
49
+ logger.fatal("Configuration name must be a string!") unless options["name"].kind_of?(String)
56
50
  logger.fatal("Configuration name must not be blank!") if options["name"].strip.empty?
57
51
 
58
52
  @name = options["name"]
@@ -65,7 +59,7 @@ module TerraformWrapper
65
59
 
66
60
  auth_azure_options = options["auth-azure-options"]
67
61
 
68
- logger.fatal("Configuration backend name must be a String!") unless options["backend"].kind_of?(String)
62
+ logger.fatal("Configuration backend name must be a string!") unless options["backend"].kind_of?(String)
69
63
  logger.fatal("Configuration backend name must not be blank!") if options["backend"].strip.empty?
70
64
 
71
65
  backend = options["backend"]
@@ -75,71 +69,37 @@ module TerraformWrapper
75
69
  backend_options = options["backend-options"]
76
70
 
77
71
  @code = code
78
- @path = find
72
+ @path = ::TerraformWrapper.find(base: @base, name: @name, exts: @@config_exts, description: "Configuration")
79
73
 
80
74
  yaml = YAML.load(File.read(@path))
81
-
82
75
  logger.fatal("Invalid YAML in configuration file: #{@path}") unless yaml.kind_of?(Hash)
83
76
 
84
- if yaml.key?("variables") then
85
- logger.fatal("Key 'variables' is not a hash in configuration file: #{@path}") unless yaml["variables"].kind_of?(Hash)
86
- @variables = TerraformWrapper::Shared::Variables.new(values: yaml["variables"])
87
- else
88
- @variables = TerraformWrapper::Shared::Variables.new()
89
- 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")
90
80
 
91
- @variable_files = yaml.key?("terraform") ? validate(variable_files: yaml["terraform"]) : Array.new
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"]
84
+
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
92
88
 
93
89
  @auths = Array.new
94
- @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
95
91
 
96
92
  if backend == "local" then
97
- @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)
98
94
  elsif backend == "aws" then
99
- @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)
100
96
  elsif backend == "azure" then
101
- @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)
102
98
  else
103
99
  logger.fatal("Backend: #{backend} is not valid!")
104
100
  end
105
101
  end
106
102
 
107
- ###############################################################################
108
-
109
- private
110
-
111
- ###############################################################################
112
-
113
- def find
114
- @@config_exts.each do |config_ext|
115
- path = File.join(@base, @name + config_ext)
116
- return path if File.file?(path)
117
- end
118
-
119
- logger.fatal("Terraform configuration name: #{@name} not found in location: #{@base}!")
120
- end
121
-
122
- ###############################################################################
123
-
124
- def validate(variable_files:)
125
- logger.fatal("Optional key 'variable_files' must be a list of strings!") unless variable_files.kind_of?(Array)
126
-
127
- result = Array.new
128
-
129
- variable_files.each do |variable_file|
130
- logger.fatal("All elements of 'variable_files' must be strings!") unless variable_file.kind_of?(String)
131
- logger.fatal("All elements of 'variable_files' must not be blank!") if variable_file.strip.empty?
132
- path = File.join(@base, @@variable_files_name, variable_file.strip + @@variable_files_ext)
133
- if File.file?(path) then
134
- result.append(path)
135
- else
136
- logger.fatal("Terraform variables file: #{variable_file}, path: #{path} does not exist!")
137
- end
138
- end
139
-
140
- return result
141
- end
142
-
143
103
  ###############################################################################
144
104
 
145
105
  end
@@ -29,7 +29,6 @@ module TerraformWrapper
29
29
  @code = code
30
30
 
31
31
  @initialised = false
32
- @ready = false
33
32
  end
34
33
 
35
34
  ###############################################################################
@@ -48,11 +47,11 @@ module TerraformWrapper
48
47
  def import(address: nil, id: nil)
49
48
  logger.fatal("Cannot Terraform import before initialising backend!") unless initialised
50
49
 
51
- logger.fatal("Terraform state address for import must be a String!") unless address.kind_of?(String)
52
- logger.fatal("Terraform state address for import must be a String!") unless address.kind_of?(String)
50
+ logger.fatal("Terraform state address for import must be a string!") unless address.kind_of?(String)
51
+ logger.fatal("Terraform state address for import must be a string!") unless address.kind_of?(String)
53
52
  logger.fatal("Terraform state address for import must not be blank!") if address.strip.empty?
54
53
 
55
- logger.fatal("Identification for infrastructure to import must be a String!") unless id.kind_of?(String)
54
+ logger.fatal("Identification for infrastructure to import must be a string!") unless id.kind_of?(String)
56
55
  logger.fatal("Identification for infrastructure to import must not be blank!") if id.strip.empty?
57
56
 
58
57
  parameters = Array.new
@@ -152,8 +151,8 @@ module TerraformWrapper
152
151
 
153
152
  result = Array.new
154
153
 
155
- @config.variable_files.each do |variable_file|
156
- result.append("-var-file=\"#{variable_file}\"")
154
+ @config.variables.files.each do |file|
155
+ result.append("-var-file=\"#{file}\"")
157
156
  end
158
157
 
159
158
  return result
@@ -166,10 +165,6 @@ module TerraformWrapper
166
165
 
167
166
  result = Array.new
168
167
 
169
- result.append("-var=\"component=#{@code.name}\"")
170
- result.append("-var=\"config=#{@config.name}\"")
171
- result.append("-var=\"service=#{@config.service}\"")
172
-
173
168
  @config.variables.values.each do |key, value|
174
169
  result.append("-var=\"#{key.to_s}=#{value}\"")
175
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
@@ -133,7 +133,7 @@ module TerraformWrapper
133
133
  def extract(archive:, binary:, destination:)
134
134
  logger.info("Extracting: #{archive}")
135
135
 
136
- Zip::ZipFile.open(archive) do |zip|
136
+ Zip::File.open(archive) do |zip|
137
137
  zip.each do |file|
138
138
  zip.extract(file, destination) if file.name == binary
139
139
  end
@@ -4,7 +4,7 @@ module TerraformWrapper
4
4
 
5
5
  ###############################################################################
6
6
 
7
- VERSION = "0.1.2"
7
+ VERSION = "1.0.0"
8
8
 
9
9
  ###############################################################################
10
10
 
@@ -31,6 +31,9 @@ Gem::Specification.new do |spec|
31
31
  spec.bindir = "exe"
32
32
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
33
33
  spec.require_paths = ["lib"]
34
+
35
+ spec.add_dependency 'rake', '~> 13.0'
36
+ spec.add_dependency 'rubyzip', '~> 2.3'
34
37
  end
35
38
 
36
39
  ###############################################################################
metadata CHANGED
@@ -1,15 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: terraform-wrapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.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-02-18 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2021-03-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '13.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '13.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubyzip
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.3'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.3'
13
41
  description: A ruby wrapper for managing Terraform binaries and remote state. Each
14
42
  Terraform command (plan, apply, etc) is wrapped so that the correct binary is used
15
43
  and remote state referenced.