generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 41
/
resource_policy.go
176 lines (153 loc) · 5.69 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
175
176
package policy
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 ResourcePolicy() *schema.Resource {
resource := &schema.Resource{
Description: "Resource for creating a Harness Policy.",
ReadContext: resourcePolicyRead,
UpdateContext: resourcePolicyCreateOrUpdate,
DeleteContext: resourcePolicyDelete,
CreateContext: resourcePolicyCreateOrUpdate,
Importer: helpers.OrgResourceImporter,
Schema: map[string]*schema.Schema{
"name": {
Description: "Name of the policy.",
Type: schema.TypeString,
Required: true,
Computed: false,
},
"identifier": {
Description: "Identifier of the policy.",
Type: schema.TypeString,
Required: true,
Computed: false,
},
"rego": {
Description: "Rego code for the policy.",
Type: schema.TypeString,
Required: true,
Computed: false,
},
},
}
helpers.SetMultiLevelResourceSchema(resource.Schema)
return resource
}
func resourcePolicyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*internal.Session).GetPolicyManagementClient()
id := d.Id()
localVarOptionals := policymgmt.PoliciesApiPoliciesFindOpts{
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.PoliciesApi.PoliciesFind(ctx, id, &localVarOptionals)
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
readPolicy(d, policy)
return nil
}
func resourcePolicyCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*internal.Session).GetPolicyManagementClient()
var err error
var responsePolicy policymgmt.Policy
var httpResp *http.Response
id := d.Id()
if id == "" {
body := policymgmt.CreateRequestBody{
Name: d.Get("name").(string),
Rego: d.Get("rego").(string),
Identifier: d.Get("identifier").(string),
}
localVarOptionals := policymgmt.PoliciesApiPoliciesCreateOpts{
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")
}
responsePolicy, httpResp, err = c.PoliciesApi.PoliciesCreate(ctx, body, &localVarOptionals)
} else {
body := policymgmt.UpdateRequestBody{
Name: d.Get("name").(string),
Rego: d.Get("rego").(string),
}
localVarOptionals := policymgmt.PoliciesApiPoliciesUpdateOpts{
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.PoliciesApi.PoliciesUpdate(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.PoliciesApiPoliciesFindOpts{
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")
}
responsePolicy, httpResp, err = c.PoliciesApi.PoliciesFind(ctx, id, &findLocalVarOptionals)
}
}
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
readPolicy(d, responsePolicy)
return nil
}
func resourcePolicyDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*internal.Session).GetPolicyManagementClient()
localVarOptionals := policymgmt.PoliciesApiPoliciesDeleteOpts{
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.PoliciesApi.PoliciesDelete(ctx, d.Id(), &localVarOptionals)
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
return nil
}
func readPolicy(d *schema.ResourceData, policy policymgmt.Policy) {
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("rego", policy.Rego)
}