Skip to content

Commit 2598aa7

Browse files
pmarekejohnstcn
andauthored
Add login_type to coder_workspace_owner data source #235 (#287)
* feat: add login_type to coder_workspace_owner data source * feat: remove types check * feat: update integration test * feat: run linter * feat: set empty string as the default value for the login type * feat: update integration test * feat: add a warning when the CODER_WORKSPACE_OWNER_LOGIN_TYPE is not set * feat: create a new diags variable * feat: add missing comma * feat: add missing parenthesis * feat: fix typo in summary field Co-authored-by: Cian Johnston <[email protected]> --------- Co-authored-by: Cian Johnston <[email protected]>
1 parent 332f8ae commit 2598aa7

File tree

5 files changed

+24
-1
lines changed

5 files changed

+24
-1
lines changed

docs/data-sources/workspace_owner.md

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ resource "coder_env" "git_author_email" {
5050
- `full_name` (String) The full name of the user.
5151
- `groups` (List of String) The groups of which the user is a member.
5252
- `id` (String) The UUID of the workspace owner.
53+
- `login_type` (String) The type of login the user has.
5354
- `name` (String) The username of the user.
5455
- `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.
5556
- `session_token` (String) Session token for authenticating with a Coder deployment. It is regenerated every time a workspace is started.

integration/integration_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ func TestIntegration(t *testing.T) {
112112
"workspace_owner.session_token": `.+`,
113113
"workspace_owner.ssh_private_key": `(?s)^.+?BEGIN OPENSSH PRIVATE KEY.+?END OPENSSH PRIVATE KEY.+?$`,
114114
"workspace_owner.ssh_public_key": `(?s)^ssh-ed25519.+$`,
115+
"workspace_owner.login_type": ``,
115116
},
116117
},
117118
{

integration/workspace-owner/main.tf

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ locals {
3939
"workspace_owner.session_token" : data.coder_workspace_owner.me.session_token,
4040
"workspace_owner.ssh_private_key" : data.coder_workspace_owner.me.ssh_private_key,
4141
"workspace_owner.ssh_public_key" : data.coder_workspace_owner.me.ssh_public_key,
42+
"workspace_owner.login_type" : data.coder_workspace_owner.me.login_type,
4243
}
4344
}
4445

provider/workspace_owner.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ func workspaceOwnerDataSource() *schema.Resource {
1515
return &schema.Resource{
1616
Description: "Use this data source to fetch information about the workspace owner.",
1717
ReadContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics {
18+
diags := diag.Diagnostics{}
19+
1820
if idStr := os.Getenv("CODER_WORKSPACE_OWNER_ID"); idStr != "" {
1921
rd.SetId(idStr)
2022
} else {
@@ -53,7 +55,15 @@ func workspaceOwnerDataSource() *schema.Resource {
5355
_ = rd.Set("session_token", os.Getenv("CODER_WORKSPACE_OWNER_SESSION_TOKEN"))
5456
_ = rd.Set("oidc_access_token", os.Getenv("CODER_WORKSPACE_OWNER_OIDC_ACCESS_TOKEN"))
5557

56-
return nil
58+
if os.Getenv("CODER_WORKSPACE_OWNER_LOGIN_TYPE") == "" {
59+
diags = append(diags, diag.Diagnostic{
60+
Severity: diag.Warning,
61+
Summary: "WARNING: The CODER_WORKSPACE_OWNER_LOGIN_TYPE env variable is not set",
62+
})
63+
}
64+
_ = rd.Set("login_type", os.Getenv("CODER_WORKSPACE_OWNER_LOGIN_TYPE"))
65+
66+
return diags
5767
},
5868
Schema: map[string]*schema.Schema{
5969
"id": {
@@ -107,6 +117,11 @@ func workspaceOwnerDataSource() *schema.Resource {
107117
"This is only available if the workspace owner authenticated with OpenID Connect. " +
108118
"If a valid token cannot be obtained, this value will be an empty string.",
109119
},
120+
"login_type": {
121+
Type: schema.TypeString,
122+
Computed: true,
123+
Description: "The type of login the user has.",
124+
},
110125
},
111126
}
112127
}

provider/workspace_owner_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func TestWorkspaceOwnerDatasource(t *testing.T) {
3535
t.Setenv("CODER_WORKSPACE_OWNER_GROUPS", `["group1", "group2"]`)
3636
t.Setenv("CODER_WORKSPACE_OWNER_SESSION_TOKEN", `supersecret`)
3737
t.Setenv("CODER_WORKSPACE_OWNER_OIDC_ACCESS_TOKEN", `alsosupersecret`)
38+
t.Setenv("CODER_WORKSPACE_OWNER_LOGIN_TYPE", `github`)
3839

3940
resource.Test(t, resource.TestCase{
4041
Providers: map[string]*schema.Provider{
@@ -63,6 +64,8 @@ func TestWorkspaceOwnerDatasource(t *testing.T) {
6364
assert.Equal(t, `group2`, attrs["groups.1"])
6465
assert.Equal(t, `supersecret`, attrs["session_token"])
6566
assert.Equal(t, `alsosupersecret`, attrs["oidc_access_token"])
67+
assert.Equal(t, `github`, attrs["login_type"])
68+
6669
return nil
6770
},
6871
}},
@@ -80,6 +83,7 @@ func TestWorkspaceOwnerDatasource(t *testing.T) {
8083
"CODER_WORKSPACE_OWNER_OIDC_ACCESS_TOKEN",
8184
"CODER_WORKSPACE_OWNER_SSH_PUBLIC_KEY",
8285
"CODER_WORKSPACE_OWNER_SSH_PRIVATE_KEY",
86+
"CODER_WORKSPACE_OWNER_LOGIN_TYPE",
8387
} { // https://github.com/golang/go/issues/52817
8488
t.Setenv(v, "")
8589
os.Unsetenv(v)
@@ -111,6 +115,7 @@ func TestWorkspaceOwnerDatasource(t *testing.T) {
111115
assert.Empty(t, attrs["groups.0"])
112116
assert.Empty(t, attrs["session_token"])
113117
assert.Empty(t, attrs["oidc_access_token"])
118+
assert.Empty(t, attrs["login_type"])
114119
return nil
115120
},
116121
}},

0 commit comments

Comments
 (0)