-
Notifications
You must be signed in to change notification settings - Fork 14
/
resource_zpa_service_edge_group.go
372 lines (344 loc) · 11.6 KB
/
resource_zpa_service_edge_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
package zpa
import (
"log"
"strconv"
"strings"
"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/serviceedgegroup"
)
func resourceServiceEdgeGroup() *schema.Resource {
return &schema.Resource{
Create: resourceServiceEdgeGroupCreate,
Read: resourceServiceEdgeGroupRead,
Update: resourceServiceEdgeGroupUpdate,
Delete: resourceServiceEdgeGroupDelete,
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.serviceedgegroup.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{
"id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "Name of the Service Edge Group.",
},
"city_country": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"country_code": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: "Description of the Service Edge Group.",
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Whether this Service Edge Group is enabled or not.",
},
"is_public": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Enable or disable public access for the Service Edge Group.",
},
"latitude": {
Type: schema.TypeString,
Required: true,
ValidateFunc: ValidateLatitude,
DiffSuppressFunc: DiffSuppressFuncCoordinate,
Description: "Latitude for the Service Edge Group.",
// ValidateFunc: ValidateStringFloatBetween(-90, 90),
},
"location": {
Type: schema.TypeString,
Required: true,
Description: "Location for the Service Edge Group.",
},
"longitude": {
Type: schema.TypeString,
Required: true,
ValidateFunc: ValidateLongitude,
DiffSuppressFunc: DiffSuppressFuncCoordinate,
Description: "Longitude for the Service Edge Group.",
// ValidateFunc: ValidateStringFloatBetween(-180.0, 180.0),
},
"override_version_profile": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Whether the default version profile of the App Connector Group is applied or overridden.",
},
"service_edges": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
"trusted_networks": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Description: "List of trusted network IDs.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
"upgrade_day": {
Type: schema.TypeString,
Optional: true,
Default: "SUNDAY",
Description: "Service Edges in this group will attempt to update to a newer version of the software during this specified day.",
},
"upgrade_time_in_secs": {
Type: schema.TypeString,
Optional: true,
Default: "66600",
Description: "Service Edges in this group will attempt to update to a newer version of the software during this specified time.",
},
"version_profile_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "ID of the version profile.",
ValidateFunc: validation.StringInSlice([]string{
"0", "1", "2",
}, false),
},
"version_profile_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "ID of the version profile.",
ValidateFunc: validation.StringInSlice([]string{
"Default", "Previous Default", "New Release",
}, false),
},
"version_profile_visibility_scope": {
Type: schema.TypeString,
Computed: true,
Description: "ID of the version profile.",
},
},
}
}
func resourceServiceEdgeGroupCreate(d *schema.ResourceData, m interface{}) error {
zClient := m.(*Client)
if err := validateAndSetProfileNameID(d); err != nil {
return err
}
req := expandServiceEdgeGroup(d)
log.Printf("[INFO] Creating zpa service edge group with request\n%+v\n", req)
resp, _, err := zClient.serviceedgegroup.Create(req)
if err != nil {
return err
}
log.Printf("[INFO] Created service edge group request. ID: %v\n", resp)
d.SetId(resp.ID)
return resourceServiceEdgeGroupRead(d, m)
}
func resourceServiceEdgeGroupRead(d *schema.ResourceData, m interface{}) error {
zClient := m.(*Client)
resp, _, err := zClient.serviceedgegroup.Get(d.Id())
if err != nil {
if err.(*client.ErrorResponse).IsObjectNotFound() {
log.Printf("[WARN] Removing service edge group %s from state because it no longer exists in ZPA", d.Id())
d.SetId("")
return nil
}
return err
}
log.Printf("[INFO] Getting service edge group:\n%+v\n", resp)
d.SetId(resp.ID)
isPublic, _ := strconv.ParseBool(resp.IsPublic)
_ = d.Set("name", resp.Name)
_ = d.Set("city_country", resp.CityCountry)
_ = d.Set("country_code", resp.CountryCode)
_ = d.Set("description", resp.Description)
_ = d.Set("enabled", resp.Enabled)
_ = d.Set("is_public", isPublic)
_ = d.Set("latitude", resp.Latitude)
_ = d.Set("longitude", resp.Longitude)
_ = d.Set("location", resp.Location)
_ = d.Set("upgrade_day", resp.UpgradeDay)
_ = d.Set("upgrade_time_in_secs", resp.UpgradeTimeInSecs)
_ = d.Set("override_version_profile", resp.OverrideVersionProfile)
_ = d.Set("version_profile_id", resp.VersionProfileID)
_ = d.Set("version_profile_name", resp.VersionProfileName)
_ = d.Set("version_profile_visibility_scope", resp.VersionProfileVisibilityScope)
// _ = d.Set("trusted_networks", flattenTrustedNetworks(resp))
_ = d.Set("trusted_networks", flattenAppTrustedNetworksSimple(resp.TrustedNetworks))
_ = d.Set("service_edges", flattenServiceEdges(resp))
return nil
}
func flattenAppTrustedNetworksSimple(trustedNetworks []serviceedgegroup.TrustedNetworks) []interface{} {
result := make([]interface{}, 1)
mapIds := make(map[string]interface{})
ids := make([]string, len(trustedNetworks))
for i, networks := range trustedNetworks {
ids[i] = networks.ID
}
mapIds["id"] = ids
result[0] = mapIds
return result
}
func resourceServiceEdgeGroupUpdate(d *schema.ResourceData, m interface{}) error {
zClient := m.(*Client)
if err := validateAndSetProfileNameID(d); err != nil {
return err
}
id := d.Id()
log.Printf("[INFO] Updating service edge group ID: %v\n", id)
req := expandServiceEdgeGroup(d)
if _, _, err := zClient.serviceedgegroup.Get(id); err != nil {
if respErr, ok := err.(*client.ErrorResponse); ok && respErr.IsObjectNotFound() {
d.SetId("")
return nil
}
}
if _, err := zClient.serviceedgegroup.Update(id, &req); err != nil {
return err
}
return resourceServiceEdgeGroupRead(d, m)
}
func resourceServiceEdgeGroupDelete(d *schema.ResourceData, m interface{}) error {
zClient := m.(*Client)
log.Printf("[INFO] Deleting service edge group ID: %v\n", d.Id())
if _, err := zClient.serviceedgegroup.Delete(d.Id()); err != nil {
return err
}
d.SetId("")
log.Printf("[INFO] service edge group deleted")
return nil
}
func expandServiceEdgeGroup(d *schema.ResourceData) serviceedgegroup.ServiceEdgeGroup {
serviceEdgeGroup := serviceedgegroup.ServiceEdgeGroup{
ID: d.Get("id").(string),
Name: d.Get("name").(string),
CityCountry: d.Get("city_country").(string),
CountryCode: d.Get("country_code").(string),
Description: d.Get("description").(string),
Enabled: d.Get("enabled").(bool),
IsPublic: strings.ToUpper(strconv.FormatBool(d.Get("is_public").(bool))),
Latitude: d.Get("latitude").(string),
Location: d.Get("location").(string),
Longitude: d.Get("longitude").(string),
UpgradeDay: d.Get("upgrade_day").(string),
UpgradeTimeInSecs: d.Get("upgrade_time_in_secs").(string),
VersionProfileID: d.Get("version_profile_id").(string),
VersionProfileName: d.Get("version_profile_name").(string),
VersionProfileVisibilityScope: d.Get("version_profile_visibility_scope").(string),
OverrideVersionProfile: d.Get("override_version_profile").(bool),
ServiceEdges: expandServiceEdges(d),
TrustedNetworks: expandTrustedNetworks(d),
}
return serviceEdgeGroup
}
func expandServiceEdges(d *schema.ResourceData) []serviceedgegroup.ServiceEdges {
serviceEdgesGroupInterface, ok := d.GetOk("service_edges")
if ok {
serviceEdge := serviceEdgesGroupInterface.(*schema.Set)
log.Printf("[INFO] service edges data: %+v\n", serviceEdge)
var serviceEdgesGroups []serviceedgegroup.ServiceEdges
for _, serviceEdgesGroup := range serviceEdge.List() {
serviceEdgesGroup, ok := serviceEdgesGroup.(map[string]interface{})
if ok {
for _, id := range serviceEdgesGroup["id"].([]interface{}) {
serviceEdgesGroups = append(serviceEdgesGroups, serviceedgegroup.ServiceEdges{
ID: id.(string),
})
}
}
}
return serviceEdgesGroups
}
return []serviceedgegroup.ServiceEdges{}
}
/*
func expandTrustedNetworks(d *schema.ResourceData) []serviceedgegroup.TrustedNetworks {
trustedNetworksInterface, ok := d.GetOk("trusted_networks")
if ok {
network := trustedNetworksInterface.(*schema.Set)
log.Printf("[INFO] trusted network data: %+v\n", network)
var trustedNetworks []serviceedgegroup.TrustedNetworks
for _, trustedNetwork := range network.List() {
trustedNetwork, ok := trustedNetwork.(map[string]interface{})
if ok {
for _, id := range trustedNetwork["id"].([]interface{}) {
trustedNetworks = append(trustedNetworks, serviceedgegroup.TrustedNetworks{
ID: id.(string),
})
}
}
}
return trustedNetworks
}
return []serviceedgegroup.TrustedNetworks{}
}
*/
func expandTrustedNetworks(d *schema.ResourceData) []serviceedgegroup.TrustedNetworks {
trustedNetworksInterface, ok := d.GetOk("trusted_networks")
if ok {
trusteNetwork := trustedNetworksInterface.(*schema.Set)
log.Printf("[INFO] trusted network data: %+v\n", trusteNetwork)
var trusteNetworks []serviceedgegroup.TrustedNetworks
for _, trusteNetwork := range trusteNetwork.List() {
trusteNetwork, _ := trusteNetwork.(map[string]interface{})
if trusteNetwork != nil {
for _, id := range trusteNetwork["id"].([]interface{}) {
trusteNetworks = append(trusteNetworks, serviceedgegroup.TrustedNetworks{
ID: id.(string),
})
}
}
}
return trusteNetworks
}
return []serviceedgegroup.TrustedNetworks{}
}