Skip to content

Commit

Permalink
feat: add template_id and template_name to workspace data source (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
sreya committed Sep 13, 2023
1 parent ac4d723 commit 73943b2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
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

0 comments on commit 73943b2

Please sign in to comment.