Class: TerraformDSL::AWS::TerraformVisitor

Inherits:
Visitor
  • Object
show all
Defined in:
lib/terraformdsl/aws.rb

Instance Method Summary collapse

Methods inherited from Visitor

#visit

Constructor Details

#initializeTerraformVisitor

Returns a new instance of TerraformVisitor.



605
606
607
# File 'lib/terraformdsl/aws.rb', line 605

def initialize
  @buf = []
end

Instance Method Details

#on_AMI(ami) ⇒ Object



637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
# File 'lib/terraformdsl/aws.rb', line 637

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) ⇒ Object



633
634
635
# File 'lib/terraformdsl/aws.rb', line 633

def on_AZ(az)
  yield
end

#on_EC2(ec2) ⇒ Object



788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
# File 'lib/terraformdsl/aws.rb', line 788

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_Egress(egress, &blk) ⇒ Object



751
752
753
# File 'lib/terraformdsl/aws.rb', line 751

def on_Egress(egress, &blk)
  _on_anygress('egress', egress, &blk)
end

#on_EIP(eip) ⇒ Object



817
818
819
820
821
822
823
824
825
826
827
828
829
# File 'lib/terraformdsl/aws.rb', line 817

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_Global(global) ⇒ Object



617
618
619
# File 'lib/terraformdsl/aws.rb', line 617

def on_Global(global)
  yield
end

#on_IAM(route53) ⇒ Object



1033
1034
1035
# File 'lib/terraformdsl/aws.rb', line 1033

def on_IAM(route53)
  yield
end

#on_IAM_PolicyAttachment(pa) ⇒ Object



1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
# File 'lib/terraformdsl/aws.rb', line 1052

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) ⇒ Object



1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
# File 'lib/terraformdsl/aws.rb', line 1037

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) ⇒ Object



613
614
615
# File 'lib/terraformdsl/aws.rb', line 613

def on_Infra(infra)
  yield
end

#on_Ingress(ingress, &blk) ⇒ Object



747
748
749
# File 'lib/terraformdsl/aws.rb', line 747

def on_Ingress(ingress, &blk)
  _on_anygress('ingress', ingress, &blk)
end

#on_InternetGateway(gw) ⇒ Object



668
669
670
671
672
673
674
675
676
677
678
679
# File 'lib/terraformdsl/aws.rb', line 668

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) ⇒ Object



905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
# File 'lib/terraformdsl/aws.rb', line 905

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) ⇒ Object



873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
# File 'lib/terraformdsl/aws.rb', line 873

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) ⇒ Object



847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
# File 'lib/terraformdsl/aws.rb', line 847

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) ⇒ Object



972
973
974
# File 'lib/terraformdsl/aws.rb', line 972

def on_RDS_ReadReplica(instance, &block)
  on_RDS_Instance(instance, &block)
end

#on_RDS_SubnetGroup(subnetgrp) ⇒ Object



831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
# File 'lib/terraformdsl/aws.rb', line 831

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) ⇒ Object



621
622
623
624
625
626
627
628
629
630
631
# File 'lib/terraformdsl/aws.rb', line 621

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) ⇒ Object



720
721
722
723
724
725
726
727
728
# File 'lib/terraformdsl/aws.rb', line 720

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) ⇒ Object



976
977
978
# File 'lib/terraformdsl/aws.rb', line 976

def on_Route53(route53)
  yield
end

#on_Route53_PrivateZone(zone) ⇒ Object



993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
# File 'lib/terraformdsl/aws.rb', line 993

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) ⇒ Object



1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
# File 'lib/terraformdsl/aws.rb', line 1009

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) ⇒ Object



980
981
982
983
984
985
986
987
988
989
990
991
# File 'lib/terraformdsl/aws.rb', line 980

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) ⇒ Object



705
706
707
708
709
710
711
712
713
714
715
716
717
718
# File 'lib/terraformdsl/aws.rb', line 705

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) ⇒ Object



730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
# File 'lib/terraformdsl/aws.rb', line 730

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) ⇒ Object



681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
# File 'lib/terraformdsl/aws.rb', line 681

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) ⇒ Object



653
654
655
656
657
658
659
660
661
662
663
664
665
666
# File 'lib/terraformdsl/aws.rb', line 653

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

#outputObject



609
610
611
# File 'lib/terraformdsl/aws.rb', line 609

def output
  return @buf.join("")
end