Skip to content

Commit

Permalink
Add additional_project_globs to stack
Browse files Browse the repository at this point in the history
Signed-off-by: tomasmik <[email protected]>
  • Loading branch information
tomasmik committed Nov 23, 2023
1 parent 4961bcb commit a8171d4
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 88 deletions.
1 change: 1 addition & 0 deletions docs/data-sources/stack.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ data "spacelift_stack" "k8s-core" {

### Optional

- `additional_project_globs` (Set of String) Project globs is an optional list of paths to track changes of in addition to the project root.
- `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
Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/stacks.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ Required:

Read-Only:

- `additional_project_globs` (Set of String)
- `administrative` (Boolean)
- `after_apply` (List of String)
- `after_destroy` (List of String)
Expand Down
1 change: 1 addition & 0 deletions docs/resources/stack.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ resource "spacelift_stack" "ansible-stack" {

### Optional

- `additional_project_globs` (Set of String) Project globs is an optional list of paths to track changes of in addition to the project root.
- `administrative` (Boolean) Indicates whether this stack can manage others. Defaults to `false`.
- `after_apply` (List of String) List of after-apply scripts
- `after_destroy` (List of String) List of after-destroy scripts
Expand Down
12 changes: 12 additions & 0 deletions spacelift/data_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@ func dataStack() *schema.Resource {
Description: "Project root is the optional directory relative to the workspace root containing the entrypoint to the Stack.",
Computed: true,
},
"additional_project_globs": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Description: "Project globs is an optional list of paths to track changes of in addition to the project root.",
},
"protect_from_deletion": {
Type: schema.TypeBool,
Description: "Protect this stack from accidental deletion. If set, attempts to delete this stack will fail.",
Expand Down Expand Up @@ -451,6 +457,12 @@ func dataStackRead(ctx context.Context, d *schema.ResourceData, meta interface{}
}
d.Set("labels", labels)

globs := schema.NewSet(schema.HashString, []interface{}{})
for _, gb := range stack.AdditionalProjectGlobs {
globs.Add(gb)
}
d.Set("additional_project_globs", globs)

if iacKey, iacSettings := stack.IaCSettings(); iacKey != "" {
if err := d.Set(iacKey, []interface{}{iacSettings}); err != nil {
return diag.Errorf("could not set IaC settings: %v", err)
Expand Down
2 changes: 2 additions & 0 deletions spacelift/data_stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestStackData(t *testing.T) {
labels = ["one", "two"]
name = "Test stack %s"
project_root = "root"
additional_project_globs = ["/bacon", "/bacon/eggs/*"]
repository = "demo"
runner_image = "custom_image:runner"
terraform_workspace = "bacon"
Expand Down Expand Up @@ -83,6 +84,7 @@ func TestStackData(t *testing.T) {
SetEquals("labels", "one", "two"),
Attribute("name", StartsWith("Test stack")),
Attribute("project_root", Equals("root")),
SetEquals("additional_project_globs", "/bacon", "/bacon/eggs/*"),
Attribute("repository", Equals("demo")),
Attribute("runner_image", Equals("custom_image:runner")),
Attribute("terraform_workspace", Equals("bacon")),
Expand Down
71 changes: 36 additions & 35 deletions spacelift/internal/structs/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,42 @@ const StackConfigVendorKubernetes = "StackConfigVendorKubernetes"

// Stack represents the Stack data relevant to the provider.
type Stack struct {
ID string `graphql:"id"`
Administrative bool `graphql:"administrative"`
AfterApply []string `graphql:"afterApply"`
AfterDestroy []string `graphql:"afterDestroy"`
AfterInit []string `graphql:"afterInit"`
AfterPerform []string `graphql:"afterPerform"`
AfterPlan []string `graphql:"afterPlan"`
AfterRun []string `graphql:"afterRun"`
Autodeploy bool `graphql:"autodeploy"`
Autoretry bool `graphql:"autoretry"`
BeforeApply []string `graphql:"beforeApply"`
BeforeDestroy []string `graphql:"beforeDestroy"`
BeforeInit []string `graphql:"beforeInit"`
BeforePerform []string `graphql:"beforePerform"`
BeforePlan []string `graphql:"beforePlan"`
Branch string `graphql:"branch"`
Deleting bool `graphql:"deleting"`
Description *string `graphql:"description"`
IsDisabled bool `graphql:"isDisabled"`
GitHubActionDeploy bool `graphql:"githubActionDeploy"`
Integrations *Integrations `graphql:"integrations"`
Labels []string `graphql:"labels"`
LocalPreviewEnabled bool `graphql:"localPreviewEnabled"`
ManagesStateFile bool `graphql:"managesStateFile"`
Name string `graphql:"name"`
Namespace string `graphql:"namespace"`
ProjectRoot *string `graphql:"projectRoot"`
ProtectFromDeletion bool `graphql:"protectFromDeletion"`
Provider string `graphql:"provider"`
Repository string `graphql:"repository"`
RepositoryURL *string `graphql:"repositoryURL"`
RunnerImage *string `graphql:"runnerImage"`
Space string `graphql:"space"`
TerraformVersion *string `graphql:"terraformVersion"`
VendorConfig struct {
ID string `graphql:"id"`
Administrative bool `graphql:"administrative"`
AfterApply []string `graphql:"afterApply"`
AfterDestroy []string `graphql:"afterDestroy"`
AfterInit []string `graphql:"afterInit"`
AfterPerform []string `graphql:"afterPerform"`
AfterPlan []string `graphql:"afterPlan"`
AfterRun []string `graphql:"afterRun"`
Autodeploy bool `graphql:"autodeploy"`
Autoretry bool `graphql:"autoretry"`
BeforeApply []string `graphql:"beforeApply"`
BeforeDestroy []string `graphql:"beforeDestroy"`
BeforeInit []string `graphql:"beforeInit"`
BeforePerform []string `graphql:"beforePerform"`
BeforePlan []string `graphql:"beforePlan"`
Branch string `graphql:"branch"`
Deleting bool `graphql:"deleting"`
Description *string `graphql:"description"`
IsDisabled bool `graphql:"isDisabled"`
GitHubActionDeploy bool `graphql:"githubActionDeploy"`
Integrations *Integrations `graphql:"integrations"`
Labels []string `graphql:"labels"`
LocalPreviewEnabled bool `graphql:"localPreviewEnabled"`
ManagesStateFile bool `graphql:"managesStateFile"`
Name string `graphql:"name"`
Namespace string `graphql:"namespace"`
ProjectRoot *string `graphql:"projectRoot"`
AdditionalProjectGlobs []string `graphql:"additionalProjectGlobs"`
ProtectFromDeletion bool `graphql:"protectFromDeletion"`
Provider string `graphql:"provider"`
Repository string `graphql:"repository"`
RepositoryURL *string `graphql:"repositoryURL"`
RunnerImage *string `graphql:"runnerImage"`
Space string `graphql:"space"`
TerraformVersion *string `graphql:"terraformVersion"`
VendorConfig struct {
Typename string `graphql:"__typename"`
Ansible struct {
Playbook string `graphql:"playbook"`
Expand Down
61 changes: 31 additions & 30 deletions spacelift/internal/structs/stack_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,37 @@ import "github.com/shurcooL/graphql"

// StackInput represents the input required to create or update a Stack.
type StackInput struct {
Administrative graphql.Boolean `json:"administrative"`
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"`
Autodeploy graphql.Boolean `json:"autodeploy"`
Autoretry graphql.Boolean `json:"autoretry"`
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"`
Branch graphql.String `json:"branch"`
Description *graphql.String `json:"description"`
GitHubActionDeploy graphql.Boolean `json:"githubActionDeploy"`
Labels *[]graphql.String `json:"labels"`
LocalPreviewEnabled graphql.Boolean `json:"localPreviewEnabled"`
Name graphql.String `json:"name"`
Namespace *graphql.String `json:"namespace"`
ProjectRoot *graphql.String `json:"projectRoot"`
ProtectFromDeletion graphql.Boolean `json:"protectFromDeletion"`
Provider *graphql.String `json:"provider"`
Repository graphql.String `json:"repository"`
RepositoryURL *graphql.String `json:"repositoryURL"`
RunnerImage *graphql.String `json:"runnerImage"`
Space *graphql.String `json:"space"`
VendorConfig *VendorConfigInput `json:"vendorConfig"`
WorkerPool *graphql.ID `json:"workerPool"`
Administrative graphql.Boolean `json:"administrative"`
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"`
Autodeploy graphql.Boolean `json:"autodeploy"`
Autoretry graphql.Boolean `json:"autoretry"`
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"`
Branch graphql.String `json:"branch"`
Description *graphql.String `json:"description"`
GitHubActionDeploy graphql.Boolean `json:"githubActionDeploy"`
Labels *[]graphql.String `json:"labels"`
LocalPreviewEnabled graphql.Boolean `json:"localPreviewEnabled"`
Name graphql.String `json:"name"`
Namespace *graphql.String `json:"namespace"`
ProjectRoot *graphql.String `json:"projectRoot"`
AddditionalProjectGlobs *[]graphql.String `json:"additionalProjectGlobs"`
ProtectFromDeletion graphql.Boolean `json:"protectFromDeletion"`
Provider *graphql.String `json:"provider"`
Repository graphql.String `json:"repository"`
RepositoryURL *graphql.String `json:"repositoryURL"`
RunnerImage *graphql.String `json:"runnerImage"`
Space *graphql.String `json:"space"`
VendorConfig *VendorConfigInput `json:"vendorConfig"`
WorkerPool *graphql.ID `json:"workerPool"`
}

// VendorConfigInput represents vendor-specific configuration.
Expand Down
14 changes: 14 additions & 0 deletions spacelift/resource_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,12 @@ func resourceStack() *schema.Resource {
Description: "Project root is the optional directory relative to the workspace root containing the entrypoint to the Stack.",
Optional: true,
},
"additional_project_globs": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Description: "Project globs is an optional list of paths to track changes of in addition to the project root.",
},
"protect_from_deletion": {
Type: schema.TypeBool,
Description: "Protect this stack from accidental deletion. If set, attempts to delete this stack will fail. Defaults to `false`.",
Expand Down Expand Up @@ -777,6 +783,14 @@ func stackInput(d *schema.ResourceData) structs.StackInput {
ret.ProjectRoot = toOptionalString(projectRoot)
}

if globsSet, ok := d.Get("additional_project_globs").(*schema.Set); ok {
var gbs []graphql.String
for _, gb := range globsSet.List() {
gbs = append(gbs, graphql.String(gb.(string)))
}
ret.AddditionalProjectGlobs = &gbs
}

if runnerImage, ok := d.GetOk("runner_image"); ok {
ret.RunnerImage = toOptionalString(runnerImage)
}
Expand Down
48 changes: 25 additions & 23 deletions spacelift/resource_stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,30 @@ func TestStackResource(t *testing.T) {
config := func(description string, protectFromDeletion bool) string {
return fmt.Sprintf(`
resource "spacelift_stack" "test" {
administrative = true
after_apply = ["ls -la", "rm -rf /"]
after_destroy = ["echo 'after_destroy'"]
after_init = ["terraform fmt -check", "tflint"]
after_perform = ["echo 'after_perform'"]
after_plan = ["echo 'after_plan'"]
after_run = ["echo 'after_run'"]
autodeploy = true
autoretry = false
before_apply = ["ls -la", "rm -rf /"]
before_destroy = ["echo 'before_destroy'"]
before_init = ["terraform fmt -check", "tflint"]
before_perform = ["echo 'before_perform'"]
before_plan = ["echo 'before_plan'"]
branch = "master"
description = "%s"
import_state = "{}"
labels = ["one", "two"]
name = "Provider test stack %s"
project_root = "root"
protect_from_deletion = %t
repository = "demo"
runner_image = "custom_image:runner"
administrative = true
after_apply = ["ls -la", "rm -rf /"]
after_destroy = ["echo 'after_destroy'"]
after_init = ["terraform fmt -check", "tflint"]
after_perform = ["echo 'after_perform'"]
after_plan = ["echo 'after_plan'"]
after_run = ["echo 'after_run'"]
autodeploy = true
autoretry = false
before_apply = ["ls -la", "rm -rf /"]
before_destroy = ["echo 'before_destroy'"]
before_init = ["terraform fmt -check", "tflint"]
before_perform = ["echo 'before_perform'"]
before_plan = ["echo 'before_plan'"]
branch = "master"
description = "%s"
import_state = "{}"
labels = ["one", "two"]
name = "Provider test stack %s"
project_root = "root"
additional_project_globs = ["/bacon", "/bacon/eggs/*"]
protect_from_deletion = %t
repository = "demo"
runner_image = "custom_image:runner"
}
`, description, randomID, protectFromDeletion)
}
Expand Down Expand Up @@ -90,6 +91,7 @@ func TestStackResource(t *testing.T) {
SetEquals("labels", "one", "two"),
Attribute("name", StartsWith("Provider test stack")),
Attribute("project_root", Equals("root")),
SetEquals("additional_project_globs", "/bacon", "/bacon/eggs/*"),
Attribute("protect_from_deletion", Equals("true")),
Attribute("repository", Equals("demo")),
Attribute("runner_image", Equals("custom_image:runner")),
Expand Down

0 comments on commit a8171d4

Please sign in to comment.