Skip to content

feat: add session token data source #120

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 3 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions docs/data-sources/workspace.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ resource "kubernetes_pod" "dev" {
- `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".

Expand Down
8 changes: 8 additions & 0 deletions provider/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ func workspaceDataSource() *schema.Resource {
}
rd.Set("name", name)

sessionToken := os.Getenv("CODER_WORKSPACE_OWNER_SESSION_TOKEN")
_ = rd.Set("owner_session_token", sessionToken)

id := os.Getenv("CODER_WORKSPACE_ID")
if id == "" {
id = uuid.NewString()
Expand Down Expand Up @@ -131,6 +134,11 @@ func workspaceDataSource() *schema.Resource {
Computed: true,
Description: "Name of the workspace.",
},
"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.",
},
},
}
}
5 changes: 4 additions & 1 deletion provider/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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_WORKSPACE_OWNER_SESSION_TOKEN", "abc123")

resource.Test(t, resource.TestCase{
Providers: map[string]*schema.Provider{
Expand All @@ -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["owner_session_token"])
return nil
},
}},
Expand Down