terraform-wrapper 0.1.0 → 0.2.1

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: 8c1c8efca42bd473f3bbfa5c0e97f2782b8cc2652f96fa17b213c8fa87d4b0ae
4
- data.tar.gz: df46cad597ad4101afc53b242a94e11452e9bd14b722a1e76eb91ad9a02909a3
3
+ metadata.gz: 55d9fe8e3823828068db9f36f402b43a3a163e13a21eeae6372f4f62d54c8b55
4
+ data.tar.gz: 815b07b1e492517c8712b9c2f4c604d73dfe30a5ad16c571e77b34bfe6fb6285
5
5
  SHA512:
6
- metadata.gz: 97be709f764c69cd868834dfa6d6ffbe1917d40ede4ef0ceaf2a01fc26fdf84f9d5da0a45c8ac02f017f29eb42071cbf8ce50e3d8862123c2a96f4ce9adfcc04
7
- data.tar.gz: e1f8420bc2165a6819bc08a85fbf5d3290d3e40154866d5d6e47ddabd51eb8124ad748668cae8dfb6fd4e7964b4d6cb68eec747d32aa92fc29991166713b7192
6
+ metadata.gz: ea5f272f105000df04c9681d06a46f3cc19e588eefda12560bc3c637f465b0195c94e64477e3a03fb1c4c74084aa0a0b3317ee28fbc27a3d692ad91ecc8022dc
7
+ data.tar.gz: 9e03e5875feb041349cf51b3ff40a6796d4e1dee8f8e1307b281bca8ab710803cd2d67fc0b83258a2c73048c06db484cb8cb6c8b6ad0ede2961b5c8c380131d5
data/.gitlab-ci.yml CHANGED
@@ -31,5 +31,7 @@ deploy:
31
31
  - hash gem
32
32
  - echo "-- Publishing Gem --"
33
33
  - rake release
34
+ - echo "-- Listing Sums --"
35
+ - sha256sum "pkg"/*.gem
34
36
 
35
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
- ###############################################################################
@@ -14,7 +14,7 @@ module TerraformWrapper
14
14
 
15
15
  ###############################################################################
16
16
 
17
- @logger = Shared::Logging.logger_for("TerraformWrapper")
17
+ @logger = TerraformWrapper::Shared::Logging.logger_for("TerraformWrapper")
18
18
 
19
19
  ###############################################################################
20
20
 
@@ -51,9 +51,11 @@ module TerraformWrapper
51
51
  tasks << TerraformWrapper::Tasks::Binary.new(binary: binary)
52
52
  tasks << TerraformWrapper::Tasks::Clean.new(code: code)
53
53
  tasks << TerraformWrapper::Tasks::Destroy.new(binary: binary, code: code, options: config_options)
54
+ tasks << TerraformWrapper::Tasks::Import.new(binary: binary, code: code, options: config_options)
54
55
  tasks << TerraformWrapper::Tasks::Init.new(binary: binary, code: code, options: config_options)
55
56
  tasks << TerraformWrapper::Tasks::Plan.new(binary: binary, code: code, options: config_options)
56
57
  tasks << TerraformWrapper::Tasks::PlanDestroy.new(binary: binary, code: code, options: config_options)
58
+ tasks << TerraformWrapper::Tasks::Upgrade.new(binary: binary, code: code)
57
59
  tasks << TerraformWrapper::Tasks::Validate.new(binary: binary, code: code)
58
60
  return tasks
59
61
  end
@@ -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,27 +120,34 @@ 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
@@ -29,6 +29,7 @@ module TerraformWrapper
29
29
  ###############################################################################
30
30
 
31
31
  attr_reader :bucket
32
+ attr_reader :encrypt
32
33
  attr_reader :key
33
34
  attr_reader :region
34
35
 
@@ -41,11 +42,17 @@ module TerraformWrapper
41
42
  ###############################################################################
42
43
 
43
44
  def hash()
44
- return {
45
- "bucket" => @bucket,
46
- "key" => @key,
47
- "region" => @region
48
- }
45
+ result = Hash.new
46
+
47
+ result["bucket"] = @bucket
48
+ result["region"] = @region
49
+ result["key"] = @key
50
+ result["encrypt"] = @encrypt.to_s
51
+
52
+ result["kms_key_id"] = @kms unless @kms.nil?
53
+ result["role_arn"] = @role unless @role.nil?
54
+
55
+ return result
49
56
  end
50
57
 
51
58
  ###############################################################################
@@ -55,24 +62,45 @@ module TerraformWrapper
55
62
  ###############################################################################
56
63
 
57
64
  def specific()
65
+ kms = nil
66
+ role = nil
67
+
58
68
  logger.fatal("AWS backend mandatory option 'bucket' has not been set!") unless @options.key?("bucket")
59
69
  logger.fatal("AWS backend mandatory option 'region' has not been set!") unless @options.key?("region")
60
70
 
61
71
  bucket = @options["bucket"]
62
72
 
63
- 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)
64
74
  logger.fatal("AWS backend S3 bucket name must not be blank!") if bucket.strip.empty?
65
75
 
66
76
  region = @options["region"]
67
77
 
68
- 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)
69
79
  logger.fatal("AWS backend S3 bucket region must not be blank!") if region.strip.empty?
70
80
 
71
81
  key = @options.key?("key") ? @options["key"] : File.join("%{service}", "%{config}", "%{component}" + @@ext)
72
82
 
73
- 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)
74
84
  logger.fatal("AWS backend S3 bucket key must not be blank!") if key.strip.empty?
75
85
 
86
+ encrypt = @options.key?("encrypt") ? @options["encrypt"] : true
87
+
88
+ logger.fatal("AWS backend S3 bucket encryption enabled must be a Boolean!") unless [ true, false ].include?(encrypt)
89
+
90
+ if @options.key?("kms") then
91
+ kms = @options["kms"]
92
+
93
+ logger.fatal("AWS backend S3 bucket encryption KMS key ARN must be a string if specified!") unless kms.kind_of?(String)
94
+ logger.fatal("AWS backend S3 bucket encryption KMS key ARN must not be blank if specified!") if kms.strip.empty?
95
+ end
96
+
97
+ if @options.key?("role") then
98
+ role = @options["role"]
99
+
100
+ logger.fatal("AWS backend role to assume ARN must be a string if specified!") unless role.kind_of?(String)
101
+ logger.fatal("AWS backend role to assume ARN must not be blank if specified!") if role.strip.empty?
102
+ end
103
+
76
104
  logger.fatal("AWS backend S3 bucket name or key must include %{service}.") unless (bucket.include?("%{service}") or key.include?("%{service}"))
77
105
  logger.fatal("AWS backend S3 bucket name or key must include %{config}.") unless (bucket.include?("%{config}") or key.include?("%{config}"))
78
106
  logger.fatal("AWS backend S3 bucket name or key must include %{component}.") unless (bucket.include?("%{component}") or key.include?("%{component}"))
@@ -81,15 +109,20 @@ module TerraformWrapper
81
109
  bucket = bucket % @variables
82
110
  region = region % @variables
83
111
  key = key % @variables
112
+ kms = kms % @variables unless kms.nil?
113
+ role = role % @variables unless role.nil?
84
114
  rescue
85
115
  logger.fatal("AWS backend options contain variables that are not included in the configuration file!")
86
116
  end
87
117
 
88
118
  logger.fatal("Key: #{key} is too long for backend of type: #{@@type}") if key.length > 1024
89
119
 
90
- @bucket = bucket
91
- @region = region
92
- @key = key
120
+ @bucket = bucket
121
+ @region = region
122
+ @key = key
123
+ @encrypt = encrypt
124
+ @kms = kms
125
+ @role = role
93
126
  end
94
127
 
95
128
  ###############################################################################
@@ -57,22 +57,22 @@ 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
78
  logger.fatal("Azure backend container or key must include %{service}.") unless (container.include?("%{service}") or key.include?("%{service}"))
@@ -49,7 +49,7 @@ 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
55
  logger.fatal("Local backend path must include %{service} or the path to this repository.") unless (path.include?("%{service}") or path.include?(Dir.pwd))
@@ -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"]
@@ -24,8 +24,8 @@ module TerraformWrapper
24
24
 
25
25
  ###############################################################################
26
26
 
27
- @@variable_files_name = "tfvars"
28
- @@variable_files_ext = ".tfvars"
27
+ @@variable_files_name = "tfvars"
28
+ @@variable_files_exts = [ ".tfvars" ]
29
29
 
30
30
  ###############################################################################
31
31
 
@@ -42,17 +42,17 @@ module TerraformWrapper
42
42
  ###############################################################################
43
43
 
44
44
  def initialize(code:, options:)
45
- logger.fatal("Configuration base path must be a String!") unless options["base"].kind_of?(String)
45
+ logger.fatal("Configuration base path must be a string!") unless options["base"].kind_of?(String)
46
46
  logger.fatal("Configuration base path must not be blank!") if options["base"].strip.empty?
47
47
 
48
48
  @base = options["base"]
49
49
 
50
- logger.fatal("Configuration service name must be a String!") unless options["service"].kind_of?(String)
50
+ logger.fatal("Configuration service name must be a string!") unless options["service"].kind_of?(String)
51
51
  logger.fatal("Configuration service name must not be blank!") if options["service"].strip.empty?
52
52
 
53
53
  @service = options["service"]
54
54
 
55
- logger.fatal("Configuration name must be a String!") unless options["name"].kind_of?(String)
55
+ logger.fatal("Configuration name must be a string!") unless options["name"].kind_of?(String)
56
56
  logger.fatal("Configuration name must not be blank!") if options["name"].strip.empty?
57
57
 
58
58
  @name = options["name"]
@@ -65,7 +65,7 @@ module TerraformWrapper
65
65
 
66
66
  auth_azure_options = options["auth-azure-options"]
67
67
 
68
- logger.fatal("Configuration backend name must be a String!") unless options["backend"].kind_of?(String)
68
+ logger.fatal("Configuration backend name must be a string!") unless options["backend"].kind_of?(String)
69
69
  logger.fatal("Configuration backend name must not be blank!") if options["backend"].strip.empty?
70
70
 
71
71
  backend = options["backend"]
@@ -75,10 +75,9 @@ module TerraformWrapper
75
75
  backend_options = options["backend-options"]
76
76
 
77
77
  @code = code
78
- @path = find
78
+ @path = find(base: @base, name: @name, exts: @@config_exts, description: "Configuration")
79
79
 
80
80
  yaml = YAML.load(File.read(@path))
81
-
82
81
  logger.fatal("Invalid YAML in configuration file: #{@path}") unless yaml.kind_of?(Hash)
83
82
 
84
83
  if yaml.key?("variables") then
@@ -110,13 +109,13 @@ module TerraformWrapper
110
109
 
111
110
  ###############################################################################
112
111
 
113
- def find
112
+ def find(base:, name:, exts:, description: "File")
114
113
  @@config_exts.each do |config_ext|
115
- path = File.join(@base, @name + config_ext)
114
+ path = File.join(base, name + config_ext)
116
115
  return path if File.file?(path)
117
116
  end
118
117
 
119
- logger.fatal("Terraform configuration name: #{@name} not found in location: #{@base}!")
118
+ logger.fatal("#{description} name: #{@name} not found in location: #{@base}!")
120
119
  end
121
120
 
122
121
  ###############################################################################
@@ -127,14 +126,9 @@ module TerraformWrapper
127
126
  result = Array.new
128
127
 
129
128
  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
129
+ logger.fatal("All elements of 'terraform' must be strings!") unless variable_file.kind_of?(String)
130
+ logger.fatal("All elements of 'terraform' must not be blank!") if variable_file.strip.empty?
131
+ result.append(find(base: File.join(@base, @@variable_files_name), name: variable_file.strip, exts: @@variable_files_exts, description: "Terraform values file"))
138
132
  end
139
133
 
140
134
  return result
@@ -39,19 +39,19 @@ module TerraformWrapper
39
39
  ###############################################################################
40
40
 
41
41
  def warn(message)
42
- warn(format(colour: 33, message: message))
42
+ super(format(colour: 33, message: message))
43
43
  end
44
44
 
45
45
  ###############################################################################
46
46
 
47
47
  def error(message)
48
- error(format(colour: 31, message: message))
48
+ super(format(colour: 31, message: message))
49
49
  end
50
50
 
51
51
  ###############################################################################
52
52
 
53
53
  def fatal(message)
54
- fatal(format(colour: 31, message: message))
54
+ super(format(colour: 31, message: message))
55
55
  exit(1)
56
56
  end
57
57
 
@@ -29,12 +29,11 @@ module TerraformWrapper
29
29
  @code = code
30
30
 
31
31
  @initialised = false
32
- @ready = false
33
32
  end
34
33
 
35
34
  ###############################################################################
36
35
 
37
- def download(upgrade: true)
36
+ def download(upgrade: false)
38
37
  parameters = Array.new
39
38
  parameters.append("-backend=false")
40
39
  parameters.append("-upgrade") if upgrade
@@ -45,10 +44,31 @@ module TerraformWrapper
45
44
 
46
45
  ###############################################################################
47
46
 
48
- def init(config:, upgrade: true)
47
+ def import(address: nil, id: nil)
48
+ logger.fatal("Cannot Terraform import before initialising backend!") unless initialised
49
+
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)
52
+ logger.fatal("Terraform state address for import must not be blank!") if address.strip.empty?
53
+
54
+ logger.fatal("Identification for infrastructure to import must be a string!") unless id.kind_of?(String)
55
+ logger.fatal("Identification for infrastructure to import must not be blank!") if id.strip.empty?
56
+
57
+ parameters = Array.new
58
+ parameters.concat(variable_files)
59
+ parameters.concat(variable_strings)
60
+
61
+ parameters.append("'#{address}'")
62
+ parameters.append("'#{id}'")
63
+
64
+ logger.fatal("Terraform import failed!") unless run(action: "import", parameters: parameters)
65
+ end
66
+
67
+ ###############################################################################
68
+
69
+ def init(config:)
49
70
  parameters = Array.new
50
71
  parameters.append("-reconfigure")
51
- parameters.append("-upgrade") if upgrade
52
72
 
53
73
  config.backend.hash.each do |key, value|
54
74
  parameters.append("-backend-config=\"#{key}=#{value}\"")
@@ -1,12 +1,18 @@
1
1
  ###############################################################################
2
2
 
3
+ require 'rake/tasklib'
4
+
5
+ ###############################################################################
6
+
3
7
  require_relative 'tasks/apply'
4
8
  require_relative 'tasks/binary'
5
9
  require_relative 'tasks/clean'
6
10
  require_relative 'tasks/destroy'
11
+ require_relative 'tasks/import'
7
12
  require_relative 'tasks/init'
8
13
  require_relative 'tasks/plan'
9
14
  require_relative 'tasks/plandestroy'
15
+ require_relative 'tasks/upgrade'
10
16
  require_relative 'tasks/validate'
11
17
 
12
18
  ###############################################################################
@@ -1,9 +1,5 @@
1
1
  ###############################################################################
2
2
 
3
- require 'rake/tasklib'
4
-
5
- ###############################################################################
6
-
7
3
  module TerraformWrapper
8
4
 
9
5
  ###############################################################################
@@ -3,7 +3,6 @@
3
3
  require 'digest'
4
4
  require 'fileutils'
5
5
  require 'net/http'
6
- require 'rake/tasklib'
7
6
  require 'uri'
8
7
  require 'zip'
9
8
 
@@ -134,7 +133,7 @@ module TerraformWrapper
134
133
  def extract(archive:, binary:, destination:)
135
134
  logger.info("Extracting: #{archive}")
136
135
 
137
- Zip::ZipFile.open(archive) do |zip|
136
+ Zip::File.open(archive) do |zip|
138
137
  zip.each do |file|
139
138
  zip.extract(file, destination) if file.name == binary
140
139
  end
@@ -1,9 +1,5 @@
1
1
  ###############################################################################
2
2
 
3
- require 'rake/tasklib'
4
-
5
- ###############################################################################
6
-
7
3
  module TerraformWrapper
8
4
 
9
5
  ###############################################################################
@@ -1,9 +1,5 @@
1
1
  ###############################################################################
2
2
 
3
- require 'rake/tasklib'
4
-
5
- ###############################################################################
6
-
7
3
  module TerraformWrapper
8
4
 
9
5
  ###############################################################################
@@ -0,0 +1,66 @@
1
+ ###############################################################################
2
+
3
+ module TerraformWrapper
4
+
5
+ ###############################################################################
6
+
7
+ module Tasks
8
+
9
+ ###############################################################################
10
+
11
+ class Import < ::Rake::TaskLib
12
+
13
+ ###############################################################################
14
+
15
+ include TerraformWrapper::Shared::Logging
16
+
17
+ ###############################################################################
18
+
19
+ @binary
20
+ @code
21
+ @options
22
+
23
+ ###############################################################################
24
+
25
+ def initialize(binary:, code:, options:)
26
+ @binary = binary
27
+ @code = code
28
+ @options = options
29
+
30
+ yield self if block_given?
31
+
32
+ import_task
33
+ end
34
+
35
+ ###############################################################################
36
+
37
+ def import_task
38
+ desc "Import a piece of existing infrastructure into Terraform state for a given configuration on an infrastructure component."
39
+ task :import, [:config, :address, :id] => :binary do |t, args|
40
+ options = @options.merge({"name" => args[:config]})
41
+
42
+ logger.info("Processing configuration for Terraform import...")
43
+
44
+ config = TerraformWrapper::Shared::Config.new(code: @code, options: options)
45
+ runner = TerraformWrapper::Shared::Runner.new(binary: @binary, code: @code)
46
+
47
+ logger.info("Running Terraform import for service: #{config.service}, component: #{@code.name}...")
48
+
49
+ runner.init(config: config)
50
+ runner.import(address: args[:address], id: args[:id])
51
+ end
52
+ end
53
+
54
+ ###############################################################################
55
+
56
+ end
57
+
58
+ ###############################################################################
59
+
60
+ end
61
+
62
+ ###############################################################################
63
+
64
+ end
65
+
66
+ ###############################################################################
@@ -1,9 +1,5 @@
1
1
  ###############################################################################
2
2
 
3
- require 'rake/tasklib'
4
-
5
- ###############################################################################
6
-
7
3
  module TerraformWrapper
8
4
 
9
5
  ###############################################################################
@@ -1,9 +1,5 @@
1
1
  ###############################################################################
2
2
 
3
- require 'rake/tasklib'
4
-
5
- ###############################################################################
6
-
7
3
  module TerraformWrapper
8
4
 
9
5
  ###############################################################################
@@ -1,9 +1,5 @@
1
1
  ###############################################################################
2
2
 
3
- require 'rake/tasklib'
4
-
5
- ###############################################################################
6
-
7
3
  module TerraformWrapper
8
4
 
9
5
  ###############################################################################
@@ -0,0 +1,58 @@
1
+ ###############################################################################
2
+
3
+ module TerraformWrapper
4
+
5
+ ###############################################################################
6
+
7
+ module Tasks
8
+
9
+ ###############################################################################
10
+
11
+ class Upgrade < ::Rake::TaskLib
12
+
13
+ ###############################################################################
14
+
15
+ include TerraformWrapper::Shared::Logging
16
+
17
+ ###############################################################################
18
+
19
+ @binary
20
+ @code
21
+
22
+ ###############################################################################
23
+
24
+ def initialize(binary:, code:)
25
+ @binary = binary
26
+ @code = code
27
+
28
+ yield self if block_given?
29
+
30
+ upgrade_task
31
+ end
32
+
33
+ ###############################################################################
34
+
35
+ def upgrade_task
36
+ desc "Upgrades the Terraform infrastructure component modules, providers and lock file."
37
+ task :upgrade => :binary do |t, args|
38
+ runner = TerraformWrapper::Shared::Runner.new(binary: @binary, code: @code)
39
+
40
+ logger.info("Upgrading Terraform component: #{@code.name}...")
41
+
42
+ runner.download(upgrade: true)
43
+ end
44
+ end
45
+
46
+ ###############################################################################
47
+
48
+ end
49
+
50
+ ###############################################################################
51
+
52
+ end
53
+
54
+ ###############################################################################
55
+
56
+ end
57
+
58
+ ###############################################################################
@@ -1,9 +1,5 @@
1
1
  ###############################################################################
2
2
 
3
- require 'rake/tasklib'
4
-
5
- ###############################################################################
6
-
7
3
  module TerraformWrapper
8
4
 
9
5
  ###############################################################################
@@ -4,7 +4,7 @@ module TerraformWrapper
4
4
 
5
5
  ###############################################################################
6
6
 
7
- VERSION = "0.1.0"
7
+ VERSION = "0.2.1"
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.0
4
+ version: 0.2.1
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-15 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2021-03-05 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.
@@ -50,9 +78,11 @@ files:
50
78
  - lib/terraform-wrapper/tasks/binary.rb
51
79
  - lib/terraform-wrapper/tasks/clean.rb
52
80
  - lib/terraform-wrapper/tasks/destroy.rb
81
+ - lib/terraform-wrapper/tasks/import.rb
53
82
  - lib/terraform-wrapper/tasks/init.rb
54
83
  - lib/terraform-wrapper/tasks/plan.rb
55
84
  - lib/terraform-wrapper/tasks/plandestroy.rb
85
+ - lib/terraform-wrapper/tasks/upgrade.rb
56
86
  - lib/terraform-wrapper/tasks/validate.rb
57
87
  - lib/terraform-wrapper/version.rb
58
88
  - terraform-wrapper.gemspec