github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_placement_group.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/aws/awserr"
    10  	"github.com/aws/aws-sdk-go/service/ec2"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/helper/schema"
    13  )
    14  
    15  func resourceAwsPlacementGroup() *schema.Resource {
    16  	return &schema.Resource{
    17  		Create: resourceAwsPlacementGroupCreate,
    18  		Read:   resourceAwsPlacementGroupRead,
    19  		Delete: resourceAwsPlacementGroupDelete,
    20  
    21  		Schema: map[string]*schema.Schema{
    22  			"name": &schema.Schema{
    23  				Type:     schema.TypeString,
    24  				Required: true,
    25  				ForceNew: true,
    26  			},
    27  			"strategy": &schema.Schema{
    28  				Type:     schema.TypeString,
    29  				Required: true,
    30  				ForceNew: true,
    31  			},
    32  		},
    33  	}
    34  }
    35  
    36  func resourceAwsPlacementGroupCreate(d *schema.ResourceData, meta interface{}) error {
    37  	conn := meta.(*AWSClient).ec2conn
    38  
    39  	name := d.Get("name").(string)
    40  	input := ec2.CreatePlacementGroupInput{
    41  		GroupName: aws.String(name),
    42  		Strategy:  aws.String(d.Get("strategy").(string)),
    43  	}
    44  	log.Printf("[DEBUG] Creating EC2 Placement group: %s", input)
    45  	_, err := conn.CreatePlacementGroup(&input)
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	wait := resource.StateChangeConf{
    51  		Pending:    []string{"pending"},
    52  		Target:     "available",
    53  		Timeout:    5 * time.Minute,
    54  		MinTimeout: 1 * time.Second,
    55  		Refresh: func() (interface{}, string, error) {
    56  			out, err := conn.DescribePlacementGroups(&ec2.DescribePlacementGroupsInput{
    57  				GroupNames: []*string{aws.String(name)},
    58  			})
    59  
    60  			if err != nil {
    61  				return out, "", err
    62  			}
    63  
    64  			if len(out.PlacementGroups) == 0 {
    65  				return out, "", fmt.Errorf("Placement group not found (%q)", name)
    66  			}
    67  			pg := out.PlacementGroups[0]
    68  
    69  			return out, *pg.State, nil
    70  		},
    71  	}
    72  
    73  	_, err = wait.WaitForState()
    74  	if err != nil {
    75  		return err
    76  	}
    77  
    78  	log.Printf("[DEBUG] EC2 Placement group created: %q", name)
    79  
    80  	d.SetId(name)
    81  
    82  	return resourceAwsPlacementGroupRead(d, meta)
    83  }
    84  
    85  func resourceAwsPlacementGroupRead(d *schema.ResourceData, meta interface{}) error {
    86  	conn := meta.(*AWSClient).ec2conn
    87  	input := ec2.DescribePlacementGroupsInput{
    88  		GroupNames: []*string{aws.String(d.Get("name").(string))},
    89  	}
    90  	out, err := conn.DescribePlacementGroups(&input)
    91  	if err != nil {
    92  		return err
    93  	}
    94  	pg := out.PlacementGroups[0]
    95  
    96  	log.Printf("[DEBUG] Received EC2 Placement Group: %s", pg)
    97  
    98  	d.Set("name", pg.GroupName)
    99  	d.Set("strategy", pg.Strategy)
   100  
   101  	return nil
   102  }
   103  
   104  func resourceAwsPlacementGroupDelete(d *schema.ResourceData, meta interface{}) error {
   105  	conn := meta.(*AWSClient).ec2conn
   106  
   107  	log.Printf("[DEBUG] Deleting EC2 Placement Group %q", d.Id())
   108  	_, err := conn.DeletePlacementGroup(&ec2.DeletePlacementGroupInput{
   109  		GroupName: aws.String(d.Id()),
   110  	})
   111  	if err != nil {
   112  		return err
   113  	}
   114  
   115  	wait := resource.StateChangeConf{
   116  		Pending:    []string{"deleting"},
   117  		Target:     "deleted",
   118  		Timeout:    5 * time.Minute,
   119  		MinTimeout: 1 * time.Second,
   120  		Refresh: func() (interface{}, string, error) {
   121  			out, err := conn.DescribePlacementGroups(&ec2.DescribePlacementGroupsInput{
   122  				GroupNames: []*string{aws.String(d.Id())},
   123  			})
   124  
   125  			if err != nil {
   126  				awsErr := err.(awserr.Error)
   127  				if awsErr.Code() == "InvalidPlacementGroup.Unknown" {
   128  					return out, "deleted", nil
   129  				}
   130  				return out, "", awsErr
   131  			}
   132  
   133  			if len(out.PlacementGroups) == 0 {
   134  				return out, "deleted", nil
   135  			}
   136  
   137  			pg := out.PlacementGroups[0]
   138  
   139  			return out, *pg.State, nil
   140  		},
   141  	}
   142  
   143  	_, err = wait.WaitForState()
   144  	if err != nil {
   145  		return err
   146  	}
   147  
   148  	d.SetId("")
   149  	return nil
   150  }