From 8f726142a3f931e69f6b4aa471b59a0420f77d18 Mon Sep 17 00:00:00 2001 From: Jon Ayers Date: Tue, 9 May 2023 00:43:51 +0000 Subject: [PATCH 1/3] feat: add session token data source --- provider/workspace.go | 8 ++++++++ provider/workspace_test.go | 5 ++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/provider/workspace.go b/provider/workspace.go index 797acc60..dffd4a6b 100644 --- a/provider/workspace.go +++ b/provider/workspace.go @@ -51,6 +51,9 @@ func workspaceDataSource() *schema.Resource { } rd.Set("name", name) + sessionToken := os.Getenv("CODER_SESSION_TOKEN") + _ = rd.Set("coder_session_token", sessionToken) + id := os.Getenv("CODER_WORKSPACE_ID") if id == "" { id = uuid.NewString() @@ -131,6 +134,11 @@ func workspaceDataSource() *schema.Resource { Computed: true, Description: "Name of the workspace.", }, + "coder_session_token": { + Type: schema.TypeString, + Computed: true, + Description: "Session token for interfacing with a Coder deployment. It is regenerated everytime a workspace is started.", + }, }, } } diff --git a/provider/workspace_test.go b/provider/workspace_test.go index 5473f20f..d6597d90 100644 --- a/provider/workspace_test.go +++ b/provider/workspace_test.go @@ -3,16 +3,18 @@ package provider_test import ( "testing" - "github.com/coder/terraform-provider-coder/provider" "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 TestWorkspace(t *testing.T) { t.Setenv("CODER_WORKSPACE_OWNER", "owner123") t.Setenv("CODER_WORKSPACE_OWNER_EMAIL", "owner123@example.com") + t.Setenv("CODER_SESSION_TOKEN", "abc123") resource.Test(t, resource.TestCase{ Providers: map[string]*schema.Provider{ @@ -39,6 +41,7 @@ func TestWorkspace(t *testing.T) { require.Equal(t, "8080", attribs["access_port"]) require.Equal(t, "owner123", attribs["owner"]) require.Equal(t, "owner123@example.com", attribs["owner_email"]) + require.Equal(t, "abc123", attribs["coder_session_token"]) return nil }, }}, From 6696b303d24a877c6ac9c18be3663338a010e181 Mon Sep 17 00:00:00 2001 From: Jon Ayers Date: Wed, 10 May 2023 02:16:46 +0000 Subject: [PATCH 2/3] make gen --- docs/data-sources/workspace.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/data-sources/workspace.md b/docs/data-sources/workspace.md index 865a2d98..4c4cdfd2 100644 --- a/docs/data-sources/workspace.md +++ b/docs/data-sources/workspace.md @@ -28,6 +28,7 @@ resource "kubernetes_pod" "dev" { - `access_port` (Number) The access port of the Coder deployment provisioning this workspace. - `access_url` (String) The access URL of the Coder deployment provisioning this workspace. +- `coder_session_token` (String) Session token for interfacing with a Coder deployment. It is regenerated everytime a workspace is started. - `id` (String) UUID of the workspace. - `name` (String) Name of the workspace. - `owner` (String) Username of the workspace owner. From c73161d27650d2f9b69b188c5df23a585696d3f2 Mon Sep 17 00:00:00 2001 From: Jon Ayers Date: Thu, 18 May 2023 04:09:45 +0000 Subject: [PATCH 3/3] update field name to owner_session_token --- docs/data-sources/workspace.md | 2 +- provider/workspace.go | 6 +++--- provider/workspace_test.go | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/data-sources/workspace.md b/docs/data-sources/workspace.md index 4c4cdfd2..ffe49eb8 100644 --- a/docs/data-sources/workspace.md +++ b/docs/data-sources/workspace.md @@ -28,13 +28,13 @@ resource "kubernetes_pod" "dev" { - `access_port` (Number) The access port of the Coder deployment provisioning this workspace. - `access_url` (String) The access URL of the Coder deployment provisioning this workspace. -- `coder_session_token` (String) Session token for interfacing with a Coder deployment. It is regenerated everytime a workspace is started. - `id` (String) UUID of the workspace. - `name` (String) Name of the workspace. - `owner` (String) Username of the workspace owner. - `owner_email` (String) Email address of the workspace owner. - `owner_id` (String) UUID 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. +- `owner_session_token` (String) Session token for interfacing with a Coder deployment. It is regenerated everytime a workspace is started. - `start_count` (Number) A computed count based on "transition" state. If "start", count will equal 1. - `transition` (String) Either "start" or "stop". Use this to start/stop resources with "count". diff --git a/provider/workspace.go b/provider/workspace.go index dffd4a6b..47f0e799 100644 --- a/provider/workspace.go +++ b/provider/workspace.go @@ -51,8 +51,8 @@ func workspaceDataSource() *schema.Resource { } rd.Set("name", name) - sessionToken := os.Getenv("CODER_SESSION_TOKEN") - _ = rd.Set("coder_session_token", sessionToken) + sessionToken := os.Getenv("CODER_WORKSPACE_OWNER_SESSION_TOKEN") + _ = rd.Set("owner_session_token", sessionToken) id := os.Getenv("CODER_WORKSPACE_ID") if id == "" { @@ -134,7 +134,7 @@ func workspaceDataSource() *schema.Resource { Computed: true, Description: "Name of the workspace.", }, - "coder_session_token": { + "owner_session_token": { Type: schema.TypeString, Computed: true, Description: "Session token for interfacing with a Coder deployment. It is regenerated everytime a workspace is started.", diff --git a/provider/workspace_test.go b/provider/workspace_test.go index d6597d90..7662f4bc 100644 --- a/provider/workspace_test.go +++ b/provider/workspace_test.go @@ -14,7 +14,7 @@ import ( func TestWorkspace(t *testing.T) { t.Setenv("CODER_WORKSPACE_OWNER", "owner123") t.Setenv("CODER_WORKSPACE_OWNER_EMAIL", "owner123@example.com") - t.Setenv("CODER_SESSION_TOKEN", "abc123") + t.Setenv("CODER_WORKSPACE_OWNER_SESSION_TOKEN", "abc123") resource.Test(t, resource.TestCase{ Providers: map[string]*schema.Provider{ @@ -41,7 +41,7 @@ func TestWorkspace(t *testing.T) { require.Equal(t, "8080", attribs["access_port"]) require.Equal(t, "owner123", attribs["owner"]) require.Equal(t, "owner123@example.com", attribs["owner_email"]) - require.Equal(t, "abc123", attribs["coder_session_token"]) + require.Equal(t, "abc123", attribs["owner_session_token"]) return nil }, }},