Skip to content

Commit 4961bcb

Browse files
Introduce support for context hooks (#443)
1 parent 23aa28a commit 4961bcb

File tree

9 files changed

+387
-21
lines changed

9 files changed

+387
-21
lines changed

.vscode/settings.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
{
2-
"go.testEnvFile": "${workspaceFolder}/test.env"
3-
}
2+
"go.testEnvFile": "${workspaceFolder}/test.env",
3+
"cSpell.words": [
4+
"autoretry",
5+
"datacenter",
6+
"pulumi",
7+
"Spacelift"
8+
]
9+
}

docs/data-sources/context.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ data "spacelift_context" "prod-k8s-ie" {
2525

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

28+
### Optional
29+
30+
- `after_apply` (List of String) List of after-apply scripts
31+
- `after_destroy` (List of String) List of after-destroy scripts
32+
- `after_init` (List of String) List of after-init scripts
33+
- `after_perform` (List of String) List of after-perform scripts
34+
- `after_plan` (List of String) List of after-plan scripts
35+
- `after_run` (List of String) List of after-run scripts
36+
- `before_apply` (List of String) List of before-apply scripts
37+
- `before_destroy` (List of String) List of before-destroy scripts
38+
- `before_init` (List of String) List of before-init scripts
39+
- `before_perform` (List of String) List of before-perform scripts
40+
- `before_plan` (List of String) List of before-plan scripts
41+
2842
### Read-Only
2943

3044
- `description` (String) free-form context description for users

docs/resources/context.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ resource "spacelift_context" "prod-k8s-ie" {
2828

2929
### Optional
3030

31+
- `after_apply` (List of String) List of after-apply scripts
32+
- `after_destroy` (List of String) List of after-destroy scripts
33+
- `after_init` (List of String) List of after-init scripts
34+
- `after_perform` (List of String) List of after-perform scripts
35+
- `after_plan` (List of String) List of after-plan scripts
36+
- `after_run` (List of String) List of after-run scripts
37+
- `before_apply` (List of String) List of before-apply scripts
38+
- `before_destroy` (List of String) List of before-destroy scripts
39+
- `before_init` (List of String) List of before-init scripts
40+
- `before_perform` (List of String) List of before-perform scripts
41+
- `before_plan` (List of String) List of before-plan scripts
3142
- `description` (String) Free-form context description for users
3243
- `labels` (Set of String)
3344
- `space_id` (String) ID (slug) of the space the context is in

spacelift/data_context.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,82 @@ func dataContext() *schema.Resource {
2323
ReadContext: dataContextRead,
2424

2525
Schema: map[string]*schema.Schema{
26+
"after_apply": {
27+
Type: schema.TypeList,
28+
Description: "List of after-apply scripts",
29+
Elem: &schema.Schema{Type: schema.TypeString},
30+
Optional: true,
31+
Computed: true,
32+
},
33+
"after_destroy": {
34+
Type: schema.TypeList,
35+
Description: "List of after-destroy scripts",
36+
Elem: &schema.Schema{Type: schema.TypeString},
37+
Optional: true,
38+
Computed: true,
39+
},
40+
"after_init": {
41+
Type: schema.TypeList,
42+
Description: "List of after-init scripts",
43+
Elem: &schema.Schema{Type: schema.TypeString},
44+
Optional: true,
45+
Computed: true,
46+
},
47+
"after_perform": {
48+
Type: schema.TypeList,
49+
Description: "List of after-perform scripts",
50+
Elem: &schema.Schema{Type: schema.TypeString},
51+
Optional: true,
52+
Computed: true,
53+
},
54+
"after_plan": {
55+
Type: schema.TypeList,
56+
Description: "List of after-plan scripts",
57+
Elem: &schema.Schema{Type: schema.TypeString},
58+
Optional: true,
59+
Computed: true,
60+
},
61+
"after_run": {
62+
Type: schema.TypeList,
63+
Description: "List of after-run scripts",
64+
Elem: &schema.Schema{Type: schema.TypeString},
65+
Optional: true,
66+
},
67+
"before_apply": {
68+
Type: schema.TypeList,
69+
Description: "List of before-apply scripts",
70+
Elem: &schema.Schema{Type: schema.TypeString},
71+
Optional: true,
72+
Computed: true,
73+
},
74+
"before_destroy": {
75+
Type: schema.TypeList,
76+
Description: "List of before-destroy scripts",
77+
Elem: &schema.Schema{Type: schema.TypeString},
78+
Optional: true,
79+
Computed: true,
80+
},
81+
"before_init": {
82+
Type: schema.TypeList,
83+
Description: "List of before-init scripts",
84+
Elem: &schema.Schema{Type: schema.TypeString},
85+
Optional: true,
86+
Computed: true,
87+
},
88+
"before_perform": {
89+
Type: schema.TypeList,
90+
Description: "List of before-perform scripts",
91+
Elem: &schema.Schema{Type: schema.TypeString},
92+
Optional: true,
93+
Computed: true,
94+
},
95+
"before_plan": {
96+
Type: schema.TypeList,
97+
Description: "List of before-plan scripts",
98+
Elem: &schema.Schema{Type: schema.TypeString},
99+
Optional: true,
100+
Computed: true,
101+
},
26102
"context_id": {
27103
Type: schema.TypeString,
28104
Description: "immutable ID (slug) of the context",
@@ -84,5 +160,17 @@ func dataContextRead(ctx context.Context, d *schema.ResourceData, meta interface
84160
}
85161
d.Set("labels", labels)
86162

163+
d.Set("after_apply", context.Hooks.AfterApply)
164+
d.Set("after_destroy", context.Hooks.AfterDestroy)
165+
d.Set("after_init", context.Hooks.AfterInit)
166+
d.Set("after_perform", context.Hooks.AfterPerform)
167+
d.Set("after_plan", context.Hooks.AfterPlan)
168+
d.Set("after_run", context.Hooks.AfterRun)
169+
d.Set("before_apply", context.Hooks.BeforeApply)
170+
d.Set("before_destroy", context.Hooks.BeforeDestroy)
171+
d.Set("before_init", context.Hooks.BeforeInit)
172+
d.Set("before_perform", context.Hooks.BeforePerform)
173+
d.Set("before_plan", context.Hooks.BeforePlan)
174+
87175
return nil
88176
}

spacelift/data_context_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ func TestContextData(t *testing.T) {
2020
name = "Provider test context %s"
2121
description = "description"
2222
labels = ["one", "two"]
23+
after_apply = ["after_apply"]
24+
after_destroy = ["after_destroy"]
25+
after_init = ["after_init"]
26+
after_perform = ["after_perform"]
27+
after_plan = ["after_plan"]
28+
after_run = ["after_run"]
29+
before_apply = ["before_apply"]
30+
before_destroy = ["before_destroy"]
31+
before_init = ["before_init"]
32+
before_perform = ["before_perform"]
33+
before_plan = ["before_plan"]
2334
}
2435
data "spacelift_context" "test" {
2536
context_id = spacelift_context.test.id
@@ -31,6 +42,28 @@ func TestContextData(t *testing.T) {
3142
Attribute("name", StartsWith("Provider test context")),
3243
Attribute("description", Equals("description")),
3344
SetEquals("labels", "one", "two"),
45+
Attribute("after_apply.#", Equals("1")),
46+
Attribute("after_apply.0", Equals("after_apply")),
47+
Attribute("after_destroy.#", Equals("1")),
48+
Attribute("after_destroy.0", Equals("after_destroy")),
49+
Attribute("after_init.#", Equals("1")),
50+
Attribute("after_init.0", Equals("after_init")),
51+
Attribute("after_perform.#", Equals("1")),
52+
Attribute("after_perform.0", Equals("after_perform")),
53+
Attribute("after_plan.#", Equals("1")),
54+
Attribute("after_plan.0", Equals("after_plan")),
55+
Attribute("after_run.#", Equals("1")),
56+
Attribute("after_run.0", Equals("after_run")),
57+
Attribute("before_apply.#", Equals("1")),
58+
Attribute("before_apply.0", Equals("before_apply")),
59+
Attribute("before_destroy.#", Equals("1")),
60+
Attribute("before_destroy.0", Equals("before_destroy")),
61+
Attribute("before_init.#", Equals("1")),
62+
Attribute("before_init.0", Equals("before_init")),
63+
Attribute("before_perform.#", Equals("1")),
64+
Attribute("before_perform.0", Equals("before_perform")),
65+
Attribute("before_plan.#", Equals("1")),
66+
Attribute("before_plan.0", Equals("before_plan")),
3467
),
3568
}})
3669
})

spacelift/internal/structs/context.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,17 @@ type Context struct {
77
Labels []string `graphql:"labels"`
88
Name string `graphql:"name"`
99
Space string `graphql:"space"`
10+
Hooks struct {
11+
AfterApply []string `graphql:"afterApply"`
12+
AfterDestroy []string `graphql:"afterDestroy"`
13+
AfterInit []string `graphql:"afterInit"`
14+
AfterPerform []string `graphql:"afterPerform"`
15+
AfterPlan []string `graphql:"afterPlan"`
16+
AfterRun []string `graphql:"afterRun"`
17+
BeforeApply []string `graphql:"beforeApply"`
18+
BeforeDestroy []string `graphql:"beforeDestroy"`
19+
BeforeInit []string `graphql:"beforeInit"`
20+
BeforePerform []string `graphql:"beforePerform"`
21+
BeforePlan []string `graphql:"beforePlan"`
22+
} `graphql:"hooks"`
1023
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package structs
2+
3+
import "github.com/shurcooL/graphql"
4+
5+
// ContextInput represents the input required to create or update a Context.
6+
type ContextInput struct {
7+
Name graphql.String `json:"name"`
8+
Description *graphql.String `json:"description"`
9+
Hooks *HooksInput `json:"hooks"`
10+
Labels *[]graphql.String `json:"labels"`
11+
Space *graphql.ID `json:"space"`
12+
}
13+
14+
// HooksInput represents the input required to create or update Hooks.
15+
type HooksInput struct {
16+
AfterApply []graphql.String `json:"afterApply"`
17+
AfterDestroy []graphql.String `json:"afterDestroy"`
18+
AfterInit []graphql.String `json:"afterInit"`
19+
AfterPerform []graphql.String `json:"afterPerform"`
20+
AfterPlan []graphql.String `json:"afterPlan"`
21+
AfterRun []graphql.String `json:"afterRun"`
22+
BeforeApply []graphql.String `json:"beforeApply"`
23+
BeforeDestroy []graphql.String `json:"beforeDestroy"`
24+
BeforeInit []graphql.String `json:"beforeInit"`
25+
BeforePerform []graphql.String `json:"beforePerform"`
26+
BeforePlan []graphql.String `json:"beforePlan"`
27+
}

0 commit comments

Comments
 (0)