Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use policy api v2 #532

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions spacelift/internal/structs/policy.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package structs

import "github.com/shurcooL/graphql"

// PolicyType represents a policy type.
type PolicyType string

Expand All @@ -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,
}
}
46 changes: 21 additions & 25 deletions spacelift/resource_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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))
}

Expand Down
Loading