-
Notifications
You must be signed in to change notification settings - Fork 22
feat: support workspace tags #223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
85760c6
feat: support workspace tags
mtojek 2601c43
fmt
mtojek 434b170
WIP
mtojek 2b87956
WIP
mtojek 537e372
unit test
mtojek 1d66a39
Env
mtojek f0f2f34
just basic data resource
mtojek 98d4df8
fmt
mtojek 9a46f65
Tags as map
mtojek 4aa79df
james and chris
mtojek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
provider "coder" {} | ||
|
||
data "coder_parameter" "os_selector" { | ||
name = "os_selector" | ||
display_name = "Operating System" | ||
mutable = false | ||
|
||
default = "osx" | ||
|
||
option { | ||
icon = "/icons/linux.png" | ||
name = "Linux" | ||
value = "linux" | ||
} | ||
option { | ||
icon = "/icons/osx.png" | ||
name = "OSX" | ||
value = "osx" | ||
} | ||
option { | ||
icon = "/icons/windows.png" | ||
name = "Windows" | ||
value = "windows" | ||
} | ||
} | ||
|
||
data "coder_parameter" "feature_cache_enabled" { | ||
name = "feature_cache_enabled" | ||
display_name = "Enable cache?" | ||
type = "bool" | ||
|
||
default = false | ||
} | ||
|
||
data "coder_parameter" "feature_debug_enabled" { | ||
name = "feature_debug_enabled" | ||
display_name = "Enable debug?" | ||
type = "bool" | ||
|
||
default = true | ||
} | ||
|
||
data "coder_workspace_tags" "custom_workspace_tags" { | ||
tags = { | ||
"cluster" = "developers" | ||
"os" = data.coder_parameter.os_selector.value | ||
"debug" = "${data.coder_parameter.feature_debug_enabled.value}+12345" | ||
"cache" = data.coder_parameter.feature_cache_enabled.value == "true" ? "nix-with-cache" : "no-cache" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/google/uuid" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
type WorkspaceTags struct { | ||
Tags map[string]string | ||
} | ||
|
||
func workspaceTagDataSource() *schema.Resource { | ||
return &schema.Resource{ | ||
Description: "Use this data source to configure workspace tags to select provisioners.", | ||
ReadContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics { | ||
rd.SetId(uuid.NewString()) | ||
return nil | ||
}, | ||
Schema: map[string]*schema.Schema{ | ||
"tags": { | ||
Type: schema.TypeMap, | ||
Description: `Key-value map with workspace tags`, | ||
ForceNew: true, | ||
Optional: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
}, | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package provider_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/coder/terraform-provider-coder/provider" | ||
) | ||
|
||
func TestWorkspaceTags(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
Providers: map[string]*schema.Provider{ | ||
"coder": provider.New(), | ||
}, | ||
IsUnitTest: true, | ||
Steps: []resource.TestStep{{ | ||
Config: ` | ||
provider "coder" { | ||
} | ||
data "coder_parameter" "animal" { | ||
name = "animal" | ||
type = "string" | ||
default = "chris" | ||
} | ||
data "coder_workspace_tags" "wt" { | ||
tags = { | ||
"cat" = "james" | ||
"dog" = data.coder_parameter.animal.value | ||
} | ||
}`, | ||
Check: func(state *terraform.State) error { | ||
require.Len(t, state.Modules, 1) | ||
require.Len(t, state.Modules[0].Resources, 2) | ||
resource := state.Modules[0].Resources["data.coder_workspace_tags.wt"] | ||
require.NotNil(t, resource) | ||
|
||
attribs := resource.Primary.Attributes | ||
require.Equal(t, "james", attribs["tags.cat"]) | ||
require.Equal(t, "chris", attribs["tags.dog"]) | ||
return nil | ||
}, | ||
}}, | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For reviewers:
Originally I wanted to place here the logic to load HCL files and read raw content of
value
properties. Unfortunately, I hit an interface blocker. Here is the story:CODER_TF_WORK_DIR
from provisioner with disk path to the template.BUT...
schema.ResourceData
is not aware of its own identity. It knows only its child properties, raw config and state diff. I'm afraid this is a blocker we can't find a workaround for, and we have to implement this logic in coderd.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why we want or need to do this here. This gets called at build time to allow other terraform resources to read data. I think we can just return the actual tag values and call it a day.
The loading of the files and parsing HCL should happen during the template version import, where we already have access to the raw files: https://github.com/coder/coder/blob/f14927955d7361d9a50c633e69fbebca2d9946cb/provisioner/terraform/parse.go#L26
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't want to leak HCL parsing from the provider, but as long as we're doing it in template version import, I will extend that part.