diff --git a/spacelift/internal/structs/policy.go b/spacelift/internal/structs/policy.go index 7074ffd0..d035da45 100644 --- a/spacelift/internal/structs/policy.go +++ b/spacelift/internal/structs/policy.go @@ -1,5 +1,7 @@ package structs +import "github.com/shurcooL/graphql" + // PolicyType represents a policy type. type PolicyType string @@ -13,3 +15,33 @@ type Policy struct { Space string `graphql:"space"` Description string `graphql:"description"` } + +type PolicyCreateInput struct { + PolicyUpdateInput + Type PolicyType `json:"type"` +} + +func NewPolicyCreateInput(name, body graphql.String, policyType PolicyType) PolicyCreateInput { + return PolicyCreateInput{ + PolicyUpdateInput: PolicyUpdateInput{ + Name: name, + Body: body, + }, + Type: policyType, + } +} + +type PolicyUpdateInput struct { + Name graphql.String `json:"name"` + Body graphql.String `json:"body"` + Labels *[]graphql.String `json:"labels"` + Space *graphql.ID `json:"space"` + Description *graphql.String `json:"description"` +} + +func NewPolicyUpdateInput(name, body graphql.String) PolicyUpdateInput { + return PolicyUpdateInput{ + Name: name, + Body: body, + } +} diff --git a/spacelift/resource_policy.go b/spacelift/resource_policy.go index f8b5e41b..de0e08d6 100644 --- a/spacelift/resource_policy.go +++ b/spacelift/resource_policy.go @@ -109,17 +109,10 @@ func resourcePolicy() *schema.Resource { func resourcePolicyCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var mutation struct { - CreatePolicy structs.Policy `graphql:"policyCreate(name: $name, body: $body, type: $type, labels: $labels, space: $space, description: $description)"` + CreatePolicy structs.Policy `graphql:"policyCreatev2(input: $input)"` } - variables := map[string]interface{}{ - "name": toString(d.Get("name")), - "body": toString(d.Get("body")), - "type": structs.PolicyType(d.Get("type").(string)), - "labels": (*[]graphql.String)(nil), - "space": (*graphql.ID)(nil), - "description": toString(d.Get("description")), - } + input := structs.NewPolicyCreateInput(toString(d.Get("name")), toString(d.Get("body")), structs.PolicyType(d.Get("type").(string))) if labelSet, ok := d.Get("labels").(*schema.Set); ok { var labels []graphql.String @@ -128,14 +121,20 @@ func resourcePolicyCreate(ctx context.Context, d *schema.ResourceData, meta inte labels = append(labels, graphql.String(label.(string))) } - variables["labels"] = &labels + input.Labels = &labels } if spaceID, ok := d.GetOk("space_id"); ok { - variables["space"] = graphql.NewID(spaceID) + input.Space = graphql.NewID(spaceID) } - if err := meta.(*internal.Client).Mutate(ctx, "PolicyCreate", &mutation, variables); err != nil { + if description, ok := d.GetOk("description"); ok { + input.Description = toOptionalString(description) + } + + variables := map[string]interface{}{"input": input} + + if err := meta.(*internal.Client).Mutate(ctx, "PolicyCreateV2", &mutation, variables); err != nil { return diag.Errorf("could not create policy %v: %v", toString(d.Get("name")), internal.FromSpaceliftError(err)) } @@ -184,20 +183,13 @@ func resourcePolicyRead(ctx context.Context, d *schema.ResourceData, meta interf func resourcePolicyUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var mutation struct { - UpdatePolicy structs.Policy `graphql:"policyUpdate(id: $id, name: $name, body: $body, labels: $labels, space: $space, description: $description)"` + UpdatePolicy structs.Policy `graphql:"policyUpdatev2(id: $id, input: $input)"` } - variables := map[string]interface{}{ - "id": toID(d.Id()), - "name": toString(d.Get("name")), - "description": (*graphql.String)(nil), - "body": toString(d.Get("body")), - "labels": (*[]graphql.String)(nil), - "space": (*graphql.ID)(nil), - } + input := structs.NewPolicyUpdateInput(toString(d.Get("name")), toString(d.Get("body"))) if desc, ok := d.GetOk("description"); ok { - variables["description"] = graphql.String(desc.(string)) + input.Description = toOptionalString(desc) } if labelSet, ok := d.Get("labels").(*schema.Set); ok { @@ -207,16 +199,20 @@ func resourcePolicyUpdate(ctx context.Context, d *schema.ResourceData, meta inte labels = append(labels, graphql.String(label.(string))) } - variables["labels"] = &labels + input.Labels = &labels } if spaceID, ok := d.GetOk("space_id"); ok { - variables["space"] = graphql.NewID(spaceID) + input.Space = graphql.NewID(spaceID) } var ret diag.Diagnostics + variables := map[string]interface{}{ + "id": toID(d.Id()), + "input": input, + } - if err := meta.(*internal.Client).Mutate(ctx, "PolicyUpdate", &mutation, variables); err != nil { + if err := meta.(*internal.Client).Mutate(ctx, "PolicyUpdateV2", &mutation, variables); err != nil { ret = diag.Errorf("could not update policy: %v", internal.FromSpaceliftError(err)) }