Exemple #1
0
func (_ *FirewallRule) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *FirewallRule) error {
	g, err := e.mapToGCE(t.Project)
	if err != nil {
		return err
	}

	var allowed []*terraformAllow
	if g.Allowed != nil {
		for _, ga := range g.Allowed {
			a := &terraformAllow{
				Protocol: ga.IPProtocol,
				Ports:    ga.Ports,
			}

			allowed = append(allowed, a)
		}
	}
	tf := &terraformFirewall{
		Name:         g.Name,
		SourceRanges: g.SourceRanges,
		TargetTags:   g.TargetTags,
		Allowed:      allowed,
	}

	// TODO: This doesn't seem right, but it looks like a TF problem
	tf.Network = e.Network.TerraformName()

	return t.RenderResource("google_compute_firewall", *e.Name, tf)
}
Exemple #2
0
func (_ *AutoscalingGroup) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *AutoscalingGroup) error {
	tf := &terraformAutoscalingGroup{
		Name:                    e.Name,
		MinSize:                 e.MinSize,
		MaxSize:                 e.MaxSize,
		LaunchConfigurationName: e.LaunchConfiguration.TerraformLink(),
	}

	for _, s := range e.Subnets {
		tf.VPCZoneIdentifier = append(tf.VPCZoneIdentifier, s.TerraformLink())
	}

	tags := e.buildTags(t.Cloud)
	// Make sure we output in a stable order
	var tagKeys []string
	for k := range tags {
		tagKeys = append(tagKeys, k)
	}
	sort.Strings(tagKeys)
	for _, k := range tagKeys {
		v := tags[k]
		tf.Tags = append(tf.Tags, &terraformASGTag{
			Key:               fi.String(k),
			Value:             fi.String(v),
			PropagateAtLaunch: fi.Bool(true),
		})
	}

	return t.RenderResource("aws_autoscaling_group", *e.Name, tf)
}
Exemple #3
0
func (_ *DNSZone) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *DNSZone) error {
	cloud := t.Cloud.(*awsup.AWSCloud)

	// As a special case, we check for an existing zone
	// It is really painful to have TF create a new one...
	// (you have to reconfigure the DNS NS records)
	glog.Infof("Check for existing route53 zone to re-use with name %q", *e.Name)
	z, err := e.findExisting(cloud)
	if err != nil {
		return err
	}

	if z != nil {
		glog.Infof("Existing zone %q found; will configure TF to reuse", aws.StringValue(z.Name))

		e.ID = z.Id
		return nil
	}

	tf := &terraformRoute53Zone{
		Name: e.Name,
		//Tags:               cloud.BuildTags(e.Name, nil),
	}

	return t.RenderResource("aws_route53_zone", *e.Name, tf)
}
func (_ *RouteTableAssociation) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *RouteTableAssociation) error {
	tf := &terraformRouteTableAssociation{
		SubnetID:     e.Subnet.TerraformLink(),
		RouteTableID: e.RouteTable.TerraformLink(),
	}

	return t.RenderResource("aws_route_table_association", *e.Name, tf)
}
func (_ *VPCDHCPOptionsAssociation) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *VPCDHCPOptionsAssociation) error {
	tf := &terraformVPCDHCPOptionsAssociation{
		VPCID:         e.VPC.TerraformLink(),
		DHCPOptionsID: e.DHCPOptions.TerraformLink(),
	}

	return t.RenderResource("aws_vpc_dhcp_options_association", *e.VPC.Name, tf)
}
func (_ *IAMInstanceProfileRole) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *IAMInstanceProfileRole) error {
	tf := &terraformIAMInstanceProfile{
		Name:  e.InstanceProfile.Name,
		Roles: []*terraform.Literal{e.Role.TerraformLink()},
	}

	return t.RenderResource("aws_iam_instance_profile", *e.InstanceProfile.Name, tf)
}
Exemple #7
0
func (_ *Subnet) RenderSubnet(t *terraform.TerraformTarget, a, e, changes *Subnet) error {
	tf := &terraformSubnet{
		Name:    e.Name,
		Network: e.Network.TerraformName(),
		Region:  e.Region,
		CIDR:    e.CIDR,
	}
	return t.RenderResource("google_compute_subnetwork", *e.Name, tf)
}
Exemple #8
0
func (_ *PersistentDisk) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *PersistentDisk) error {
	tf := &terraformDisk{
		Name:       e.Name,
		VolumeType: e.VolumeType,
		SizeGB:     e.SizeGB,
		Zone:       e.Zone,
	}
	return t.RenderResource("google_compute_disk", *e.Name, tf)
}
Exemple #9
0
func (_ *Network) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *Network) error {
	tf := &terraformNetwork{
		Name: e.Name,
		CIDR: e.CIDR,
		//AutoCreateSubnetworks: false,
	}

	return t.RenderResource("google_compute_network", *e.Name, tf)
}
Exemple #10
0
func (_ *RouteTable) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *RouteTable) error {
	cloud := t.Cloud.(*awsup.AWSCloud)

	tf := &terraformRouteTable{
		VPCID: e.VPC.TerraformLink(),
		Tags:  cloud.BuildTags(e.Name),
	}

	return t.RenderResource("aws_route_table", *e.Name, tf)
}
Exemple #11
0
func (_ *EBSVolume) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *EBSVolume) error {
	tf := &terraformVolume{
		AvailabilityZone: e.AvailabilityZone,
		Size:             e.SizeGB,
		Type:             e.VolumeType,
		Tags:             e.Tags,
	}

	return t.RenderResource("aws_ebs_volume", *e.Name, tf)
}
Exemple #12
0
func (_ *ManagedInstanceGroup) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *ManagedInstanceGroup) error {
	tf := &terraformInstanceGroupManager{
		Name:             e.Name,
		Zone:             e.Zone,
		BaseInstanceName: e.BaseInstanceName,
		InstanceTemplate: e.InstanceTemplate.TerraformLink(),
		TargetSize:       e.TargetSize,
	}

	return t.RenderResource("google_compute_instance_group_manager", *e.Name, tf)
}
Exemple #13
0
func (_ *SecurityGroup) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *SecurityGroup) error {
	cloud := t.Cloud.(*awsup.AWSCloud)

	tf := &terraformSecurityGroup{
		Name:        e.Name,
		VPCID:       e.VPC.TerraformLink(),
		Description: e.Description,
		Tags:        cloud.BuildTags(e.Name),
	}

	return t.RenderResource("aws_security_group", *e.Name, tf)
}
Exemple #14
0
func (_ *Subnet) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *Subnet) error {
	cloud := t.Cloud.(*awsup.AWSCloud)

	tf := &terraformSubnet{
		VPCID:            e.VPC.TerraformLink(),
		CIDR:             e.CIDR,
		AvailabilityZone: e.AvailabilityZone,
		Tags:             cloud.BuildTags(e.Name),
	}

	return t.RenderResource("aws_subnet", *e.Name, tf)
}
Exemple #15
0
func (_ *SSHKey) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *SSHKey) error {
	publicKey, err := t.AddFile("aws_key_pair", *e.Name, "public_key", e.PublicKey)
	if err != nil {
		return fmt.Errorf("error rendering PublicKey: %v", err)
	}

	tf := &terraformSSHKey{
		Name:      e.Name,
		PublicKey: publicKey,
	}

	return t.RenderResource("aws_key_pair", *e.Name, tf)
}
Exemple #16
0
func (_ *IAMRole) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *IAMRole) error {
	policy, err := t.AddFile("aws_iam_role", *e.Name, "policy", e.RolePolicyDocument)
	if err != nil {
		return fmt.Errorf("error rendering RolePolicyDocument: %v", err)
	}

	tf := &terraformIAMRole{
		Name:             e.Name,
		AssumeRolePolicy: policy,
	}

	return t.RenderResource("aws_iam_role", *e.Name, tf)
}
Exemple #17
0
func (_ *LaunchConfiguration) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *LaunchConfiguration) error {
	cloud := t.Cloud.(*awsup.AWSCloud)

	if e.ImageID == nil {
		return fi.RequiredField("ImageID")
	}
	image, err := cloud.ResolveImage(*e.ImageID)
	if err != nil {
		return err
	}

	tf := &terraformLaunchConfiguration{
		NamePrefix:   fi.String(*e.Name + "-"),
		ImageID:      image.ImageId,
		InstanceType: e.InstanceType,
	}

	if e.SSHKey != nil {
		tf.KeyName = e.SSHKey.TerraformLink()
	}

	for _, sg := range e.SecurityGroups {
		tf.SecurityGroups = append(tf.SecurityGroups, sg.TerraformLink())
	}
	tf.AssociatePublicIpAddress = e.AssociatePublicIP

	if e.BlockDeviceMappings != nil {
		tf.EphemeralBlockDevice = []*terraformBlockDevice{}
		for deviceName, bdm := range e.BlockDeviceMappings {
			tf.EphemeralBlockDevice = append(tf.EphemeralBlockDevice, &terraformBlockDevice{
				VirtualName: bdm.VirtualName,
				DeviceName:  fi.String(deviceName),
			})
		}
	}

	if e.UserData != nil {
		tf.UserData, err = t.AddFile("aws_launch_configuration", *e.Name, "user_data", e.UserData)
		if err != nil {
			return err
		}
	}
	if e.IAMInstanceProfile != nil {
		tf.IAMInstanceProfile = e.IAMInstanceProfile.TerraformLink()
	}

	// So that we can update configurations
	tf.Lifecycle = &terraformLifecycle{CreateBeforeDestroy: fi.Bool(true)}

	return t.RenderResource("aws_launch_configuration", *e.Name, tf)
}
Exemple #18
0
func (_ *DHCPOptions) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *DHCPOptions) error {
	cloud := t.Cloud.(*awsup.AWSCloud)

	tf := &terraformDHCPOptions{
		DomainName: e.DomainName,
		Tags:       cloud.BuildTags(e.Name),
	}

	if e.DomainNameServers != nil {
		tf.DomainNameServers = strings.Split(*e.DomainNameServers, ",")
	}

	return t.RenderResource("aws_vpc_dhcp_options", *e.Name, tf)
}
Exemple #19
0
func (_ *InstanceTemplate) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *InstanceTemplate) error {
	project := t.Project

	i, err := e.mapToGCE(project)
	if err != nil {
		return err
	}

	tf := &terraformInstanceTemplate{
		Name:         i.Name,
		CanIPForward: i.Properties.CanIpForward,
		//Description: i.Properties.Description,
		MachineType: i.Properties.MachineType,
		Tags:        i.Properties.Tags.Items,
	}

	tf.AddServiceAccounts(i.Properties.ServiceAccounts)

	for _, d := range i.Properties.Disks {
		tfd := &terraformAttachedDisk{
			AutoDelete:  d.AutoDelete,
			Boot:        d.Boot,
			DeviceName:  d.DeviceName,
			DiskName:    d.InitializeParams.DiskName,
			SourceImage: d.InitializeParams.SourceImage,
			Source:      d.Source,
			Interface:   d.Interface,
			Mode:        d.Mode,
			DiskType:    d.InitializeParams.DiskType,
			DiskSizeGB:  d.InitializeParams.DiskSizeGb,
			Type:        d.Type,
		}
		tf.Disks = append(tf.Disks, tfd)
	}

	tf.AddNetworks(e.Network, e.Subnet, i.Properties.NetworkInterfaces)

	tf.AddMetadata(i.Properties.Metadata)

	if i.Properties.Scheduling != nil {
		tf.Scheduling = &terraformScheduling{
			AutomaticRestart:  i.Properties.Scheduling.AutomaticRestart,
			OnHostMaintenance: i.Properties.Scheduling.OnHostMaintenance,
			Preemptible:       i.Properties.Scheduling.Preemptible,
		}
	}

	return t.RenderResource("google_compute_instance_template", i.Name, tf)
}
Exemple #20
0
func (_ *InternetGateway) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *InternetGateway) error {
	shared := fi.BoolValue(e.Shared)
	if shared {
		// Not terraform owned / managed
		return nil
	}

	cloud := t.Cloud.(*awsup.AWSCloud)

	tf := &terraformInternetGateway{
		VPCID: e.VPC.TerraformLink(),
		Tags:  cloud.BuildTags(e.Name),
	}

	return t.RenderResource("aws_internet_gateway", *e.Name, tf)
}
Exemple #21
0
func (_ *Route) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *Route) error {
	tf := &terraformRoute{
		CIDR:         e.CIDR,
		RouteTableID: e.RouteTable.TerraformLink(),
	}

	if e.InternetGateway != nil {
		tf.InternetGatewayID = e.InternetGateway.TerraformLink()
	}

	if e.Instance != nil {
		tf.InstanceID = e.Instance.TerraformLink()
	}

	return t.RenderResource("aws_route", *e.Name, tf)
}
Exemple #22
0
func (_ *DNSName) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *DNSName) error {
	tf := &terraformRoute53Record{
		Name:   e.Name,
		ZoneID: e.Zone.TerraformLink(),
		Type:   e.ResourceType,
	}

	if e.TargetLoadBalancer != nil {
		tf.Alias = &terraformAlias{
			Name:                 e.TargetLoadBalancer.DNSName,
			EvaluateTargetHealth: aws.Bool(false),
			HostedZoneId:         e.TargetLoadBalancer.HostedZoneId,
		}
	}

	return t.RenderResource("aws_route53_record", *e.Name, tf)
}
Exemple #23
0
func (_ *VPC) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *VPC) error {
	cloud := t.Cloud.(*awsup.AWSCloud)

	shared := fi.BoolValue(e.Shared)
	if shared {
		// Not terraform owned / managed
		return nil
	}

	tf := &terraformVPC{
		CIDR:               e.CIDR,
		Tags:               cloud.BuildTags(e.Name),
		EnableDNSHostnames: e.EnableDNSHostnames,
		EnableDNSSupport:   e.EnableDNSSupport,
	}

	return t.RenderResource("aws_vpc", *e.Name, tf)
}
Exemple #24
0
func (_ *SecurityGroupRule) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *SecurityGroupRule) error {
	tf := &terraformSecurityGroupIngress{
		Type:          fi.String("ingress"),
		SecurityGroup: e.SecurityGroup.TerraformLink(),
		FromPort:      e.FromPort,
		ToPort:        e.ToPort,
		Protocol:      e.Protocol,
	}
	if fi.BoolValue(e.Egress) {
		tf.Type = fi.String("egress")
	}

	if e.Protocol == nil {
		tf.Protocol = fi.String("-1")
		tf.FromPort = fi.Int64(0)
		tf.ToPort = fi.Int64(0)
	}

	if tf.FromPort == nil {
		// FromPort is required by tf
		tf.FromPort = fi.Int64(0)
	}
	if tf.ToPort == nil {
		// ToPort is required by tf
		tf.ToPort = fi.Int64(65535)
	}

	if e.SourceGroup != nil {
		tf.SourceGroup = e.SourceGroup.TerraformLink()
	}

	if e.CIDR != nil {
		tf.CIDRBlocks = append(tf.CIDRBlocks, *e.CIDR)
	}
	return t.RenderResource("aws_security_group_rule", *e.Name, tf)
}
Exemple #25
0
func (_ *IPAddress) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *IPAddress) error {
	tf := &terraformAddress{
		Name: e.Name,
	}
	return t.RenderResource("google_compute_address", *e.Name, tf)
}
Exemple #26
0
func (_ *Instance) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *Instance) error {
	project := t.Project

	// This is a "little" hacky...
	ipAddressResolver := func(ip *IPAddress) (*string, error) {
		tf := "${google_compute_address." + *ip.Name + ".address}"
		return &tf, nil
	}

	i, err := e.mapToGCE(project, ipAddressResolver)
	if err != nil {
		return err
	}

	tf := &terraformInstanceTemplate{
		Name:         i.Name,
		CanIPForward: i.CanIpForward,
		MachineType:  lastComponent(i.MachineType),
		Zone:         i.Zone,
		Tags:         i.Tags.Items,
	}

	// TF requires zone
	if tf.Zone == "" && e.Zone != nil {
		tf.Zone = *e.Zone
	}

	tf.AddServiceAccounts(i.ServiceAccounts)

	for _, d := range i.Disks {
		tfd := &terraformAttachedDisk{
			AutoDelete: d.AutoDelete,
			Scratch:    d.Type == "SCRATCH",
			DeviceName: d.DeviceName,

			// TODO: Does this need to be a TF link?
			Disk: lastComponent(d.Source),
		}
		if d.InitializeParams != nil {
			tfd.Disk = d.InitializeParams.DiskName
			tfd.Image = d.InitializeParams.SourceImage
			tfd.Type = d.InitializeParams.DiskType
			tfd.Size = d.InitializeParams.DiskSizeGb
		}
		tf.Disks = append(tf.Disks, tfd)
	}

	tf.AddNetworks(e.Network, e.Subnet, i.NetworkInterfaces)

	tf.AddMetadata(i.Metadata)

	// Using metadata_startup_script is now mandatory (?)
	{
		startupScript, found := tf.Metadata["startup-script"]
		if found {
			delete(tf.Metadata, "startup-script")
		}
		tf.MetadataStartupScript = startupScript
	}

	if i.Scheduling != nil {
		tf.Scheduling = &terraformScheduling{
			AutomaticRestart:  i.Scheduling.AutomaticRestart,
			OnHostMaintenance: i.Scheduling.OnHostMaintenance,
			Preemptible:       i.Scheduling.Preemptible,
		}
	}

	return t.RenderResource("google_compute_instance", i.Name, tf)
}