-
Notifications
You must be signed in to change notification settings - Fork 14
/
data_source_zpa_inspection_custom_controls.go
209 lines (198 loc) · 5.12 KB
/
data_source_zpa_inspection_custom_controls.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
package zpa
import (
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/zscaler/zscaler-sdk-go/zpa/services/inspectioncontrol/inspection_custom_controls"
)
func dataSourceInspectionCustomControls() *schema.Resource {
return &schema.Resource{
Read: dataSourceInspectionCustomControlsRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"action": {
Type: schema.TypeString,
Computed: true,
},
"action_value": {
Type: schema.TypeString,
Computed: true,
},
"control_number": {
Type: schema.TypeString,
Computed: true,
},
"control_rule_json": {
Type: schema.TypeString,
Computed: true,
},
"control_type": {
Type: schema.TypeString,
Computed: true,
},
"creation_time": {
Type: schema.TypeString,
Computed: true,
},
"default_action": {
Type: schema.TypeString,
Computed: true,
},
"default_action_value": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"modifiedby": {
Type: schema.TypeString,
Computed: true,
},
"modified_time": {
Type: schema.TypeString,
Computed: true,
},
"paranoia_level": {
Type: schema.TypeString,
Computed: true,
},
"protocol_type": {
Type: schema.TypeString,
Computed: true,
},
"rules": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"conditions": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"lhs": {
Type: schema.TypeString,
Computed: true,
},
"op": {
Type: schema.TypeString,
Computed: true,
},
"rhs": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"names": {
Type: schema.TypeSet,
Computed: true,
Description: "Name of the rules. If rules.type is set to REQUEST_HEADERS, REQUEST_COOKIES, or RESPONSE_HEADERS, the rules.name field is required.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"type": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"severity": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
"version": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceInspectionCustomControlsRead(d *schema.ResourceData, m interface{}) error {
zClient := m.(*Client)
var resp *inspection_custom_controls.InspectionCustomControl
id, ok := d.Get("id").(string)
if ok && id != "" {
log.Printf("[INFO] Getting data for custom inspection control %s\n", id)
res, _, err := zClient.inspection_custom_controls.Get(id)
if err != nil {
return err
}
resp = res
}
name, ok := d.Get("name").(string)
if ok && name != "" {
log.Printf("[INFO] Getting data for custom inspection control name %s\n", name)
res, _, err := zClient.inspection_custom_controls.GetByName(name)
if err != nil {
return err
}
resp = res
}
if resp != nil {
d.SetId(resp.ID)
_ = d.Set("action", resp.Action)
_ = d.Set("action_value", resp.ActionValue)
_ = d.Set("control_number", resp.ControlNumber)
_ = d.Set("control_rule_json", resp.ControlRuleJson)
_ = d.Set("control_type", resp.ControlType)
_ = d.Set("creation_time", resp.CreationTime)
_ = d.Set("default_action", resp.DefaultAction)
_ = d.Set("default_action_value", resp.DefaultActionValue)
_ = d.Set("description", resp.Description)
_ = d.Set("modifiedby", resp.ModifiedBy)
_ = d.Set("modified_time", resp.ModifiedTime)
_ = d.Set("name", resp.Name)
_ = d.Set("paranoia_level", resp.ParanoiaLevel)
_ = d.Set("protocol_type", resp.ProtocolType)
_ = d.Set("severity", resp.Severity)
_ = d.Set("version", resp.Version)
_ = d.Set("type", resp.Type)
if err := d.Set("rules", flattenInspectionCustomRules(resp.Rules)); err != nil {
return err
}
} else {
return fmt.Errorf("couldn't find any custom inspection controls with name '%s' or id '%s'", name, id)
}
return nil
}
func flattenInspectionCustomRules(rule []inspection_custom_controls.Rules) []interface{} {
rules := make([]interface{}, len(rule))
for i, rule := range rule {
rules[i] = map[string]interface{}{
"conditions": flattenInspectionRuleConditions(rule),
"names": rule.Names,
"type": rule.Type,
}
}
return rules
}
func flattenInspectionRuleConditions(condition inspection_custom_controls.Rules) []interface{} {
conditions := make([]interface{}, len(condition.Conditions))
for i, val := range condition.Conditions {
conditions[i] = map[string]interface{}{
"lhs": val.LHS,
"rhs": val.RHS,
"op": val.OP,
}
}
return conditions
}