-
Notifications
You must be signed in to change notification settings - Fork 16
/
resource_zpa_server_group.go
426 lines (392 loc) · 13 KB
/
resource_zpa_server_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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package zpa
import (
"fmt"
"log"
"strconv"
"sync"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
client "github.com/zscaler/zscaler-sdk-go/zpa"
"github.com/zscaler/zscaler-sdk-go/zpa/services/appconnectorgroup"
"github.com/zscaler/zscaler-sdk-go/zpa/services/applicationsegment"
"github.com/zscaler/zscaler-sdk-go/zpa/services/policysetcontroller"
"github.com/zscaler/zscaler-sdk-go/zpa/services/servergroup"
)
var detachLock sync.Mutex
func resourceServerGroup() *schema.Resource {
return &schema.Resource{
Create: resourceServerGroupCreate,
Read: resourceServerGroupRead,
Update: resourceServerGroupUpdate,
Delete: resourceServerGroupDelete,
Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
zClient := m.(*Client)
id := d.Id()
_, parseIDErr := strconv.ParseInt(id, 10, 64)
if parseIDErr == nil {
// assume if the passed value is an int
_ = d.Set("id", id)
} else {
resp, _, err := zClient.servergroup.GetByName(id)
if err == nil {
d.SetId(resp.ID)
_ = d.Set("id", resp.ID)
} else {
return []*schema.ResourceData{d}, err
}
}
return []*schema.ResourceData{d}, nil
},
},
Schema: map[string]*schema.Schema{
"config_space": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
"DEFAULT",
"SIEM",
}, false),
Default: "DEFAULT",
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: "This field is the description of the server group.",
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Description: "This field defines if the server group is enabled or disabled.",
},
"id": {
Type: schema.TypeString,
Computed: true,
},
"ip_anchored": {
Type: schema.TypeBool,
Optional: true,
},
"dynamic_discovery": {
Type: schema.TypeBool,
Optional: true,
Description: "This field controls dynamic discovery of the servers.",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "This field defines the name of the server group.",
},
"servers": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Description: "This field is a list of servers that are applicable only when dynamic discovery is disabled. Server name is required only in cases where the new servers need to be created in this API. For existing servers, pass only the serverId.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
"applications": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Description: "This field is a json array of app-connector-id only.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
"app_connector_groups": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Description: "List of app-connector IDs.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
},
}
}
func resourceServerGroupCreate(d *schema.ResourceData, m interface{}) error {
zClient := m.(*Client)
req := expandServerGroup(d)
log.Printf("[INFO] Creating zpa server group with request\n%+v\n", req)
if len(req.Servers) > 0 && req.DynamicDiscovery {
log.Printf("[ERROR] An application server can only be attached to a server when DynamicDiscovery is disabled\n")
return fmt.Errorf("an application server can only be attached to a server when DynamicDiscovery is disabled")
}
if !req.DynamicDiscovery && len(req.Servers) == 0 {
log.Printf("[ERROR] Servers must not be empty when DynamicDiscovery is disabled\n")
return fmt.Errorf("servers must not be empty when DynamicDiscovery is disabled")
}
resp, _, err := zClient.servergroup.Create(&req)
if err != nil {
return err
}
log.Printf("[INFO] Created server group request. ID: %v\n", resp)
d.SetId(resp.ID)
return resourceServerGroupRead(d, m)
}
func resourceServerGroupRead(d *schema.ResourceData, m interface{}) error {
zClient := m.(*Client)
resp, _, err := zClient.servergroup.Get(d.Id())
if err != nil {
if err.(*client.ErrorResponse).IsObjectNotFound() {
log.Printf("[WARN] Removing server group %s from state because it no longer exists in ZPA", d.Id())
d.SetId("")
return nil
}
return err
}
log.Printf("[INFO] Getting server group:\n%+v\n", resp)
d.SetId(resp.ID)
_ = d.Set("config_space", resp.ConfigSpace)
_ = d.Set("description", resp.Description)
_ = d.Set("enabled", resp.Enabled)
_ = d.Set("ip_anchored", resp.IpAnchored)
_ = d.Set("dynamic_discovery", resp.DynamicDiscovery)
_ = d.Set("enabled", resp.Enabled)
_ = d.Set("name", resp.Name)
_ = d.Set("app_connector_groups", flattenAppConnectorGroupsSimple(resp.AppConnectorGroups))
_ = d.Set("applications", flattenServerGroupApplicationsSimple(resp.Applications))
_ = d.Set("servers", flattenServers(resp.Servers))
return nil
}
func flattenAppConnectorGroupsSimple(appConnectorGroups []servergroup.AppConnectorGroups) []interface{} {
result := make([]interface{}, 1)
mapIds := make(map[string]interface{})
ids := make([]string, len(appConnectorGroups))
for i, group := range appConnectorGroups {
ids[i] = group.ID
}
mapIds["id"] = ids
result[0] = mapIds
return result
}
func flattenServerGroupApplicationsSimple(apps []servergroup.Applications) []interface{} {
result := make([]interface{}, 1)
mapIds := make(map[string]interface{})
ids := make([]string, len(apps))
for i, app := range apps {
ids[i] = app.ID
}
mapIds["id"] = ids
result[0] = mapIds
return result
}
func resourceServerGroupUpdate(d *schema.ResourceData, m interface{}) error {
zClient := m.(*Client)
id := d.Id()
log.Printf("[INFO] Updating server group ID: %v\n", id)
req := expandServerGroup(d)
if (d.HasChange("servers") || d.HasChange("dynamic_discovery")) && req.DynamicDiscovery && len(req.Servers) > 0 {
log.Printf("[ERROR] Can't update the server group: an application server can only be attached to a server when DynamicDiscovery is disabled\n")
return fmt.Errorf("can't perform the changes: an application server can only be attached to a server when DynamicDiscovery is disabled")
}
if (d.HasChange("servers") || d.HasChange("dynamic_discovery")) && !req.DynamicDiscovery && len(req.Servers) == 0 {
log.Printf("[ERROR] Can't update server group: servers must not be empty when DynamicDiscovery is disabled\n")
return fmt.Errorf("can't update server group: servers must not be empty when DynamicDiscovery is disabled")
}
if _, _, err := zClient.servergroup.Get(id); err != nil {
if respErr, ok := err.(*client.ErrorResponse); ok && respErr.IsObjectNotFound() {
d.SetId("")
return nil
}
}
if _, err := zClient.servergroup.Update(id, &req); err != nil {
return err
}
return resourceServerGroupRead(d, m)
}
func detachServerGroupFromAllAccessPolicyRules(id string, zClient *Client) {
accessPolicySet, _, err := zClient.policysetcontroller.GetByPolicyType("ACCESS_POLICY")
if err != nil {
return
}
accessPolicyRules, _, err := zClient.policysetcontroller.GetAllByType("ACCESS_POLICY")
if err != nil {
return
}
for _, accessPolicyRule := range accessPolicyRules {
ids := []policysetcontroller.AppServerGroups{}
for _, app := range accessPolicyRule.AppServerGroups {
if app.ID == id {
continue
}
ids = append(ids, policysetcontroller.AppServerGroups{
ID: app.ID,
})
}
accessPolicyRule.AppServerGroups = ids
if _, err := zClient.policysetcontroller.Update(accessPolicySet.ID, accessPolicyRule.ID, &accessPolicyRule); err != nil {
continue
}
}
}
func detachServerGroupFromAllAppSegments(id string, zClient *Client) {
apps, _, err := zClient.applicationsegment.GetAll()
if err != nil {
return
}
for _, app := range apps {
ids := []applicationsegment.AppServerGroups{}
for _, app := range app.ServerGroups {
if app.ID == id {
continue
}
ids = append(ids, applicationsegment.AppServerGroups{
ID: app.ID,
})
}
app.ServerGroups = ids
if _, err := zClient.applicationsegment.Update(app.ID, app); err != nil {
continue
}
}
}
func resourceServerGroupDelete(d *schema.ResourceData, m interface{}) error {
zClient := m.(*Client)
log.Printf("[INFO] Deleting server group ID: %v\n", d.Id())
err := detachServerGroupFromAppConnectorGroups(zClient, d.Id())
if err != nil {
log.Printf("[ERROR] Detaching server group ID: %v from app connector groups failed:%v\n", d.Id(), err)
}
detachServerGroupFromAllAccessPolicyRules(d.Id(), zClient)
detachServerGroupFromAllAppSegments(d.Id(), zClient)
if _, err := zClient.servergroup.Delete(d.Id()); err != nil {
return err
}
d.SetId("")
log.Printf("[INFO] server group deleted")
return nil
}
func detachServerGroupFromAppConnectorGroups(client *Client, serverGroupID string) error {
log.Printf("[INFO] Detaching Server Group %s from App Connector Groups\n", serverGroupID)
serverGroup, _, err := client.servergroup.Get(serverGroupID)
if err != nil {
return err
}
// lock to avoid updating app connector group with a deleted server group ID when running in parallel
detachLock.Lock()
defer detachLock.Unlock()
for _, appConnectorGroup := range serverGroup.AppConnectorGroups {
app, _, err := client.appconnectorgroup.Get(appConnectorGroup.ID)
if err != nil {
continue
}
appServerGroups := []appconnectorgroup.AppServerGroup{}
for _, s := range app.AppServerGroup {
if s.ID == serverGroupID {
continue
}
appServerGroups = append(appServerGroups, s)
}
app.AppServerGroup = appServerGroups
_, err = client.appconnectorgroup.Update(app.ID, app)
if err != nil {
continue
}
}
return nil
}
func expandServerGroup(d *schema.ResourceData) servergroup.ServerGroup {
result := servergroup.ServerGroup{
ID: d.Id(),
Enabled: d.Get("enabled").(bool),
Description: d.Get("description").(string),
IpAnchored: d.Get("ip_anchored").(bool),
ConfigSpace: d.Get("config_space").(string),
DynamicDiscovery: d.Get("dynamic_discovery").(bool),
AppConnectorGroups: expandAppConnectorGroups(d),
Applications: expandServerGroupApplications(d),
Servers: expandApplicationServers(d),
}
if d.HasChange("name") {
result.Name = d.Get("name").(string)
}
return result
}
func expandAppConnectorGroups(d *schema.ResourceData) []servergroup.AppConnectorGroups {
appConnectorGroupsInterface, ok := d.GetOk("app_connector_groups")
if ok {
appConnector := appConnectorGroupsInterface.(*schema.Set)
log.Printf("[INFO] app connector groups data: %+v\n", appConnector)
var appConnectorGroups []servergroup.AppConnectorGroups
for _, appConnectorGroup := range appConnector.List() {
appConnectorGroup, ok := appConnectorGroup.(map[string]interface{})
if ok {
for _, id := range appConnectorGroup["id"].(*schema.Set).List() {
appConnectorGroups = append(appConnectorGroups, servergroup.AppConnectorGroups{
ID: id.(string),
})
}
}
}
return appConnectorGroups
}
return []servergroup.AppConnectorGroups{}
}
func expandServerGroupApplications(d *schema.ResourceData) []servergroup.Applications {
serverGroupAppsInterface, ok := d.GetOk("applications")
if ok {
serverGroupApp := serverGroupAppsInterface.(*schema.Set)
log.Printf("[INFO] server group application data: %+v\n", serverGroupApp)
var serverGroupApps []servergroup.Applications
for _, serverGroupApp := range serverGroupApp.List() {
serverGroupApp, ok := serverGroupApp.(map[string]interface{})
if ok {
for _, id := range serverGroupApp["id"].([]interface{}) {
serverGroupApps = append(serverGroupApps, servergroup.Applications{
ID: id.(string),
})
}
}
}
return serverGroupApps
}
return []servergroup.Applications{}
}
func expandApplicationServers(d *schema.ResourceData) []servergroup.ApplicationServer {
applicationServersInterface, ok := d.GetOk("servers")
if ok {
applicationServer := applicationServersInterface.(*schema.Set)
log.Printf("[INFO] server group application data: %+v\n", applicationServer)
var applicationServers []servergroup.ApplicationServer
for _, applicationServer := range applicationServer.List() {
applicationServer, _ := applicationServer.(map[string]interface{})
if applicationServer != nil {
for _, id := range applicationServer["id"].([]interface{}) {
applicationServers = append(applicationServers, servergroup.ApplicationServer{
ID: id.(string),
})
}
}
}
return applicationServers
}
return []servergroup.ApplicationServer{}
}