-
Notifications
You must be signed in to change notification settings - Fork 14
/
resource_zpa_lss_config_controller.go
447 lines (415 loc) · 13.5 KB
/
resource_zpa_lss_config_controller.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
package zpa
import (
"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/lssconfigcontroller"
)
func getPolicyRuleResourceSchema() map[string]*schema.Schema {
return MergeSchema(
CommonPolicySchema(), map[string]*schema.Schema{
"action": {
Type: schema.TypeString,
Optional: true,
Description: " This is for providing the rule action.",
ValidateFunc: validation.StringInSlice([]string{
"ALLOW",
"DENY",
}, false),
},
"policy_set_id": {
Type: schema.TypeString,
Optional: true,
},
"conditions": {
Type: schema.TypeList,
Optional: true,
Description: "This is for proviidng the set of conditions for the policy.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"negated": {
Type: schema.TypeBool,
Optional: true,
},
"operator": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"AND",
"OR",
}, false),
},
"operands": {
Type: schema.TypeList,
Optional: true,
Description: "This signifies the various policy criteria.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"values": {
Type: schema.TypeSet,
Optional: true,
Description: "This denotes a list of values for the given object type. The value depend upon the key. If rhs is defined this list will be ignored",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"object_type": {
Type: schema.TypeString,
Required: true,
Description: " This is for specifying the policy critiera.",
ValidateFunc: validation.StringInSlice([]string{
"APP",
"APP_GROUP",
"CLIENT_TYPE",
}, false),
},
},
},
},
},
},
},
},
)
}
func resourceLSSConfigController() *schema.Resource {
return &schema.Resource{
Create: resourceLSSConfigControllerCreate,
Read: resourceLSSConfigControllerRead,
Update: resourceLSSConfigControllerUpdate,
Delete: resourceLSSConfigControllerDelete,
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.lssconfigcontroller.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,
},
"policy_rule_id": {
Type: schema.TypeString,
Computed: true,
},
"policy_rule_resource": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: getPolicyRuleResourceSchema(),
},
},
"connector_groups": {
Type: schema.TypeSet,
Optional: true,
Description: "App Connector Group(s) to be added to the LSS configuration",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
},
},
},
},
"config": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"audit_message": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: "Description of the LSS configuration",
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Whether this LSS configuration is enabled or not. Supported values: true, false",
},
"filter": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Description: "Filter for the LSS configuration. Format given by the following API to get status codes: /mgmtconfig/v2/admin/lssConfig/statusCodes",
},
"format": {
Type: schema.TypeString,
Required: true,
Description: "Format of the log type. Format given by the following API to get formats: /mgmtconfig/v2/admin/lssConfig/logType/formats",
},
"id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
Description: "Name of the LSS configuration",
},
"lss_host": {
Type: schema.TypeString,
Required: true,
Description: "Host of the LSS configuration",
},
"lss_port": {
Type: schema.TypeString,
Required: true,
Description: "Port of the LSS configuration",
},
"source_log_type": {
Type: schema.TypeString,
Required: true,
Description: "Log type of the LSS configuration",
ValidateFunc: validation.StringInSlice([]string{
"zpn_trans_log",
"zpn_auth_log",
"zpn_ast_auth_log",
"zpn_http_trans_log",
"zpn_audit_log",
"zpn_ast_comprehensive_stats",
"zpn_sys_auth_log",
"zpn_waf_http_exchanges_log",
"zpn_pbroker_comprehensive_stats",
}, false),
},
"use_tls": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
},
},
},
}
}
func resourceLSSConfigControllerCreate(d *schema.ResourceData, m interface{}) error {
zClient := m.(*Client)
req := expandLSSResource(d)
log.Printf("[INFO] Creating zpa lss config controller with request\n%+v\n", req)
resp, _, err := zClient.lssconfigcontroller.Create(&req)
if err != nil {
return err
}
log.Printf("[INFO] Created lss config controller request. ID: %v\n", resp)
d.SetId(resp.ID)
return resourceLSSConfigControllerRead(d, m)
}
func resourceLSSConfigControllerRead(d *schema.ResourceData, m interface{}) error {
zClient := m.(*Client)
resp, _, err := zClient.lssconfigcontroller.Get(d.Id())
if err != nil {
if err.(*client.ErrorResponse).IsObjectNotFound() {
log.Printf("[WARN] Removing lss config controller %s from state because it no longer exists in ZPA", d.Id())
d.SetId("")
return nil
}
return err
}
log.Printf("[INFO] Getting lss config controller:\n%+v\n", resp)
d.SetId(resp.ID)
if resp.PolicyRule != nil {
_ = d.Set("policy_rule_id", resp.PolicyRule.ID)
}
_ = d.Set("config", flattenLSSConfig(resp.LSSConfig))
_ = d.Set("connector_groups", flattenConnectorGroupsSimple(resp.ConnectorGroups))
return nil
}
func resourceLSSConfigControllerUpdate(d *schema.ResourceData, m interface{}) error {
zClient := m.(*Client)
id := d.Id()
log.Printf("[INFO] Updating lss config controller ID: %v\n", id)
req := expandLSSResource(d)
if _, _, err := zClient.lssconfigcontroller.Get(id); err != nil {
if respErr, ok := err.(*client.ErrorResponse); ok && respErr.IsObjectNotFound() {
d.SetId("")
return nil
}
}
if _, err := zClient.lssconfigcontroller.Update(id, &req); err != nil {
return err
}
return resourceLSSConfigControllerRead(d, m)
}
func resourceLSSConfigControllerDelete(d *schema.ResourceData, m interface{}) error {
zClient := m.(*Client)
log.Printf("[INFO] Deleting lss config controller ID: %v\n", d.Id())
if _, err := zClient.lssconfigcontroller.Delete(d.Id()); err != nil {
return err
}
d.SetId("")
log.Printf("[INFO] lss config controller deleted")
return nil
}
func expandLSSResource(d *schema.ResourceData) lssconfigcontroller.LSSResource {
policy, err := expandPolicyRuleResource(d)
if err != nil {
log.Printf("[ERROR] failed reading policy rule resource: %v\n", err)
}
req := lssconfigcontroller.LSSResource{
ID: d.Get("id").(string),
PolicyRuleResource: policy,
LSSConfig: expandLSSConfigController(d),
ConnectorGroups: expandConnectorGroups(d),
}
return req
}
func expandPolicyRuleResource(d *schema.ResourceData) (*lssconfigcontroller.PolicyRuleResource, error) {
policyObj, ok := d.GetOk("policy_rule_resource")
if !ok {
return nil, nil
}
policyList := policyObj.([]interface{})
if len(policyList) == 0 {
return nil, nil
}
polictSet := policyList[0].(map[string]interface{})
conditions, err := ExpandPolicyRuleResourceConditions(polictSet)
if err != nil {
return nil, err
}
return &lssconfigcontroller.PolicyRuleResource{
Action: polictSet["action"].(string),
ActionID: polictSet["action_id"].(string),
CustomMsg: polictSet["custom_msg"].(string),
DefaultRule: polictSet["default_rule"].(bool),
Description: polictSet["description"].(string),
ID: polictSet["id"].(string),
Name: polictSet["name"].(string),
Operator: polictSet["operator"].(string),
PolicyType: polictSet["policy_type"].(string),
Priority: polictSet["priority"].(string),
ReauthDefaultRule: polictSet["reauth_default_rule"].(bool),
ReauthIdleTimeout: polictSet["reauth_idle_timeout"].(string),
ReauthTimeout: polictSet["reauth_timeout"].(string),
RuleOrder: polictSet["rule_order"].(string),
LssDefaultRule: polictSet["lss_default_rule"].(bool),
Conditions: conditions,
}, nil
}
func ExpandPolicyRuleResourceConditions(d map[string]interface{}) ([]lssconfigcontroller.PolicyRuleResourceConditions, error) {
conditionInterface, ok := d["conditions"]
if ok {
conditions := conditionInterface.([]interface{})
log.Printf("[INFO] conditions data: %+v\n", conditions)
var conditionSets []lssconfigcontroller.PolicyRuleResourceConditions
for _, condition := range conditions {
conditionSet, _ := condition.(map[string]interface{})
if conditionSet != nil {
operands, err := expandPolicyRuleResourceOperandsList(conditionSet["operands"])
if err != nil {
return nil, err
}
conditionSets = append(conditionSets, lssconfigcontroller.PolicyRuleResourceConditions{
Negated: conditionSet["negated"].(bool),
Operator: conditionSet["operator"].(string),
Operands: &operands,
})
}
}
return conditionSets, nil
}
return []lssconfigcontroller.PolicyRuleResourceConditions{}, nil
}
func expandPolicyRuleResourceOperandsList(ops interface{}) ([]lssconfigcontroller.PolicyRuleResourceOperands, error) {
if ops != nil {
operands := ops.([]interface{})
log.Printf("[INFO] operands data: %+v\n", operands)
var operandsSets []lssconfigcontroller.PolicyRuleResourceOperands
for _, operand := range operands {
operandSet, _ := operand.(map[string]interface{})
valuesSet := operandSet["values"].(*schema.Set)
op := lssconfigcontroller.PolicyRuleResourceOperands{
Values: SetToStringSlice(valuesSet),
ObjectType: operandSet["object_type"].(string),
}
operandsSets = append(operandsSets, op)
}
return operandsSets, nil
}
return []lssconfigcontroller.PolicyRuleResourceOperands{}, nil
}
func expandLSSConfigController(d *schema.ResourceData) *lssconfigcontroller.LSSConfig {
configInterface, ok := d.GetOk("config")
if ok {
configList := configInterface.([]interface{})
if len(configList) == 0 {
return nil
}
config, _ := configList[0].(map[string]interface{})
filterSet, _ := config["filter"].(*schema.Set)
return &lssconfigcontroller.LSSConfig{
ID: d.Get("id").(string),
AuditMessage: config["audit_message"].(string),
Description: config["description"].(string),
Enabled: config["enabled"].(bool),
Filter: SetToStringSlice(filterSet),
Format: config["format"].(string),
Name: config["name"].(string),
LSSHost: config["lss_host"].(string),
LSSPort: config["lss_port"].(string),
SourceLogType: config["source_log_type"].(string),
UseTLS: config["use_tls"].(bool),
}
}
return nil
}
func expandConnectorGroups(d *schema.ResourceData) []lssconfigcontroller.ConnectorGroups {
appConnectorGroupsInterface, ok := d.GetOk("connector_groups")
if ok {
appConnector := appConnectorGroupsInterface.(*schema.Set)
log.Printf("[INFO] connector groups data: %+v\n", appConnector)
var appConnectorGroups []lssconfigcontroller.ConnectorGroups
for _, appConnectorGroup := range appConnector.List() {
appConnectorGroup, ok := appConnectorGroup.(map[string]interface{})
if ok {
for _, id := range appConnectorGroup["id"].([]interface{}) {
appConnectorGroups = append(appConnectorGroups, lssconfigcontroller.ConnectorGroups{
ID: id.(string),
})
}
}
}
return appConnectorGroups
}
return []lssconfigcontroller.ConnectorGroups{}
}
func flattenConnectorGroupsSimple(lssConnectorGroup []lssconfigcontroller.ConnectorGroups) []interface{} {
result := make([]interface{}, 1)
mapIds := make(map[string]interface{})
ids := make([]string, len(lssConnectorGroup))
for i, item := range lssConnectorGroup {
ids[i] = item.ID
}
mapIds["id"] = ids
result[0] = mapIds
return result
}