Skip to content

Commit

Permalink
feat(env-vars): Add description field to Terraform spacelift_environm…
Browse files Browse the repository at this point in the history
…ent_variable resource (#564)
  • Loading branch information
0michalsokolowski0 authored Aug 14, 2024
1 parent 2c80c47 commit dee0f60
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 69 deletions.
1 change: 1 addition & 0 deletions docs/data-sources/environment_variable.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ data "spacelift_environment_variable" "core-kubeconfig" {
### Read-Only

- `checksum` (String) SHA-256 checksum of the value
- `description` (String) Description of the environment variable
- `id` (String) The ID of this resource.
- `value` (String, Sensitive) value of the environment variable
- `write_only` (Boolean) indicates whether the value can be read back outside a Run
28 changes: 16 additions & 12 deletions docs/resources/environment_variable.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,29 @@ description: |-
```terraform
# For a context
resource "spacelift_environment_variable" "ireland-kubeconfig" {
context_id = "prod-k8s-ie"
name = "KUBECONFIG"
value = "/project/spacelift/kubeconfig"
write_only = false
context_id = "prod-k8s-ie"
name = "KUBECONFIG"
value = "/project/spacelift/kubeconfig"
write_only = false
description = "Kubeconfig for Ireland Kubernetes cluster"
}
# For a module
resource "spacelift_environment_variable" "module-kubeconfig" {
module_id = "k8s-module"
name = "KUBECONFIG"
value = "/project/spacelift/kubeconfig"
write_only = false
module_id = "k8s-module"
name = "KUBECONFIG"
value = "/project/spacelift/kubeconfig"
write_only = false
description = "Kubeconfig for the module"
}
# For a stack
resource "spacelift_environment_variable" "core-kubeconfig" {
stack_id = "k8s-core"
name = "KUBECONFIG"
value = "/project/spacelift/kubeconfig"
write_only = false
stack_id = "k8s-core"
name = "KUBECONFIG"
value = "/project/spacelift/kubeconfig"
write_only = false
description = "Kubeconfig for the core stack"
}
```

Expand All @@ -48,6 +51,7 @@ resource "spacelift_environment_variable" "core-kubeconfig" {
### Optional

- `context_id` (String) ID of the context on which the environment variable is defined
- `description` (String) Description of the environment variable
- `module_id` (String) ID of the module on which the environment variable is defined
- `stack_id` (String) ID of the stack on which the environment variable is defined
- `value` (String, Sensitive) Value of the environment variable. Defaults to an empty string.
Expand Down
27 changes: 15 additions & 12 deletions examples/resources/spacelift_environment_variable/resource.tf
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
# For a context
resource "spacelift_environment_variable" "ireland-kubeconfig" {
context_id = "prod-k8s-ie"
name = "KUBECONFIG"
value = "/project/spacelift/kubeconfig"
write_only = false
context_id = "prod-k8s-ie"
name = "KUBECONFIG"
value = "/project/spacelift/kubeconfig"
write_only = false
description = "Kubeconfig for Ireland Kubernetes cluster"
}

# For a module
resource "spacelift_environment_variable" "module-kubeconfig" {
module_id = "k8s-module"
name = "KUBECONFIG"
value = "/project/spacelift/kubeconfig"
write_only = false
module_id = "k8s-module"
name = "KUBECONFIG"
value = "/project/spacelift/kubeconfig"
write_only = false
description = "Kubeconfig for the module"
}

# For a stack
resource "spacelift_environment_variable" "core-kubeconfig" {
stack_id = "k8s-core"
name = "KUBECONFIG"
value = "/project/spacelift/kubeconfig"
write_only = false
stack_id = "k8s-core"
name = "KUBECONFIG"
value = "/project/spacelift/kubeconfig"
write_only = false
description = "Kubeconfig for the core stack"
}
11 changes: 11 additions & 0 deletions spacelift/data_environment_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ func dataEnvironmentVariable() *schema.Resource {
Description: "indicates whether the value can be read back outside a Run",
Computed: true,
},
"description": {
Type: schema.TypeString,
Description: "Description of the environment variable",
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -193,4 +198,10 @@ func populateEnvironmentVariable(d *schema.ResourceData, el *structs.ConfigEleme
} else {
d.Set("value", nil)
}

if el.Description != nil {
d.Set("description", *el.Description)
} else {
d.Set("description", nil)
}
}
27 changes: 16 additions & 11 deletions spacelift/data_environment_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ func TestEnvironmentVariableData(t *testing.T) {
resource "spacelift_environment_variable" "test" {
context_id = spacelift_context.test.id
name = "BACON"
value = "is tasty"
write_only = true
name = "BACON"
value = "is tasty"
write_only = true
description = "Bacon is tasty"
}
data "spacelift_environment_variable" "test" {
context_id = spacelift_environment_variable.test.context_id
name = "BACON"
context_id = spacelift_environment_variable.test.context_id
name = "BACON"
}
`, randomID),
Check: Resource(
Expand All @@ -39,6 +40,7 @@ func TestEnvironmentVariableData(t *testing.T) {
Attribute("name", Equals("BACON")),
Attribute("value", IsEmpty()),
Attribute("write_only", Equals("true")),
Attribute("description", Equals("Bacon is tasty")),
AttributeNotPresent("module_id"),
AttributeNotPresent("stack_id"),
),
Expand All @@ -57,10 +59,10 @@ func TestEnvironmentVariableData(t *testing.T) {
}
resource "spacelift_environment_variable" "test" {
module_id = spacelift_module.test.id
name = "BACON"
value = "is tasty"
write_only = false
module_id = spacelift_module.test.id
name = "BACON"
value = "is tasty"
write_only = false
}
data "spacelift_environment_variable" "test" {
Expand All @@ -73,6 +75,7 @@ func TestEnvironmentVariableData(t *testing.T) {
Attribute("module_id", Equals(fmt.Sprintf("terraform-default-test-module-%s", randomID))),
Attribute("value", Equals("is tasty")),
Attribute("write_only", Equals("false")),
Attribute("description", IsEmpty()),
AttributeNotPresent("context_id"),
AttributeNotPresent("stack_id"),
),
Expand All @@ -92,8 +95,9 @@ func TestEnvironmentVariableData(t *testing.T) {
resource "spacelift_environment_variable" "test" {
stack_id = spacelift_stack.test.id
value = "is tasty"
name = "BACON"
value = "is tasty"
name = "BACON"
description = "Bacon is tasty"
}
data "spacelift_environment_variable" "test" {
Expand All @@ -105,6 +109,7 @@ func TestEnvironmentVariableData(t *testing.T) {
"data.spacelift_environment_variable.test",
Attribute("stack_id", StartsWith("test-stack-")),
Attribute("stack_id", Contains(randomID)),
Attribute("description", Equals("Bacon is tasty")),
AttributeNotPresent("context_id"),
AttributeNotPresent("module_id"),
),
Expand Down
11 changes: 6 additions & 5 deletions spacelift/internal/structs/config_element.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package structs

// ConfigElement represents a single configuration element.
type ConfigElement struct {
ID string
Checksum string
Type ConfigType
Value *string
WriteOnly bool
ID string
Checksum string
Type ConfigType
Value *string
WriteOnly bool
Description *string
}
9 changes: 5 additions & 4 deletions spacelift/internal/structs/config_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ type ConfigType string
// ConfigInput represents the input required to create or update a config
// element.
type ConfigInput struct {
ID graphql.ID `json:"id"`
Type ConfigType `json:"type"`
Value graphql.String `json:"value"`
WriteOnly graphql.Boolean `json:"writeOnly"`
ID graphql.ID `json:"id"`
Type ConfigType `json:"type"`
Value graphql.String `json:"value"`
WriteOnly graphql.Boolean `json:"writeOnly"`
Description *graphql.String `json:"description"`
}
21 changes: 17 additions & 4 deletions spacelift/resource_environment_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,24 @@ func resourceEnvironmentVariable() *schema.Resource {
Default: true,
ForceNew: true,
},
"description": {
Type: schema.TypeString,
Description: "Description of the environment variable",
Optional: true,
ForceNew: true,
},
},
}
}

func resourceEnvironmentVariableCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
variables := map[string]interface{}{
"config": structs.ConfigInput{
ID: toID(d.Get("name")),
Type: structs.ConfigType("ENVIRONMENT_VARIABLE"),
Value: toString(d.Get("value")),
WriteOnly: graphql.Boolean(d.Get("write_only").(bool)),
ID: toID(d.Get("name")),
Type: structs.ConfigType("ENVIRONMENT_VARIABLE"),
Value: toString(d.Get("value")),
WriteOnly: graphql.Boolean(d.Get("write_only").(bool)),
Description: toOptionalString(d.Get("description")),
},
}

Expand Down Expand Up @@ -204,6 +211,12 @@ func resourceEnvironmentVariableRead(ctx context.Context, d *schema.ResourceData
d.Set("value", element.Checksum)
}

if description := element.Description; description != nil {
d.Set("description", *description)
} else {
d.Set("description", nil)
}

return nil
}

Expand Down
47 changes: 26 additions & 21 deletions spacelift/resource_environment_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,41 @@ import (
)

func TestEnvironmentVariableResource(t *testing.T) {
const resourceName = "spacelift_environment_variable.test"

t.Run("with a context", func(t *testing.T) {
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)

config := func(writeOnly bool) string {
config := func(writeOnly bool, description string) string {
return fmt.Sprintf(`
resource "spacelift_context" "test" {
name = "My first context %s"
}
resource "spacelift_environment_variable" "test" {
context_id = spacelift_context.test.id
name = "BACON"
value = "is tasty"
write_only = %t
context_id = spacelift_context.test.id
name = "BACON"
value = "is tasty"
write_only = %t
description = %s
}
`, randomID, writeOnly)
`, randomID, writeOnly, description)
}

const resourceName = "spacelift_environment_variable.test"

testSteps(t, []resource.TestStep{
{
Config: config(true),
Config: config(true, `"Bacon is tasty"`),
Check: Resource(
"spacelift_environment_variable.test",
resourceName,
Attribute("id", IsNotEmpty()),
Attribute("checksum", Equals("4d5d01ea427b10dd483e8fce5b5149fb5a9814e9ee614176b756ca4a65c8f154")),
Attribute("context_id", Contains(randomID)),
Attribute("name", Equals("BACON")),
Attribute("value", Equals("4d5d01ea427b10dd483e8fce5b5149fb5a9814e9ee614176b756ca4a65c8f154")),
Attribute("write_only", Equals("true")),
Attribute("description", Equals("Bacon is tasty")),
AttributeNotPresent("module_id"),
AttributeNotPresent("stack_id"),
),
Expand All @@ -52,11 +56,12 @@ func TestEnvironmentVariableResource(t *testing.T) {
ImportStateVerify: true,
},
{
Config: config(false),
Config: config(false, "null"),
Check: Resource(
"spacelift_environment_variable.test",
resourceName,
Attribute("value", Equals("is tasty")),
Attribute("write_only", Equals("false")),
Attribute("description", IsEmpty()),
),
},
})
Expand All @@ -65,8 +70,6 @@ func TestEnvironmentVariableResource(t *testing.T) {
t.Run("with a module", func(t *testing.T) {
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)

const resourceName = "spacelift_environment_variable.test"

testSteps(t, []resource.TestStep{
{
Config: fmt.Sprintf(`
Expand All @@ -77,16 +80,18 @@ func TestEnvironmentVariableResource(t *testing.T) {
}
resource "spacelift_environment_variable" "test" {
module_id = spacelift_module.test.id
name = "BACON"
value = "is tasty"
module_id = spacelift_module.test.id
name = "BACON"
value = "is tasty"
description = "Bacon is tasty"
}
`, randomID),
Check: Resource(
resourceName,
Attribute("module_id", Equals(fmt.Sprintf("terraform-default-test-module-%s", randomID))),
Attribute("value", Equals("4d5d01ea427b10dd483e8fce5b5149fb5a9814e9ee614176b756ca4a65c8f154")),
Attribute("write_only", Equals("true")),
Attribute("description", Equals("Bacon is tasty")),
AttributeNotPresent("context_id"),
AttributeNotPresent("stack_id"),
),
Expand All @@ -100,8 +105,6 @@ func TestEnvironmentVariableResource(t *testing.T) {
})

t.Run("with a stack", func(t *testing.T) {
const resourceName = "spacelift_environment_variable.test"

randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)

testSteps(t, []resource.TestStep{
Expand All @@ -114,16 +117,18 @@ func TestEnvironmentVariableResource(t *testing.T) {
}
resource "spacelift_environment_variable" "test" {
stack_id = spacelift_stack.test.id
value = "is tasty"
name = "BACON"
stack_id = spacelift_stack.test.id
value = "is tasty"
name = "BACON"
description = "Bacon is tasty"
}
`, randomID),
Check: Resource(
"spacelift_environment_variable.test",
resourceName,
Attribute("stack_id", StartsWith("test-stack-")),
Attribute("stack_id", Contains(randomID)),
Attribute("value", Equals("4d5d01ea427b10dd483e8fce5b5149fb5a9814e9ee614176b756ca4a65c8f154")),
Attribute("description", Equals("Bacon is tasty")),
AttributeNotPresent("context_id"),
AttributeNotPresent("module_id"),
),
Expand Down

0 comments on commit dee0f60

Please sign in to comment.