Skip to content

Commit 0ffd87d

Browse files
pmarekejohnstcn
authored andcommitted
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]> (cherry picked from commit 2598aa7)
1 parent 15ec603 commit 0ffd87d

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
@@ -52,6 +52,7 @@ resource "coder_env" "git_author_email" {
5252
- `full_name` (String) The full name of the user.
5353
- `groups` (List of String) The groups of which the user is a member.
5454
- `id` (String) The UUID of the workspace owner.
55+
- `login_type` (String) The type of login the user has.
5556
- `name` (String) The username of the user.
5657
- `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.
5758
- `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
@@ -124,6 +124,7 @@ func TestIntegration(t *testing.T) {
124124
"workspace_owner.session_token": `.+`,
125125
"workspace_owner.ssh_private_key": `(?s)^.+?BEGIN OPENSSH PRIVATE KEY.+?END OPENSSH PRIVATE KEY.+?$`,
126126
"workspace_owner.ssh_public_key": `(?s)^ssh-ed25519.+$`,
127+
"workspace_owner.login_type": ``,
127128
},
128129
},
129130
{

integration/workspace-owner/main.tf

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ locals {
4747
"workspace_owner.session_token" : data.coder_workspace_owner.me.session_token,
4848
"workspace_owner.ssh_private_key" : data.coder_workspace_owner.me.ssh_private_key,
4949
"workspace_owner.ssh_public_key" : data.coder_workspace_owner.me.ssh_public_key,
50+
"workspace_owner.login_type" : data.coder_workspace_owner.me.login_type,
5051
}
5152
}
5253

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
@@ -33,6 +33,7 @@ func TestWorkspaceOwnerDatasource(t *testing.T) {
3333
t.Setenv("CODER_WORKSPACE_OWNER_GROUPS", `["group1", "group2"]`)
3434
t.Setenv("CODER_WORKSPACE_OWNER_SESSION_TOKEN", `supersecret`)
3535
t.Setenv("CODER_WORKSPACE_OWNER_OIDC_ACCESS_TOKEN", `alsosupersecret`)
36+
t.Setenv("CODER_WORKSPACE_OWNER_LOGIN_TYPE", `github`)
3637

3738
resource.Test(t, resource.TestCase{
3839
ProviderFactories: coderFactory(),
@@ -59,6 +60,8 @@ func TestWorkspaceOwnerDatasource(t *testing.T) {
5960
assert.Equal(t, `group2`, attrs["groups.1"])
6061
assert.Equal(t, `supersecret`, attrs["session_token"])
6162
assert.Equal(t, `alsosupersecret`, attrs["oidc_access_token"])
63+
assert.Equal(t, `github`, attrs["login_type"])
64+
6265
return nil
6366
},
6467
}},
@@ -76,6 +79,7 @@ func TestWorkspaceOwnerDatasource(t *testing.T) {
7679
"CODER_WORKSPACE_OWNER_OIDC_ACCESS_TOKEN",
7780
"CODER_WORKSPACE_OWNER_SSH_PUBLIC_KEY",
7881
"CODER_WORKSPACE_OWNER_SSH_PRIVATE_KEY",
82+
"CODER_WORKSPACE_OWNER_LOGIN_TYPE",
7983
} { // https://github.com/golang/go/issues/52817
8084
t.Setenv(v, "")
8185
os.Unsetenv(v)
@@ -105,6 +109,7 @@ func TestWorkspaceOwnerDatasource(t *testing.T) {
105109
assert.Empty(t, attrs["groups.0"])
106110
assert.Empty(t, attrs["session_token"])
107111
assert.Empty(t, attrs["oidc_access_token"])
112+
assert.Empty(t, attrs["login_type"])
108113
return nil
109114
},
110115
}},

0 commit comments

Comments
 (0)