github.com/renier/terraform@v0.7.8-0.20161024133817-eb8a9ef5471a/builtin/providers/aws/resource_aws_redshift_subnet_group.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"regexp"
     7  	"time"
     8  
     9  	"github.com/aws/aws-sdk-go/aws"
    10  	"github.com/aws/aws-sdk-go/aws/awserr"
    11  	"github.com/aws/aws-sdk-go/service/redshift"
    12  	"github.com/hashicorp/terraform/helper/resource"
    13  	"github.com/hashicorp/terraform/helper/schema"
    14  )
    15  
    16  func resourceAwsRedshiftSubnetGroup() *schema.Resource {
    17  	return &schema.Resource{
    18  		Create: resourceAwsRedshiftSubnetGroupCreate,
    19  		Read:   resourceAwsRedshiftSubnetGroupRead,
    20  		Update: resourceAwsRedshiftSubnetGroupUpdate,
    21  		Delete: resourceAwsRedshiftSubnetGroupDelete,
    22  		Importer: &schema.ResourceImporter{
    23  			State: schema.ImportStatePassthrough,
    24  		},
    25  
    26  		Schema: map[string]*schema.Schema{
    27  			"name": &schema.Schema{
    28  				Type:         schema.TypeString,
    29  				ForceNew:     true,
    30  				Required:     true,
    31  				ValidateFunc: validateRedshiftSubnetGroupName,
    32  			},
    33  
    34  			"description": &schema.Schema{
    35  				Type:     schema.TypeString,
    36  				Optional: true,
    37  				Default:  "Managed by Terraform",
    38  			},
    39  
    40  			"subnet_ids": &schema.Schema{
    41  				Type:     schema.TypeSet,
    42  				Required: true,
    43  				Elem:     &schema.Schema{Type: schema.TypeString},
    44  				Set:      schema.HashString,
    45  			},
    46  		},
    47  	}
    48  }
    49  
    50  func resourceAwsRedshiftSubnetGroupCreate(d *schema.ResourceData, meta interface{}) error {
    51  	conn := meta.(*AWSClient).redshiftconn
    52  
    53  	subnetIdsSet := d.Get("subnet_ids").(*schema.Set)
    54  	subnetIds := make([]*string, subnetIdsSet.Len())
    55  	for i, subnetId := range subnetIdsSet.List() {
    56  		subnetIds[i] = aws.String(subnetId.(string))
    57  	}
    58  
    59  	createOpts := redshift.CreateClusterSubnetGroupInput{
    60  		ClusterSubnetGroupName: aws.String(d.Get("name").(string)),
    61  		Description:            aws.String(d.Get("description").(string)),
    62  		SubnetIds:              subnetIds,
    63  	}
    64  
    65  	log.Printf("[DEBUG] Create Redshift Subnet Group: %#v", createOpts)
    66  	_, err := conn.CreateClusterSubnetGroup(&createOpts)
    67  	if err != nil {
    68  		return fmt.Errorf("Error creating Redshift Subnet Group: %s", err)
    69  	}
    70  
    71  	d.SetId(*createOpts.ClusterSubnetGroupName)
    72  	log.Printf("[INFO] Redshift Subnet Group ID: %s", d.Id())
    73  	return resourceAwsRedshiftSubnetGroupRead(d, meta)
    74  }
    75  
    76  func resourceAwsRedshiftSubnetGroupRead(d *schema.ResourceData, meta interface{}) error {
    77  	conn := meta.(*AWSClient).redshiftconn
    78  
    79  	describeOpts := redshift.DescribeClusterSubnetGroupsInput{
    80  		ClusterSubnetGroupName: aws.String(d.Id()),
    81  	}
    82  
    83  	describeResp, err := conn.DescribeClusterSubnetGroups(&describeOpts)
    84  	if err != nil {
    85  		if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "ClusterSubnetGroupNotFoundFault" {
    86  			log.Printf("[INFO] Redshift Subnet Group: %s was not found", d.Id())
    87  			d.SetId("")
    88  			return nil
    89  		}
    90  		return err
    91  	}
    92  
    93  	if len(describeResp.ClusterSubnetGroups) == 0 {
    94  		return fmt.Errorf("Unable to find Redshift Subnet Group: %#v", describeResp.ClusterSubnetGroups)
    95  	}
    96  
    97  	d.Set("name", d.Id())
    98  	d.Set("description", describeResp.ClusterSubnetGroups[0].Description)
    99  	d.Set("subnet_ids", subnetIdsToSlice(describeResp.ClusterSubnetGroups[0].Subnets))
   100  
   101  	return nil
   102  }
   103  
   104  func resourceAwsRedshiftSubnetGroupUpdate(d *schema.ResourceData, meta interface{}) error {
   105  	conn := meta.(*AWSClient).redshiftconn
   106  	if d.HasChange("subnet_ids") || d.HasChange("description") {
   107  		_, n := d.GetChange("subnet_ids")
   108  		if n == nil {
   109  			n = new(schema.Set)
   110  		}
   111  		ns := n.(*schema.Set)
   112  
   113  		var sIds []*string
   114  		for _, s := range ns.List() {
   115  			sIds = append(sIds, aws.String(s.(string)))
   116  		}
   117  
   118  		_, err := conn.ModifyClusterSubnetGroup(&redshift.ModifyClusterSubnetGroupInput{
   119  			ClusterSubnetGroupName: aws.String(d.Id()),
   120  			Description:            aws.String(d.Get("description").(string)),
   121  			SubnetIds:              sIds,
   122  		})
   123  
   124  		if err != nil {
   125  			return err
   126  		}
   127  	}
   128  
   129  	return nil
   130  }
   131  
   132  func resourceAwsRedshiftSubnetGroupDelete(d *schema.ResourceData, meta interface{}) error {
   133  	stateConf := &resource.StateChangeConf{
   134  		Pending:    []string{"pending"},
   135  		Target:     []string{"destroyed"},
   136  		Refresh:    resourceAwsRedshiftSubnetGroupDeleteRefreshFunc(d, meta),
   137  		Timeout:    3 * time.Minute,
   138  		MinTimeout: 1 * time.Second,
   139  	}
   140  	_, err := stateConf.WaitForState()
   141  	return err
   142  }
   143  
   144  func resourceAwsRedshiftSubnetGroupDeleteRefreshFunc(d *schema.ResourceData, meta interface{}) resource.StateRefreshFunc {
   145  	conn := meta.(*AWSClient).redshiftconn
   146  
   147  	return func() (interface{}, string, error) {
   148  
   149  		deleteOpts := redshift.DeleteClusterSubnetGroupInput{
   150  			ClusterSubnetGroupName: aws.String(d.Id()),
   151  		}
   152  
   153  		if _, err := conn.DeleteClusterSubnetGroup(&deleteOpts); err != nil {
   154  			redshiftErr, ok := err.(awserr.Error)
   155  			if !ok {
   156  				return d, "error", err
   157  			}
   158  
   159  			if redshiftErr.Code() != "ClusterSubnetGroupNotFoundFault" {
   160  				return d, "error", err
   161  			}
   162  		}
   163  
   164  		return d, "destroyed", nil
   165  	}
   166  }
   167  
   168  func subnetIdsToSlice(subnetIds []*redshift.Subnet) []string {
   169  	subnetsSlice := make([]string, 0, len(subnetIds))
   170  	for _, s := range subnetIds {
   171  		subnetsSlice = append(subnetsSlice, *s.SubnetIdentifier)
   172  	}
   173  	return subnetsSlice
   174  }
   175  
   176  func validateRedshiftSubnetGroupName(v interface{}, k string) (ws []string, errors []error) {
   177  	value := v.(string)
   178  	if !regexp.MustCompile(`^[0-9a-z-]+$`).MatchString(value) {
   179  		errors = append(errors, fmt.Errorf(
   180  			"only lowercase alphanumeric characters and hyphens allowed in %q", k))
   181  	}
   182  	if len(value) > 255 {
   183  		errors = append(errors, fmt.Errorf(
   184  			"%q cannot be longer than 255 characters", k))
   185  	}
   186  	if regexp.MustCompile(`(?i)^default$`).MatchString(value) {
   187  		errors = append(errors, fmt.Errorf(
   188  			"%q is not allowed as %q", "Default", k))
   189  	}
   190  	return
   191  }