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