Skip to content

Commit

Permalink
Introduce support for context hooks (#443)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinwyszynski authored Nov 16, 2023
1 parent 23aa28a commit 4961bcb
Show file tree
Hide file tree
Showing 9 changed files with 387 additions and 21 deletions.
10 changes: 8 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
{
"go.testEnvFile": "${workspaceFolder}/test.env"
}
"go.testEnvFile": "${workspaceFolder}/test.env",
"cSpell.words": [
"autoretry",
"datacenter",
"pulumi",
"Spacelift"
]
}
14 changes: 14 additions & 0 deletions docs/data-sources/context.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ data "spacelift_context" "prod-k8s-ie" {

- `context_id` (String) immutable ID (slug) of the context

### Optional

- `after_apply` (List of String) List of after-apply scripts
- `after_destroy` (List of String) List of after-destroy scripts
- `after_init` (List of String) List of after-init scripts
- `after_perform` (List of String) List of after-perform scripts
- `after_plan` (List of String) List of after-plan scripts
- `after_run` (List of String) List of after-run scripts
- `before_apply` (List of String) List of before-apply scripts
- `before_destroy` (List of String) List of before-destroy scripts
- `before_init` (List of String) List of before-init scripts
- `before_perform` (List of String) List of before-perform scripts
- `before_plan` (List of String) List of before-plan scripts

### Read-Only

- `description` (String) free-form context description for users
Expand Down
11 changes: 11 additions & 0 deletions docs/resources/context.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ resource "spacelift_context" "prod-k8s-ie" {

### Optional

- `after_apply` (List of String) List of after-apply scripts
- `after_destroy` (List of String) List of after-destroy scripts
- `after_init` (List of String) List of after-init scripts
- `after_perform` (List of String) List of after-perform scripts
- `after_plan` (List of String) List of after-plan scripts
- `after_run` (List of String) List of after-run scripts
- `before_apply` (List of String) List of before-apply scripts
- `before_destroy` (List of String) List of before-destroy scripts
- `before_init` (List of String) List of before-init scripts
- `before_perform` (List of String) List of before-perform scripts
- `before_plan` (List of String) List of before-plan scripts
- `description` (String) Free-form context description for users
- `labels` (Set of String)
- `space_id` (String) ID (slug) of the space the context is in
Expand Down
88 changes: 88 additions & 0 deletions spacelift/data_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,82 @@ func dataContext() *schema.Resource {
ReadContext: dataContextRead,

Schema: map[string]*schema.Schema{
"after_apply": {
Type: schema.TypeList,
Description: "List of after-apply scripts",
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Computed: true,
},
"after_destroy": {
Type: schema.TypeList,
Description: "List of after-destroy scripts",
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Computed: true,
},
"after_init": {
Type: schema.TypeList,
Description: "List of after-init scripts",
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Computed: true,
},
"after_perform": {
Type: schema.TypeList,
Description: "List of after-perform scripts",
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Computed: true,
},
"after_plan": {
Type: schema.TypeList,
Description: "List of after-plan scripts",
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Computed: true,
},
"after_run": {
Type: schema.TypeList,
Description: "List of after-run scripts",
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
},
"before_apply": {
Type: schema.TypeList,
Description: "List of before-apply scripts",
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Computed: true,
},
"before_destroy": {
Type: schema.TypeList,
Description: "List of before-destroy scripts",
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Computed: true,
},
"before_init": {
Type: schema.TypeList,
Description: "List of before-init scripts",
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Computed: true,
},
"before_perform": {
Type: schema.TypeList,
Description: "List of before-perform scripts",
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Computed: true,
},
"before_plan": {
Type: schema.TypeList,
Description: "List of before-plan scripts",
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Computed: true,
},
"context_id": {
Type: schema.TypeString,
Description: "immutable ID (slug) of the context",
Expand Down Expand Up @@ -84,5 +160,17 @@ func dataContextRead(ctx context.Context, d *schema.ResourceData, meta interface
}
d.Set("labels", labels)

d.Set("after_apply", context.Hooks.AfterApply)
d.Set("after_destroy", context.Hooks.AfterDestroy)
d.Set("after_init", context.Hooks.AfterInit)
d.Set("after_perform", context.Hooks.AfterPerform)
d.Set("after_plan", context.Hooks.AfterPlan)
d.Set("after_run", context.Hooks.AfterRun)
d.Set("before_apply", context.Hooks.BeforeApply)
d.Set("before_destroy", context.Hooks.BeforeDestroy)
d.Set("before_init", context.Hooks.BeforeInit)
d.Set("before_perform", context.Hooks.BeforePerform)
d.Set("before_plan", context.Hooks.BeforePlan)

return nil
}
33 changes: 33 additions & 0 deletions spacelift/data_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ func TestContextData(t *testing.T) {
name = "Provider test context %s"
description = "description"
labels = ["one", "two"]
after_apply = ["after_apply"]
after_destroy = ["after_destroy"]
after_init = ["after_init"]
after_perform = ["after_perform"]
after_plan = ["after_plan"]
after_run = ["after_run"]
before_apply = ["before_apply"]
before_destroy = ["before_destroy"]
before_init = ["before_init"]
before_perform = ["before_perform"]
before_plan = ["before_plan"]
}
data "spacelift_context" "test" {
context_id = spacelift_context.test.id
Expand All @@ -31,6 +42,28 @@ func TestContextData(t *testing.T) {
Attribute("name", StartsWith("Provider test context")),
Attribute("description", Equals("description")),
SetEquals("labels", "one", "two"),
Attribute("after_apply.#", Equals("1")),
Attribute("after_apply.0", Equals("after_apply")),
Attribute("after_destroy.#", Equals("1")),
Attribute("after_destroy.0", Equals("after_destroy")),
Attribute("after_init.#", Equals("1")),
Attribute("after_init.0", Equals("after_init")),
Attribute("after_perform.#", Equals("1")),
Attribute("after_perform.0", Equals("after_perform")),
Attribute("after_plan.#", Equals("1")),
Attribute("after_plan.0", Equals("after_plan")),
Attribute("after_run.#", Equals("1")),
Attribute("after_run.0", Equals("after_run")),
Attribute("before_apply.#", Equals("1")),
Attribute("before_apply.0", Equals("before_apply")),
Attribute("before_destroy.#", Equals("1")),
Attribute("before_destroy.0", Equals("before_destroy")),
Attribute("before_init.#", Equals("1")),
Attribute("before_init.0", Equals("before_init")),
Attribute("before_perform.#", Equals("1")),
Attribute("before_perform.0", Equals("before_perform")),
Attribute("before_plan.#", Equals("1")),
Attribute("before_plan.0", Equals("before_plan")),
),
}})
})
Expand Down
13 changes: 13 additions & 0 deletions spacelift/internal/structs/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,17 @@ type Context struct {
Labels []string `graphql:"labels"`
Name string `graphql:"name"`
Space string `graphql:"space"`
Hooks struct {
AfterApply []string `graphql:"afterApply"`
AfterDestroy []string `graphql:"afterDestroy"`
AfterInit []string `graphql:"afterInit"`
AfterPerform []string `graphql:"afterPerform"`
AfterPlan []string `graphql:"afterPlan"`
AfterRun []string `graphql:"afterRun"`
BeforeApply []string `graphql:"beforeApply"`
BeforeDestroy []string `graphql:"beforeDestroy"`
BeforeInit []string `graphql:"beforeInit"`
BeforePerform []string `graphql:"beforePerform"`
BeforePlan []string `graphql:"beforePlan"`
} `graphql:"hooks"`
}
27 changes: 27 additions & 0 deletions spacelift/internal/structs/context_input.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package structs

import "github.com/shurcooL/graphql"

// ContextInput represents the input required to create or update a Context.
type ContextInput struct {
Name graphql.String `json:"name"`
Description *graphql.String `json:"description"`
Hooks *HooksInput `json:"hooks"`
Labels *[]graphql.String `json:"labels"`
Space *graphql.ID `json:"space"`
}

// HooksInput represents the input required to create or update Hooks.
type HooksInput struct {
AfterApply []graphql.String `json:"afterApply"`
AfterDestroy []graphql.String `json:"afterDestroy"`
AfterInit []graphql.String `json:"afterInit"`
AfterPerform []graphql.String `json:"afterPerform"`
AfterPlan []graphql.String `json:"afterPlan"`
AfterRun []graphql.String `json:"afterRun"`
BeforeApply []graphql.String `json:"beforeApply"`
BeforeDestroy []graphql.String `json:"beforeDestroy"`
BeforeInit []graphql.String `json:"beforeInit"`
BeforePerform []graphql.String `json:"beforePerform"`
BeforePlan []graphql.String `json:"beforePlan"`
}
Loading

0 comments on commit 4961bcb

Please sign in to comment.