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 all 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
1 change: 1 addition & 0 deletions docs/data-sources/workspace.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ resource "kubernetes_pod" "dev" {
- `id` (String) UUID of the workspace.
- `name` (String) Name of the workspace.
- `owner` (String) Username of the workspace owner.
- `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
14 changes: 14 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,31 +69,40 @@ 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)

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 +126,11 @@ func New() *schema.Provider {
Computed: true,
Description: "Username of the workspace owner.",
},
"owner_email": {
Type: schema.TypeString,
Computed: true,
Description: "Email address of the workspace owner.",
},
"owner_id": {
Type: schema.TypeString,
Computed: true,
Expand Down
10 changes: 8 additions & 2 deletions internal/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ func TestProvider(t *testing.T) {
}

func TestWorkspace(t *testing.T) {
t.Parallel()
t.Setenv("CODER_WORKSPACE_OWNER", "owner123")
t.Setenv("CODER_WORKSPACE_OWNER_EMAIL", "owner123@example.com")

resource.Test(t, resource.TestCase{
Providers: map[string]*schema.Provider{
"coder": provider.New(),
Expand All @@ -37,9 +39,13 @@ 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, "owner123@example.com", attribs["owner_email"])
return nil
},
}},
Expand Down