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