forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_azure_affinity_group.go
168 lines (143 loc) · 4.91 KB
/
resource_azure_affinity_group.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package azure
import (
"fmt"
"log"
"github.com/Azure/azure-sdk-for-go/management"
"github.com/Azure/azure-sdk-for-go/management/affinitygroup"
"github.com/hashicorp/terraform/helper/schema"
)
// resourceAzureAffinityGroup returns the *schema.Resource associated to a
// resource affinity group on Azure.
func resourceAzureAffinityGroup() *schema.Resource {
return &schema.Resource{
Create: resourceAzureAffinityGroupCreate,
Read: resourceAzureAffinityGroupRead,
Update: resourceAzureAffinityGroupUpdate,
Exists: resourceAzureAffinityGroupExists,
Delete: resourceAzureAffinityGroupDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"location": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"label": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
}
}
// resourceAzureAffinityGroupCreate does all the necessary API calls to
// create an affinity group on Azure.
func resourceAzureAffinityGroupCreate(d *schema.ResourceData, meta interface{}) error {
affinityGroupClient := meta.(*Client).affinityGroupClient
log.Println("[INFO] Begun creating Azure Affinity Group creation request.")
name := d.Get("name").(string)
params := affinitygroup.CreateAffinityGroupParams{
Name: name,
Label: d.Get("label").(string),
Location: d.Get("location").(string),
}
if desc, ok := d.GetOk("description"); ok {
params.Description = desc.(string)
}
log.Println("[INFO] Sending Affinity Group creation request to Azure.")
err := affinityGroupClient.CreateAffinityGroup(params)
if err != nil {
return fmt.Errorf("Error issuing Azure Affinity Group creation: %s", err)
}
d.SetId(name)
return nil
}
// resourceAzureAffinityGroupRead does all the necessary API calls to
// read the state of the affinity group off Azure.
func resourceAzureAffinityGroupRead(d *schema.ResourceData, meta interface{}) error {
affinityGroupClient := meta.(*Client).affinityGroupClient
log.Println("[INFO] Issuing Azure Affinity Group list request.")
affinityGroups, err := affinityGroupClient.ListAffinityGroups()
if err != nil {
return fmt.Errorf("Error obtaining Affinity Group list off Azure: %s", err)
}
var found bool
name := d.Get("name").(string)
for _, group := range affinityGroups.AffinityGroups {
if group.Name == name {
found = true
d.Set("location", group.Location)
d.Set("label", group.Label)
d.Set("description", group.Description)
break
}
}
if !found {
// it means the affinity group has been deleted in the meantime, so we
// must stop tracking it:
d.SetId("")
}
return nil
}
// resourceAzureAffinityGroupUpdate does all the necessary API calls to
// update the state of the affinity group on Azure.
func resourceAzureAffinityGroupUpdate(d *schema.ResourceData, meta interface{}) error {
affinityGroupClient := meta.(*Client).affinityGroupClient
name := d.Get("name").(string)
clabel := d.HasChange("label")
cdesc := d.HasChange("description")
if clabel || cdesc {
log.Println("[INFO] Beginning Affinity Group update process.")
params := affinitygroup.UpdateAffinityGroupParams{}
if clabel {
params.Label = d.Get("label").(string)
}
if cdesc {
params.Description = d.Get("description").(string)
}
log.Println("[INFO] Sending Affinity Group update request to Azure.")
err := affinityGroupClient.UpdateAffinityGroup(name, params)
if err != nil {
return fmt.Errorf("Error updating Azure Affinity Group parameters: %s", err)
}
}
return nil
}
// resourceAzureAffinityGroupExists does all the necessary API calls to
// check for the existence of the affinity group on Azure.
func resourceAzureAffinityGroupExists(d *schema.ResourceData, meta interface{}) (bool, error) {
affinityGroupClient := meta.(*Client).affinityGroupClient
log.Println("[INFO] Issuing Azure Affinity Group get request.")
name := d.Get("name").(string)
_, err := affinityGroupClient.GetAffinityGroup(name)
if err != nil {
if management.IsResourceNotFoundError(err) {
// it means that the affinity group has been deleted in the
// meantime, so we must untrack it from the schema:
d.SetId("")
return false, nil
} else {
return false, fmt.Errorf("Error getting Affinity Group off Azure: %s", err)
}
}
return true, nil
}
// resourceAzureAffinityGroupDelete does all the necessary API calls to
// delete the affinity group off Azure.
func resourceAzureAffinityGroupDelete(d *schema.ResourceData, meta interface{}) error {
affinityGroupClient := meta.(*Client).affinityGroupClient
log.Println("[INFO] Sending Affinity Group deletion request to Azure.")
name := d.Get("name").(string)
err := affinityGroupClient.DeleteAffinityGroup(name)
if err != nil {
return fmt.Errorf("Error deleting Azure Affinity Group: %s", err)
}
return nil
}