-
Notifications
You must be signed in to change notification settings - Fork 14
/
resource_zpa_policy_access_timeout_rule.go
195 lines (175 loc) · 6.11 KB
/
resource_zpa_policy_access_timeout_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
package zpa
import (
"fmt"
"log"
"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/policysetcontroller"
)
func resourcePolicyTimeoutRule() *schema.Resource {
return &schema.Resource{
Create: resourcePolicyTimeoutRuleCreate,
Read: resourcePolicyTimeoutRuleRead,
Update: resourcePolicyTimeoutRuleUpdate,
Delete: resourcePolicyTimeoutRuleDelete,
Importer: &schema.ResourceImporter{
StateContext: importPolicyStateContextFunc([]string{"TIMEOUT_POLICY", "REAUTH_POLICY"}),
},
Schema: MergeSchema(
CommonPolicySchema(),
map[string]*schema.Schema{
"action": {
Type: schema.TypeString,
Optional: true,
Description: " This is for providing the rule action.",
ValidateFunc: validation.StringInSlice([]string{
"RE_AUTH",
}, false),
},
"conditions": GetPolicyConditionsSchema([]string{
"APP",
"APP_GROUP",
"CLIENT_TYPE",
"CLOUD_CONNECTOR_GROUP",
"IDP",
"POSTURE",
"SAML",
"SCIM",
"SCIM_GROUP",
"TRUSTED_NETWORK",
}),
},
),
}
}
func resourcePolicyTimeoutRuleCreate(d *schema.ResourceData, m interface{}) error {
zClient := m.(*Client)
req, err := expandCreatePolicyTimeoutRule(d)
if err != nil {
return err
}
log.Printf("[INFO] Creating zpa policy timeout rule with request\n%+v\n", req)
if ValidateConditions(req.Conditions, zClient) {
policysetcontroller, _, err := zClient.policysetcontroller.Create(req)
if err != nil {
return err
}
d.SetId(policysetcontroller.ID)
order, ok := d.GetOk("rule_order")
if ok {
reorder(order, policysetcontroller.PolicySetID, "TIMEOUT_POLICY", policysetcontroller.ID, zClient)
}
return resourcePolicyTimeoutRuleRead(d, m)
} else {
return fmt.Errorf("couldn't validate the zpa policy timeout (%s) operands, please make sure you are using valid inputs for APP type, LHS & RHS", req.Name)
}
}
func resourcePolicyTimeoutRuleRead(d *schema.ResourceData, m interface{}) error {
zClient := m.(*Client)
globalPolicySet, _, err := zClient.policysetcontroller.GetByPolicyType("TIMEOUT_POLICY")
if err != nil {
return err
}
log.Printf("[INFO] Getting Policy Set Rule: globalPolicySet:%s id: %s\n", globalPolicySet.ID, d.Id())
resp, _, err := zClient.policysetcontroller.GetPolicyRule(globalPolicySet.ID, d.Id())
if err != nil {
if obj, ok := err.(*client.ErrorResponse); ok && obj.IsObjectNotFound() {
log.Printf("[WARN] Removing policy rule %s from state because it no longer exists in ZPA", d.Id())
d.SetId("")
return nil
}
return err
}
log.Printf("[INFO] Got Policy Set Timeout Rule:\n%+v\n", resp)
d.SetId(resp.ID)
_ = d.Set("action", resp.Action)
_ = d.Set("action_id", resp.ActionID)
_ = d.Set("custom_msg", resp.CustomMsg)
_ = d.Set("description", resp.Description)
_ = d.Set("name", resp.Name)
_ = d.Set("bypass_default_rule", resp.BypassDefaultRule)
_ = d.Set("operator", resp.Operator)
_ = d.Set("policy_set_id", resp.PolicySetID)
_ = d.Set("policy_type", resp.PolicyType)
_ = d.Set("priority", resp.Priority)
_ = d.Set("reauth_default_rule", resp.ReauthDefaultRule)
_ = d.Set("reauth_idle_timeout", resp.ReauthIdleTimeout)
_ = d.Set("reauth_timeout", resp.ReauthTimeout)
_ = d.Set("rule_order", resp.RuleOrder)
_ = d.Set("conditions", flattenPolicyConditions(resp.Conditions))
return nil
}
func resourcePolicyTimeoutRuleUpdate(d *schema.ResourceData, m interface{}) error {
zClient := m.(*Client)
globalPolicySet, _, err := zClient.policysetcontroller.GetByPolicyType("TIMEOUT_POLICY")
if err != nil {
return err
}
ruleID := d.Id()
log.Printf("[INFO] Updating policy timeout rule ID: %v\n", ruleID)
req, err := expandCreatePolicyRule(d)
if err != nil {
return err
}
if ValidateConditions(req.Conditions, zClient) {
if _, _, err := zClient.policysetcontroller.GetPolicyRule(globalPolicySet.ID, ruleID); err != nil {
if respErr, ok := err.(*client.ErrorResponse); ok && respErr.IsObjectNotFound() {
d.SetId("")
return nil
}
}
if _, err := zClient.policysetcontroller.Update(globalPolicySet.ID, ruleID, req); err != nil {
return err
}
if d.HasChange("rule_order") {
order, ok := d.GetOk("rule_order")
if ok {
reorder(order, globalPolicySet.ID, "TIMEOUT_POLICY", ruleID, zClient)
}
}
return resourcePolicyTimeoutRuleRead(d, m)
} else {
return fmt.Errorf("couldn't validate the zpa policy timeout (%s) operands, please make sure you are using valid inputs for APP type, LHS & RHS", req.Name)
}
}
func resourcePolicyTimeoutRuleDelete(d *schema.ResourceData, m interface{}) error {
zClient := m.(*Client)
globalPolicySet, _, err := zClient.policysetcontroller.GetByPolicyType("TIMEOUT_POLICY")
if err != nil {
return err
}
log.Printf("[INFO] Deleting policy timeout rule with id %v\n", d.Id())
if _, err := zClient.policysetcontroller.Delete(globalPolicySet.ID, d.Id()); err != nil {
return err
}
return nil
}
func expandCreatePolicyTimeoutRule(d *schema.ResourceData) (*policysetcontroller.PolicyRule, error) {
policySetID, ok := d.Get("policy_set_id").(string)
if !ok {
return nil, fmt.Errorf("policy_set_id is not set")
}
log.Printf("[INFO] action_id:%v\n", d.Get("action_id"))
conditions, err := ExpandPolicyConditions(d)
if err != nil {
return nil, err
}
return &policysetcontroller.PolicyRule{
Action: d.Get("action").(string),
ActionID: d.Get("action_id").(string),
CustomMsg: d.Get("custom_msg").(string),
Description: d.Get("description").(string),
ID: d.Get("id").(string),
Name: d.Get("name").(string),
Operator: d.Get("operator").(string),
PolicySetID: policySetID,
PolicyType: d.Get("policy_type").(string),
Priority: d.Get("priority").(string),
ReauthDefaultRule: d.Get("reauth_default_rule").(bool),
ReauthIdleTimeout: d.Get("reauth_idle_timeout").(string),
ReauthTimeout: d.Get("reauth_timeout").(string),
RuleOrder: d.Get("rule_order").(string),
Conditions: conditions,
}, nil
}