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
@@ -0,0 +1,52 @@
1
+ module Terraforming::Resource
2
+ class DBSubnetGroup
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_subnet_group")
19
+ end
20
+
21
+ def tfstate
22
+ resources = db_subnet_groups.inject({}) do |result, subnet_group|
23
+ attributes = {
24
+ "description" => subnet_group.db_subnet_group_description,
25
+ "name" => subnet_group.db_subnet_group_name,
26
+ "subnet_ids.#" => subnet_group.subnets.length.to_s
27
+ }
28
+ result["aws_db_subnet_group.#{module_name_of(subnet_group)}"] = {
29
+ "type" => "aws_db_subnet_group",
30
+ "primary" => {
31
+ "id" => subnet_group.db_subnet_group_name,
32
+ "attributes" => attributes
33
+ }
34
+ }
35
+
36
+ result
37
+ end
38
+
39
+ generate_tfstate(resources)
40
+ end
41
+
42
+ private
43
+
44
+ def db_subnet_groups
45
+ @client.describe_db_subnet_groups.db_subnet_groups
46
+ end
47
+
48
+ def module_name_of(subnet_group)
49
+ normalize_module_name(subnet_group.db_subnet_group_name)
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,69 @@
1
+ module Terraforming::Resource
2
+ class EC2
3
+ include Terraforming::Util
4
+
5
+ def self.tf(client = Aws::EC2::Client.new)
6
+ self.new(client).tf
7
+ end
8
+
9
+ def self.tfstate(client = Aws::EC2::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/ec2")
19
+ end
20
+
21
+ def tfstate
22
+ resources = instances.inject({}) do |result, instance|
23
+ attributes = {
24
+ "ami"=> instance.image_id,
25
+ "associate_public_ip_address"=> "true",
26
+ "availability_zone"=> instance.placement.availability_zone,
27
+ "ebs_block_device.#"=> instance.block_device_mappings.length.to_s,
28
+ "ebs_optimized"=> instance.ebs_optimized.to_s,
29
+ "ephemeral_block_device.#"=> "0",
30
+ "id"=> instance.instance_id,
31
+ "instance_type"=> instance.instance_type,
32
+ "private_dns"=> instance.private_dns_name,
33
+ "private_ip"=> instance.private_ip_address,
34
+ "public_dns"=> instance.public_dns_name,
35
+ "public_ip"=> instance.public_ip_address,
36
+ "root_block_device.#"=> instance.root_device_name ? "1" : "0",
37
+ "security_groups.#"=> instance.security_groups.length.to_s,
38
+ "source_dest_check"=> instance.source_dest_check.to_s,
39
+ "subnet_id"=> instance.subnet_id,
40
+ "tenancy"=> instance.placement.tenancy
41
+ }
42
+ result["aws_instance.#{module_name_of(instance)}"] = {
43
+ "type" => "aws_instance",
44
+ "primary" => {
45
+ "id" => instance.instance_id,
46
+ "attributes" => attributes,
47
+ "meta" => {
48
+ "schema_version" => "1"
49
+ }
50
+ }
51
+ }
52
+
53
+ result
54
+ end
55
+
56
+ generate_tfstate(resources)
57
+ end
58
+
59
+ private
60
+
61
+ def instances
62
+ @client.describe_instances.reservations.map(&:instances).flatten
63
+ end
64
+
65
+ def module_name_of(instance)
66
+ normalize_module_name(name_from_tag(instance, instance.instance_id))
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,56 @@
1
+ module Terraforming::Resource
2
+ class ELB
3
+ include Terraforming::Util
4
+
5
+ def self.tf(client = Aws::ElasticLoadBalancing::Client.new)
6
+ self.new(client).tf
7
+ end
8
+
9
+ def self.tfstate(client = Aws::ElasticLoadBalancing::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/elb")
19
+ end
20
+
21
+ def tfstate
22
+ resources = load_balancers.inject({}) do |result, load_balancer|
23
+ attributes = {
24
+ "availability_zones.#" => load_balancer.availability_zones.length.to_s,
25
+ "dns_name" => load_balancer.dns_name,
26
+ "health_check.#" => "1",
27
+ "id" => load_balancer.load_balancer_name,
28
+ "instances.#" => load_balancer.instances.length.to_s,
29
+ "listener.#" => load_balancer.listener_descriptions.length.to_s,
30
+ "name" => load_balancer.load_balancer_name,
31
+ "security_groups.#" => load_balancer.security_groups.length.to_s,
32
+ "subnets.#" => load_balancer.subnets.length.to_s,
33
+ }
34
+ result["aws_elb.#{module_name_of(load_balancer)}"] = {
35
+ "type" => "aws_elb",
36
+ "primary" => {
37
+ "id" => load_balancer.load_balancer_name,
38
+ "attributes" => attributes
39
+ }
40
+ }
41
+
42
+ result
43
+ end
44
+
45
+ generate_tfstate(resources)
46
+ end
47
+
48
+ def load_balancers
49
+ @client.describe_load_balancers.load_balancer_descriptions
50
+ end
51
+
52
+ def module_name_of(load_balancer)
53
+ normalize_module_name(load_balancer.load_balancer_name)
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,74 @@
1
+ module Terraforming::Resource
2
+ class RDS
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/rds")
19
+ end
20
+
21
+ def tfstate
22
+ resources = db_instances.inject({}) do |result, instance|
23
+ attributes = {
24
+ "address" => instance.endpoint.address,
25
+ "allocated_storage" => instance.allocated_storage.to_s,
26
+ "availability_zone" => instance.availability_zone,
27
+ "backup_retention_period" => instance.backup_retention_period.to_s,
28
+ "backup_window" => instance.preferred_backup_window,
29
+ "db_subnet_group_name" => instance.db_subnet_group ? instance.db_subnet_group.db_subnet_group_name : "",
30
+ "endpoint" => instance.endpoint.address,
31
+ "engine" => instance.engine,
32
+ "engine_version" => instance.engine_version,
33
+ "final_snapshot_identifier" => "#{instance.db_instance_identifier}-final",
34
+ "id" => instance.db_instance_identifier,
35
+ "identifier" => instance.db_instance_identifier,
36
+ "instance_class" => instance.db_instance_class,
37
+ "maintenance_window" => instance.preferred_maintenance_window,
38
+ "multi_az" => instance.multi_az.to_s,
39
+ "name" => instance.db_name,
40
+ "parameter_group_name" => instance.db_parameter_groups[0].db_parameter_group_name,
41
+ "password" => "xxxxxxxx",
42
+ "port" => instance.endpoint.port.to_s,
43
+ "publicly_accessible" => instance.publicly_accessible.to_s,
44
+ "security_group_names.#" => instance.db_security_groups.length.to_s,
45
+ "status" => instance.db_instance_status,
46
+ "storage_type" => instance.storage_type,
47
+ "username" => instance.master_username,
48
+ "vpc_security_group_ids.#" => instance.vpc_security_groups.length.to_s,
49
+ }
50
+ result["aws_db_instance.#{module_name_of(instance)}"] = {
51
+ "type" => "aws_db_instance",
52
+ "primary" => {
53
+ "id" => instance.db_instance_identifier,
54
+ "attributes" => attributes
55
+ }
56
+ }
57
+
58
+ result
59
+ end
60
+
61
+ generate_tfstate(resources)
62
+ end
63
+
64
+ private
65
+
66
+ def db_instances
67
+ @client.describe_db_instances.db_instances
68
+ end
69
+
70
+ def module_name_of(instance)
71
+ normalize_module_name(instance.db_instance_identifier)
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,51 @@
1
+ module Terraforming::Resource
2
+ class S3
3
+ include Terraforming::Util
4
+
5
+ def self.tf(client = Aws::S3::Client.new)
6
+ self.new(client).tf
7
+ end
8
+
9
+ def self.tfstate(client = Aws::S3::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/s3")
19
+ end
20
+
21
+ def tfstate
22
+ resources = buckets.inject({}) do |result, bucket|
23
+ result["aws_s3_bucket.#{module_name_of(bucket)}"] = {
24
+ "type" => "aws_s3_bucket",
25
+ "primary" => {
26
+ "id" => bucket.name,
27
+ "attributes" => {
28
+ "acl" => "private",
29
+ "bucket" => bucket.name,
30
+ "id" => bucket.name
31
+ }
32
+ }
33
+ }
34
+
35
+ result
36
+ end
37
+
38
+ generate_tfstate(resources)
39
+ end
40
+
41
+ private
42
+
43
+ def buckets
44
+ @client.list_buckets.buckets
45
+ end
46
+
47
+ def module_name_of(bucket)
48
+ normalize_module_name(bucket.name)
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,56 @@
1
+ module Terraforming::Resource
2
+ class SecurityGroup
3
+ include Terraforming::Util
4
+
5
+ def self.tf(client = Aws::EC2::Client.new)
6
+ self.new(client).tf
7
+ end
8
+
9
+ def self.tfstate(client = Aws::EC2::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/security_group")
19
+ end
20
+
21
+ def tfstate
22
+ resources = security_groups.inject({}) do |result, security_group|
23
+ attributes = {
24
+ "description" => security_group.description,
25
+ "egress.#" => security_group.ip_permissions_egress.length.to_s,
26
+ "id" => security_group.group_id,
27
+ "ingress.#" => security_group.ip_permissions.length.to_s,
28
+ "name" => security_group.group_name,
29
+ "owner_id" => security_group.owner_id,
30
+ "vpc_id" => security_group.vpc_id || "",
31
+ }
32
+ result["aws_security_group.#{module_name_of(security_group)}"] = {
33
+ "type" => "aws_security_group",
34
+ "primary" => {
35
+ "id" => security_group.group_id,
36
+ "attributes" => attributes
37
+ }
38
+ }
39
+
40
+ result
41
+ end
42
+
43
+ generate_tfstate(resources)
44
+ end
45
+
46
+ private
47
+
48
+ def module_name_of(security_group)
49
+ normalize_module_name(security_group.group_name)
50
+ end
51
+
52
+ def security_groups
53
+ @client.describe_security_groups.security_groups
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,55 @@
1
+ module Terraforming::Resource
2
+ class Subnet
3
+ include Terraforming::Util
4
+
5
+ def self.tf(client = Aws::EC2::Client.new)
6
+ self.new(client).tf
7
+ end
8
+
9
+ def self.tfstate(client = Aws::EC2::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/subnet")
19
+ end
20
+
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" => {
34
+ "id" => subnet.subnet_id,
35
+ "attributes" => attributes
36
+ }
37
+ }
38
+
39
+ result
40
+ end
41
+
42
+ generate_tfstate(resources)
43
+ end
44
+
45
+ private
46
+
47
+ def subnets
48
+ @client.describe_subnets.subnets
49
+ end
50
+
51
+ def module_name_of(subnet)
52
+ normalize_module_name(name_from_tag(subnet, subnet.subnet_id))
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,67 @@
1
+ module Terraforming::Resource
2
+ class VPC
3
+ include Terraforming::Util
4
+
5
+ def self.tf(client = Aws::EC2::Client.new)
6
+ self.new(client).tf
7
+ end
8
+
9
+ def self.tfstate(client = Aws::EC2::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/vpc")
19
+ end
20
+
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" => {
34
+ "id" => vpc.vpc_id,
35
+ "attributes" => attributes
36
+ }
37
+ }
38
+
39
+ result
40
+ end
41
+
42
+ generate_tfstate(resources)
43
+ end
44
+
45
+ private
46
+
47
+ def enable_dns_hostnames?(vpc)
48
+ vpc_attribute(vpc, :enableDnsHostnames).enable_dns_hostnames.value
49
+ end
50
+
51
+ def enable_dns_support?(vpc)
52
+ vpc_attribute(vpc, :enableDnsSupport).enable_dns_support.value
53
+ end
54
+
55
+ def module_name_of(vpc)
56
+ normalize_module_name(name_from_tag(vpc, vpc.vpc_id))
57
+ end
58
+
59
+ def vpcs
60
+ @client.describe_vpcs.vpcs
61
+ end
62
+
63
+ def vpc_attribute(vpc, attribute)
64
+ @client.describe_vpc_attribute(vpc_id: vpc.vpc_id, attribute: attribute)
65
+ end
66
+ end
67
+ end