Skip to content

feat: add template_id and template_name to workspace data source #158

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 1 commit into from
Sep 13, 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
2 changes: 2 additions & 0 deletions docs/data-sources/workspace.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ resource "kubernetes_pod" "dev" {
- `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 authenticating 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.
- `template_id` (String) ID of the workspace's template.
- `template_name` (String) Name of the workspace's template.
- `transition` (String) Either "start" or "stop". Use this to start/stop resources with "count".
16 changes: 16 additions & 0 deletions provider/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ func workspaceDataSource() *schema.Resource {
}
rd.SetId(id)

templateID := os.Getenv("CODER_WORKSPACE_TEMPLATE_ID")
_ = rd.Set("template_id", templateID)

templateName := os.Getenv("CODER_WORKSPACE_TEMPLATE_NAME")
_ = rd.Set("template_name", templateName)

config, valid := i.(config)
if !valid {
return diag.Errorf("config was unexpected type %q", reflect.TypeOf(i).String())
Expand Down Expand Up @@ -139,6 +145,16 @@ func workspaceDataSource() *schema.Resource {
Computed: true,
Description: "Session token for authenticating with a Coder deployment. It is regenerated everytime a workspace is started.",
},
"template_id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of the workspace's template.",
},
"template_name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of the workspace's template.",
},
},
}
}
6 changes: 6 additions & 0 deletions provider/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ 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")
t.Setenv("CODER_WORKSPACE_TEMPLATE_ID", "templateID")
t.Setenv("CODER_WORKSPACE_TEMPLATE_NAME", "template123")

resource.Test(t, resource.TestCase{
Providers: map[string]*schema.Provider{
Expand Down Expand Up @@ -42,6 +44,8 @@ func TestWorkspace(t *testing.T) {
require.Equal(t, "owner123", attribs["owner"])
require.Equal(t, "owner123@example.com", attribs["owner_email"])
require.Equal(t, "abc123", attribs["owner_session_token"])
require.Equal(t, "templateID", attribs["template_id"])
require.Equal(t, "template123", attribs["template_name"])
return nil
},
}},
Expand Down Expand Up @@ -71,6 +75,8 @@ func TestWorkspace(t *testing.T) {
require.Equal(t, "https://example.com:8080", attribs["access_url"])
require.Equal(t, "owner123", attribs["owner"])
require.Equal(t, "owner123@example.com", attribs["owner_email"])
require.Equal(t, "templateID", attribs["template_id"])
require.Equal(t, "template123", attribs["template_name"])
return nil
},
}},
Expand Down