From 6189a910765daad176aee2fd11292a8394f2a1eb Mon Sep 17 00:00:00 2001 From: Garrett Delfosse <garrett@coder.com> Date: Tue, 2 Apr 2024 17:57:50 +0000 Subject: [PATCH 1/7] feat: add owner group to workspace data --- docs/data-sources/workspace.md | 1 + provider/workspace.go | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/docs/data-sources/workspace.md b/docs/data-sources/workspace.md index f5b434a9..2ed7c63d 100644 --- a/docs/data-sources/workspace.md +++ b/docs/data-sources/workspace.md @@ -32,6 +32,7 @@ resource "kubernetes_pod" "dev" { - `name` (String) Name of the workspace. - `owner` (String) Username of the workspace owner. - `owner_email` (String) Email address of the workspace owner. +- `owner_groups` (List of String) List of groups the workspace owner belongs to. - `owner_id` (String) UUID of the workspace owner. - `owner_name` (String) Name of the workspace owner. - `owner_oidc_access_token` (String) A valid OpenID Connect access token of the workspace owner. This is only available if the workspace owner authenticated with OpenID Connect. If a valid token cannot be obtained, this value will be an empty string. diff --git a/provider/workspace.go b/provider/workspace.go index be7bf03c..74988daa 100644 --- a/provider/workspace.go +++ b/provider/workspace.go @@ -36,6 +36,9 @@ func workspaceDataSource() *schema.Resource { ownerEmail := os.Getenv("CODER_WORKSPACE_OWNER_EMAIL") _ = rd.Set("owner_email", ownerEmail) + ownerGroups := os.Getenv("CODER_WORKSPACE_OWNER_GROUPS") + _ = rd.Set("owner_groups", ownerGroups) + ownerName := os.Getenv("CODER_WORKSPACE_OWNER_NAME") _ = rd.Set("owner_name", ownerName) @@ -141,6 +144,11 @@ func workspaceDataSource() *schema.Resource { "This is only available if the workspace owner authenticated with OpenID Connect. " + "If a valid token cannot be obtained, this value will be an empty string.", }, + "owner_groups": { + Type: schema.TypeList, + Computed: true, + Description: "List of groups the workspace owner belongs to. ", + }, "id": { Type: schema.TypeString, Computed: true, From f046a3a435dc035fa7b4d93d957075694f85372c Mon Sep 17 00:00:00 2001 From: Garrett Delfosse <garrett@coder.com> Date: Tue, 2 Apr 2024 18:48:41 +0000 Subject: [PATCH 2/7] make list --- provider/workspace.go | 5 ++++- provider/workspace_test.go | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/provider/workspace.go b/provider/workspace.go index 74988daa..f0a8533b 100644 --- a/provider/workspace.go +++ b/provider/workspace.go @@ -145,7 +145,10 @@ func workspaceDataSource() *schema.Resource { "If a valid token cannot be obtained, this value will be an empty string.", }, "owner_groups": { - Type: schema.TypeList, + Type: schema.TypeList, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, Computed: true, Description: "List of groups the workspace owner belongs to. ", }, diff --git a/provider/workspace_test.go b/provider/workspace_test.go index 38f9c743..c717b5cc 100644 --- a/provider/workspace_test.go +++ b/provider/workspace_test.go @@ -16,6 +16,7 @@ func TestWorkspace(t *testing.T) { t.Setenv("CODER_WORKSPACE_OWNER_NAME", "Mr Owner") t.Setenv("CODER_WORKSPACE_OWNER_EMAIL", "owner123@example.com") t.Setenv("CODER_WORKSPACE_OWNER_SESSION_TOKEN", "abc123") + t.Setenv("CODER_WORKSPACE_OWNER_GROUPS", "[ \"group1\", \"group2\" ]") t.Setenv("CODER_WORKSPACE_TEMPLATE_ID", "templateID") t.Setenv("CODER_WORKSPACE_TEMPLATE_NAME", "template123") t.Setenv("CODER_WORKSPACE_TEMPLATE_VERSION", "v1.2.3") @@ -47,6 +48,7 @@ func TestWorkspace(t *testing.T) { require.Equal(t, "Mr Owner", attribs["owner_name"]) require.Equal(t, "owner123@example.com", attribs["owner_email"]) require.Equal(t, "abc123", attribs["owner_session_token"]) + require.Equal(t, "[ \"group1\", \"group2\" ]", attribs["owner_groups"]) require.Equal(t, "templateID", attribs["template_id"]) require.Equal(t, "template123", attribs["template_name"]) require.Equal(t, "v1.2.3", attribs["template_version"]) @@ -80,6 +82,7 @@ func TestWorkspace(t *testing.T) { require.Equal(t, "owner123", attribs["owner"]) require.Equal(t, "Mr Owner", attribs["owner_name"]) require.Equal(t, "owner123@example.com", attribs["owner_email"]) + require.Equal(t, "[ \"group1\", \"group2\" ]", attribs["owner_groups"]) require.Equal(t, "templateID", attribs["template_id"]) require.Equal(t, "template123", attribs["template_name"]) require.Equal(t, "v1.2.3", attribs["template_version"]) From ee217c4dc097acfda69c1e1e9cb98afa1885fb91 Mon Sep 17 00:00:00 2001 From: Garrett Delfosse <garrett@coder.com> Date: Tue, 2 Apr 2024 20:04:10 +0000 Subject: [PATCH 3/7] convert to string --- provider/workspace.go | 7 ++----- provider/workspace_test.go | 6 +++--- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/provider/workspace.go b/provider/workspace.go index f0a8533b..b12973c9 100644 --- a/provider/workspace.go +++ b/provider/workspace.go @@ -145,12 +145,9 @@ func workspaceDataSource() *schema.Resource { "If a valid token cannot be obtained, this value will be an empty string.", }, "owner_groups": { - Type: schema.TypeList, - Elem: &schema.Schema{ - Type: schema.TypeString, - }, + Type: schema.TypeString, Computed: true, - Description: "List of groups the workspace owner belongs to. ", + Description: "Comma separated list of groups the workspace owner belongs to.", }, "id": { Type: schema.TypeString, diff --git a/provider/workspace_test.go b/provider/workspace_test.go index c717b5cc..f0eea7de 100644 --- a/provider/workspace_test.go +++ b/provider/workspace_test.go @@ -16,7 +16,7 @@ func TestWorkspace(t *testing.T) { t.Setenv("CODER_WORKSPACE_OWNER_NAME", "Mr Owner") t.Setenv("CODER_WORKSPACE_OWNER_EMAIL", "owner123@example.com") t.Setenv("CODER_WORKSPACE_OWNER_SESSION_TOKEN", "abc123") - t.Setenv("CODER_WORKSPACE_OWNER_GROUPS", "[ \"group1\", \"group2\" ]") + t.Setenv("CODER_WORKSPACE_OWNER_GROUPS", "group1,group2") t.Setenv("CODER_WORKSPACE_TEMPLATE_ID", "templateID") t.Setenv("CODER_WORKSPACE_TEMPLATE_NAME", "template123") t.Setenv("CODER_WORKSPACE_TEMPLATE_VERSION", "v1.2.3") @@ -48,7 +48,7 @@ func TestWorkspace(t *testing.T) { require.Equal(t, "Mr Owner", attribs["owner_name"]) require.Equal(t, "owner123@example.com", attribs["owner_email"]) require.Equal(t, "abc123", attribs["owner_session_token"]) - require.Equal(t, "[ \"group1\", \"group2\" ]", attribs["owner_groups"]) + require.Equal(t, "group1,group2", attribs["owner_groups"]) require.Equal(t, "templateID", attribs["template_id"]) require.Equal(t, "template123", attribs["template_name"]) require.Equal(t, "v1.2.3", attribs["template_version"]) @@ -82,7 +82,7 @@ func TestWorkspace(t *testing.T) { require.Equal(t, "owner123", attribs["owner"]) require.Equal(t, "Mr Owner", attribs["owner_name"]) require.Equal(t, "owner123@example.com", attribs["owner_email"]) - require.Equal(t, "[ \"group1\", \"group2\" ]", attribs["owner_groups"]) + require.Equal(t, "group1,group2", attribs["owner_groups"]) require.Equal(t, "templateID", attribs["template_id"]) require.Equal(t, "template123", attribs["template_name"]) require.Equal(t, "v1.2.3", attribs["template_version"]) From d96eefd2a94fe04865ad6bc13979a45cf5f697c9 Mon Sep 17 00:00:00 2001 From: Garrett Delfosse <garrett@coder.com> Date: Tue, 2 Apr 2024 20:06:36 +0000 Subject: [PATCH 4/7] update docs --- docs/data-sources/workspace.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/data-sources/workspace.md b/docs/data-sources/workspace.md index 2ed7c63d..1d3ef6dc 100644 --- a/docs/data-sources/workspace.md +++ b/docs/data-sources/workspace.md @@ -32,7 +32,7 @@ resource "kubernetes_pod" "dev" { - `name` (String) Name of the workspace. - `owner` (String) Username of the workspace owner. - `owner_email` (String) Email address of the workspace owner. -- `owner_groups` (List of String) List of groups the workspace owner belongs to. +- `owner_groups` (String) Comma separated list of groups the workspace owner belongs to. - `owner_id` (String) UUID of the workspace owner. - `owner_name` (String) Name of the workspace owner. - `owner_oidc_access_token` (String) A valid OpenID Connect access token of the workspace owner. This is only available if the workspace owner authenticated with OpenID Connect. If a valid token cannot be obtained, this value will be an empty string. From a5bd88b146d4ba795e2a40be75a6d8f1baf91beb Mon Sep 17 00:00:00 2001 From: Garrett Delfosse <garrett@coder.com> Date: Tue, 2 Apr 2024 22:28:57 +0000 Subject: [PATCH 5/7] switch back to list --- docs/data-sources/workspace.md | 2 +- provider/workspace.go | 13 ++++++++++--- provider/workspace_test.go | 8 +++++--- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/docs/data-sources/workspace.md b/docs/data-sources/workspace.md index 1d3ef6dc..2ed7c63d 100644 --- a/docs/data-sources/workspace.md +++ b/docs/data-sources/workspace.md @@ -32,7 +32,7 @@ resource "kubernetes_pod" "dev" { - `name` (String) Name of the workspace. - `owner` (String) Username of the workspace owner. - `owner_email` (String) Email address of the workspace owner. -- `owner_groups` (String) Comma separated list of groups the workspace owner belongs to. +- `owner_groups` (List of String) List of groups the workspace owner belongs to. - `owner_id` (String) UUID of the workspace owner. - `owner_name` (String) Name of the workspace owner. - `owner_oidc_access_token` (String) A valid OpenID Connect access token of the workspace owner. This is only available if the workspace owner authenticated with OpenID Connect. If a valid token cannot be obtained, this value will be an empty string. diff --git a/provider/workspace.go b/provider/workspace.go index b12973c9..5d97cff4 100644 --- a/provider/workspace.go +++ b/provider/workspace.go @@ -2,6 +2,7 @@ package provider import ( "context" + "encoding/json" "os" "reflect" "strconv" @@ -36,7 +37,12 @@ func workspaceDataSource() *schema.Resource { ownerEmail := os.Getenv("CODER_WORKSPACE_OWNER_EMAIL") _ = rd.Set("owner_email", ownerEmail) - ownerGroups := os.Getenv("CODER_WORKSPACE_OWNER_GROUPS") + ownerGroupsText := os.Getenv("CODER_WORKSPACE_OWNER_GROUPS") + var ownerGroups []string + err := json.Unmarshal([]byte(ownerGroupsText), &ownerGroups) + if err != nil { + return diag.Errorf("couldn't parse owner groups %q", ownerGroupsText) + } _ = rd.Set("owner_groups", ownerGroups) ownerName := os.Getenv("CODER_WORKSPACE_OWNER_NAME") @@ -145,9 +151,10 @@ func workspaceDataSource() *schema.Resource { "If a valid token cannot be obtained, this value will be an empty string.", }, "owner_groups": { - Type: schema.TypeString, + Type: schema.TypeList, + Elem: &schema.Schema{Type: schema.TypeString}, Computed: true, - Description: "Comma separated list of groups the workspace owner belongs to.", + Description: "List of groups the workspace owner belongs to.", }, "id": { Type: schema.TypeString, diff --git a/provider/workspace_test.go b/provider/workspace_test.go index f0eea7de..d7703bc5 100644 --- a/provider/workspace_test.go +++ b/provider/workspace_test.go @@ -16,7 +16,7 @@ func TestWorkspace(t *testing.T) { t.Setenv("CODER_WORKSPACE_OWNER_NAME", "Mr Owner") t.Setenv("CODER_WORKSPACE_OWNER_EMAIL", "owner123@example.com") t.Setenv("CODER_WORKSPACE_OWNER_SESSION_TOKEN", "abc123") - t.Setenv("CODER_WORKSPACE_OWNER_GROUPS", "group1,group2") + t.Setenv("CODER_WORKSPACE_OWNER_GROUPS", "[\"group1\",\"group2\"]") t.Setenv("CODER_WORKSPACE_TEMPLATE_ID", "templateID") t.Setenv("CODER_WORKSPACE_TEMPLATE_NAME", "template123") t.Setenv("CODER_WORKSPACE_TEMPLATE_VERSION", "v1.2.3") @@ -48,7 +48,8 @@ func TestWorkspace(t *testing.T) { require.Equal(t, "Mr Owner", attribs["owner_name"]) require.Equal(t, "owner123@example.com", attribs["owner_email"]) require.Equal(t, "abc123", attribs["owner_session_token"]) - require.Equal(t, "group1,group2", attribs["owner_groups"]) + require.Equal(t, "group1", attribs["owner_groups.0"]) + require.Equal(t, "group2", attribs["owner_groups.1"]) require.Equal(t, "templateID", attribs["template_id"]) require.Equal(t, "template123", attribs["template_name"]) require.Equal(t, "v1.2.3", attribs["template_version"]) @@ -82,7 +83,8 @@ func TestWorkspace(t *testing.T) { require.Equal(t, "owner123", attribs["owner"]) require.Equal(t, "Mr Owner", attribs["owner_name"]) require.Equal(t, "owner123@example.com", attribs["owner_email"]) - require.Equal(t, "group1,group2", attribs["owner_groups"]) + require.Equal(t, "group1", attribs["owner_groups.0"]) + require.Equal(t, "group2", attribs["owner_groups.1"]) require.Equal(t, "templateID", attribs["template_id"]) require.Equal(t, "template123", attribs["template_name"]) require.Equal(t, "v1.2.3", attribs["template_version"]) From 43475652b68cb0ca1a35192f6bb6f841189c8593 Mon Sep 17 00:00:00 2001 From: Garrett Delfosse <garrett@coder.com> Date: Tue, 2 Apr 2024 22:30:08 +0000 Subject: [PATCH 6/7] fmt --- provider/workspace.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/provider/workspace.go b/provider/workspace.go index 5d97cff4..1511d2b5 100644 --- a/provider/workspace.go +++ b/provider/workspace.go @@ -151,8 +151,10 @@ func workspaceDataSource() *schema.Resource { "If a valid token cannot be obtained, this value will be an empty string.", }, "owner_groups": { - Type: schema.TypeList, - Elem: &schema.Schema{Type: schema.TypeString}, + Type: schema.TypeList, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, Computed: true, Description: "List of groups the workspace owner belongs to.", }, From 7b1926ca5ca7270545923164cc812eab3d08e427 Mon Sep 17 00:00:00 2001 From: Garrett Delfosse <garrett@coder.com> Date: Tue, 2 Apr 2024 22:59:45 +0000 Subject: [PATCH 7/7] use raw string --- provider/workspace_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/provider/workspace_test.go b/provider/workspace_test.go index d7703bc5..78aeac4b 100644 --- a/provider/workspace_test.go +++ b/provider/workspace_test.go @@ -16,7 +16,7 @@ func TestWorkspace(t *testing.T) { t.Setenv("CODER_WORKSPACE_OWNER_NAME", "Mr Owner") t.Setenv("CODER_WORKSPACE_OWNER_EMAIL", "owner123@example.com") t.Setenv("CODER_WORKSPACE_OWNER_SESSION_TOKEN", "abc123") - t.Setenv("CODER_WORKSPACE_OWNER_GROUPS", "[\"group1\",\"group2\"]") + t.Setenv("CODER_WORKSPACE_OWNER_GROUPS", `["group1", "group2"]`) t.Setenv("CODER_WORKSPACE_TEMPLATE_ID", "templateID") t.Setenv("CODER_WORKSPACE_TEMPLATE_NAME", "template123") t.Setenv("CODER_WORKSPACE_TEMPLATE_VERSION", "v1.2.3")