package aws import ( "fmt" "log" "regexp" "strings" "time" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/rds" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" ) func resourceAwsDbSubnetGroup() *schema.Resource { return &schema.Resource{ Create: resourceAwsDbSubnetGroupCreate, Read: resourceAwsDbSubnetGroupRead, Update: resourceAwsDbSubnetGroupUpdate, Delete: resourceAwsDbSubnetGroupDelete, Importer: &schema.ResourceImporter{ State: schema.ImportStatePassthrough, }, Schema: map[string]*schema.Schema{ "arn": &schema.Schema{ Type: schema.TypeString, Computed: true, }, "name": &schema.Schema{ Type: schema.TypeString, ForceNew: true, Required: true, ValidateFunc: validateSubnetGroupName, }, "description": &schema.Schema{ Type: schema.TypeString, Optional: true, Default: "Managed by Terraform", }, "subnet_ids": &schema.Schema{ Type: schema.TypeSet, Required: true, Elem: &schema.Schema{Type: schema.TypeString}, Set: schema.HashString, }, "tags": tagsSchema(), }, } } func resourceAwsDbSubnetGroupCreate(d *schema.ResourceData, meta interface{}) error { rdsconn := meta.(*AWSClient).rdsconn tags := tagsFromMapRDS(d.Get("tags").(map[string]interface{})) subnetIdsSet := d.Get("subnet_ids").(*schema.Set) subnetIds := make([]*string, subnetIdsSet.Len()) for i, subnetId := range subnetIdsSet.List() { subnetIds[i] = aws.String(subnetId.(string)) } createOpts := rds.CreateDBSubnetGroupInput{ DBSubnetGroupName: aws.String(d.Get("name").(string)), DBSubnetGroupDescription: aws.String(d.Get("description").(string)), SubnetIds: subnetIds, Tags: tags, } log.Printf("[DEBUG] Create DB Subnet Group: %#v", createOpts) _, err := rdsconn.CreateDBSubnetGroup(&createOpts) if err != nil { return fmt.Errorf("Error creating DB Subnet Group: %s", err) } d.SetId(*createOpts.DBSubnetGroupName) log.Printf("[INFO] DB Subnet Group ID: %s", d.Id()) return resourceAwsDbSubnetGroupRead(d, meta) } func resourceAwsDbSubnetGroupRead(d *schema.ResourceData, meta interface{}) error { rdsconn := meta.(*AWSClient).rdsconn describeOpts := rds.DescribeDBSubnetGroupsInput{ DBSubnetGroupName: aws.String(d.Id()), } describeResp, err := rdsconn.DescribeDBSubnetGroups(&describeOpts) if err != nil { if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "DBSubnetGroupNotFoundFault" { // Update state to indicate the db subnet no longer exists. d.SetId("") return nil } return err } if len(describeResp.DBSubnetGroups) == 0 { return fmt.Errorf("Unable to find DB Subnet Group: %#v", describeResp.DBSubnetGroups) } var subnetGroup *rds.DBSubnetGroup for _, s := range describeResp.DBSubnetGroups { // AWS is down casing the name provided, so we compare lower case versions // of the names. We lower case both our name and their name in the check, // incase they change that someday. if strings.ToLower(d.Id()) == strings.ToLower(*s.DBSubnetGroupName) { subnetGroup = describeResp.DBSubnetGroups[0] } } if subnetGroup.DBSubnetGroupName == nil { return fmt.Errorf("Unable to find DB Subnet Group: %#v", describeResp.DBSubnetGroups) } d.Set("name", subnetGroup.DBSubnetGroupName) d.Set("description", subnetGroup.DBSubnetGroupDescription) subnets := make([]string, 0, len(subnetGroup.Subnets)) for _, s := range subnetGroup.Subnets { subnets = append(subnets, *s.SubnetIdentifier) } d.Set("subnet_ids", subnets) // list tags for resource // set tags conn := meta.(*AWSClient).rdsconn arn, err := buildRDSsubgrpARN(d.Id(), meta.(*AWSClient).accountid, meta.(*AWSClient).region) if err != nil { log.Printf("[DEBUG] Error building ARN for DB Subnet Group, not setting Tags for group %s", *subnetGroup.DBSubnetGroupName) } else { d.Set("arn", arn) resp, err := conn.ListTagsForResource(&rds.ListTagsForResourceInput{ ResourceName: aws.String(arn), }) if err != nil { log.Printf("[DEBUG] Error retreiving tags for ARN: %s", arn) } var dt []*rds.Tag if len(resp.TagList) > 0 { dt = resp.TagList } d.Set("tags", tagsToMapRDS(dt)) } return nil } func resourceAwsDbSubnetGroupUpdate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).rdsconn if d.HasChange("subnet_ids") || d.HasChange("description") { _, n := d.GetChange("subnet_ids") if n == nil { n = new(schema.Set) } ns := n.(*schema.Set) var sIds []*string for _, s := range ns.List() { sIds = append(sIds, aws.String(s.(string))) } _, err := conn.ModifyDBSubnetGroup(&rds.ModifyDBSubnetGroupInput{ DBSubnetGroupName: aws.String(d.Id()), DBSubnetGroupDescription: aws.String(d.Get("description").(string)), SubnetIds: sIds, }) if err != nil { return err } } if arn, err := buildRDSsubgrpARN(d.Id(), meta.(*AWSClient).accountid, meta.(*AWSClient).region); err == nil { if err := setTagsRDS(conn, d, arn); err != nil { return err } else { d.SetPartial("tags") } } return resourceAwsDbSubnetGroupRead(d, meta) } func resourceAwsDbSubnetGroupDelete(d *schema.ResourceData, meta interface{}) error { stateConf := &resource.StateChangeConf{ Pending: []string{"pending"}, Target: []string{"destroyed"}, Refresh: resourceAwsDbSubnetGroupDeleteRefreshFunc(d, meta), Timeout: 3 * time.Minute, MinTimeout: 1 * time.Second, } _, err := stateConf.WaitForState() return err } func resourceAwsDbSubnetGroupDeleteRefreshFunc( d *schema.ResourceData, meta interface{}) resource.StateRefreshFunc { rdsconn := meta.(*AWSClient).rdsconn return func() (interface{}, string, error) { deleteOpts := rds.DeleteDBSubnetGroupInput{ DBSubnetGroupName: aws.String(d.Id()), } if _, err := rdsconn.DeleteDBSubnetGroup(&deleteOpts); err != nil { rdserr, ok := err.(awserr.Error) if !ok { return d, "error", err } if rdserr.Code() != "DBSubnetGroupNotFoundFault" { return d, "error", err } } return d, "destroyed", nil } } func buildRDSsubgrpARN(identifier, accountid, region string) (string, error) { if accountid == "" { return "", fmt.Errorf("Unable to construct RDS ARN because of missing AWS Account ID") } arn := fmt.Sprintf("arn:aws:rds:%s:%s:subgrp:%s", region, accountid, identifier) return arn, nil } func validateSubnetGroupName(v interface{}, k string) (ws []string, errors []error) { value := v.(string) if !regexp.MustCompile(`^[ .0-9a-z-_]+$`).MatchString(value) { errors = append(errors, fmt.Errorf( "only lowercase alphanumeric characters, hyphens, underscores, periods, and spaces allowed in %q", k)) } if len(value) > 255 { errors = append(errors, fmt.Errorf( "%q cannot be longer than 255 characters", k)) } if regexp.MustCompile(`(?i)^default$`).MatchString(value) { errors = append(errors, fmt.Errorf( "%q is not allowed as %q", "Default", k)) } return }