github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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(GetRegion(d)) 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(GetRegion(d)) 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 d.Set("region", GetRegion(d)) 102 103 return nil 104 } 105 106 func resourceComputeServerGroupV2Delete(d *schema.ResourceData, meta interface{}) error { 107 config := meta.(*Config) 108 computeClient, err := config.computeV2Client(GetRegion(d)) 109 if err != nil { 110 return fmt.Errorf("Error creating OpenStack compute client: %s", err) 111 } 112 113 log.Printf("[DEBUG] Deleting ServerGroup %s", d.Id()) 114 if err := servergroups.Delete(computeClient, d.Id()).ExtractErr(); err != nil { 115 return fmt.Errorf("Error deleting ServerGroup: %s", err) 116 } 117 118 return nil 119 } 120 121 func resourceServerGroupPoliciesV2(d *schema.ResourceData) []string { 122 rawPolicies := d.Get("policies").([]interface{}) 123 policies := make([]string, len(rawPolicies)) 124 for i, raw := range rawPolicies { 125 policies[i] = raw.(string) 126 } 127 return policies 128 }