Skip to content
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

[datadog_user] Add computed attributes to datadog_user datasource #2787

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
79 changes: 74 additions & 5 deletions datadog/data_source_datadog_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package datadog

import (
"context"
"time"

"github.com/DataDog/datadog-api-client-go/v2/api/datadogV2"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -29,16 +30,66 @@ func dataSourceDatadogUser() *schema.Resource {
Optional: true,
},
// Computed values
"created_at": {
Description: "Creation time of the user (RFC3339 format).",
Type: schema.TypeString,
Computed: true,
},
"disabled": {
Description: "Whether the user is disabled.",
Type: schema.TypeBool,
Computed: true,
},
"email": {
Description: "Email of the user.",
Type: schema.TypeString,
Computed: true,
},
"handle": {
Description: "Handle of the user.",
Type: schema.TypeString,
Computed: true,
},
"icon": {
Description: "URL of the user's icon.",
Type: schema.TypeString,
Computed: true,
},
"mfa_enabled": {
Description: "If user has MFA enabled.",
Type: schema.TypeBool,
Computed: true,
},
"modified_at": {
Description: "Time that the user was last modified (RFC3339 format).",
Type: schema.TypeString,
Computed: true,
},
"name": {
Description: "Name of the user.",
Type: schema.TypeString,
Computed: true,
},
"service_account": {
Description: "Whether the user is a service account.",
Type: schema.TypeBool,
Computed: true,
},
"status": {
Description: "Status of the user.",
Type: schema.TypeString,
Computed: true,
},
"title": {
Description: "Title of the user.",
Type: schema.TypeString,
Computed: true,
},
"verified": {
Description: "Whether the user is verified.",
Type: schema.TypeBool,
Computed: true,
},
}
},
}
Expand Down Expand Up @@ -96,13 +147,31 @@ func dataSourceDatadogUserRead(ctx context.Context, d *schema.ResourceData, meta
if err := utils.CheckForUnparsed(matchedUser); err != nil {
return diag.FromErr(err)
}

d.SetId(matchedUser.GetId())
if err := d.Set("name", matchedUser.Attributes.GetName()); err != nil {
return diag.FromErr(err)
mapAttrString := map[string]func() string{
"created_at": func() string { return matchedUser.Attributes.GetCreatedAt().Format(time.RFC3339) },
"email": matchedUser.Attributes.GetEmail,
"handle": matchedUser.Attributes.GetHandle,
"icon": matchedUser.Attributes.GetIcon,
"modified_at": func() string { return matchedUser.Attributes.GetModifiedAt().Format(time.RFC3339) },
"name": matchedUser.Attributes.GetName,
"status": matchedUser.Attributes.GetStatus,
"title": matchedUser.Attributes.GetTitle,
}
if err := d.Set("email", matchedUser.Attributes.GetEmail()); err != nil {
return diag.FromErr(err)
for key, value := range mapAttrString {
if err := d.Set(key, value()); err != nil {
return diag.FromErr(err)
}
}
mapAttrBool := map[string]func() bool{
"disabled": matchedUser.Attributes.GetDisabled,
"mfa_enabled": matchedUser.Attributes.GetMfaEnabled,
"service_account": matchedUser.Attributes.GetServiceAccount,
}
for key, value := range mapAttrBool {
if err := d.Set(key, value()); err != nil {
return diag.FromErr(err)
}
}
return nil
}
10 changes: 10 additions & 0 deletions docs/data-sources/user.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ data "datadog_user" "test" {

### Read-Only

- `created_at` (String) Creation time of the user (RFC3339 format).
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- `created_at` (String) Creation time of the user (RFC3339 format).
- `created_at` (String) The time when the user was created (RFC3339 format).

- `disabled` (Boolean) Whether the user is disabled.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- `disabled` (Boolean) Whether the user is disabled.
- `disabled` (Boolean) Indicates whether the user is disabled.

- `email` (String) Email of the user.
- `handle` (String) Handle of the user.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- `handle` (String) Handle of the user.
- `handle` (String) The user's handle.

- `icon` (String) URL of the user's icon.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- `icon` (String) URL of the user's icon.
- `icon` (String) The URL where the user's icon is located.

- `id` (String) The ID of this resource.
- `mfa_enabled` (Boolean) If user has MFA enabled.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- `mfa_enabled` (Boolean) If user has MFA enabled.
- `mfa_enabled` (Boolean) Indicates whether the user has enabled MFA.

- `modified_at` (String) Time that the user was last modified (RFC3339 format).
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- `modified_at` (String) Time that the user was last modified (RFC3339 format).
- `modified_at` (String) The time at which the user was last updated (RFC3339 format).

- `name` (String) Name of the user.
- `service_account` (Boolean) Whether the user is a service account.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- `service_account` (Boolean) Whether the user is a service account.
- `service_account` (Boolean) Indicates whether the user is a service account.

- `status` (String) Status of the user.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- `status` (String) Status of the user.
- `status` (String) The user's status.

- `title` (String) Title of the user.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- `title` (String) Title of the user.
- `title` (String) The user's title.

- `verified` (Boolean) Whether the user is verified.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- `verified` (Boolean) Whether the user is verified.
- `verified` (Boolean) Indicates whether the user is verified.

Loading