Skip to content

Add data source attribute for owner email address #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/data-sources/workspace.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ resource "kubernetes_pod" "dev" {
- `id` (String) UUID of the workspace.
- `name` (String) Name of the workspace.
- `owner` (String) Username of the workspace owner.
- `owner_name` (String) Display-friendly name of the workspace owner (or their username, if a name is not known).
- `owner_email` (String) Email address of the workspace owner.
- `owner_id` (String) UUID of the workspace owner.
- `start_count` (Number) A computed count based on "transition" state. If "start", count will equal 1.
- `transition` (String) Either "start" or "stop". Use this to start/stop resources with "count".
Expand Down
25 changes: 25 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,31 +69,46 @@ func New() *schema.Provider {
count = 1
}
_ = rd.Set("start_count", count)

owner := os.Getenv("CODER_WORKSPACE_OWNER")
if owner == "" {
owner = "default"
}
_ = rd.Set("owner", owner)

ownerName := os.Getenv("CODER_WORKSPACE_OWNER_NAME")
if ownerName == "" {
ownerName = owner
}
_ = rd.Set("owner_name", ownerName)

ownerEmail := os.Getenv("CODER_WORKSPACE_OWNER_EMAIL")
_ = rd.Set("owner_email", ownerEmail)
Comment on lines +79 to +80
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other attributes handled by this function all have a non-empty default value in case the expected environment var is missing. But I can't think of a good default for the email address, and in my testing, there didn't seem to be any problem with leaving it empty.


ownerID := os.Getenv("CODER_WORKSPACE_OWNER_ID")
if ownerID == "" {
ownerID = uuid.Nil.String()
}
_ = rd.Set("owner_id", ownerID)

name := os.Getenv("CODER_WORKSPACE_NAME")
if name == "" {
name = "default"
}
rd.Set("name", name)

id := os.Getenv("CODER_WORKSPACE_ID")
if id == "" {
id = uuid.NewString()
}
rd.SetId(id)

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())

return nil
},
Schema: map[string]*schema.Schema{
Expand All @@ -117,6 +132,16 @@ func New() *schema.Provider {
Computed: true,
Description: "Username of the workspace owner.",
},
"owner_name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of the workspace owner (e.g. for Git commits) if known, otherwise their username.",
},
"owner_email": {
Type: schema.TypeString,
Computed: true,
Description: "Email address of the workspace owner.",
},
"owner_id": {
Type: schema.TypeString,
Computed: true,
Expand Down
12 changes: 10 additions & 2 deletions internal/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ func TestProvider(t *testing.T) {
}

func TestWorkspace(t *testing.T) {
t.Parallel()
t.Setenv("CODER_WORKSPACE_OWNER", "owner123")
t.Setenv("CODER_WORKSPACE_OWNER_NAME", "Workspace Owner Jr.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kinda messed this up in the other issue, and I think it could be better to remove this for now. Since we don't have a display name, it could be easy to confuse _name with username.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I was thinking it would be convenient to have a separate variable, because in the future we'll probably be able to get a display name from e.g. an OIDC provider. But you're right that for now, it's potentially confusing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good idea to expose the attributes. Interesting

t.Setenv("CODER_WORKSPACE_OWNER_EMAIL", "[email protected]")

resource.Test(t, resource.TestCase{
Providers: map[string]*schema.Provider{
"coder": provider.New(),
Expand All @@ -37,9 +40,14 @@ func TestWorkspace(t *testing.T) {
require.Len(t, state.Modules[0].Resources, 1)
resource := state.Modules[0].Resources["data.coder_workspace.me"]
require.NotNil(t, resource)
value := resource.Primary.Attributes["transition"]

attribs := resource.Primary.Attributes
value := attribs["transition"]
require.NotNil(t, value)
t.Log(value)
require.Equal(t, "owner123", attribs["owner"])
require.Equal(t, "Workspace Owner Jr.", attribs["owner_name"])
require.Equal(t, "[email protected]", attribs["owner_email"])
return nil
},
}},
Expand Down