terraforming 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,55 +1,57 @@
1
- module Terraforming::Resource
2
- class Subnet
3
- include Terraforming::Util
1
+ module Terraforming
2
+ module Resource
3
+ class Subnet
4
+ include Terraforming::Util
4
5
 
5
- def self.tf(client = Aws::EC2::Client.new)
6
- self.new(client).tf
7
- end
6
+ def self.tf(client = Aws::EC2::Client.new)
7
+ self.new(client).tf
8
+ end
8
9
 
9
- def self.tfstate(client = Aws::EC2::Client.new)
10
- self.new(client).tfstate
11
- end
10
+ def self.tfstate(client = Aws::EC2::Client.new)
11
+ self.new(client).tfstate
12
+ end
12
13
 
13
- def initialize(client)
14
- @client = client
15
- end
14
+ def initialize(client)
15
+ @client = client
16
+ end
16
17
 
17
- def tf
18
- apply_template(@client, "tf/subnet")
19
- end
18
+ def tf
19
+ apply_template(@client, "tf/subnet")
20
+ end
20
21
 
21
- def tfstate
22
- resources = subnets.inject({}) do |result, subnet|
23
- attributes = {
24
- "availability_zone" => subnet.availability_zone,
25
- "cidr_block" => subnet.cidr_block,
26
- "id" => subnet.subnet_id,
27
- "map_public_ip_on_launch" => subnet.map_public_ip_on_launch.to_s,
28
- "tags.#" => subnet.tags.length.to_s,
29
- "vpc_id" => subnet.vpc_id,
30
- }
31
- result["aws_subnet.#{module_name_of(subnet)}"] = {
32
- "type" => "aws_subnet",
33
- "primary" => {
22
+ def tfstate
23
+ resources = subnets.inject({}) do |result, subnet|
24
+ attributes = {
25
+ "availability_zone" => subnet.availability_zone,
26
+ "cidr_block" => subnet.cidr_block,
34
27
  "id" => subnet.subnet_id,
35
- "attributes" => attributes
28
+ "map_public_ip_on_launch" => subnet.map_public_ip_on_launch.to_s,
29
+ "tags.#" => subnet.tags.length.to_s,
30
+ "vpc_id" => subnet.vpc_id,
31
+ }
32
+ result["aws_subnet.#{module_name_of(subnet)}"] = {
33
+ "type" => "aws_subnet",
34
+ "primary" => {
35
+ "id" => subnet.subnet_id,
36
+ "attributes" => attributes
37
+ }
36
38
  }
37
- }
38
39
 
39
- result
40
- end
40
+ result
41
+ end
41
42
 
42
- generate_tfstate(resources)
43
- end
43
+ generate_tfstate(resources)
44
+ end
44
45
 
45
- private
46
+ private
46
47
 
47
- def subnets
48
- @client.describe_subnets.subnets
49
- end
48
+ def subnets
49
+ @client.describe_subnets.subnets
50
+ end
50
51
 
51
- def module_name_of(subnet)
52
- normalize_module_name(name_from_tag(subnet, subnet.subnet_id))
52
+ def module_name_of(subnet)
53
+ normalize_module_name(name_from_tag(subnet, subnet.subnet_id))
54
+ end
53
55
  end
54
56
  end
55
57
  end
@@ -1,67 +1,69 @@
1
- module Terraforming::Resource
2
- class VPC
3
- include Terraforming::Util
1
+ module Terraforming
2
+ module Resource
3
+ class VPC
4
+ include Terraforming::Util
4
5
 
5
- def self.tf(client = Aws::EC2::Client.new)
6
- self.new(client).tf
7
- end
6
+ def self.tf(client = Aws::EC2::Client.new)
7
+ self.new(client).tf
8
+ end
8
9
 
9
- def self.tfstate(client = Aws::EC2::Client.new)
10
- self.new(client).tfstate
11
- end
10
+ def self.tfstate(client = Aws::EC2::Client.new)
11
+ self.new(client).tfstate
12
+ end
12
13
 
13
- def initialize(client)
14
- @client = client
15
- end
14
+ def initialize(client)
15
+ @client = client
16
+ end
16
17
 
17
- def tf
18
- apply_template(@client, "tf/vpc")
19
- end
18
+ def tf
19
+ apply_template(@client, "tf/vpc")
20
+ end
20
21
 
21
- def tfstate
22
- resources = vpcs.inject({}) do |result, vpc|
23
- attributes = {
24
- "cidr_block" => vpc.cidr_block,
25
- "enable_dns_hostnames" => enable_dns_hostnames?(vpc).to_s,
26
- "enable_dns_support" => enable_dns_support?(vpc).to_s,
27
- "id" => vpc.vpc_id,
28
- "instance_tenancy" => vpc.instance_tenancy,
29
- "tags.#" => vpc.tags.length.to_s,
30
- }
31
- result["aws_vpc.#{module_name_of(vpc)}"] = {
32
- "type" => "aws_vpc",
33
- "primary" => {
22
+ def tfstate
23
+ resources = vpcs.inject({}) do |result, vpc|
24
+ attributes = {
25
+ "cidr_block" => vpc.cidr_block,
26
+ "enable_dns_hostnames" => enable_dns_hostnames?(vpc).to_s,
27
+ "enable_dns_support" => enable_dns_support?(vpc).to_s,
34
28
  "id" => vpc.vpc_id,
35
- "attributes" => attributes
29
+ "instance_tenancy" => vpc.instance_tenancy,
30
+ "tags.#" => vpc.tags.length.to_s,
31
+ }
32
+ result["aws_vpc.#{module_name_of(vpc)}"] = {
33
+ "type" => "aws_vpc",
34
+ "primary" => {
35
+ "id" => vpc.vpc_id,
36
+ "attributes" => attributes
37
+ }
36
38
  }
37
- }
38
39
 
39
- result
40
- end
40
+ result
41
+ end
41
42
 
42
- generate_tfstate(resources)
43
- end
43
+ generate_tfstate(resources)
44
+ end
44
45
 
45
- private
46
+ private
46
47
 
47
- def enable_dns_hostnames?(vpc)
48
- vpc_attribute(vpc, :enableDnsHostnames).enable_dns_hostnames.value
49
- end
48
+ def enable_dns_hostnames?(vpc)
49
+ vpc_attribute(vpc, :enableDnsHostnames).enable_dns_hostnames.value
50
+ end
50
51
 
51
- def enable_dns_support?(vpc)
52
- vpc_attribute(vpc, :enableDnsSupport).enable_dns_support.value
53
- end
52
+ def enable_dns_support?(vpc)
53
+ vpc_attribute(vpc, :enableDnsSupport).enable_dns_support.value
54
+ end
54
55
 
55
- def module_name_of(vpc)
56
- normalize_module_name(name_from_tag(vpc, vpc.vpc_id))
57
- end
56
+ def module_name_of(vpc)
57
+ normalize_module_name(name_from_tag(vpc, vpc.vpc_id))
58
+ end
58
59
 
59
- def vpcs
60
- @client.describe_vpcs.vpcs
61
- end
60
+ def vpcs
61
+ @client.describe_vpcs.vpcs
62
+ end
62
63
 
63
- def vpc_attribute(vpc, attribute)
64
- @client.describe_vpc_attribute(vpc_id: vpc.vpc_id, attribute: attribute)
64
+ def vpc_attribute(vpc, attribute)
65
+ @client.describe_vpc_attribute(vpc_id: vpc.vpc_id, attribute: attribute)
66
+ end
65
67
  end
66
68
  end
67
69
  end
@@ -0,0 +1,34 @@
1
+ <% network_acls.each do |network_acl| -%>
2
+ resource "aws_network_acl" "<%= module_name_of(network_acl) %>" {
3
+ vpc_id = "<%= network_acl.vpc_id %>"
4
+
5
+ <% ingresses_of(network_acl).each do |ingress| -%>
6
+ ingress {
7
+ from_port = <%= from_port_of(ingress) %>
8
+ to_port = <%= to_port_of(ingress) %>
9
+ rule_no = <%= ingress.rule_number %>
10
+ action = "<%= ingress.rule_action %>"
11
+ protocol = "<%= ingress.protocol %>"
12
+ cidr_block = "<%= ingress.cidr_block %>"
13
+ }
14
+
15
+ <% end -%>
16
+ <% egresses_of(network_acl).each do |egress| -%>
17
+ egress {
18
+ from_port = <%= from_port_of(egress) %>
19
+ to_port = <%= to_port_of(egress) %>
20
+ rule_no = <%= egress.rule_number %>
21
+ action = "<%= egress.rule_action %>"
22
+ protocol = "<%= egress.protocol %>"
23
+ cidr_block = "<%= egress.cidr_block %>"
24
+ }
25
+
26
+ <% end -%>
27
+ tags {
28
+ <% network_acl.tags.each do |tag| -%>
29
+ <%= tag.key %> = "<%= tag.value %>"
30
+ <% end -%>
31
+ }
32
+ }
33
+
34
+ <% end -%>
@@ -1,34 +1,36 @@
1
- module Terraforming::Util
2
- def apply_template(client, erb)
3
- ERB.new(open(template_path(erb)).read, nil, "-").result(binding)
4
- end
1
+ module Terraforming
2
+ module Util
3
+ def apply_template(client, erb)
4
+ ERB.new(open(template_path(erb)).read, nil, "-").result(binding)
5
+ end
5
6
 
6
- def name_from_tag(resource, default_name)
7
- name_tag = resource.tags.find { |tag| tag.key == "Name" }
8
- name_tag ? name_tag.value : default_name
9
- end
7
+ def name_from_tag(resource, default_name)
8
+ name_tag = resource.tags.find { |tag| tag.key == "Name" }
9
+ name_tag ? name_tag.value : default_name
10
+ end
10
11
 
11
- def normalize_module_name(name)
12
- name.gsub(/[^a-zA-Z0-9_-]/, "-")
13
- end
12
+ def normalize_module_name(name)
13
+ name.gsub(/[^a-zA-Z0-9_-]/, "-")
14
+ end
14
15
 
15
- def template_path(template_name)
16
- File.join(File.expand_path(File.dirname(__FILE__)), "template", template_name) << ".erb"
17
- end
16
+ def template_path(template_name)
17
+ File.join(File.expand_path(File.dirname(__FILE__)), "template", template_name) << ".erb"
18
+ end
18
19
 
19
- def generate_tfstate(resources)
20
- tfstate = {
21
- "version" => 1,
22
- "serial" => 1,
23
- "modules" => {
24
- "path" => [
25
- "root"
26
- ],
27
- "outputs" => {},
28
- "resources" => resources
20
+ def generate_tfstate(resources)
21
+ tfstate = {
22
+ "version" => 1,
23
+ "serial" => 1,
24
+ "modules" => {
25
+ "path" => [
26
+ "root"
27
+ ],
28
+ "outputs" => {},
29
+ "resources" => resources
30
+ }
29
31
  }
30
- }
31
32
 
32
- JSON.pretty_generate(tfstate)
33
+ JSON.pretty_generate(tfstate)
34
+ end
33
35
  end
34
36
  end
@@ -1,3 +1,3 @@
1
1
  module Terraforming
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/terraforming.gemspec CHANGED
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["Daisuke Fujita"]
10
10
  spec.email = ["dtanshi45@gmail.com"]
11
11
 
12
- spec.summary = %q{Export existing AWS resources to Terraform style (tf, tfstate)}
13
- spec.description = %q{Export existing AWS resources to Terraform style (tf, tfstate)}
12
+ spec.summary = %q{Import existing AWS resources into Terraform style (tf, tfstate)}
13
+ spec.description = %q{Import existing AWS resources into Terraform style (tf, tfstate)}
14
14
  spec.homepage = "https://github.com/dtan4/terraforming"
15
15
  spec.license = "MIT"
16
16
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: terraforming
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daisuke Fujita
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-23 00:00:00.000000000 Z
11
+ date: 2015-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk
@@ -170,7 +170,7 @@ dependencies:
170
170
  - - ">="
171
171
  - !ruby/object:Gem::Version
172
172
  version: '0'
173
- description: Export existing AWS resources to Terraform style (tf, tfstate)
173
+ description: Import existing AWS resources into Terraform style (tf, tfstate)
174
174
  email:
175
175
  - dtanshi45@gmail.com
176
176
  executables:
@@ -195,6 +195,7 @@ files:
195
195
  - lib/terraforming/resource/db_subnet_group.rb
196
196
  - lib/terraforming/resource/ec2.rb
197
197
  - lib/terraforming/resource/elb.rb
198
+ - lib/terraforming/resource/network_acl.rb
198
199
  - lib/terraforming/resource/rds.rb
199
200
  - lib/terraforming/resource/s3.rb
200
201
  - lib/terraforming/resource/security_group.rb
@@ -205,6 +206,7 @@ files:
205
206
  - lib/terraforming/template/tf/db_subnet_group.erb
206
207
  - lib/terraforming/template/tf/ec2.erb
207
208
  - lib/terraforming/template/tf/elb.erb
209
+ - lib/terraforming/template/tf/network_acl.erb
208
210
  - lib/terraforming/template/tf/rds.erb
209
211
  - lib/terraforming/template/tf/s3.erb
210
212
  - lib/terraforming/template/tf/security_group.erb
@@ -238,5 +240,5 @@ rubyforge_project:
238
240
  rubygems_version: 2.4.5
239
241
  signing_key:
240
242
  specification_version: 4
241
- summary: Export existing AWS resources to Terraform style (tf, tfstate)
243
+ summary: Import existing AWS resources into Terraform style (tf, tfstate)
242
244
  test_files: []