-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathworkspace.go
208 lines (190 loc) · 6.04 KB
/
workspace.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package provider
import (
"context"
"encoding/json"
"os"
"reflect"
"strconv"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func workspaceDataSource() *schema.Resource {
return &schema.Resource{
Description: "Use this data source to get information for the active workspace build.",
ReadContext: func(c context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics {
transition := os.Getenv("CODER_WORKSPACE_TRANSITION")
if transition == "" {
// Default to start!
transition = "start"
}
_ = rd.Set("transition", transition)
count := 0
if transition == "start" {
count = 1
}
_ = rd.Set("start_count", count)
owner := os.Getenv("CODER_WORKSPACE_OWNER")
if owner == "" {
owner = "default"
}
_ = rd.Set("owner", owner)
ownerEmail := os.Getenv("CODER_WORKSPACE_OWNER_EMAIL")
if ownerEmail == "" {
ownerEmail = "[email protected]"
}
_ = rd.Set("owner_email", ownerEmail)
ownerGroupsText := os.Getenv("CODER_WORKSPACE_OWNER_GROUPS")
var ownerGroups []string
if ownerGroupsText != "" {
err := json.Unmarshal([]byte(ownerGroupsText), &ownerGroups)
if err != nil {
return diag.Errorf("couldn't parse owner groups %q", ownerGroupsText)
}
}
_ = rd.Set("owner_groups", ownerGroups)
ownerName := os.Getenv("CODER_WORKSPACE_OWNER_NAME")
if ownerName == "" {
ownerName = "default"
}
_ = rd.Set("owner_name", ownerName)
ownerID := os.Getenv("CODER_WORKSPACE_OWNER_ID")
if ownerID == "" {
ownerID = uuid.Nil.String()
}
_ = rd.Set("owner_id", ownerID)
ownerOIDCAccessToken := os.Getenv("CODER_WORKSPACE_OWNER_OIDC_ACCESS_TOKEN")
_ = rd.Set("owner_oidc_access_token", ownerOIDCAccessToken)
name := os.Getenv("CODER_WORKSPACE_NAME")
if name == "" {
name = "default"
}
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()
}
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)
templateVersion := os.Getenv("CODER_WORKSPACE_TEMPLATE_VERSION")
_ = rd.Set("template_version", templateVersion)
config, valid := i.(config)
if !valid {
return diag.Errorf("config was unexpected type %q", reflect.TypeOf(i).String())
}
rd.Set("access_url", config.URL.String())
rawPort := config.URL.Port()
if rawPort == "" {
rawPort = "80"
if config.URL.Scheme == "https" {
rawPort = "443"
}
}
port, err := strconv.Atoi(rawPort)
if err != nil {
return diag.Errorf("couldn't parse port %q", port)
}
rd.Set("access_port", port)
return nil
},
Schema: map[string]*schema.Schema{
"access_url": {
Type: schema.TypeString,
Computed: true,
Description: "The access URL of the Coder deployment provisioning this workspace.",
},
"access_port": {
Type: schema.TypeInt,
Computed: true,
Description: "The access port of the Coder deployment provisioning this workspace.",
},
"start_count": {
Type: schema.TypeInt,
Computed: true,
Description: `A computed count based on "transition" state. If "start", count will equal 1.`,
},
"transition": {
Type: schema.TypeString,
Computed: true,
Description: `Either "start" or "stop". Use this to start/stop resources with "count".`,
},
"owner": {
Type: schema.TypeString,
Computed: true,
Description: "Username of the workspace owner.",
Deprecated: "Use `coder_workspace_owner.name` instead.",
},
"owner_email": {
Type: schema.TypeString,
Computed: true,
Description: "Email address of the workspace owner.",
Deprecated: "Use `coder_workspace_owner.email` instead.",
},
"owner_id": {
Type: schema.TypeString,
Computed: true,
Description: "UUID of the workspace owner.",
Deprecated: "Use `coder_workspace_owner.id` instead.",
},
"owner_name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of the workspace owner.",
Deprecated: "Use `coder_workspace_owner.full_name` instead.",
},
"owner_oidc_access_token": {
Type: schema.TypeString,
Computed: true,
Description: "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.",
Deprecated: "Use `coder_workspace_owner.oidc_access_token` instead.",
},
"owner_groups": {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Computed: true,
Description: "List of groups the workspace owner belongs to.",
Deprecated: "Use `coder_workspace_owner.groups` instead.",
},
"id": {
Type: schema.TypeString,
Computed: true,
Description: "UUID of the workspace.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of the workspace.",
},
"owner_session_token": {
Type: schema.TypeString,
Computed: true,
Description: "Session token for authenticating with a Coder deployment. It is regenerated everytime a workspace is started.",
Deprecated: "Use `coder_workspace_owner.session_token` instead.",
},
"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.",
},
"template_version": {
Type: schema.TypeString,
Computed: true,
Description: "Version of the workspace's template.",
},
},
}
}