-
Notifications
You must be signed in to change notification settings - Fork 16
/
resource_service.go
290 lines (238 loc) · 7.87 KB
/
resource_service.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package main
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/sky-uk/gonsx"
"github.com/sky-uk/gonsx/api/service"
"log"
)
func getSingleService(scopeid, name string, nsxclient *gonsx.NSXClient) (*service.ApplicationService, error) {
getAllAPI := service.NewGetAll(scopeid)
err := nsxclient.Do(getAllAPI)
if err != nil {
return nil, err
}
if getAllAPI.StatusCode() != 200 {
return nil, fmt.Errorf("Status code: %d, Response: %s", getAllAPI.StatusCode(), getAllAPI.ResponseObject())
}
service := getAllAPI.GetResponse().FilterByName(name)
if service.ObjectID == "" {
return nil, fmt.Errorf("Not found %s", name)
}
return service, nil
}
func resourceService() *schema.Resource {
return &schema.Resource{
Create: resourceServiceCreate,
Read: resourceServiceRead,
Delete: resourceServiceDelete,
Update: resourceServiceUpdate,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"scopeid": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"description": {
Type: schema.TypeString,
Required: true,
},
"protocol": {
Type: schema.TypeString,
Required: true,
},
"ports": {
Type: schema.TypeString,
Required: true,
},
},
}
}
func resourceServiceCreate(d *schema.ResourceData, meta interface{}) error {
nsxclient := meta.(*gonsx.NSXClient)
var name, scopeid, description, protocol, ports string
// Gather the attributes for the resource.
if v, ok := d.GetOk("name"); ok {
name = v.(string)
} else {
return fmt.Errorf("name argument is required")
}
if v, ok := d.GetOk("scopeid"); ok {
scopeid = v.(string)
} else {
return fmt.Errorf("scopeid argument is required")
}
if v, ok := d.GetOk("description"); ok {
description = v.(string)
} else {
return fmt.Errorf("description argument is required")
}
if v, ok := d.GetOk("protocol"); ok {
protocol = v.(string)
} else {
return fmt.Errorf("protocol argument is required")
}
if v, ok := d.GetOk("ports"); ok {
ports = v.(string)
} else {
return fmt.Errorf("ports argument is required")
}
// Create the API, use it and check for errors.
log.Printf(fmt.Sprintf("[DEBUG] service.NewCreate(%s, %s, %s, %s, %s)", scopeid, name, description, protocol, ports))
createAPI := service.NewCreate(scopeid, name, description, protocol, ports)
err := nsxclient.Do(createAPI)
if err != nil {
return fmt.Errorf("Error: %v", err)
}
if createAPI.StatusCode() != 201 {
return fmt.Errorf("%s", createAPI.ResponseObject())
}
// If we get here, everything is OK. Set the ID for the Terraform state
// and return the response from the READ method.
d.SetId(createAPI.GetResponse())
return resourceServiceRead(d, meta)
}
func resourceServiceRead(d *schema.ResourceData, meta interface{}) error {
nsxclient := meta.(*gonsx.NSXClient)
var scopeid, name string
// Gather the attributes for the resource.
if v, ok := d.GetOk("scopeid"); ok {
scopeid = v.(string)
} else {
return fmt.Errorf("scopeid argument is required")
}
if v, ok := d.GetOk("name"); ok {
name = v.(string)
} else {
return fmt.Errorf("name argument is required")
}
// Gather all the resources that are associated with the specified
// scopeid.
log.Printf(fmt.Sprintf("[DEBUG] service.NewGetAll(%s)", scopeid))
api := service.NewGetAll(scopeid)
err := nsxclient.Do(api)
if err != nil {
return err
}
// See if we can find our specifically named resource within the list of
// resources associated with the scopeid.
log.Printf(fmt.Sprintf("[DEBUG] api.GetResponse().FilterByName(\"%s\").ObjectID", name))
serviceObject, err := getSingleService(scopeid, name, nsxclient)
id := serviceObject.ObjectID
log.Printf(fmt.Sprintf("[DEBUG] id := %s", id))
// If the resource has been removed manually, notify Terraform of this fact.
if id == "" {
d.SetId("")
}
return nil
}
func resourceServiceDelete(d *schema.ResourceData, meta interface{}) error {
nsxclient := meta.(*gonsx.NSXClient)
var name, scopeid string
// Gather the attributes for the resource.
if v, ok := d.GetOk("scopeid"); ok {
scopeid = v.(string)
} else {
return fmt.Errorf("scopeid argument is required")
}
if v, ok := d.GetOk("name"); ok {
name = v.(string)
} else {
return fmt.Errorf("name argument is required")
}
// Gather all the resources that are associated with the specified
// scopeid.
log.Printf(fmt.Sprintf("[DEBUG] service.NewGetAll(%s)", scopeid))
api := service.NewGetAll(scopeid)
err := nsxclient.Do(api)
if err != nil {
return err
}
// See if we can find our specifically named resource within the list of
// resources associated with the scopeid.
log.Printf(fmt.Sprintf("[DEBUG] api.GetResponse().FilterByName(\"%s\").ObjectID", name))
serviceObject, err := getSingleService(scopeid, name, nsxclient)
id := serviceObject.ObjectID
log.Printf(fmt.Sprintf("[DEBUG] id := %s", id))
// If the resource has been removed manually, notify Terraform of this fact.
if id == "" {
d.SetId("")
return nil
}
// If we got here, the resource exists, so we attempt to delete it.
deleteAPI := service.NewDelete(id)
err = nsxclient.Do(deleteAPI)
if err != nil {
return err
}
// If we got here, the resource had existed, we deleted it and there was
// no error. Notify Terraform of this fact and return successful
// completion.
d.SetId("")
log.Printf(fmt.Sprintf("[DEBUG] id %s deleted.", id))
return nil
}
func resourceServiceUpdate(d *schema.ResourceData, meta interface{}) error {
nsxclient := meta.(*gonsx.NSXClient)
var scopeid string
hasChanges := false
// Gather the attributes for the resource.
if v, ok := d.GetOk("scopeid"); ok {
scopeid = v.(string)
} else {
return fmt.Errorf("scopeid argument is required")
}
// Do a GetAll on service resources that are associated with the specified scopeid.
log.Printf(fmt.Sprintf("[DEBUG] service.NewGetAll(%s)", scopeid))
api := service.NewGetAll(scopeid)
err := nsxclient.Do(api)
if err != nil {
log.Printf(fmt.Sprintf("[DEBUG] Error during getting all service resources: %s", err))
return err
}
// Find the resource with current name within all the scopeid resources.
oldName, newName := d.GetChange("name")
log.Printf(fmt.Sprintf("[DEBUG] api.GetResponse().FilterByName(\"%s\").ObjectID", oldName.(string)))
serviceObject, err := getSingleService(scopeid, oldName.(string), nsxclient)
id := serviceObject.ObjectID
log.Printf(fmt.Sprintf("[DEBUG] id := %s", id))
// If the resource has been removed manually, notify Terraform of this fact.
if id == "" {
d.SetId("")
log.Printf(fmt.Sprintf("[DEBUG] Could not find the service resource with %s name, state will be cleared", oldName))
return nil
}
if d.HasChange("name") {
hasChanges = true
serviceObject.Name = newName.(string)
log.Printf(fmt.Sprintf("[DEBUG] Changing name of service from %s to %s", oldName.(string), newName.(string)))
}
if d.HasChange("description") {
hasChanges = true
oldDesc, newDesc := d.GetChange("description")
serviceObject.Description = newDesc.(string)
log.Printf(fmt.Sprintf("[DEBUG] Changing description of service from %s to %s", oldDesc.(string), newDesc.(string)))
}
if d.HasChange("protocol") || d.HasChange("ports") {
hasChanges = true
oldProtocol, newProtocol := d.GetChange("protocol")
oldPorts, newPorts := d.GetChange("ports")
newElement := service.Element{ApplicationProtocol: newProtocol.(string), Value: newPorts.(string)}
serviceObject.Element = []service.Element{newElement}
log.Printf(fmt.Sprintf("[DEBUG] Changing protocol and/or ports of service from %s:%s to %s:%s",
oldProtocol.(string), oldPorts.(string), newProtocol.(string), newPorts.(string)))
}
if hasChanges {
updateAPI := service.NewUpdate(id, serviceObject)
err = nsxclient.Do(updateAPI)
if err != nil {
log.Printf(fmt.Sprintf("[DEBUG] Error updating service resource: %s", err))
return err
}
}
return resourceServiceRead(d, meta)
}