aws-insight 0.14.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.rubocop.yml +84 -0
- data/.travis.yml +19 -0
- data/CHANGELOG.md +336 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/CONTRIBUTING.md +62 -0
- data/Gemfile +19 -0
- data/Guardfile +12 -0
- data/LICENSE +22 -0
- data/README.md +393 -0
- data/Rakefile +6 -0
- data/bin/terraforming +5 -0
- data/contrib/zsh-completion/_terraforming +65 -0
- data/lib/terraforming.rb +54 -0
- data/lib/terraforming/cli.rb +340 -0
- data/lib/terraforming/resource/alb.rb +98 -0
- data/lib/terraforming/resource/auto_scaling_group.rb +90 -0
- data/lib/terraforming/resource/cloud_watch_alarm.rb +87 -0
- data/lib/terraforming/resource/config.rb +98 -0
- data/lib/terraforming/resource/db_parameter_group.rb +58 -0
- data/lib/terraforming/resource/db_security_group.rb +57 -0
- data/lib/terraforming/resource/db_subnet_group.rb +52 -0
- data/lib/terraforming/resource/ec2.rb +130 -0
- data/lib/terraforming/resource/efs_file_system.rb +60 -0
- data/lib/terraforming/resource/eip.rb +66 -0
- data/lib/terraforming/resource/elasti_cache_cluster.rb +80 -0
- data/lib/terraforming/resource/elasti_cache_subnet_group.rb +56 -0
- data/lib/terraforming/resource/elb.rb +203 -0
- data/lib/terraforming/resource/iam_group.rb +54 -0
- data/lib/terraforming/resource/iam_group_membership.rb +63 -0
- data/lib/terraforming/resource/iam_group_policy.rb +71 -0
- data/lib/terraforming/resource/iam_instance_profile.rb +54 -0
- data/lib/terraforming/resource/iam_policy.rb +63 -0
- data/lib/terraforming/resource/iam_policy_attachment.rb +86 -0
- data/lib/terraforming/resource/iam_role.rb +56 -0
- data/lib/terraforming/resource/iam_role_policy.rb +71 -0
- data/lib/terraforming/resource/iam_user.rb +55 -0
- data/lib/terraforming/resource/iam_user_policy.rb +71 -0
- data/lib/terraforming/resource/internet_gateway.rb +54 -0
- data/lib/terraforming/resource/kms_alias.rb +55 -0
- data/lib/terraforming/resource/kms_key.rb +84 -0
- data/lib/terraforming/resource/launch_configuration.rb +106 -0
- data/lib/terraforming/resource/nat_gateway.rb +57 -0
- data/lib/terraforming/resource/network_acl.rb +83 -0
- data/lib/terraforming/resource/network_interface.rb +68 -0
- data/lib/terraforming/resource/rds.rb +74 -0
- data/lib/terraforming/resource/redshift.rb +67 -0
- data/lib/terraforming/resource/route53_record.rb +106 -0
- data/lib/terraforming/resource/route53_zone.rb +89 -0
- data/lib/terraforming/resource/route_table.rb +122 -0
- data/lib/terraforming/resource/route_table_association.rb +59 -0
- data/lib/terraforming/resource/s3.rb +69 -0
- data/lib/terraforming/resource/security_group.rb +188 -0
- data/lib/terraforming/resource/sns_topic.rb +75 -0
- data/lib/terraforming/resource/sns_topic_subscription.rb +83 -0
- data/lib/terraforming/resource/sqs.rb +70 -0
- data/lib/terraforming/resource/subnet.rb +55 -0
- data/lib/terraforming/resource/vpc.rb +67 -0
- data/lib/terraforming/resource/vpn_gateway.rb +55 -0
- data/lib/terraforming/template/tf/alb.erb +28 -0
- data/lib/terraforming/template/tf/auto_scaling_group.erb +28 -0
- data/lib/terraforming/template/tf/cloud_watch_alarm.erb +33 -0
- data/lib/terraforming/template/tf/db_parameter_group.erb +17 -0
- data/lib/terraforming/template/tf/db_security_group.erb +26 -0
- data/lib/terraforming/template/tf/db_subnet_group.erb +8 -0
- data/lib/terraforming/template/tf/ec2.erb +56 -0
- data/lib/terraforming/template/tf/eip.erb +11 -0
- data/lib/terraforming/template/tf/elasti_cache_cluster.erb +22 -0
- data/lib/terraforming/template/tf/elasti_cache_subnet_group.erb +8 -0
- data/lib/terraforming/template/tf/elastic_filesystem.erb +18 -0
- data/lib/terraforming/template/tf/elb.erb +51 -0
- data/lib/terraforming/template/tf/iam_group.erb +7 -0
- data/lib/terraforming/template/tf/iam_group_membership.erb +8 -0
- data/lib/terraforming/template/tf/iam_group_policy.erb +10 -0
- data/lib/terraforming/template/tf/iam_instance_profile.erb +8 -0
- data/lib/terraforming/template/tf/iam_policy.erb +12 -0
- data/lib/terraforming/template/tf/iam_policy_attachment.erb +10 -0
- data/lib/terraforming/template/tf/iam_role.erb +10 -0
- data/lib/terraforming/template/tf/iam_role_policy.erb +10 -0
- data/lib/terraforming/template/tf/iam_user.erb +7 -0
- data/lib/terraforming/template/tf/iam_user_policy.erb +10 -0
- data/lib/terraforming/template/tf/internet_gateway.erb +14 -0
- data/lib/terraforming/template/tf/kms_alias.erb +7 -0
- data/lib/terraforming/template/tf/kms_key.erb +13 -0
- data/lib/terraforming/template/tf/launch_configuration.erb +68 -0
- data/lib/terraforming/template/tf/nat_gateway.erb +9 -0
- data/lib/terraforming/template/tf/network_acl.erb +43 -0
- data/lib/terraforming/template/tf/network_interface.erb +22 -0
- data/lib/terraforming/template/tf/rds.erb +25 -0
- data/lib/terraforming/template/tf/redshift.erb +23 -0
- data/lib/terraforming/template/tf/route53_record.erb +52 -0
- data/lib/terraforming/template/tf/route53_zone.erb +18 -0
- data/lib/terraforming/template/tf/route_table.erb +34 -0
- data/lib/terraforming/template/tf/route_table_association.erb +9 -0
- data/lib/terraforming/template/tf/s3.erb +12 -0
- data/lib/terraforming/template/tf/security_group.erb +56 -0
- data/lib/terraforming/template/tf/sns_topic.erb +17 -0
- data/lib/terraforming/template/tf/sns_topic_subscription.erb +23 -0
- data/lib/terraforming/template/tf/sqs.erb +21 -0
- data/lib/terraforming/template/tf/subnet.erb +15 -0
- data/lib/terraforming/template/tf/vpc.erb +15 -0
- data/lib/terraforming/template/tf/vpn_gateway.erb +14 -0
- data/lib/terraforming/util.rb +30 -0
- data/lib/terraforming/version.rb +3 -0
- data/script/console +14 -0
- data/script/generate +97 -0
- data/script/setup +7 -0
- data/templates/resource.erb.erb +3 -0
- data/templates/resource.rb.erb +31 -0
- data/templates/resource_spec.rb.erb +39 -0
- data/terraforming.gemspec +31 -0
- metadata +271 -0
@@ -0,0 +1,52 @@
|
|
1
|
+
<% records.each do |r| -%>
|
2
|
+
<%- record, zone_id = r[:record], r[:zone_id] -%>
|
3
|
+
<%- counter = r[:counter] -%>
|
4
|
+
resource "aws_route53_record" "<%= module_name_of(record, counter) %>" {
|
5
|
+
zone_id = "<%= zone_id %>"
|
6
|
+
name = "<%= name_of(record.name.sub(/\\052/, '*')) %>"
|
7
|
+
type = "<%= record.type %>"
|
8
|
+
<%- unless record.resource_records.empty? -%>
|
9
|
+
records = <%= record.resource_records.map(&:value).to_s %>
|
10
|
+
<%- end -%>
|
11
|
+
<%- if record.ttl -%>
|
12
|
+
ttl = "<%= record.ttl %>"
|
13
|
+
<%- end -%>
|
14
|
+
<%- if record.weight -%>
|
15
|
+
weighted_routing_policy {
|
16
|
+
weight = <%= record.weight %>
|
17
|
+
}
|
18
|
+
<%- end -%>
|
19
|
+
<%- if record.region -%>
|
20
|
+
latency_routing_policy {
|
21
|
+
region = "<%= record.region %>"
|
22
|
+
}
|
23
|
+
<%- end -%>
|
24
|
+
<%- if record.geo_location -%>
|
25
|
+
|
26
|
+
geolocation_routing_policy {
|
27
|
+
<%- if record.geo_location.continent_code -%>
|
28
|
+
continent = "<%= record.geo_location.continent_code %>"
|
29
|
+
<%- end -%>
|
30
|
+
<%- if record.geo_location.country_code -%>
|
31
|
+
country = "<%= record.geo_location.country_code %>"
|
32
|
+
<%- end -%>
|
33
|
+
<%- if record.geo_location.subdivision_code -%>
|
34
|
+
subdivision = "<%= record.geo_location.subdivision_code %>"
|
35
|
+
<%- end -%>
|
36
|
+
}
|
37
|
+
<%- end -%>
|
38
|
+
<%- if record.set_identifier -%>
|
39
|
+
set_identifier = "<%= record.set_identifier %>"
|
40
|
+
<%- end -%>
|
41
|
+
|
42
|
+
<%- if record.alias_target -%>
|
43
|
+
alias {
|
44
|
+
name = "<%= name_of(record.alias_target.dns_name)
|
45
|
+
%>"
|
46
|
+
zone_id = "<%= record.alias_target.hosted_zone_id %>"
|
47
|
+
evaluate_target_health = <%= record.alias_target.evaluate_target_health %>
|
48
|
+
}
|
49
|
+
<%- end -%>
|
50
|
+
}
|
51
|
+
|
52
|
+
<% end -%>
|
@@ -0,0 +1,18 @@
|
|
1
|
+
<% hosted_zones.each do |hosted_zone| -%>
|
2
|
+
resource "aws_route53_zone" "<%= module_name_of(hosted_zone) %>" {
|
3
|
+
name = "<%= name_of(hosted_zone) %>"
|
4
|
+
comment = "<%= comment_of(hosted_zone) %>"
|
5
|
+
<%- if private_hosted_zone?(hosted_zone) -%>
|
6
|
+
<%- vpc = vpc_of(hosted_zone) -%>
|
7
|
+
vpc_id = "<%= vpc.vpc_id %>"
|
8
|
+
vpc_region = "<%= vpc.vpc_region %>"
|
9
|
+
<%- end -%>
|
10
|
+
|
11
|
+
tags {
|
12
|
+
<% tags_of(hosted_zone).each do |tag| -%>
|
13
|
+
"<%= tag.key %>" = "<%= tag.value %>"
|
14
|
+
<% end -%>
|
15
|
+
}
|
16
|
+
}
|
17
|
+
|
18
|
+
<% end -%>
|
@@ -0,0 +1,34 @@
|
|
1
|
+
<% route_tables.each do |route_table| -%>
|
2
|
+
resource "aws_route_table" "<%= module_name_of(route_table) %>" {
|
3
|
+
vpc_id = "<%= route_table.vpc_id %>"
|
4
|
+
|
5
|
+
<% routes_of(route_table).each do |route| -%>
|
6
|
+
route {
|
7
|
+
cidr_block = "<%= route.destination_cidr_block %>"
|
8
|
+
<% if route.gateway_id -%>
|
9
|
+
gateway_id = "<%= route.gateway_id %>"
|
10
|
+
<% end -%>
|
11
|
+
<% if route.instance_id -%>
|
12
|
+
instance_id = "<%= route.instance_id %>"
|
13
|
+
<% end -%>
|
14
|
+
<% if route.vpc_peering_connection_id -%>
|
15
|
+
vpc_peering_connection_id = "<%= route.vpc_peering_connection_id %>"
|
16
|
+
<% end -%>
|
17
|
+
<% if route.network_interface_id -%>
|
18
|
+
network_interface_id = "<%= route.network_interface_id %>"
|
19
|
+
<% end -%>
|
20
|
+
}
|
21
|
+
|
22
|
+
<% end -%>
|
23
|
+
<% if route_table.propagating_vgws.any? -%>
|
24
|
+
propagating_vgws = <%= propagaving_vgws_of(route_table).inspect %>
|
25
|
+
|
26
|
+
<% end -%>
|
27
|
+
tags {
|
28
|
+
<% route_table.tags.each do |tag| -%>
|
29
|
+
"<%= tag.key %>" = "<%= tag.value %>"
|
30
|
+
<% end -%>
|
31
|
+
}
|
32
|
+
}
|
33
|
+
|
34
|
+
<% end -%>
|
@@ -0,0 +1,9 @@
|
|
1
|
+
<% route_tables.each do |route_table| -%>
|
2
|
+
<% associations_of(route_table).each do |assoc| -%>
|
3
|
+
resource "aws_route_table_association" "<%= module_name_of(route_table, assoc) %>" {
|
4
|
+
route_table_id = "<%= assoc.route_table_id %>"
|
5
|
+
subnet_id = "<%= assoc.subnet_id %>"
|
6
|
+
}
|
7
|
+
|
8
|
+
<% end -%>
|
9
|
+
<% end -%>
|
@@ -0,0 +1,12 @@
|
|
1
|
+
<% buckets.each do |bucket| -%>
|
2
|
+
resource "aws_s3_bucket" "<%= module_name_of(bucket) %>" {
|
3
|
+
bucket = "<%= bucket.name %>"
|
4
|
+
acl = "private"
|
5
|
+
<%- unless (policy = bucket_policy_of(bucket)).nil? -%>
|
6
|
+
policy = <<POLICY
|
7
|
+
<%= prettify_policy(policy.policy.read) %>
|
8
|
+
POLICY
|
9
|
+
<%- end -%>
|
10
|
+
}
|
11
|
+
|
12
|
+
<% end -%>
|
@@ -0,0 +1,56 @@
|
|
1
|
+
<% security_groups.each do |security_group| -%>
|
2
|
+
resource "aws_security_group" "<%= module_name_of(security_group) %>" {
|
3
|
+
name = "<%= security_group.group_name %>"
|
4
|
+
description = "<%= security_group.description %>"
|
5
|
+
vpc_id = "<%= security_group.vpc_id || '' %>"
|
6
|
+
|
7
|
+
<% dedup_permissions(security_group.ip_permissions, security_group.group_id).each do |permission| -%>
|
8
|
+
<%- security_groups = security_groups_in(permission, security_group).reject { |group_name| group_name == security_group.group_name }.reject { |group_id| group_id == security_group.group_id } -%>
|
9
|
+
ingress {
|
10
|
+
from_port = <%= permission.from_port || 0 %>
|
11
|
+
to_port = <%= permission.to_port || 0 %>
|
12
|
+
protocol = "<%= permission.ip_protocol %>"
|
13
|
+
<%- if permission.prefix_list_ids.length > 0 -%>
|
14
|
+
prefix_list_ids = <%= permission.prefix_list_ids.map { |range| range.prefix_list_id }.inspect %>
|
15
|
+
<%- end -%>
|
16
|
+
<%- if permission.ip_ranges.length > 0 -%>
|
17
|
+
cidr_blocks = <%= permission.ip_ranges.map { |range| range.cidr_ip }.inspect %>
|
18
|
+
<%- end -%>
|
19
|
+
<%- if permission.user_id_group_pairs.length > 0 -%>
|
20
|
+
<%- self_referenced = self_referenced_permission?(security_group, permission) -%>
|
21
|
+
security_groups = <%= security_groups.inspect %>
|
22
|
+
self = <%= self_referenced %>
|
23
|
+
<%- end -%>
|
24
|
+
}
|
25
|
+
|
26
|
+
<% end -%>
|
27
|
+
|
28
|
+
<% dedup_permissions(security_group.ip_permissions_egress, security_group.group_id).each do |permission| -%>
|
29
|
+
egress {
|
30
|
+
from_port = <%= permission.from_port || 0 %>
|
31
|
+
to_port = <%= permission.to_port || 0 %>
|
32
|
+
protocol = "<%= permission.ip_protocol %>"
|
33
|
+
<%- if permission.prefix_list_ids.length > 0 -%>
|
34
|
+
prefix_list_ids = <%= permission.prefix_list_ids.map { |range| range.prefix_list_id }.inspect %>
|
35
|
+
<%- end -%>
|
36
|
+
<%- if permission.ip_ranges.length > 0 -%>
|
37
|
+
cidr_blocks = <%= permission.ip_ranges.map { |range| range.cidr_ip }.inspect %>
|
38
|
+
<%- end -%>
|
39
|
+
<%- if permission.user_id_group_pairs.length > 0 -%>
|
40
|
+
<%- self_referenced = self_referenced_permission?(security_group, permission) -%>
|
41
|
+
security_groups = <%= security_groups_in(permission, security_group).reject { |group_id| group_id == security_group.group_id }.inspect %>
|
42
|
+
self = <%= self_referenced %>
|
43
|
+
<%- end -%>
|
44
|
+
}
|
45
|
+
|
46
|
+
<% end -%>
|
47
|
+
<% if security_group.tags.length > 0 -%>
|
48
|
+
tags {
|
49
|
+
<% security_group.tags.each do |tag| -%>
|
50
|
+
"<%= tag.key %>" = "<%= tag.value %>"
|
51
|
+
<% end -%>
|
52
|
+
}
|
53
|
+
<% end -%>
|
54
|
+
}
|
55
|
+
|
56
|
+
<% end -%>
|
@@ -0,0 +1,17 @@
|
|
1
|
+
<% topics.each do |topic| -%>
|
2
|
+
resource "aws_sns_topic" "<%= module_name_of(topic) %>" {
|
3
|
+
name = "<%= module_name_of(topic) %>"
|
4
|
+
display_name = "<%= topic["DisplayName"] %>"
|
5
|
+
<% if topic.key? "Policy" -%>
|
6
|
+
policy = <<POLICY
|
7
|
+
<%= prettify_policy(topic["Policy"], unescape: true) %>
|
8
|
+
POLICY
|
9
|
+
<% end -%>
|
10
|
+
<% if topic.key? "DeliveryPolicy" -%>
|
11
|
+
delivery_policy = <<POLICY
|
12
|
+
<%= prettify_policy(topic["DeliveryPolicy"], unescape: true) %>
|
13
|
+
POLICY
|
14
|
+
<% end -%>
|
15
|
+
}
|
16
|
+
|
17
|
+
<% end -%>
|
@@ -0,0 +1,23 @@
|
|
1
|
+
<% subscriptions.each do |subscription| -%>
|
2
|
+
<% if subscription["Protocol"].include?("email") -%>
|
3
|
+
/*
|
4
|
+
<% end -%>
|
5
|
+
resource "aws_sns_topic_subscription" "<%= module_name_of(subscription) %>" {
|
6
|
+
topic_arn = "<%= subscription["TopicArn"] %>"
|
7
|
+
protocol = "<%= subscription["Protocol"] %>"
|
8
|
+
endpoint = "<%= subscription["Endpoint"] %>"
|
9
|
+
<% if subscription.key? "RawMessageDelivery" -%>
|
10
|
+
raw_message_delivery = "<%= subscription["RawMessageDelivery"] %>"
|
11
|
+
<% end -%>
|
12
|
+
<% if subscription.key? "ConfirmationTimeoutInMinutes" %>
|
13
|
+
confirmation_timeout_in_minutes = "<%= subscription["ConfirmationTimeoutInMinutes"] %>"
|
14
|
+
<% end -%>
|
15
|
+
<% if subscription.key? "EndpointAutoConfirms" %>
|
16
|
+
endpoint_auto_confirms = "<%= subscription["EndpointAutoConfirms"] %>"
|
17
|
+
<% end -%>
|
18
|
+
}
|
19
|
+
<% if subscription["Protocol"].include?("email") -%>
|
20
|
+
*/
|
21
|
+
<% end -%>
|
22
|
+
|
23
|
+
<% end -%>
|
@@ -0,0 +1,21 @@
|
|
1
|
+
<% queues.each do |queue| -%>
|
2
|
+
resource "aws_sqs_queue" "<%= module_name_of(queue) %>" {
|
3
|
+
name = "<%= module_name_of(queue) %>"
|
4
|
+
visibility_timeout_seconds = <%= queue["VisibilityTimeout"] %>
|
5
|
+
message_retention_seconds = <%= queue["MessageRetentionPeriod"] %>
|
6
|
+
max_message_size = <%= queue["MaximumMessageSize"] %>
|
7
|
+
delay_seconds = <%= queue["DelaySeconds"] %>
|
8
|
+
receive_wait_time_seconds = <%= queue["ReceiveMessageWaitTimeSeconds"] %>
|
9
|
+
<% if queue.key? "Policy" -%>
|
10
|
+
policy = <<POLICY
|
11
|
+
<%= prettify_policy(queue["Policy"], unescape: true) %>
|
12
|
+
POLICY
|
13
|
+
<% end -%>
|
14
|
+
<% if queue.key? "RedrivePolicy" -%>
|
15
|
+
redrive_policy = <<POLICY
|
16
|
+
<%= prettify_policy(queue["RedrivePolicy"], unescape: true) %>
|
17
|
+
POLICY
|
18
|
+
<% end -%>
|
19
|
+
}
|
20
|
+
|
21
|
+
<% end -%>
|
@@ -0,0 +1,15 @@
|
|
1
|
+
<% subnets.each do |subnet| -%>
|
2
|
+
resource "aws_subnet" "<%= module_name_of(subnet) %>" {
|
3
|
+
vpc_id = "<%= subnet.vpc_id %>"
|
4
|
+
cidr_block = "<%= subnet.cidr_block %>"
|
5
|
+
availability_zone = "<%= subnet.availability_zone %>"
|
6
|
+
map_public_ip_on_launch = <%= subnet.map_public_ip_on_launch %>
|
7
|
+
|
8
|
+
tags {
|
9
|
+
<% subnet.tags.each do |tag| -%>
|
10
|
+
"<%= tag.key %>" = "<%= tag.value %>"
|
11
|
+
<% end -%>
|
12
|
+
}
|
13
|
+
}
|
14
|
+
|
15
|
+
<% end -%>
|
@@ -0,0 +1,15 @@
|
|
1
|
+
<% vpcs.each do |vpc| -%>
|
2
|
+
resource "aws_vpc" "<%= module_name_of(vpc) %>" {
|
3
|
+
cidr_block = "<%= vpc.cidr_block %>"
|
4
|
+
enable_dns_hostnames = <%= enable_dns_hostnames?(vpc) %>
|
5
|
+
enable_dns_support = <%= enable_dns_support?(vpc) %>
|
6
|
+
instance_tenancy = "<%= vpc.instance_tenancy %>"
|
7
|
+
|
8
|
+
tags {
|
9
|
+
<% vpc.tags.each do |tag| -%>
|
10
|
+
"<%= tag.key %>" = "<%= tag.value %>"
|
11
|
+
<% end -%>
|
12
|
+
}
|
13
|
+
}
|
14
|
+
|
15
|
+
<% end -%>
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<% vpn_gateways.each do |vpn_gateway| -%>
|
2
|
+
<% unless vpn_gateway.vpc_attachments.empty? -%>
|
3
|
+
resource "aws_vpn_gateway" "<%= module_name_of(vpn_gateway) %>" {
|
4
|
+
vpc_id = "<%= vpn_gateway.vpc_attachments[0].vpc_id %>"
|
5
|
+
availability_zone = "<%= vpn_gateway.availability_zone %>"
|
6
|
+
tags {
|
7
|
+
<% vpn_gateway.tags.each do |tag| -%>
|
8
|
+
"<%= tag.key %>" = "<%= tag.value %>"
|
9
|
+
<% end -%>
|
10
|
+
}
|
11
|
+
}
|
12
|
+
|
13
|
+
<% end -%>
|
14
|
+
<% end -%>
|
@@ -0,0 +1,30 @@
|
|
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
|
6
|
+
|
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
|
11
|
+
|
12
|
+
def normalize_module_name(name)
|
13
|
+
name.gsub(/[^a-zA-Z0-9_-]/, "-")
|
14
|
+
end
|
15
|
+
|
16
|
+
def template_path(template_name)
|
17
|
+
File.join(File.expand_path(File.dirname(__FILE__)), "template", template_name) << ".erb"
|
18
|
+
end
|
19
|
+
|
20
|
+
def prettify_policy(document, breakline: false, unescape: false)
|
21
|
+
json = JSON.pretty_generate(JSON.parse(unescape ? CGI.unescape(document) : document))
|
22
|
+
|
23
|
+
if breakline
|
24
|
+
json[-1] != "\n" ? json << "\n" : json
|
25
|
+
else
|
26
|
+
json.strip
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/script/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "terraforming"
|
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
|
data/script/generate
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "erb"
|
4
|
+
|
5
|
+
USAGE = <<-EOS
|
6
|
+
Usage: script/generate RESOURCE
|
7
|
+
RESOURCE must be snake_case
|
8
|
+
|
9
|
+
Example:
|
10
|
+
script/generate EC2
|
11
|
+
EOS
|
12
|
+
|
13
|
+
def camelize(string)
|
14
|
+
string
|
15
|
+
.sub(/^[a-z\d]*/) { |match| match.capitalize }
|
16
|
+
.gsub(%r{(?:_|(/))([a-z\d]*)}i) { "#{$1}#{$2.capitalize}" }
|
17
|
+
.gsub('/'.freeze, '::'.freeze)
|
18
|
+
end
|
19
|
+
|
20
|
+
def generate_resource_erb(resource)
|
21
|
+
template = File.join("templates", "resource.erb.erb")
|
22
|
+
ERB.new(open(template).read, nil, "-").result(binding)
|
23
|
+
end
|
24
|
+
|
25
|
+
def generate_resource_rb(resource)
|
26
|
+
template = File.expand_path(File.join("templates", "resource.rb.erb"))
|
27
|
+
ERB.new(open(template).read, nil, "-").result(binding)
|
28
|
+
end
|
29
|
+
|
30
|
+
def generate_resource_spec_rb(resource)
|
31
|
+
template = File.join("templates", "resource_spec.rb.erb")
|
32
|
+
ERB.new(open(template).read, nil, "-").result(binding)
|
33
|
+
end
|
34
|
+
|
35
|
+
def humanize(string)
|
36
|
+
string
|
37
|
+
.sub(/\A_+/, ''.freeze)
|
38
|
+
.sub(/_id\z/, ''.freeze)
|
39
|
+
.tr('_'.freeze, ' '.freeze)
|
40
|
+
.gsub(/([a-z\d]*)/i) { |match| match.downcase }
|
41
|
+
.split(" ")
|
42
|
+
.map { |w| w[0].upcase + w[1..-1] }
|
43
|
+
.join(" ")
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
|
48
|
+
if ARGV.length != 1
|
49
|
+
$stderr.puts USAGE
|
50
|
+
exit 1
|
51
|
+
end
|
52
|
+
|
53
|
+
resource = ARGV[0]
|
54
|
+
|
55
|
+
puts "==> Generate #{resource}.rb"
|
56
|
+
resource_rb = generate_resource_rb(resource)
|
57
|
+
File.open(File.join("lib", "terraforming", "resource", "#{resource}.rb"), "w+") { |f| f.write(resource_rb) }
|
58
|
+
|
59
|
+
puts "==> Generate #{resource}_spec.rb"
|
60
|
+
resource_spec_rb = generate_resource_spec_rb(resource)
|
61
|
+
File.open(File.join("spec", "lib", "terraforming", "resource", "#{resource}_spec.rb"), "w+") { |f| f.write(resource_spec_rb) }
|
62
|
+
|
63
|
+
puts "==> Generate #{resource}.erb"
|
64
|
+
resource_erb = generate_resource_erb(resource)
|
65
|
+
File.open(File.join("lib", "terraforming", "template", "tf", "#{resource}.erb"), "w+") { |f| f.write(resource_erb) }
|
66
|
+
|
67
|
+
puts <<-EOS
|
68
|
+
|
69
|
+
Add below code by hand.
|
70
|
+
|
71
|
+
lib/terraforming.rb:
|
72
|
+
|
73
|
+
require "terraforming/resource/#{resource}"
|
74
|
+
|
75
|
+
lib/terraforming/cli.rb:
|
76
|
+
|
77
|
+
module Terraforming
|
78
|
+
class CLI < Thor
|
79
|
+
|
80
|
+
# Subcommand name should be acronym.
|
81
|
+
desc "#{resource}", "#{humanize(resource)}"
|
82
|
+
def #{resource}
|
83
|
+
execute(Terraforming::Resource::#{camelize(resource)}, options)
|
84
|
+
end
|
85
|
+
|
86
|
+
spec/lib/terraforming/cli_spec.rb:
|
87
|
+
|
88
|
+
module Terraforming
|
89
|
+
describe CLI do
|
90
|
+
context "resources" do
|
91
|
+
describe "#{resource}" do
|
92
|
+
let(:klass) { Terraforming::Resource::#{camelize(resource)} }
|
93
|
+
let(:command) { :#{resource} }
|
94
|
+
|
95
|
+
it_behaves_like "CLI examples"
|
96
|
+
end
|
97
|
+
EOS
|