Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
mtojek merged 10 commits intomainfrom13218-workspace-tags
May 16, 2024
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletionsexamples/resources/coder_workspace_tags/resource.tf
View file
Open in desktop
Original file line numberDiff line numberDiff 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"
}
}
28 changes: 17 additions & 11 deletionsprovider/examples_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,19 +14,25 @@ import (
func TestExamples(t *testing.T) {
t.Parallel()

t.Run("coder_parameter", func(t *testing.T) {
t.Parallel()
for _, testDir := range []string{
"coder_parameter",
"coder_workspace_tags",
} {
t.Run(testDir, func(t *testing.T) {
testDir := testDir
t.Parallel()

resource.Test(t, resource.TestCase{
Providers: map[string]*schema.Provider{
"coder": provider.New(),
},
IsUnitTest: true,
Steps: []resource.TestStep{{
Config: mustReadFile(t, "../examples/resources/coder_parameter/resource.tf"),
}},
resource.Test(t, resource.TestCase{
Providers: map[string]*schema.Provider{
"coder": provider.New(),
},
IsUnitTest: true,
Steps: []resource.TestStep{{
Config: mustReadFile(t, "../examples/resources/"+testDir+"/resource.tf"),
}},
})
})
})
}
}

func mustReadFile(t *testing.T, path string) string {
Expand Down
11 changes: 6 additions & 5 deletionsprovider/provider.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -68,11 +68,12 @@ func New() *schema.Provider {
}, nil
},
DataSourcesMap: map[string]*schema.Resource{
"coder_workspace": workspaceDataSource(),
"coder_provisioner": provisionerDataSource(),
"coder_parameter": parameterDataSource(),
"coder_git_auth": gitAuthDataSource(),
"coder_external_auth": externalAuthDataSource(),
"coder_workspace": workspaceDataSource(),
"coder_workspace_tags": workspaceTagDataSource(),
"coder_provisioner": provisionerDataSource(),
"coder_parameter": parameterDataSource(),
"coder_git_auth": gitAuthDataSource(),
"coder_external_auth": externalAuthDataSource(),
},
ResourcesMap: map[string]*schema.Resource{
"coder_agent": agentResource(),
Expand Down
32 changes: 32 additions & 0 deletionsprovider/workspace_tags.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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())
Copy link
MemberAuthor

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 ofvalue properties. Unfortunately, I hit an interface blocker. Here is the story:

  1. PassCODER_TF_WORK_DIR from provisioner with disk path to the template.
  2. Useterraform-config-inspect to load module and get references (filename + pos) to data resources.
  3. Read raw attributes of the relevant data resource.

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.

Copy link
Contributor

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 atbuild time to allow other terraform resources to read data. I think we can just return the actual tagvalues 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

Copy link
MemberAuthor

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.

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.

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},
},
},
}
}
48 changes: 48 additions & 0 deletionsprovider/workspace_tags_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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
},
}},
})
}

[8]ページ先頭

©2009-2025 Movatter.jp