Skip to content

Commit ab53274

Browse files
committed
feat: add login_type to coder_workspace_owner data source
1 parent 332f8ae commit ab53274

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

docs/data-sources/workspace_owner.md

+1
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,4 @@ resource "coder_env" "git_author_email" {
5555
- `session_token` (String) Session token for authenticating with a Coder deployment. It is regenerated every time a workspace is started.
5656
- `ssh_private_key` (String, Sensitive) The user's generated SSH private key.
5757
- `ssh_public_key` (String) The user's generated SSH public key.
58+
- `login_type` (String) The user's login type. The valid options are `password`, `github`, `oidc` or `none`.

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

+18
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package provider
33
import (
44
"context"
55
"encoding/json"
6+
"errors"
67
"os"
8+
"slices"
79
"strings"
810

911
"github.com/google/uuid"
@@ -53,6 +55,17 @@ 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

58+
types := []string{"password", "github", "oidc", "none"}
59+
if login_type := os.Getenv("CODER_WORKSPACE_OWNER_LOGIN_TYPE"); login_type != "" {
60+
if !slices.Contains(types, login_type) {
61+
errorMessage := "invalid login type: %s, the valid types are: 'password, github, oidc, or none'"
62+
return diag.Errorf(errorMessage, errors.New(errorMessage))
63+
}
64+
_ = rd.Set("login_type", login_type)
65+
} else {
66+
_ = rd.Set("login_type", "none")
67+
}
68+
5669
return nil
5770
},
5871
Schema: map[string]*schema.Schema{
@@ -107,6 +120,11 @@ func workspaceOwnerDataSource() *schema.Resource {
107120
"This is only available if the workspace owner authenticated with OpenID Connect. " +
108121
"If a valid token cannot be obtained, this value will be an empty string.",
109122
},
123+
"login_type": {
124+
Type: schema.TypeString,
125+
Computed: true,
126+
Description: "The type of login the user has.",
127+
},
110128
},
111129
}
112130
}

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.Equal(t, "none", attrs["login_type"])
114119
return nil
115120
},
116121
}},

0 commit comments

Comments
 (0)