terraforming 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +10 -0
  5. data/CODE_OF_CONDUCT.md +13 -0
  6. data/Gemfile +4 -0
  7. data/Guardfile +12 -0
  8. data/LICENSE +22 -0
  9. data/README.md +132 -0
  10. data/Rakefile +7 -0
  11. data/bin/terraforming +5 -0
  12. data/lib/terraforming.rb +22 -0
  13. data/lib/terraforming/cli.rb +69 -0
  14. data/lib/terraforming/resource/db_parameter_group.rb +58 -0
  15. data/lib/terraforming/resource/db_security_group.rb +53 -0
  16. data/lib/terraforming/resource/db_subnet_group.rb +52 -0
  17. data/lib/terraforming/resource/ec2.rb +69 -0
  18. data/lib/terraforming/resource/elb.rb +56 -0
  19. data/lib/terraforming/resource/rds.rb +74 -0
  20. data/lib/terraforming/resource/s3.rb +51 -0
  21. data/lib/terraforming/resource/security_group.rb +56 -0
  22. data/lib/terraforming/resource/subnet.rb +55 -0
  23. data/lib/terraforming/resource/vpc.rb +67 -0
  24. data/lib/terraforming/template/tf/db_parameter_group.erb +17 -0
  25. data/lib/terraforming/template/tf/db_security_group.erb +26 -0
  26. data/lib/terraforming/template/tf/db_subnet_group.erb +8 -0
  27. data/lib/terraforming/template/tf/ec2.erb +27 -0
  28. data/lib/terraforming/template/tf/elb.erb +28 -0
  29. data/lib/terraforming/template/tf/rds.erb +25 -0
  30. data/lib/terraforming/template/tf/s3.erb +7 -0
  31. data/lib/terraforming/template/tf/security_group.erb +46 -0
  32. data/lib/terraforming/template/tf/subnet.erb +15 -0
  33. data/lib/terraforming/template/tf/vpc.erb +15 -0
  34. data/lib/terraforming/util.rb +34 -0
  35. data/lib/terraforming/version.rb +3 -0
  36. data/scripts/console +14 -0
  37. data/scripts/setup +7 -0
  38. data/terraforming.gemspec +34 -0
  39. metadata +242 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cd7e2563a8bdca0e28d2ce67043ef45de9371c68
4
+ data.tar.gz: ed9d20b570d77f232511563e529a9b7fe2a94837
5
+ SHA512:
6
+ metadata.gz: a0cb8f5bfb4b9417a0cac3b1798f3098d4d804f1e9310fae5a75b9b3513cf138509075f8218b4ae089a8be43cd2deca43860c9e53388560e2bf4a3c3c948a0c5
7
+ data.tar.gz: 766a117c13ca47faa4ceeb5fb6697135374978cfaad7df3463d7c2682e10bb6cb4f98b6924d725bc04ab20385e50f8ded7b94bb52824bebb2d8bcc74798d20cf
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format doc
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2
4
+ - 2.1
5
+ - 2.0
6
+ env:
7
+ - secure: n0mxwnyjuXI4mJO4mp++2TnsPJ+XgCF/J1U2L5piE5j3xMhSU+5V0JrA1uFlS0Pemb44M7BjgmF9S4G35BwyAQpctpCYhqy9tFa6+Y6nxEv5hCv2cZz7BSAZM6eb+zq20409hxTHRaQOr1DBeE4R5S2PrmOXRqvYfTRv3LNSLFk=
8
+ notifications:
9
+ slack:
10
+ secure: iTI8zpxXQJqf5e9ix4buLRsGlf9lJRZqA9Fawdqm41msBrC0Zsp31XzBS7ZPiTcdHhImtOC4lccTCW2C8kR6waaG5VOF0U2BwyKaNFKsXMaVk6yrNhYkvJ9YVicuU9hL+JKLyBrSDYQ7+vXcHqaz4H4dYpPThrv6sfq4jBOp+eM=
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in terraforming.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,12 @@
1
+ guard :rspec, cmd: "bundle exec rspec" do
2
+ require "guard/rspec/dsl"
3
+ dsl = Guard::RSpec::Dsl.new(self)
4
+
5
+ rspec = dsl.rspec
6
+ watch(rspec.spec_helper) { rspec.spec_dir }
7
+ watch(rspec.spec_support) { rspec.spec_dir }
8
+ watch(rspec.spec_files)
9
+
10
+ ruby = dsl.ruby
11
+ dsl.watch_spec_files_for(ruby.lib_files)
12
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Daisuke Fujita
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,132 @@
1
+ # Terraforming
2
+
3
+ [![Build Status](https://travis-ci.org/dtan4/terraforming.svg?branch=master)](https://travis-ci.org/dtan4/terraforming)
4
+ [![Code Climate](https://codeclimate.com/github/dtan4/terraforming/badges/gpa.svg)](https://codeclimate.com/github/dtan4/terraforming)
5
+ [![Test Coverage](https://codeclimate.com/github/dtan4/terraforming/badges/coverage.svg)](https://codeclimate.com/github/dtan4/terraforming)
6
+
7
+ Export existing AWS resources to [Terraform](https://terraform.io/) style (tf, tfstate)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'terraforming'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install terraforming
24
+
25
+ ## Prerequisites
26
+
27
+ You need to set AWS credentials.
28
+
29
+ ```bash
30
+ AWS_ACCESS_KEY_ID=
31
+ AWS_SECRET_ACCESS_KEY=
32
+ AWS_REGION=
33
+ ```
34
+
35
+ ## Usage
36
+
37
+ ```bash
38
+ $ terraforming
39
+ Commands:
40
+ terraforming dbpg # Database Parameter Group
41
+ terraforming dbsg # Database Security Group
42
+ terraforming dbsn # Database Subnet Group
43
+ terraforming ec2 # EC2
44
+ terraforming elb # ELB
45
+ terraforming help [COMMAND] # Describe available commands or one specific command
46
+ terraforming rds # RDS
47
+ terraforming s3 # S3
48
+ terraforming sg # SecurityGroup
49
+ terraforming vpc # VPC
50
+ ```
51
+
52
+ Output `.tf` style (e.g. S3 buckets):
53
+
54
+ ```bash
55
+ $ terraforming s3
56
+ ```
57
+
58
+ ```go
59
+ resource "aws_s3_bucket" "hoge" {
60
+ bucket = "hoge"
61
+ acl = "private"
62
+ }
63
+
64
+ resource "aws_s3_bucket" "fuga" {
65
+ bucket = "fuga"
66
+ acl = "private"
67
+ }
68
+ ```
69
+
70
+ To output `.tfstate` style, specify `--tfstate` option (e.g. S3 buckets):
71
+
72
+ ```bash
73
+ $ terraforming s3 --tfstate
74
+ ```
75
+
76
+ ```json
77
+ {
78
+ "version": 1,
79
+ "serial": 1,
80
+ "modules": {
81
+ "path": [
82
+ "root"
83
+ ],
84
+ "outputs": {
85
+ },
86
+ "resources": {
87
+ "aws_s3_bucket.hoge": {
88
+ "type": "aws_s3_bucket",
89
+ "primary": {
90
+ "id": "hoge",
91
+ "attributes": {
92
+ "acl": "private",
93
+ "bucket": "hoge",
94
+ "id": "hoge"
95
+ }
96
+ }
97
+ },
98
+ "aws_s3_bucket.fuga": {
99
+ "type": "aws_s3_bucket",
100
+ "primary": {
101
+ "id": "fuga",
102
+ "attributes": {
103
+ "acl": "private",
104
+ "bucket": "fuga",
105
+ "id": "fuga"
106
+ }
107
+ }
108
+ }
109
+ }
110
+ }
111
+ }
112
+ ```
113
+
114
+ (Probably you have to modify the output to add it to existing `terraforming.tfstate`)
115
+
116
+ ## Development
117
+
118
+ After checking out the repo, run `scripts/setup` to install dependencies. Then, run `scripts/console` for an interactive prompt that will allow you to experiment.
119
+
120
+ 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
121
+
122
+ ## Contributing
123
+
124
+ 1. Fork it ( https://github.com/dtan4/terraforming/fork )
125
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
126
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
127
+ 4. Push to the branch (`git push origin my-new-feature`)
128
+ 5. Create a new Pull Request
129
+
130
+ ## LICENSE
131
+
132
+ [![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENSE)
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
data/bin/terraforming ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "terraforming"
4
+
5
+ Terraforming::CLI.start(ARGV)
@@ -0,0 +1,22 @@
1
+ require "oj"
2
+ require "ox"
3
+
4
+ require "aws-sdk-core"
5
+ require "erb"
6
+ require "json"
7
+ require "thor"
8
+
9
+ require "terraforming/util"
10
+ require "terraforming/version"
11
+
12
+ require "terraforming/cli"
13
+ require "terraforming/resource/db_parameter_group"
14
+ require "terraforming/resource/db_security_group"
15
+ require "terraforming/resource/db_subnet_group"
16
+ require "terraforming/resource/ec2"
17
+ require "terraforming/resource/elb"
18
+ require "terraforming/resource/rds"
19
+ require "terraforming/resource/s3"
20
+ require "terraforming/resource/security_group"
21
+ require "terraforming/resource/subnet"
22
+ require "terraforming/resource/vpc"
@@ -0,0 +1,69 @@
1
+ module Terraforming
2
+ class CLI < Thor
3
+ desc "dbpg", "Database Parameter Group"
4
+ option :tfstate, type: :boolean
5
+ def dbpg
6
+ execute(Terraforming::Resource::DBParameterGroup, options)
7
+ end
8
+
9
+ desc "dbsg", "Database Security Group"
10
+ option :tfstate, type: :boolean
11
+ def dbsg
12
+ execute(Terraforming::Resource::DBSecurityGroup, options)
13
+ end
14
+
15
+ desc "dbsn", "Database Subnet Group"
16
+ option :tfstate, type: :boolean
17
+ def dbsn
18
+ execute(Terraforming::Resource::DBSubnetGroup, options)
19
+ end
20
+
21
+ desc "ec2", "EC2"
22
+ option :tfstate, type: :boolean
23
+ def ec2
24
+ execute(Terraforming::Resource::EC2, options)
25
+ end
26
+
27
+ desc "elb", "ELB"
28
+ option :tfstate, type: :boolean
29
+ def elb
30
+ execute(Terraforming::Resource::ELB, options)
31
+ end
32
+
33
+ desc "rds", "RDS"
34
+ option :tfstate, type: :boolean
35
+ def rds
36
+ execute(Terraforming::Resource::RDS, options)
37
+ end
38
+
39
+ desc "s3", "S3"
40
+ option :tfstate, type: :boolean
41
+ def s3
42
+ execute(Terraforming::Resource::S3, options)
43
+ end
44
+
45
+ desc "sg", "SecurityGroup"
46
+ option :tfstate, type: :boolean
47
+ def sg
48
+ execute(Terraforming::Resource::SecurityGroup, options)
49
+ end
50
+
51
+ desc "sn", "Subnet"
52
+ option :tfstate, type: :boolean
53
+ def sn
54
+ execute(Terraforming::Resource::Subnet, options)
55
+ end
56
+
57
+ desc "vpc", "VPC"
58
+ option :tfstate, type: :boolean
59
+ def vpc
60
+ execute(Terraforming::Resource::VPC, options)
61
+ end
62
+
63
+ private
64
+
65
+ def execute(klass, options)
66
+ puts options[:tfstate] ? klass.tfstate : klass.tf
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,58 @@
1
+ module Terraforming::Resource
2
+ class DBParameterGroup
3
+ include Terraforming::Util
4
+
5
+ def self.tf(client = Aws::RDS::Client.new)
6
+ self.new(client).tf
7
+ end
8
+
9
+ def self.tfstate(client = Aws::RDS::Client.new)
10
+ self.new(client).tfstate
11
+ end
12
+
13
+ def initialize(client)
14
+ @client = client
15
+ end
16
+
17
+ def tf
18
+ apply_template(@client, "tf/db_parameter_group")
19
+ end
20
+
21
+ def tfstate
22
+ resources = db_parameter_groups.inject({}) do |result, parameter_group|
23
+ attributes = {
24
+ "description" => parameter_group.description,
25
+ "family" => parameter_group.db_parameter_group_family,
26
+ "id" => parameter_group.db_parameter_group_name,
27
+ "name" => parameter_group.db_parameter_group_name,
28
+ "parameter.#" => db_parameters_in(parameter_group).length.to_s
29
+ }
30
+ result["aws_db_parameter_group.#{module_name_of(parameter_group)}"] = {
31
+ "type" => "aws_db_parameter_group",
32
+ "primary" => {
33
+ "id" => parameter_group.db_parameter_group_name,
34
+ "attributes" => attributes
35
+ }
36
+ }
37
+
38
+ result
39
+ end
40
+
41
+ generate_tfstate(resources)
42
+ end
43
+
44
+ private
45
+
46
+ def db_parameter_groups
47
+ @client.describe_db_parameter_groups.db_parameter_groups
48
+ end
49
+
50
+ def db_parameters_in(parameter_group)
51
+ @client.describe_db_parameters(db_parameter_group_name: parameter_group.db_parameter_group_name).parameters
52
+ end
53
+
54
+ def module_name_of(parameter_group)
55
+ normalize_module_name(parameter_group.db_parameter_group_name)
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,53 @@
1
+ module Terraforming::Resource
2
+ class DBSecurityGroup
3
+ include Terraforming::Util
4
+
5
+ def self.tf(client = Aws::RDS::Client.new)
6
+ self.new(client).tf
7
+ end
8
+
9
+ def self.tfstate(client = Aws::RDS::Client.new)
10
+ self.new(client).tfstate
11
+ end
12
+
13
+ def initialize(client)
14
+ @client = client
15
+ end
16
+
17
+ def tf
18
+ apply_template(@client, "tf/db_security_group")
19
+ end
20
+
21
+ def tfstate
22
+ resources = db_security_groups.inject({}) do |result, security_group|
23
+ attributes = {
24
+ "db_subnet_group_name" => security_group.db_security_group_name,
25
+ "id" => security_group.db_security_group_name,
26
+ "ingress.#" => (security_group.ec2_security_groups.length + security_group.ip_ranges.length).to_s,
27
+ "name" => security_group.db_security_group_name,
28
+ }
29
+ result["aws_db_security_group.#{module_name_of(security_group)}"] = {
30
+ "type" => "aws_db_security_group",
31
+ "primary" => {
32
+ "id" => security_group.db_security_group_name,
33
+ "attributes" => attributes
34
+ }
35
+ }
36
+
37
+ result
38
+ end
39
+
40
+ generate_tfstate(resources)
41
+ end
42
+
43
+ private
44
+
45
+ def db_security_groups
46
+ @client.describe_db_security_groups.db_security_groups
47
+ end
48
+
49
+ def module_name_of(security_group)
50
+ normalize_module_name(security_group.db_security_group_name)
51
+ end
52
+ end
53
+ end