github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/openstack/resource_openstack_compute_servergroup_v2.go (about) 1 package openstack 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/servergroups" 8 "github.com/hashicorp/terraform/helper/schema" 9 ) 10 11 func resourceComputeServerGroupV2() *schema.Resource { 12 return &schema.Resource{ 13 Create: resourceComputeServerGroupV2Create, 14 Read: resourceComputeServerGroupV2Read, 15 Update: nil, 16 Delete: resourceComputeServerGroupV2Delete, 17 Importer: &schema.ResourceImporter{ 18 State: schema.ImportStatePassthrough, 19 }, 20 21 Schema: map[string]*schema.Schema{ 22 "region": &schema.Schema{ 23 Type: schema.TypeString, 24 Required: true, 25 ForceNew: true, 26 DefaultFunc: schema.EnvDefaultFunc("OS_REGION_NAME", ""), 27 }, 28 "name": &schema.Schema{ 29 Type: schema.TypeString, 30 ForceNew: true, 31 Required: true, 32 }, 33 "policies": &schema.Schema{ 34 Type: schema.TypeList, 35 Optional: true, 36 ForceNew: true, 37 Elem: &schema.Schema{Type: schema.TypeString}, 38 }, 39 "members": &schema.Schema{ 40 Type: schema.TypeList, 41 Computed: true, 42 Elem: &schema.Schema{Type: schema.TypeString}, 43 }, 44 }, 45 } 46 } 47 48 func resourceComputeServerGroupV2Create(d *schema.ResourceData, meta interface{}) error { 49 config := meta.(*Config) 50 computeClient, err := config.computeV2Client(d.Get("region").(string)) 51 if err != nil { 52 return fmt.Errorf("Error creating OpenStack compute client: %s", err) 53 } 54 55 createOpts := &servergroups.CreateOpts{ 56 Name: d.Get("name").(string), 57 Policies: resourceServerGroupPoliciesV2(d), 58 } 59 log.Printf("[DEBUG] Create Options: %#v", createOpts) 60 newSG, err := servergroups.Create(computeClient, createOpts).Extract() 61 if err != nil { 62 return fmt.Errorf("Error creating ServerGroup: %s", err) 63 } 64 65 d.SetId(newSG.ID) 66 67 return resourceComputeServerGroupV2Read(d, meta) 68 } 69 70 func resourceComputeServerGroupV2Read(d *schema.ResourceData, meta interface{}) error { 71 config := meta.(*Config) 72 computeClient, err := config.computeV2Client(d.Get("region").(string)) 73 if err != nil { 74 return fmt.Errorf("Error creating OpenStack compute client: %s", err) 75 } 76 77 sg, err := servergroups.Get(computeClient, d.Id()).Extract() 78 if err != nil { 79 return CheckDeleted(d, err, "server group") 80 } 81 82 log.Printf("[DEBUG] Retrieved ServerGroup %s: %+v", d.Id(), sg) 83 84 // Set the name 85 d.Set("name", sg.Name) 86 87 // Set the policies 88 policies := []string{} 89 for _, p := range sg.Policies { 90 policies = append(policies, p) 91 } 92 d.Set("policies", policies) 93 94 // Set the members 95 members := []string{} 96 for _, m := range sg.Members { 97 members = append(members, m) 98 } 99 d.Set("members", members) 100 101 return nil 102 } 103 104 func resourceComputeServerGroupV2Delete(d *schema.ResourceData, meta interface{}) error { 105 config := meta.(*Config) 106 computeClient, err := config.computeV2Client(d.Get("region").(string)) 107 if err != nil { 108 return fmt.Errorf("Error creating OpenStack compute client: %s", err) 109 } 110 111 log.Printf("[DEBUG] Deleting ServerGroup %s", d.Id()) 112 if err := servergroups.Delete(computeClient, d.Id()).ExtractErr(); err != nil { 113 return fmt.Errorf("Error deleting ServerGroup: %s", err) 114 } 115 116 return nil 117 } 118 119 func resourceServerGroupPoliciesV2(d *schema.ResourceData) []string { 120 rawPolicies := d.Get("policies").([]interface{}) 121 policies := make([]string, len(rawPolicies)) 122 for i, raw := range rawPolicies { 123 policies[i] = raw.(string) 124 } 125 return policies 126 }