generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 41
/
resource_policyset.go
237 lines (210 loc) · 7.71 KB
/
resource_policyset.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
package policyset
import (
"context"
"net/http"
"github.com/antihax/optional"
"github.com/harness/harness-go-sdk/harness/policymgmt"
"github.com/harness/terraform-provider-harness/helpers"
"github.com/harness/terraform-provider-harness/internal"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func ResourcePolicyset() *schema.Resource {
resource := &schema.Resource{
Description: "Resource for creating a Harness Policyset.",
ReadContext: resourcePolicysetRead,
UpdateContext: resourcePolicysetCreateOrUpdate,
DeleteContext: resourcePolicysetDelete,
CreateContext: resourcePolicysetCreateOrUpdate,
Importer: helpers.MultiLevelResourceImporter,
Schema: map[string]*schema.Schema{
"type": {
Description: "Type for the policyset.",
Type: schema.TypeString,
Required: true,
Computed: false,
},
"action": {
Description: "Action for the policyset.",
Type: schema.TypeString,
Required: true,
Computed: false,
},
"enabled": {
Description: "Enabled for the policyset.",
Type: schema.TypeBool,
Optional: true,
},
"policies": {
Description: "List of policy identifiers / severity for the policyset.",
Type: schema.TypeList,
Computed: true,
Optional: true,
MinItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"identifier": {
Description: "Account Identifier of the account",
Type: schema.TypeString,
Optional: false,
Required: true,
},
"severity": {
Description: "Policy failure response - 'warning' for continuation, 'error' for exit",
Type: schema.TypeString,
Optional: false,
Required: true,
},
},
},
},
},
}
helpers.SetMultiLevelResourceSchema(resource.Schema)
return resource
}
func resourcePolicysetRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*internal.Session).GetPolicyManagementClient()
id := d.Id()
localVarOptionals := policymgmt.PolicysetsApiPolicysetsFindOpts{
AccountIdentifier: optional.NewString(meta.(*internal.Session).AccountId),
XApiKey: optional.NewString(meta.(*internal.Session).PLClient.ApiKey),
}
// check for project and org
if d.Get("project_id").(string) != "" {
localVarOptionals.ProjectIdentifier = helpers.BuildField(d, "project_id")
}
if d.Get("org_id").(string) != "" {
localVarOptionals.OrgIdentifier = helpers.BuildField(d, "org_id")
}
policy, httpResp, err := c.PolicysetsApi.PolicysetsFind(ctx, id, &localVarOptionals)
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
readPolicyset(d, policy)
return nil
}
func resourcePolicysetCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*internal.Session).GetPolicyManagementClient()
var err error
var responsePolicyset policymgmt.PolicySet
var httpResp *http.Response
id := d.Id()
if id == "" {
body := policymgmt.CreateRequestBody2{
Name: d.Get("name").(string),
Identifier: d.Get("identifier").(string),
Action: d.Get("action").(string),
Type_: d.Get("type").(string),
Enabled: d.Get("enabled").(bool),
}
localVarOptionals := policymgmt.PolicysetsApiPolicysetsCreateOpts{
AccountIdentifier: optional.NewString(meta.(*internal.Session).AccountId),
XApiKey: optional.NewString(meta.(*internal.Session).PLClient.ApiKey),
}
// check for project and org
if d.Get("project_id").(string) != "" {
localVarOptionals.ProjectIdentifier = helpers.BuildField(d, "project_id")
}
if d.Get("org_id").(string) != "" {
localVarOptionals.OrgIdentifier = helpers.BuildField(d, "org_id")
}
createPolicySet, _, createErr := c.PolicysetsApi.PolicysetsCreate(ctx, body, &localVarOptionals)
if createErr != nil {
return helpers.HandleApiError(createErr, d, httpResp)
}
id = createPolicySet.Identifier
}
// NB we can only add policies to a policyset after it has been created, so we need to do this in the update
policies := buildPolicies(d)
body := policymgmt.UpdateRequestBody2{
Name: d.Get("name").(string),
Type_: d.Get("type").(string),
Action: d.Get("action").(string),
Enabled: d.Get("enabled").(bool),
Policies: policies,
}
localVarOptionals := policymgmt.PolicysetsApiPolicysetsUpdateOpts{
AccountIdentifier: optional.NewString(meta.(*internal.Session).AccountId),
XApiKey: optional.NewString(meta.(*internal.Session).PLClient.ApiKey),
}
if d.Get("project_id").(string) != "" {
localVarOptionals.ProjectIdentifier = helpers.BuildField(d, "project_id")
}
if d.Get("org_id").(string) != "" {
localVarOptionals.OrgIdentifier = helpers.BuildField(d, "org_id")
}
httpResp, err = c.PolicysetsApi.PolicysetsUpdate(ctx, body, id, &localVarOptionals)
if err == nil && httpResp.StatusCode == http.StatusNoContent {
// if we get a 204, we need to get the policy again to get the updated values
findLocalVarOptionals := policymgmt.PolicysetsApiPolicysetsFindOpts{
AccountIdentifier: optional.NewString(meta.(*internal.Session).AccountId),
XApiKey: optional.NewString(meta.(*internal.Session).PLClient.ApiKey),
}
// check for project and org
if d.Get("project_id").(string) != "" {
findLocalVarOptionals.ProjectIdentifier = helpers.BuildField(d, "project_id")
}
if d.Get("org_id").(string) != "" {
findLocalVarOptionals.OrgIdentifier = helpers.BuildField(d, "org_id")
}
responsePolicyset, httpResp, err = c.PolicysetsApi.PolicysetsFind(ctx, id, &findLocalVarOptionals)
}
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
readPolicyset(d, responsePolicyset)
return nil
}
func buildPolicies(d *schema.ResourceData) []policymgmt.Linkedpolicyidentifier {
policies := []policymgmt.Linkedpolicyidentifier{}
rawPolicies := d.Get("policies").([]interface{})
for _, policy := range rawPolicies {
policy := policy.(map[string]interface{})
policies = append(policies, policymgmt.Linkedpolicyidentifier{
Identifier: policy["identifier"].(string),
Severity: policy["severity"].(string),
})
}
return policies
}
func resourcePolicysetDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*internal.Session).GetPolicyManagementClient()
localVarOptionals := policymgmt.PolicysetsApiPolicysetsDeleteOpts{
AccountIdentifier: optional.NewString(meta.(*internal.Session).AccountId),
XApiKey: optional.NewString(meta.(*internal.Session).PLClient.ApiKey),
}
// check for project and org
if d.Get("project_id").(string) != "" {
localVarOptionals.ProjectIdentifier = helpers.BuildField(d, "project_id")
}
if d.Get("org_id").(string) != "" {
localVarOptionals.OrgIdentifier = helpers.BuildField(d, "org_id")
}
httpResp, err := c.PolicysetsApi.PolicysetsDelete(ctx, d.Id(), &localVarOptionals)
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
return nil
}
func readPolicyset(d *schema.ResourceData, policy policymgmt.PolicySet) {
d.SetId(policy.Identifier)
_ = d.Set("identifier", policy.Identifier)
_ = d.Set("org_id", policy.OrgId)
_ = d.Set("project_id", policy.ProjectId)
_ = d.Set("name", policy.Name)
_ = d.Set("action", policy.Action)
_ = d.Set("type", policy.Type_)
_ = d.Set("enabled", policy.Enabled)
_ = d.Set("policies", flattenPolicies(policy.Policies))
}
func flattenPolicies(policies []policymgmt.LinkedPolicy) []map[string]interface{} {
var policyList []map[string]interface{}
for _, policy := range policies {
policyList = append(policyList, map[string]interface{}{
"identifier": policy.Identifier,
"severity": policy.Severity,
})
}
return policyList
}