Skip to content

Commit dec9cce

Browse files
authored
Merge pull request #532 from spacelift-io/use-policy-api-v2
Use policy api v2
2 parents c48c864 + 19b1552 commit dec9cce

File tree

2 files changed

+53
-25
lines changed

2 files changed

+53
-25
lines changed

spacelift/internal/structs/policy.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package structs
22

3+
import "github.com/shurcooL/graphql"
4+
35
// PolicyType represents a policy type.
46
type PolicyType string
57

@@ -13,3 +15,33 @@ type Policy struct {
1315
Space string `graphql:"space"`
1416
Description string `graphql:"description"`
1517
}
18+
19+
type PolicyCreateInput struct {
20+
PolicyUpdateInput
21+
Type PolicyType `json:"type"`
22+
}
23+
24+
func NewPolicyCreateInput(name, body graphql.String, policyType PolicyType) PolicyCreateInput {
25+
return PolicyCreateInput{
26+
PolicyUpdateInput: PolicyUpdateInput{
27+
Name: name,
28+
Body: body,
29+
},
30+
Type: policyType,
31+
}
32+
}
33+
34+
type PolicyUpdateInput struct {
35+
Name graphql.String `json:"name"`
36+
Body graphql.String `json:"body"`
37+
Labels *[]graphql.String `json:"labels"`
38+
Space *graphql.ID `json:"space"`
39+
Description *graphql.String `json:"description"`
40+
}
41+
42+
func NewPolicyUpdateInput(name, body graphql.String) PolicyUpdateInput {
43+
return PolicyUpdateInput{
44+
Name: name,
45+
Body: body,
46+
}
47+
}

spacelift/resource_policy.go

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,10 @@ func resourcePolicy() *schema.Resource {
109109

110110
func resourcePolicyCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
111111
var mutation struct {
112-
CreatePolicy structs.Policy `graphql:"policyCreate(name: $name, body: $body, type: $type, labels: $labels, space: $space, description: $description)"`
112+
CreatePolicy structs.Policy `graphql:"policyCreatev2(input: $input)"`
113113
}
114114

115-
variables := map[string]interface{}{
116-
"name": toString(d.Get("name")),
117-
"body": toString(d.Get("body")),
118-
"type": structs.PolicyType(d.Get("type").(string)),
119-
"labels": (*[]graphql.String)(nil),
120-
"space": (*graphql.ID)(nil),
121-
"description": toString(d.Get("description")),
122-
}
115+
input := structs.NewPolicyCreateInput(toString(d.Get("name")), toString(d.Get("body")), structs.PolicyType(d.Get("type").(string)))
123116

124117
if labelSet, ok := d.Get("labels").(*schema.Set); ok {
125118
var labels []graphql.String
@@ -128,14 +121,20 @@ func resourcePolicyCreate(ctx context.Context, d *schema.ResourceData, meta inte
128121
labels = append(labels, graphql.String(label.(string)))
129122
}
130123

131-
variables["labels"] = &labels
124+
input.Labels = &labels
132125
}
133126

134127
if spaceID, ok := d.GetOk("space_id"); ok {
135-
variables["space"] = graphql.NewID(spaceID)
128+
input.Space = graphql.NewID(spaceID)
136129
}
137130

138-
if err := meta.(*internal.Client).Mutate(ctx, "PolicyCreate", &mutation, variables); err != nil {
131+
if description, ok := d.GetOk("description"); ok {
132+
input.Description = toOptionalString(description)
133+
}
134+
135+
variables := map[string]interface{}{"input": input}
136+
137+
if err := meta.(*internal.Client).Mutate(ctx, "PolicyCreateV2", &mutation, variables); err != nil {
139138
return diag.Errorf("could not create policy %v: %v", toString(d.Get("name")), internal.FromSpaceliftError(err))
140139
}
141140

@@ -184,20 +183,13 @@ func resourcePolicyRead(ctx context.Context, d *schema.ResourceData, meta interf
184183

185184
func resourcePolicyUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
186185
var mutation struct {
187-
UpdatePolicy structs.Policy `graphql:"policyUpdate(id: $id, name: $name, body: $body, labels: $labels, space: $space, description: $description)"`
186+
UpdatePolicy structs.Policy `graphql:"policyUpdatev2(id: $id, input: $input)"`
188187
}
189188

190-
variables := map[string]interface{}{
191-
"id": toID(d.Id()),
192-
"name": toString(d.Get("name")),
193-
"description": (*graphql.String)(nil),
194-
"body": toString(d.Get("body")),
195-
"labels": (*[]graphql.String)(nil),
196-
"space": (*graphql.ID)(nil),
197-
}
189+
input := structs.NewPolicyUpdateInput(toString(d.Get("name")), toString(d.Get("body")))
198190

199191
if desc, ok := d.GetOk("description"); ok {
200-
variables["description"] = graphql.String(desc.(string))
192+
input.Description = toOptionalString(desc)
201193
}
202194

203195
if labelSet, ok := d.Get("labels").(*schema.Set); ok {
@@ -207,16 +199,20 @@ func resourcePolicyUpdate(ctx context.Context, d *schema.ResourceData, meta inte
207199
labels = append(labels, graphql.String(label.(string)))
208200
}
209201

210-
variables["labels"] = &labels
202+
input.Labels = &labels
211203
}
212204

213205
if spaceID, ok := d.GetOk("space_id"); ok {
214-
variables["space"] = graphql.NewID(spaceID)
206+
input.Space = graphql.NewID(spaceID)
215207
}
216208

217209
var ret diag.Diagnostics
210+
variables := map[string]interface{}{
211+
"id": toID(d.Id()),
212+
"input": input,
213+
}
218214

219-
if err := meta.(*internal.Client).Mutate(ctx, "PolicyUpdate", &mutation, variables); err != nil {
215+
if err := meta.(*internal.Client).Mutate(ctx, "PolicyUpdateV2", &mutation, variables); err != nil {
220216
ret = diag.Errorf("could not update policy: %v", internal.FromSpaceliftError(err))
221217
}
222218

0 commit comments

Comments
 (0)