class TerraformDSL::AWS::TerraformVisitor
Public Class Methods
new()
click to toggle source
# File lib/terraformdsl/aws.rb, line 604 def initialize @buf = [] end
Public Instance Methods
on_AMI(ami) { || ... }
click to toggle source
# File lib/terraformdsl/aws.rb, line 636 def on_AMI(ami) owners = ami.owners.map {|x| "\"#{x}\"" } @buf << <<END data "aws_ami" "#{ami.name}" { most_recent = true owners = [#{owners.join(', ')}] filter { name = "name" values = ["#{ami.pattern}"] } } END yield end
on_AZ(az) { || ... }
click to toggle source
# File lib/terraformdsl/aws.rb, line 632 def on_AZ(az) yield end
on_EC2(ec2) { || ... }
click to toggle source
# File lib/terraformdsl/aws.rb, line 787 def on_EC2(ec2) sg_s = [ec2.security_group].flatten.collect {|sg| "\"#{sg.attr(:id)}\"" }.join(", ") @buf << <<END resource "aws_instance" "#{ec2.name}" { instance_type = "#{ec2.type}" ami = "#{ec2.ami.attr(:image_id)}" subnet_id = "#{ec2.subnet.attr(:id)}" vpc_security_group_ids = [#{sg_s}] key_name = "#{ec2.key_name}" END if ec2.cpu_credit @buf << <<END credit_specification { cpu_credits = "#{ec2.cpu_credit}" } END end @buf << <<END tags { Name = "#{ec2.name}" } } END yield end
on_EIP(eip) { || ... }
click to toggle source
# File lib/terraformdsl/aws.rb, line 816 def on_EIP(eip) @buf << <<END resource "aws_eip" "#{eip.name}" { vpc = true instance = "#{eip.ec2.attr(:id)}" tags { Name = "#{eip.name}" } } END yield end
on_Egress(egress, &blk)
click to toggle source
# File lib/terraformdsl/aws.rb, line 750 def on_Egress(egress, &blk) _on_anygress('egress', egress, &blk) end
on_Global(global) { || ... }
click to toggle source
# File lib/terraformdsl/aws.rb, line 616 def on_Global(global) yield end
on_IAM(route53) { || ... }
click to toggle source
# File lib/terraformdsl/aws.rb, line 1032 def on_IAM(route53) yield end
on_IAM_PolicyAttachment(pa) { || ... }
click to toggle source
# File lib/terraformdsl/aws.rb, line 1051 def on_IAM_PolicyAttachment(pa) groups_str = pa.groups.map {|x| "\"#{x.name}\"" }.join(', ') users_str = pa.users.map {|x| "\"#{x.name}\"" }.join(', ') roles_str = pa.roles.map {|x| "\"#{x.name}\"" }.join(', ') @buf << <<END resource "aws_iam_policy_attachment" "#{pa.name}-policy-attachment" { name = "#{pa.name}-policy-attachment" policy_arn = "arn:aws:iam::aws:policy/service-role/#{pa.name}" groups = [#{groups_str}] users = [#{users_str}] roles = [#{roles_str}] } END yield end
on_IAM_Role(role) { || ... }
click to toggle source
# File lib/terraformdsl/aws.rb, line 1036 def on_IAM_Role(role) json_str = JSON.pretty_generate(role.policy)#.sub(/\n\z/, '') @buf << <<END resource "aws_iam_role" "#{role.name}" { name = "#{role.name}" path = "#{role.path}" assume_role_policy = <<POLICY #{json_str} POLICY } END yield end
on_Infra(infra) { || ... }
click to toggle source
# File lib/terraformdsl/aws.rb, line 612 def on_Infra(infra) yield end
on_Ingress(ingress, &blk)
click to toggle source
# File lib/terraformdsl/aws.rb, line 746 def on_Ingress(ingress, &blk) _on_anygress('ingress', ingress, &blk) end
on_InternetGateway(gw) { || ... }
click to toggle source
# File lib/terraformdsl/aws.rb, line 667 def on_InternetGateway(gw) @buf << <<END resource "aws_internet_gateway" "#{gw.name}" { vpc_id = "#{gw.parent.attr(:id)}" tags { Name = "#{gw.name}" } } END yield end
on_RDS_Instance(instance) { || ... }
click to toggle source
# File lib/terraformdsl/aws.rb, line 904 def on_RDS_Instance(instance) x = instance storage_type = {general: 'gp2', iops: 'io1', magnetic: 'standard'} d = x.backup ? x.backup[:window] : nil backup_window = d ? "#{d[:start]}-#{d[:start].sub(/:00$/, ':30')}" : nil sg = (x.master_instance || x).network[:security_group] \ &.map {|g| "\"#{g.attr(:id)}\"" }&.join(", ") monitoring_role_arn = \ case x.monitoring[:role] when nil ; nil when String ; "${aws_iam_role.#{x.monitoring[:role]}.arn}" when IAM::Role; "#{x.monitoring[:role].attr(:arn)}" else ; raise "#{x.monitoring[:role].inspect}: unexpected value" end if x.monitoring[:role] == RDS::RDS_MONITORING_ROLE_NAME $_rds_monitoring_role_required = true end str = <<END resource "aws_db_instance" "#{x.name}" { allocated_storage = "#{x.storage[:size].to_i}" auto_minor_version_upgrade = "#{x.maintenance[:auto_upgrade]}" availability_zone = "#{x.network[:az].name}" backup_retention_period = "#{x.backup[:days]}" backup_window = "#{backup_window}" copy_tags_to_snapshot = "true" db_subnet_group_name = "#{x.master_instance ? nil : x.network[:subnet_group].name}" #enabled_cloudwatch_logs_exports = "" engine = "#{x.database[:engine]}" engine_version = "#{x.database[:version]}" #final_snapshot_identifier = "" #iam_database_authentication_enabled = "" identifier = "#{x.name}" #identifier_prefix = "" instance_class = "#{x.machine_type}" iops = "#{x.storage[:iops]}" kms_key_id = "#{x.encryption[:kms_key_id]}" license_model = "#{x.database[:license]}" maintenance_window = "#{x.maintenance[:window]}" monitoring_interval = "#{x.monitoring[:interval]}" monitoring_role_arn = "#{monitoring_role_arn}" multi_az = "#{x.network[:multi_az]}" name = "#{x.database[:name]}" option_group_name = "#{x.database[:option_group]&.name}" parameter_group_name = "#{(x.master_instance || x).database[:parameter_group]&.name}" password = "#{x.database[:password]}" port = "#{x.database[:port]}" publicly_accessible = "#{x.network[:public_access]}" replicate_source_db = "#{x.master_instance&.attr(:id)}" #skip_final_snapshot = "" #snapshot_identifier = "" storage_encrypted = "#{x.encryption[:enable]}" storage_type = "#{storage_type[x.storage[:type]]}" #timezone = "UTC" username = "#{x.database[:user]}" vpc_security_group_ids = [#{sg}] #s3_import = "" #tags = { # Name = "#{x.name}" #} } END str = str.gsub(/^.*""\n/, '') @buf << str yield end
on_RDS_OptionGroup(optiongrp) { || ... }
click to toggle source
# File lib/terraformdsl/aws.rb, line 872 def on_RDS_OptionGroup(optiongrp) grp = optiongrp @buf << <<END resource "aws_db_option_group" "#{grp.name}" { name = "#{grp.name}" engine_name = "#{grp.engine}" major_engine_version = "#{grp.version}" END grp.options.each do |name, kvs| @buf << <<END option { option_name = "#{name}" END kvs.each do |k, v| @buf << <<END option_settings { name = "#{k}" value = "#{v}" } END end if kvs @buf << <<END } END end @buf << <<END } END yield end
on_RDS_ParameterGroup(parametergrp) { || ... }
click to toggle source
# File lib/terraformdsl/aws.rb, line 846 def on_RDS_ParameterGroup(parametergrp) grp = parametergrp @buf << <<END resource "aws_db_parameter_group" "#{grp.name}" { name = "#{grp.name}" family = "#{grp.family}" END grp.parameters.each do |k, v| pending_reboot = false if k.end_with?('!') pending_reboot = true k = k.sub(/!$/, '') end @buf << " parameter {\n" @buf << " name = \"#{k}\"\n" @buf << " value = \"#{v}\"\n" @buf << " apply_method = \"pending-reboot\"\n" if pending_reboot @buf << " }\n" end @buf << <<END } END yield end
on_RDS_ReadReplica(instance, &block)
click to toggle source
# File lib/terraformdsl/aws.rb, line 971 def on_RDS_ReadReplica(instance, &block) on_RDS_Instance(instance, &block) end
on_RDS_SubnetGroup(subnetgrp) { || ... }
click to toggle source
# File lib/terraformdsl/aws.rb, line 830 def on_RDS_SubnetGroup(subnetgrp) grp = subnetgrp ids = grp.subnets.map {|x| "\"#{x.attr(:id)}\"" } @buf << <<END resource "aws_db_subnet_group" "#{grp.name}" { name = "#{grp.name}" subnet_ids = [#{ids.join(', ')}] tags { Name = "#{grp.name}" } } END yield end
on_Region(region) { || ... }
click to toggle source
# File lib/terraformdsl/aws.rb, line 620 def on_Region(region) @buf << <<END provider "aws" { #access_key = "${var.access_key}" #secret_key = "${var.secret_key}" region = "#{region.name}" } END yield end
on_Route(route) { || ... }
click to toggle source
# File lib/terraformdsl/aws.rb, line 719 def on_Route(route) @buf << <<END route { cidr_block = "#{route.cidr || '0.0.0.0/0'}" gateway_id = "#{route.gateway.attr(:id)}" } END yield end
on_Route53(route53) { || ... }
click to toggle source
# File lib/terraformdsl/aws.rb, line 975 def on_Route53(route53) yield end
on_Route53_PrivateZone(zone) { || ... }
click to toggle source
# File lib/terraformdsl/aws.rb, line 992 def on_Route53_PrivateZone(zone) @buf << <<END resource "aws_route53_zone" "#{zone.name}" { name = "#{zone.domain}" vpc { vpc_id = "#{zone.vpc.attr(:id)}" } tags { Name = "#{zone.name}" } } END yield end
on_Route53_Record(record) { || ... }
click to toggle source
# File lib/terraformdsl/aws.rb, line 1008 def on_Route53_Record(record) values_s = record.values.flatten.collect {|x| case x when String; "\"#{x}\"" when EIP ; "\"#{x.attr(:public_ip)}\"" when EC2 ; "\"#{x.attr(:private_ip)}\"" else raise TypeError.new("#{x.inspect}: ip address (string, EIP or EC2) expected") end }.join(", ") record_name = record.name.gsub(/[^-\w]/, '_') @buf << <<END resource "aws_route53_record" "#{record.parent.name}-#{record_name}-#{record.type}" { zone_id = "#{record.parent.attr(:zone_id)}" type = "#{record.type}" name = "#{record.name}" ttl = "#{record.opts[:ttl] || 5}" records = [#{values_s}] } END yield end
on_Route53_Zone(zone) { || ... }
click to toggle source
# File lib/terraformdsl/aws.rb, line 979 def on_Route53_Zone(zone) @buf << <<END resource "aws_route53_zone" "#{zone.name}" { name = "#{zone.domain}" tags { Name = "#{zone.name}" } } END yield end
on_RouteTable(route_table) { || ... }
click to toggle source
# File lib/terraformdsl/aws.rb, line 704 def on_RouteTable(route_table) @buf << <<END resource "aws_route_table" "#{route_table.name}" { vpc_id = "#{route_table.parent.attr(:id)}" tags { Name = "#{route_table.name}" } END yield @buf << <<END } END end
on_SecurityGroup(sg) { || ... }
click to toggle source
# File lib/terraformdsl/aws.rb, line 729 def on_SecurityGroup(sg) @buf << <<END resource "aws_security_group" "#{sg.name}" { name = "#{sg.name}" description = "#{sg.desc}" vpc_id = "#{sg.parent.attr(:id)}" tags { Name = "#{sg.name}" } END yield @buf << <<END } END end
on_Subnet(subnet) { || ... }
click to toggle source
# File lib/terraformdsl/aws.rb, line 680 def on_Subnet(subnet) @buf << <<END resource "aws_subnet" "#{subnet.name}" { vpc_id = "#{subnet.parent.attr(:id)}" availability_zone = "#{subnet.az.name}" cidr_block = "#{subnet.cidr}" tags { Name = "#{subnet.name}" } } END if subnet.route_table @buf << <<END resource "aws_route_table_association" "#{subnet.route_table.name}-#{subnet.name}" { route_table_id = "#{subnet.route_table.attr(:id)}" subnet_id = "#{subnet.attr(:id)}" } END end yield end
on_VPC(vpc) { || ... }
click to toggle source
# File lib/terraformdsl/aws.rb, line 652 def on_VPC(vpc) @buf << <<END resource "aws_vpc" "#{vpc.name}" { cidr_block = "#{vpc.cidr}" enable_dns_support = true enable_dns_hostnames = true tags { Name = "#{vpc.name}" } } END yield end
output()
click to toggle source
# File lib/terraformdsl/aws.rb, line 608 def output return @buf.join("") end
Private Instance Methods
_on_anygress(kind, x) { || ... }
click to toggle source
# File lib/terraformdsl/aws.rb, line 754 def _on_anygress(kind, x, &blk) port = x.port || "-1" protocol = x.protocol protocol = "-1" if protocol == :any || protocol.nil? cidrs = [] secgrps = [] flag_self = false [x.destination].flatten.each {|t| case t when nil ; cidrs << "0.0.0.0/0" when :any ; cidrs << "0.0.0.0/0" when :self ; flag_self = true when /^\d+\./ ; cidrs << t when EC2 ; cidrs << "#{t.attr(:private_ip)}/32" when SecurityGroup; secgrps << t.attr(:id) when /^\w[-\w]*$/ ; cidrs << "${aws_instance.#{t}.private_ip}/32" else ; cidrs << t end } cidrs_s = cidrs.map {|s| "\"#{s}\"" }.join(", ") secgrps_s = secgrps.map {|s| "\"#{s}\"" }.join(", ") @buf << " #{kind} {\n" @buf << " from_port = \"#{port}\"\n" @buf << " to_port = \"#{port}\"\n" @buf << " protocol = \"#{protocol}\"\n" @buf << " cidr_blocks = [#{cidrs_s}]\n" if ! cidrs_s.empty? @buf << " security_groups = [#{secgrps_s}]\n" if ! secgrps.empty? @buf << " self = true\n" if flag_self @buf << " }\n" yield end