Skip to content

Commit 0c0c65a

Browse files
authored
feat: Expose owner and workspace IDs to "coder_workspace" (#12)
This allows for simpler persistence of resources that shouldn't be deleted when a user changes their name.
1 parent 7e3d827 commit 0c0c65a

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

docs/data-sources/workspace.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,12 @@ resource "kubernetes_pod" "dev" {
2424
<!-- schema generated by tfplugindocs -->
2525
## Schema
2626

27-
### Optional
28-
29-
- `id` (String) The ID of this resource.
30-
3127
### Read-Only
3228

29+
- `id` (String) UUID of the workspace.
3330
- `name` (String) Name of the workspace.
3431
- `owner` (String) Username of the workspace owner.
32+
- `owner_id` (String) UUID of the workspace owner.
3533
- `start_count` (Number) A computed count based on "transition" state. If "start", count will equal 1.
3634
- `transition` (String) Either "start" or "stop". Use this to start/stop resources with "count".
3735

internal/provider/provider.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ func New() *schema.Provider {
5858
"coder_workspace": {
5959
Description: "Use this data source to get information for the active workspace build.",
6060
ReadContext: func(c context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics {
61-
rd.SetId(uuid.NewString())
6261
transition := os.Getenv("CODER_WORKSPACE_TRANSITION")
6362
if transition == "" {
6463
// Default to start!
@@ -75,11 +74,21 @@ func New() *schema.Provider {
7574
owner = "default"
7675
}
7776
_ = rd.Set("owner", owner)
77+
ownerID := os.Getenv("CODER_WORKSPACE_OWNER_ID")
78+
if ownerID == "" {
79+
ownerID = uuid.Nil.String()
80+
}
81+
_ = rd.Set("owner_id", ownerID)
7882
name := os.Getenv("CODER_WORKSPACE_NAME")
7983
if name == "" {
8084
name = "default"
8185
}
8286
rd.Set("name", name)
87+
id := os.Getenv("CODER_WORKSPACE_ID")
88+
if id == "" {
89+
id = uuid.NewString()
90+
}
91+
rd.SetId(id)
8392
return nil
8493
},
8594
Schema: map[string]*schema.Schema{
@@ -98,6 +107,16 @@ func New() *schema.Provider {
98107
Computed: true,
99108
Description: "Username of the workspace owner.",
100109
},
110+
"owner_id": {
111+
Type: schema.TypeString,
112+
Computed: true,
113+
Description: "UUID of the workspace owner.",
114+
},
115+
"id": {
116+
Type: schema.TypeString,
117+
Computed: true,
118+
Description: "UUID of the workspace.",
119+
},
101120
"name": {
102121
Type: schema.TypeString,
103122
Computed: true,

0 commit comments

Comments
 (0)