tfrb 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.rspec +3 -0
  4. data/.ruby-gemset +1 -0
  5. data/.ruby-version +1 -0
  6. data/.travis.yml +7 -0
  7. data/Gemfile +3 -0
  8. data/Gemfile.lock +57 -0
  9. data/LICENSE +202 -0
  10. data/README.md +35 -0
  11. data/Rakefile +6 -0
  12. data/bin/console +14 -0
  13. data/bin/setup +8 -0
  14. data/exe/tfrb +5 -0
  15. data/lib/tfrb/base.rb +198 -0
  16. data/lib/tfrb/block.rb +44 -0
  17. data/lib/tfrb/cli.rb +68 -0
  18. data/lib/tfrb/config.rb +15 -0
  19. data/lib/tfrb/provider/aws.rb +10 -0
  20. data/lib/tfrb/provider.rb +22 -0
  21. data/lib/tfrb/resource/aws_db_instance.rb +32 -0
  22. data/lib/tfrb/resource/aws_db_subnet_group.rb +28 -0
  23. data/lib/tfrb/resource/aws_dynamodb_table.rb +26 -0
  24. data/lib/tfrb/resource/aws_ebs_volume.rb +32 -0
  25. data/lib/tfrb/resource/aws_elasticache_replication_group.rb +30 -0
  26. data/lib/tfrb/resource/aws_elasticache_subnet_group.rb +24 -0
  27. data/lib/tfrb/resource/aws_iam_policy.rb +142 -0
  28. data/lib/tfrb/resource/aws_iam_role.rb +39 -0
  29. data/lib/tfrb/resource/aws_iam_role_policy_attachment.rb +27 -0
  30. data/lib/tfrb/resource/aws_instance.rb +46 -0
  31. data/lib/tfrb/resource/aws_kms_key.rb +26 -0
  32. data/lib/tfrb/resource/aws_s3_bucket.rb +20 -0
  33. data/lib/tfrb/resource/aws_security_group.rb +39 -0
  34. data/lib/tfrb/resource/aws_storagegateway_cache.rb +21 -0
  35. data/lib/tfrb/resource/aws_storagegateway_gateway.rb +24 -0
  36. data/lib/tfrb/resource/aws_storagegateway_nfs_file_share.rb +27 -0
  37. data/lib/tfrb/resource/aws_subnet.rb +28 -0
  38. data/lib/tfrb/resource/aws_volume_attachment.rb +30 -0
  39. data/lib/tfrb/resource/aws_vpc.rb +24 -0
  40. data/lib/tfrb/resource.rb +124 -0
  41. data/lib/tfrb/version.rb +3 -0
  42. data/lib/tfrb.rb +1 -0
  43. data/tfrb.gemspec +32 -0
  44. metadata +187 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1062c33e29d17913b910e607fc5a3b14e2c2a9907c51e53e6641e3abf529761d
4
+ data.tar.gz: 47a20f47aab5abb3e39ab89f65ad50af71a2ebad3d19d9da91eaf106b65d7982
5
+ SHA512:
6
+ metadata.gz: a1c5edcab90a0311656d77396e38414ff99f7cc23c1d27b6d5074ece57601de78cc6966ad8a32071a42f99171faee27898933ec97cd324d38132ad5741b81832
7
+ data.tar.gz: cdf678dd0b1e92e5338b2372ab5505246fbfefa5a6f0f89acd1881397ea96bbd6bcd6f8462906f2bcd119b237c62e04676f14cc35c29556b5754c34a34ffe97e
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ tfrb
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.5.3
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.3
7
+ before_install: gem install bundler -v 2.0.1
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,57 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ tfrb (0.1.0)
5
+ aws-sdk (~> 2)
6
+ mixlib-shellout
7
+ thor
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ aws-eventstream (1.0.2)
13
+ aws-sdk (2.11.256)
14
+ aws-sdk-resources (= 2.11.256)
15
+ aws-sdk-core (2.11.256)
16
+ aws-sigv4 (~> 1.0)
17
+ jmespath (~> 1.0)
18
+ aws-sdk-resources (2.11.256)
19
+ aws-sdk-core (= 2.11.256)
20
+ aws-sigv4 (1.1.0)
21
+ aws-eventstream (~> 1.0, >= 1.0.2)
22
+ coderay (1.1.2)
23
+ diff-lcs (1.3)
24
+ jmespath (1.4.0)
25
+ method_source (0.9.2)
26
+ mixlib-shellout (2.4.4)
27
+ pry (0.12.2)
28
+ coderay (~> 1.1.0)
29
+ method_source (~> 0.9.0)
30
+ rake (10.5.0)
31
+ rspec (3.8.0)
32
+ rspec-core (~> 3.8.0)
33
+ rspec-expectations (~> 3.8.0)
34
+ rspec-mocks (~> 3.8.0)
35
+ rspec-core (3.8.0)
36
+ rspec-support (~> 3.8.0)
37
+ rspec-expectations (3.8.2)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.8.0)
40
+ rspec-mocks (3.8.0)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.8.0)
43
+ rspec-support (3.8.0)
44
+ thor (0.20.3)
45
+
46
+ PLATFORMS
47
+ ruby
48
+
49
+ DEPENDENCIES
50
+ bundler (~> 2.0)
51
+ pry
52
+ rake (~> 10.0)
53
+ rspec (~> 3.0)
54
+ tfrb!
55
+
56
+ BUNDLED WITH
57
+ 2.0.1
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # tfrb
2
+
3
+ Ruby DSL for Terraform
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'tfrb'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install tfrb
20
+
21
+ ## Usage
22
+
23
+ $ tfrm --help
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Granicus/tfrb.
34
+
35
+ Please feel free to fork and modify if it does not suit your needs!
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'tfrb'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/tfrb ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'tfrb/cli'
4
+
5
+ Tfrb::CLI.start
data/lib/tfrb/base.rb ADDED
@@ -0,0 +1,198 @@
1
+ require_relative 'version'
2
+ require_relative 'block'
3
+ require_relative 'config'
4
+
5
+ require 'mixlib/shellout'
6
+
7
+ class Tfrb::Base
8
+ attr_accessor :block
9
+ attr_accessor :path
10
+ attr_accessor :temp_path
11
+ attr_accessor :environments
12
+ attr_accessor :state
13
+ attr_accessor :skip_import
14
+
15
+ def initialize(block, environments)
16
+ @skip_import = false
17
+ @block = block
18
+ @path = Tfrb::Config[:path]
19
+ @temp_path = File.join(Tfrb::Config[:temp_path], block)
20
+ Dir.mkdir(@temp_path) unless Dir.exist?(@temp_path)
21
+ @environments = environments.each_with_object({}) do |environment, hash|
22
+ hash[environment] = Tfrb::Block.load(environment)
23
+ end
24
+ @state = {}
25
+ end
26
+
27
+ def write!
28
+ @environments.each do |environment_name, environment|
29
+ File.open(File.join(temp_path, "#{environment_name}.tf.json"), 'w') do |file|
30
+ file.write(JSON.pretty_generate(environment, indent: ' '))
31
+ end
32
+ end
33
+ end
34
+
35
+ def init!
36
+ tf_init = Mixlib::ShellOut.new('terraform', 'init', shell_opts)
37
+ tf_init.run_command
38
+ tf_init.error!
39
+ end
40
+
41
+ def load_state!
42
+ return if skip_import
43
+ return unless s3_state?
44
+ tf_pullstate = Mixlib::ShellOut.new('terraform', 'state', 'pull', { cwd: temp_path })
45
+ tf_pullstate.run_command
46
+ tf_pullstate.error!
47
+ pulled_state = JSON.parse(tf_pullstate.stdout)
48
+ if pulled_state['modules']
49
+ pulled_state['modules'].each do |state_module|
50
+ if state_module['resources'] && state_module['resources'].size >= 1
51
+ state_module['resources'].each do |resource_key, resource_state|
52
+ next if resource_key =~ /^data\./
53
+ resource_type = resource_state['type']
54
+ resource_name = resource_key.sub(/^#{resource_type}./, '')
55
+ state[resource_type] = {} unless state.has_key?(resource_type)
56
+ state[resource_type][resource_name] = resource_state['primary']['attributes'] unless state[resource_type].has_key?(resource_name)
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ def import!(resource, name, id)
64
+ return if skip_import
65
+ if resource !~ /^postgresql/
66
+ environments_backup = Marshal.load(Marshal.dump(@environments))
67
+ @environments.each do |environment_name, environment|
68
+ if environment.has_key?('provider')
69
+ environment['provider'].delete('postgresql') if environment['provider'].has_key?('postgresql')
70
+ environment.delete('provider') if environment['provider'].empty?
71
+ end
72
+ if environment.has_key?('resource')
73
+ environment['resource'].delete_if { |resource, _| resource =~ /^postgresql/ }
74
+ environment.delete('resource') if environment['resource'].empty?
75
+ end
76
+ end
77
+ write!
78
+ end
79
+ providers = @environments.find { |_, e| e['resource'] && e['resource'][resource] && e['resource'][resource][name] && e['resource'][resource][name]['provider'] }
80
+ if providers && provider = providers[1]['resource'][resource][name]['provider']
81
+ tf_import = Mixlib::ShellOut.new('terraform', 'import', "-provider=#{provider}", "#{resource}.#{name}", id, cwd: temp_path)
82
+ else
83
+ tf_import = Mixlib::ShellOut.new('terraform', 'import', "#{resource}.#{name}", id, cwd: temp_path)
84
+ end
85
+ tf_import.run_command
86
+ tf_import.error!
87
+ state[resource] ||= {}
88
+ state[resource][name] = Tfrb::Resource.get_state(self, resource, name)
89
+ printf "\033[1;32mImported %s.%s from %s\033[0m\n", resource, name, id
90
+ if resource !~ /^postgresql/
91
+ @environments = environments_backup
92
+ write!
93
+ end
94
+ end
95
+
96
+ def staterm!(resource_id)
97
+ printf "\033[1;32mRemoving #{resource_id} from state...\033[0m\n"
98
+ tf_staterm = Mixlib::ShellOut.new('terraform', 'state', 'rm', resource_id, shell_opts)
99
+ tf_staterm.run_command
100
+ tf_staterm.error!
101
+ end
102
+
103
+ def taint!(resource_id)
104
+ printf "\033[1;32mTainting #{resource_id}...\033[0m\n"
105
+ tf_taint = Mixlib::ShellOut.new('terraform', 'taint', resource_id, shell_opts)
106
+ tf_taint.run_command
107
+ tf_taint.error!
108
+ end
109
+
110
+ def unlock!(lock_id)
111
+ printf "\033[1;32mForce unlocking state...\033[0m\n"
112
+ tf_unlock = Mixlib::ShellOut.new('terraform', 'force-unlock', lock_id, shell_opts.merge(input: 'yes'))
113
+ tf_unlock.run_command
114
+ tf_unlock.error!
115
+ end
116
+
117
+ def plan!
118
+ printf "\033[1;32mCalculating plan...\033[0m\n"
119
+ tf_plan = Mixlib::ShellOut.new('terraform', 'plan', '-out=plan.cache', shell_opts)
120
+ tf_plan.run_command
121
+ tf_plan.error!
122
+ end
123
+
124
+ def apply!
125
+ printf "\033[1;32mApplying plan...\033[0m\n"
126
+ tf_apply = Mixlib::ShellOut.new('terraform', 'apply', '-auto-approve', 'plan.cache', shell_opts)
127
+ tf_apply.run_command
128
+ tf_apply.error!
129
+ plan_cache = File.join(temp_path, 'plan.cache')
130
+ File.delete(plan_cache) if File.exist?(plan_cache)
131
+ end
132
+
133
+ def clean!
134
+ %w(*.tf.json).each do |glob|
135
+ Dir.glob(File.join(temp_path, glob)).each do |file|
136
+ File.delete(file) if File.exist?(file)
137
+ end
138
+ end
139
+ end
140
+
141
+ def s3_state?
142
+ environments.find { |k,v| v['terraform'] && v['terraform']['backend'] && v['terraform']['backend']['s3'] }
143
+ end
144
+
145
+ def shell_opts
146
+ {
147
+ cwd: temp_path,
148
+ live_stderr: $stderr,
149
+ live_stdout: $stdout,
150
+ timeout: 3600
151
+ }
152
+ end
153
+
154
+ class << self
155
+ def load(block, environments, skip_import = false)
156
+ Tfrb::Resource.load_helpers!
157
+
158
+ printf "\033[1;32mLoading %s...\033[0m\n", block
159
+ tfrb = self.new(block, environments)
160
+
161
+ # Set skip_import
162
+ tfrb.skip_import = skip_import
163
+
164
+ # Clean temporary files before starting
165
+ tfrb.clean!
166
+
167
+ # Load providers using extend (allows providers to fill in credentials, etc.)
168
+ Tfrb::Provider.load(tfrb)
169
+
170
+ # Write the .tf.json files to pick up any injection performed by providers
171
+ tfrb.write!
172
+
173
+ # Run terraform init if necessary
174
+ tfrb.init! unless Dir.exist?(File.join(tfrb.temp_path, '.terraform'))
175
+
176
+ # Load state
177
+ tfrb.load_state!
178
+
179
+ # Preload resources
180
+ Tfrb::Resource.preload(tfrb)
181
+
182
+ # Write the .tf.json files so terraform import can be run by custom resources
183
+ tfrb.write!
184
+
185
+ # Load resources using extend (allows them to locate existing entities and run terraform import)
186
+ Tfrb::Resource.load(tfrb)
187
+
188
+ # Write the .tf.json files again to pick up any injection performed by resources
189
+ tfrb.write!
190
+
191
+ # Return a fully loaded tfrb instance
192
+ tfrb
193
+ end
194
+ end
195
+ end
196
+
197
+ require_relative 'provider'
198
+ require_relative 'resource'
data/lib/tfrb/block.rb ADDED
@@ -0,0 +1,44 @@
1
+ module Tfrb
2
+ class Block
3
+ Tfrb::Config[:extra_modules].each do |extra_module|
4
+ include(extra_module)
5
+ end
6
+
7
+ def initialize
8
+ @data = {}
9
+ end
10
+
11
+ def to_h
12
+ @data
13
+ end
14
+
15
+ def import(filename, params = {})
16
+ instance_eval(IO.read(File.join(Tfrb::Config[:path], "#{filename}.rb")), filename)
17
+ end
18
+
19
+ def method_missing(method_name, *args, &block)
20
+ method_name = method_name.to_s.gsub(/_$/, '').to_sym if method_name.to_s =~ /_$/
21
+ if block_given?
22
+ @data[method_name.to_s] ||= {}
23
+ instance = self.class.new
24
+ instance.instance_eval(&block)
25
+ if args && args.size > 0
26
+ hash = args[0..-2].reduce(@data[method_name.to_s]) { |h, v| h[v] ||= {} }
27
+ hash[args.last] = instance.to_h
28
+ else
29
+ @data[method_name.to_s] = instance.to_h
30
+ end
31
+ # puts "method missing called for #{method_name}[#{args}] with block: #{instance.to_h}"
32
+ else
33
+ @data.merge!({ method_name.to_s => args[0] })
34
+ # puts "method missing called for #{method_name}[#{args}] without block"
35
+ end
36
+ end
37
+
38
+ def self.load(filename)
39
+ instance = self.new
40
+ instance.import(filename)
41
+ instance.to_h
42
+ end
43
+ end
44
+ end