-
Notifications
You must be signed in to change notification settings - Fork 16
/
resource_zpa_policy_credential_access_rule.go
308 lines (274 loc) · 8.91 KB
/
resource_zpa_policy_credential_access_rule.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
package zpa
import (
"log"
"net/http"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/zscaler/zscaler-sdk-go/v2/zpa/services/policysetcontrollerv2"
)
func resourcePolicyCredentialAccessRule() *schema.Resource {
return &schema.Resource{
Create: resourcePolicyCredentialAccessRuleCreate,
Read: resourcePolicyCredentialAccessRuleRead,
Update: resourcePolicyCredentialAccessRuleUpdate,
Delete: resourcePolicyCredentialAccessRuleDelete,
Importer: &schema.ResourceImporter{
StateContext: importPolicyStateContextFuncV2([]string{"CREDENTIAL_POLICY"}),
},
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "This is the name of the policy.",
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: "This is the description of the access policy.",
},
"action": {
Type: schema.TypeString,
Optional: true,
Description: " This is for providing the rule action.",
ValidateFunc: validation.StringInSlice([]string{
"INJECT_CREDENTIALS",
}, false),
},
"policy_set_id": {
Type: schema.TypeString,
Computed: true,
},
"conditions": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Description: "This is for proviidng the set of conditions for the policy.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"operator": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
"AND",
"OR",
}, false),
},
"operands": {
Type: schema.TypeList,
Optional: true,
Computed: 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,
Optional: true,
Computed: true,
Description: " This is for specifying the policy critiera.",
ValidateFunc: validation.StringInSlice([]string{
"CONSOLE",
"SAML",
"SCIM",
"SCIM_GROUP",
}, false),
},
"entry_values": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"rhs": {
Type: schema.TypeString,
Optional: true,
},
"lhs": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
},
},
},
},
},
},
"credential": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"microtenant_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}
func resourcePolicyCredentialAccessRuleCreate(d *schema.ResourceData, m interface{}) error {
client := m.(*Client)
service := client.policysetcontrollerv2.WithMicroTenant(GetString(d.Get("microtenant_id")))
// Automatically determining policy_set_id for "CREDENTIAL_POLICY"
policySetID, err := fetchPolicySetIDByType(client, "CREDENTIAL_POLICY", GetString(d.Get("microtenant_id")))
if err != nil {
return err
}
// Setting the policy_set_id for further use
d.Set("policy_set_id", policySetID)
req, err := expandCredentialPolicyRule(d, policySetID)
if err != nil {
return err
}
log.Printf("[INFO] Creating zpa policy credential rule with request\n%+v\n", req)
if err := ValidatePolicyRuleConditions(d); err != nil {
return err
}
resp, _, err := service.CreateRule(req)
if err != nil {
return err
}
d.SetId(resp.ID)
return resourcePolicyCredentialAccessRuleRead(d, m)
}
func resourcePolicyCredentialAccessRuleRead(d *schema.ResourceData, m interface{}) error {
client := m.(*Client)
microTenantID := GetString(d.Get("microtenant_id"))
policySetID, err := fetchPolicySetIDByType(client, "CREDENTIAL_POLICY", microTenantID)
if err != nil {
return err
}
service := client.policysetcontrollerv2.WithMicroTenant(microTenantID)
log.Printf("[INFO] Getting Policy Set Rule: globalPolicySet:%s id: %s\n", policySetID, d.Id())
resp, respErr, err := service.GetPolicyRule(policySetID, d.Id())
if err != nil {
// Adjust this error handling to match how your client library exposes HTTP response details
if respErr != nil && (respErr.StatusCode == 404 || respErr.StatusCode == http.StatusNotFound) {
log.Printf("[WARN] Removing policy rule %s from state because it no longer exists in ZPA", d.Id())
d.SetId("")
return nil
}
return err
}
v2PolicyRule := policysetcontrollerv2.ConvertV1ResponseToV2Request(*resp)
log.Printf("[INFO] Got Policy Set Rule:\n%+v\n", resp)
d.SetId(resp.ID)
_ = d.Set("name", v2PolicyRule.Name)
_ = d.Set("description", v2PolicyRule.Description)
_ = d.Set("action", v2PolicyRule.Action)
_ = d.Set("policy_set_id", policySetID) // Here, you're setting it based on fetched ID
_ = d.Set("microtenant_id", v2PolicyRule.MicroTenantID)
_ = d.Set("conditions", flattenConditionsV2(v2PolicyRule.Conditions))
_ = d.Set("credential", flattenCredential(resp.Credential))
return nil
}
func resourcePolicyCredentialAccessRuleUpdate(d *schema.ResourceData, m interface{}) error {
client := m.(*Client)
service := client.policysetcontrollerv2.WithMicroTenant(GetString(d.Get("microtenant_id")))
// Automatically determining policy_set_id for "CREDENTIAL_POLICY"
policySetID, err := fetchPolicySetIDByType(client, "CREDENTIAL_POLICY", GetString(d.Get("microtenant_id")))
if err != nil {
return err
}
// Setting the policy_set_id for further use
d.Set("policy_set_id", policySetID)
ruleID := d.Id()
log.Printf("[INFO] Updating policy credential rule ID: %v\n", ruleID)
req, err := expandCredentialPolicyRule(d, policySetID)
if err != nil {
return err
}
if err := ValidatePolicyRuleConditions(d); err != nil {
return err
}
// Checking the current state of the rule to handle cases where it might have been deleted outside Terraform
_, respErr, err := service.GetPolicyRule(policySetID, ruleID)
if err != nil {
if respErr != nil && (respErr.StatusCode == http.StatusNotFound) {
d.SetId("")
return nil
}
return err
}
_, err = service.UpdateRule(policySetID, ruleID, req)
if err != nil {
return err
}
return resourcePolicyCredentialAccessRuleRead(d, m)
}
func resourcePolicyCredentialAccessRuleDelete(d *schema.ResourceData, m interface{}) error {
client := m.(*Client)
microTenantID := GetString(d.Get("microtenant_id"))
// Assume "CREDENTIAL_POLICY" is the policy type for this resource. Adjust as needed.
policySetID, err := fetchPolicySetIDByType(client, "CREDENTIAL_POLICY", microTenantID)
if err != nil {
return err
}
log.Printf("[INFO] Deleting policy credential rule with id %v\n", d.Id())
service := client.policysetcontrollerv2.WithMicroTenant(microTenantID)
if _, err := service.Delete(policySetID, d.Id()); err != nil {
return err
}
return nil
}
func flattenCredential(credential *policysetcontrollerv2.Credential) []interface{} {
if credential == nil || credential.ID == "" {
return []interface{}{}
}
m := make(map[string]interface{})
m["id"] = credential.ID
return []interface{}{m}
}
func expandCredentialPolicyRule(d *schema.ResourceData, policySetID string) (*policysetcontrollerv2.PolicyRule, error) {
conditions, err := ExpandPolicyConditionsV2(d)
if err != nil {
return nil, err
}
credential := expandCredential(d)
return &policysetcontrollerv2.PolicyRule{
ID: d.Get("id").(string),
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Action: d.Get("action").(string),
MicroTenantID: d.Get("microtenant_id").(string),
PolicySetID: policySetID,
Conditions: conditions,
Credential: credential,
}, nil
}
func expandCredential(d *schema.ResourceData) *policysetcontrollerv2.Credential {
if v, ok := d.GetOk("credential"); ok && len(v.([]interface{})) > 0 {
credentialMap := v.([]interface{})[0].(map[string]interface{})
return &policysetcontrollerv2.Credential{
ID: credentialMap["id"].(string),
}
}
return nil
}