-
Notifications
You must be signed in to change notification settings - Fork 14
/
resource_zpa_server_group.go
334 lines (306 loc) · 10.3 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
package zpa
import (
"fmt"
"log"
"strconv"
"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/servergroup"
)
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 resourceServerGroupDelete(d *schema.ResourceData, m interface{}) error {
zClient := m.(*Client)
log.Printf("[INFO] Deleting server group ID: %v\n", d.Id())
if _, err := zClient.servergroup.Delete(d.Id()); err != nil {
return err
}
d.SetId("")
log.Printf("[INFO] server group deleted")
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{}
}