-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathworkspace_owner.go
123 lines (111 loc) · 3.65 KB
/
workspace_owner.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
package provider
import (
"context"
"encoding/json"
"os"
"strings"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func workspaceOwnerDataSource() *schema.Resource {
return &schema.Resource{
Description: "Use this data source to fetch information about the workspace owner.",
ReadContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics {
diags := diag.Diagnostics{}
if idStr := os.Getenv("CODER_WORKSPACE_OWNER_ID"); idStr != "" {
rd.SetId(idStr)
} else {
rd.SetId(uuid.NewString())
}
if username := os.Getenv("CODER_WORKSPACE_OWNER"); username != "" {
_ = rd.Set("name", username)
} else {
_ = rd.Set("name", "default")
}
if fullname := os.Getenv("CODER_WORKSPACE_OWNER_NAME"); fullname != "" {
_ = rd.Set("full_name", fullname)
} else { // compat: field can be blank, fill in default
_ = rd.Set("full_name", "default")
}
if email := os.Getenv("CODER_WORKSPACE_OWNER_EMAIL"); email != "" {
_ = rd.Set("email", email)
} else {
_ = rd.Set("email", "[email protected]")
}
_ = rd.Set("ssh_public_key", os.Getenv("CODER_WORKSPACE_OWNER_SSH_PUBLIC_KEY"))
_ = rd.Set("ssh_private_key", os.Getenv("CODER_WORKSPACE_OWNER_SSH_PRIVATE_KEY"))
var groups []string
if groupsRaw, ok := os.LookupEnv("CODER_WORKSPACE_OWNER_GROUPS"); ok {
if err := json.NewDecoder(strings.NewReader(groupsRaw)).Decode(&groups); err != nil {
return diag.Errorf("invalid user groups: %s", err.Error())
}
}
_ = rd.Set("groups", groups)
_ = rd.Set("session_token", os.Getenv("CODER_WORKSPACE_OWNER_SESSION_TOKEN"))
_ = rd.Set("oidc_access_token", os.Getenv("CODER_WORKSPACE_OWNER_OIDC_ACCESS_TOKEN"))
if loginType := os.Getenv("CODER_WORKSPACE_OWNER_LOGIN_TYPE"); loginType != "" {
_ = rd.Set("login_type", loginType)
}
return diags
},
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The UUID of the workspace owner.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The username of the user.",
},
"full_name": {
Type: schema.TypeString,
Computed: true,
Description: "The full name of the user.",
},
"email": {
Type: schema.TypeString,
Computed: true,
Description: "The email address of the user.",
},
"ssh_public_key": {
Type: schema.TypeString,
Computed: true,
Description: "The user's generated SSH public key.",
},
"ssh_private_key": {
Type: schema.TypeString,
Computed: true,
Description: "The user's generated SSH private key.",
Sensitive: true,
},
"groups": {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Computed: true,
Description: "The groups of which the user is a member.",
},
"session_token": {
Type: schema.TypeString,
Computed: true,
Description: "Session token for authenticating with a Coder deployment. It is regenerated every time a workspace is started.",
},
"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.",
},
"login_type": {
Type: schema.TypeString,
Computed: true,
Description: "The type of login the user has.",
},
},
}
}