/
resource_policy.go
174 lines (147 loc) · 5.03 KB
/
resource_policy.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
package border0
import (
"context"
"encoding/json"
"log"
"reflect"
"strings"
border0client "github.com/borderzero/border0-go/client"
"github.com/borderzero/terraform-provider-border0/internal/diagnostics"
"github.com/borderzero/terraform-provider-border0/internal/schemautil"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourcePolicy() *schema.Resource {
return &schema.Resource{
Description: "The policy resource allows you to create and manage a Border0 policy.",
ReadContext: resourcePolicyRead,
CreateContext: resourcePolicyCreate,
UpdateContext: resourcePolicyUpdate,
DeleteContext: resourcePolicyDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "The name of the policy. Policy name must contain only lowercase letters, numbers and dashes.",
},
"policy_data": {
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: suppressEquivalentPolicyDiffs,
DiffSuppressOnRefresh: true,
Description: "The policy data. This is a JSON string.",
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: "The description of the policy.",
},
"org_wide": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Whether the policy should be applied to all sockets in the organization.",
},
},
}
}
func resourcePolicyRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(border0client.Requester)
policy, err := client.Policy(ctx, d.Id())
if !d.IsNewResource() && border0client.NotFound(err) {
// in case if the policy was deleted without Terraform knowing about it, we need to remove it from the state
log.Printf("[WARN] Policy (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
if err != nil {
return diagnostics.Error(err, "Failed to fetch policy")
}
policyData, err := json.Marshal(&policy.PolicyData)
if err != nil {
return diagnostics.Error(err, "Failed to marshal policy data")
}
return schemautil.SetValues(d, map[string]any{
"name": policy.Name,
"policy_data": string(policyData),
"description": policy.Description,
"org_wide": policy.OrgWide,
})
}
func resourcePolicyCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(border0client.Requester)
policy := &border0client.Policy{
Name: d.Get("name").(string),
}
var policyData border0client.PolicyData
if err := json.Unmarshal([]byte(d.Get("policy_data").(string)), &policyData); err != nil {
return diagnostics.Error(err, "Failed to unmarshal policy data")
}
policy.PolicyData = policyData
if v, ok := d.GetOk("description"); ok {
policy.Description = v.(string)
}
if v, ok := d.GetOk("org_wide"); ok {
policy.OrgWide = v.(bool)
}
created, err := client.CreatePolicy(ctx, policy)
if err != nil {
return diagnostics.Error(err, "Failed to create policy")
}
d.SetId(created.ID)
return resourcePolicyRead(ctx, d, m)
}
func resourcePolicyUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(border0client.Requester)
if d.HasChangesExcept("org_wide") {
policyUpdate := &border0client.Policy{
Name: d.Get("name").(string),
}
var policyData border0client.PolicyData
if err := json.Unmarshal([]byte(d.Get("policy_data").(string)), &policyData); err != nil {
return diagnostics.Error(err, "Failed to unmarshal policy data")
}
policyUpdate.PolicyData = policyData
if v, ok := d.GetOk("description"); ok {
policyUpdate.Description = v.(string)
}
_, err := client.UpdatePolicy(ctx, d.Id(), policyUpdate)
if err != nil {
return diagnostics.Error(err, "Failed to update policy")
}
}
return resourcePolicyRead(ctx, d, m)
}
func resourcePolicyDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(border0client.Requester)
if err := client.DeletePolicy(ctx, d.Id()); err != nil {
return diagnostics.Error(err, "Failed to delete policy")
}
d.SetId("")
return nil
}
func suppressEquivalentPolicyDiffs(k, old, new string, d *schema.ResourceData) bool {
if strings.TrimSpace(old) == "" && strings.TrimSpace(new) == "" {
return true
}
if strings.TrimSpace(old) == "{}" && strings.TrimSpace(new) == "" {
return true
}
if strings.TrimSpace(old) == "" && strings.TrimSpace(new) == "{}" {
return true
}
if strings.TrimSpace(old) == "{}" && strings.TrimSpace(new) == "{}" {
return true
}
var oldJSONAsPolicyData, newJSONAsPolicyData border0client.PolicyData
if err := json.Unmarshal([]byte(old), &oldJSONAsPolicyData); err != nil {
return false
}
if err := json.Unmarshal([]byte(new), &newJSONAsPolicyData); err != nil {
return false
}
return reflect.DeepEqual(oldJSONAsPolicyData, newJSONAsPolicyData)
}