Skip to content

Commit

Permalink
add support for policy description (#530)
Browse files Browse the repository at this point in the history
Co-authored-by: Mohamed El Mouctar HAIDARA <[email protected]>
Co-authored-by: Marcin Wyszynski <[email protected]>
  • Loading branch information
3 people authored Mar 21, 2024
1 parent 4e1dd29 commit 2993eb7
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 28 deletions.
1 change: 1 addition & 0 deletions docs/data-sources/policies.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ output "policy_ids" {

Read-Only:

- `description` (String)
- `id` (String)
- `labels` (Set of String)
- `name` (String)
Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ output "policy_body" {
### Read-Only

- `body` (String) body of the policy
- `description` (String) description of the policy
- `id` (String) The ID of this resource.
- `labels` (Set of String)
- `name` (String) name of the policy
Expand Down
1 change: 1 addition & 0 deletions docs/resources/policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ resource "spacelift_policy_attachment" "no-weekend-deploys" {

### Optional

- `description` (String) Description of the policy
- `labels` (Set of String)
- `space_id` (String) ID (slug) of the space the policy is in

Expand Down
27 changes: 17 additions & 10 deletions spacelift/data_policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ func dataPolicies() *schema.Resource {
Description: "Type of the policy",
Computed: true,
},
"description": {
Type: schema.TypeString,
Description: "description of the policy",
Computed: true,
},
},
},
},
Expand All @@ -70,11 +75,12 @@ func dataPoliciesRead(ctx context.Context, d *schema.ResourceData, meta interfac
d.SetId(fmt.Sprintf("policies/%s/%s", d.Get("type").(string), d.Get("labels").(*schema.Set).List()))
var query struct {
Policies []struct {
ID string `graphql:"id"`
Labels []string `graphql:"labels"`
Name string `graphql:"name"`
Type string `graphql:"type"`
Space string `graphql:"space"`
ID string `graphql:"id"`
Labels []string `graphql:"labels"`
Name string `graphql:"name"`
Type string `graphql:"type"`
Space string `graphql:"space"`
Description string `graphql:"description"`
} `graphql:"policies()"`
}

Expand Down Expand Up @@ -110,11 +116,12 @@ func dataPoliciesRead(ctx context.Context, d *schema.ResourceData, meta interfac
}
}
policies = append(policies, map[string]interface{}{
"id": policy.ID,
"labels": policy.Labels,
"name": policy.Name,
"type": policy.Type,
"space_id": policy.Space,
"id": policy.ID,
"labels": policy.Labels,
"name": policy.Name,
"type": policy.Type,
"space_id": policy.Space,
"description": policy.Description,
})
}

Expand Down
6 changes: 6 additions & 0 deletions spacelift/data_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ func dataPolicy() *schema.Resource {
Description: "name of the policy",
Computed: true,
},
"description": {
Type: schema.TypeString,
Description: "description of the policy",
Computed: true,
},
"space_id": {
Type: schema.TypeString,
Description: "ID (slug) of the space the policy is in",
Expand Down Expand Up @@ -76,6 +81,7 @@ func dataPolicyRead(ctx context.Context, d *schema.ResourceData, meta interface{
d.Set("body", policy.Body)
d.Set("type", policy.Type)
d.Set("space_id", policy.Space)
d.Set("description", policy.Description)

labels := schema.NewSet(schema.HashString, []interface{}{})
for _, label := range policy.Labels {
Expand Down
4 changes: 4 additions & 0 deletions spacelift/data_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestPolicyData(t *testing.T) {
resource "spacelift_policy" "test" {
name = "My first policy %s"
labels = ["one", "two"]
description = "My awesome policy"
body = <<EOF
package spacelift
deny["boom"] { true }
Expand All @@ -35,6 +36,7 @@ func TestPolicyData(t *testing.T) {
Attribute("body", Contains("boom")),
Attribute("type", Equals("PLAN")),
SetEquals("labels", "one", "two"),
Attribute("description", Equals("My awesome policy")),
),
}})
})
Expand All @@ -48,6 +50,7 @@ func TestPolicyDataSpace(t *testing.T) {
Config: fmt.Sprintf(`
resource "spacelift_policy" "test" {
name = "My first policy %s"
description = "My awesome policy"
labels = ["one", "two"]
space_id = "root"
body = <<EOF
Expand All @@ -69,6 +72,7 @@ func TestPolicyDataSpace(t *testing.T) {
Attribute("type", Equals("PLAN")),
Attribute("space_id", Equals("root")),
SetEquals("labels", "one", "two"),
Attribute("description", Equals("My awesome policy")),
),
}})
})
Expand Down
13 changes: 7 additions & 6 deletions spacelift/internal/structs/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ type PolicyType string

// Policy represents Policy data relevant to the provider.
type Policy struct {
ID string `graphql:"id"`
Labels []string `graphql:"labels"`
Name string `graphql:"name"`
Body string `graphql:"body"`
Type string `graphql:"type"`
Space string `graphql:"space"`
ID string `graphql:"id"`
Labels []string `graphql:"labels"`
Name string `graphql:"name"`
Body string `graphql:"body"`
Type string `graphql:"type"`
Space string `graphql:"space"`
Description string `graphql:"description"`
}
37 changes: 25 additions & 12 deletions spacelift/resource_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,28 @@ func resourcePolicy() *schema.Resource {
false, // case-sensitive match
),
},
"description": {
Type: schema.TypeString,
Description: "Description of the policy",
Optional: true,
ValidateDiagFunc: validations.DisallowEmptyString,
},
},
}
}

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)"`
CreatePolicy structs.Policy `graphql:"policyCreate(name: $name, body: $body, type: $type, labels: $labels, space: $space, description: $description)"`
}

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),
"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")),
}

if labelSet, ok := d.Get("labels").(*schema.Set); ok {
Expand Down Expand Up @@ -157,6 +164,7 @@ func resourcePolicyRead(ctx context.Context, d *schema.ResourceData, meta interf
d.Set("body", policy.Body)
d.Set("type", policy.Type)
d.Set("space_id", policy.Space)
d.Set("description", policy.Description)

labels := schema.NewSet(schema.HashString, []interface{}{})
for _, label := range policy.Labels {
Expand All @@ -176,15 +184,20 @@ 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)"`
UpdatePolicy structs.Policy `graphql:"policyUpdate(id: $id, name: $name, body: $body, labels: $labels, space: $space, description: $description)"`
}

variables := map[string]interface{}{
"id": toID(d.Id()),
"name": toString(d.Get("name")),
"body": toString(d.Get("body")),
"labels": (*[]graphql.String)(nil),
"space": (*graphql.ID)(nil),
"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),
}

if desc, ok := d.GetOk("description"); ok {
variables["description"] = graphql.String(desc.(string))
}

if labelSet, ok := d.Get("labels").(*schema.Set); ok {
Expand Down
4 changes: 4 additions & 0 deletions spacelift/resource_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestPolicyResource(t *testing.T) {
resource "spacelift_policy" "test" {
name = "My first policy %s"
labels = ["one", "two"]
description = "My awesome policy"
body = <<EOF
package spacelift
deny["%s"] { true }
Expand All @@ -39,6 +40,7 @@ func TestPolicyResource(t *testing.T) {
Attribute("body", Contains("boom")),
Attribute("type", Equals("PLAN")),
SetEquals("labels", "one", "two"),
Attribute("description", Equals("My awesome policy")),
),
},
{
Expand Down Expand Up @@ -99,6 +101,7 @@ func TestPolicyResourceSpace(t *testing.T) {
resource "spacelift_policy" "test" {
name = "My first policy %s"
labels = ["one", "two"]
description = "My awesome policy"
space_id = "root"
body = <<EOF
package spacelift
Expand All @@ -120,6 +123,7 @@ func TestPolicyResourceSpace(t *testing.T) {
Attribute("type", Equals("PLAN")),
SetEquals("labels", "one", "two"),
Attribute("space_id", Equals("root")),
Attribute("description", Equals("My awesome policy")),
),
},
{
Expand Down

0 comments on commit 2993eb7

Please sign in to comment.