-
Notifications
You must be signed in to change notification settings - Fork 14
/
resource_zpa_policy_access_forwarding_rule.go
197 lines (177 loc) · 6.19 KB
/
resource_zpa_policy_access_forwarding_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
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 resourcePolicyForwardingRule() *schema.Resource {
return &schema.Resource{
Create: resourcePolicyForwardingRuleCreate,
Read: resourcePolicyForwardingRuleRead,
Update: resourcePolicyForwardingRuleUpdate,
Delete: resourcePolicyForwardingRuleDelete,
Importer: &schema.ResourceImporter{
StateContext: importPolicyStateContextFunc([]string{"CLIENT_FORWARDING_POLICY", "BYPASS_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{
"BYPASS",
"INTERCEPT",
"INTERCEPT_ACCESSIBLE",
}, false),
},
"conditions": GetPolicyConditionsSchema([]string{
"APP",
"APP_GROUP",
"CLIENT_TYPE",
"EDGE_CONNECTOR_GROUP",
"POSTURE",
"MACHINE_GRP",
"TRUSTED_NETWORK",
"IDP",
"SAML",
"SCIM",
"SCIM_GROUP",
}),
},
),
}
}
func resourcePolicyForwardingRuleCreate(d *schema.ResourceData, m interface{}) error {
zClient := m.(*Client)
req, err := expandCreatePolicyForwardingRule(d)
if err != nil {
return err
}
log.Printf("[INFO] Creating zpa policy rule with request\n%+v\n", req)
if err := validateAccessPolicyRuleOrder(req.RuleOrder, zClient); err != nil {
return err
}
if !ValidateConditions(req.Conditions, zClient) {
return fmt.Errorf("couldn't validate the zpa policy access forwarding rule (%s) operands, please make sure you are using valid inputs for APP type, LHS & RHS", req.Name)
}
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, "CLIENT_FORWARDING_POLICY", policysetcontroller.ID, zClient)
}
return resourcePolicyForwardingRuleRead(d, m)
}
func resourcePolicyForwardingRuleRead(d *schema.ResourceData, m interface{}) error {
zClient := m.(*Client)
globalPolicySet, _, err := zClient.policysetcontroller.GetByPolicyType("CLIENT_FORWARDING_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 Forwarding Rule:\n%+v\n", resp)
d.SetId(resp.ID)
_ = d.Set("name", resp.Name)
_ = d.Set("description", resp.Description)
_ = d.Set("action", resp.Action)
_ = d.Set("action_id", resp.ActionID)
_ = d.Set("custom_msg", resp.CustomMsg)
_ = d.Set("bypass_default_rule", resp.BypassDefaultRule)
_ = d.Set("default_rule", resp.DefaultRule)
_ = 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("rule_order", resp.RuleOrder)
_ = d.Set("conditions", flattenPolicyConditions(resp.Conditions))
return nil
}
func resourcePolicyForwardingRuleUpdate(d *schema.ResourceData, m interface{}) error {
zClient := m.(*Client)
globalPolicySet, _, err := zClient.policysetcontroller.GetByPolicyType("CLIENT_FORWARDING_POLICY")
if err != nil {
return err
}
ruleID := d.Id()
log.Printf("[INFO] Updating policy forwarding rule ID: %v\n", ruleID)
req, err := expandCreatePolicyForwardingRule(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, "CLIENT_FORWARDING_POLICY", ruleID, zClient)
}
}
return resourcePolicyForwardingRuleRead(d, m)
} else {
return fmt.Errorf("couldn't validate the zpa policy forwarding (%s) operands, please make sure you are using valid inputs for APP type, LHS & RHS", req.Name)
}
}
func resourcePolicyForwardingRuleDelete(d *schema.ResourceData, m interface{}) error {
zClient := m.(*Client)
globalPolicySet, _, err := zClient.policysetcontroller.GetByPolicyType("CLIENT_FORWARDING_POLICY")
if err != nil {
return err
}
log.Printf("[INFO] Deleting policy forwarding rule with id %v\n", d.Id())
if _, err := zClient.policysetcontroller.Delete(globalPolicySet.ID, d.Id()); err != nil {
return err
}
return nil
}
func expandCreatePolicyForwardingRule(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{
ID: d.Get("id").(string),
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Action: d.Get("action").(string),
ActionID: d.Get("action_id").(string),
CustomMsg: d.Get("custom_msg").(string),
BypassDefaultRule: d.Get("bypass_default_rule").(bool),
DefaultRule: d.Get("default_rule").(bool),
Operator: d.Get("operator").(string),
PolicySetID: policySetID,
PolicyType: d.Get("policy_type").(string),
Priority: d.Get("priority").(string),
RuleOrder: d.Get("rule_order").(string),
Conditions: conditions,
}, nil
}