Skip to content

Commit

Permalink
feat(usermanagement): add data source to get current user details #2596
Browse files Browse the repository at this point in the history
chore(usermanagement): minor refactor in a bid to improve reusability
  • Loading branch information
pranav-new-relic committed Aug 20, 2024
1 parent a4fa318 commit 2581918
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 14 deletions.
48 changes: 48 additions & 0 deletions newrelic/data_source_newrelic_user_current.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package newrelic

import (
"context"
"fmt"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceNewRelicCurrentUser() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceNewRelicCurrentUserRead,
Schema: map[string]*schema.Schema{
UserDataSourceUserNameAttrLabel: dataSourceNewRelicCurrentUserSchemaConstructor(UserDataSourceUserNameAttrLabel),
UserDataSourceUserEmailAttrLabel: dataSourceNewRelicCurrentUserSchemaConstructor(UserDataSourceUserEmailAttrLabel),
},
}
}

func dataSourceNewRelicCurrentUserSchemaConstructor(attributeLabel string) *schema.Schema {
return &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: fmt.Sprintf("The %s of the current user, i.e. the user owning the API key the Terraform Provider has been initialised with.", attributeLabel),
}
}

func dataSourceNewRelicCurrentUserRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerConfig := meta.(*ProviderConfig)
client := providerConfig.NewClient

resp, err := client.CustomerAdministration.GetUser()

if err != nil {
return diag.FromErr(err)
}

if resp == nil {
return diag.FromErr(fmt.Errorf("failed to fetch current user"))
}

d.SetId(strconv.Itoa(resp.ID))
_ = d.Set(UserDataSourceUserNameAttrLabel, resp.Name)
_ = d.Set(UserDataSourceUserEmailAttrLabel, resp.Email)
return nil
}
45 changes: 45 additions & 0 deletions newrelic/data_source_newrelic_user_current_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//go:build integration
// +build integration

package newrelic

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccNewRelicCurrentUserDataSource_Basic(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccNewRelicCurrentUserDataSourceConfiguration(),
Check: resource.ComposeTestCheckFunc(
testAccNewRelicCheckCurrentUserDataSourceExists(t, "data.newrelic_current_user.foo"),
),
},
},
})
}

func testAccNewRelicCheckCurrentUserDataSourceExists(t *testing.T, n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
r := s.RootModule().Resources[n]
a := r.Primary.Attributes

if a["id"] == "" {
return fmt.Errorf("expected to get an ID of the matching user")
}

return nil
}
}

func testAccNewRelicCurrentUserDataSourceConfiguration() string {
return fmt.Sprintf(`
data "newrelic_current_user" "foo" {
}`)
}
34 changes: 20 additions & 14 deletions newrelic/data_source_newrelic_user_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,25 @@ func dataSourceNewRelicUser() *schema.Resource {
Required: true,
Description: "The ID of the Authentication Domain the user being queried would belong to.",
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The name of the user to be queried.",
AtLeastOneOf: []string{"name", "email_id"},
UserDataSourceUserNameAttrLabel: {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The name of the user to be queried.",
AtLeastOneOf: []string{
UserDataSourceUserNameAttrLabel,
UserDataSourceUserEmailAttrLabel,
},
},
"email_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The email ID of the user to be queried.",
AtLeastOneOf: []string{"name", "email_id"},
UserDataSourceUserEmailAttrLabel: {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The email ID of the user to be queried.",
AtLeastOneOf: []string{
UserDataSourceUserNameAttrLabel,
UserDataSourceUserEmailAttrLabel,
},
},
"id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -88,8 +94,8 @@ func dataSourceNewRelicUserRead(ctx context.Context, d *schema.ResourceData, met
if authDomain.ID == authenticationDomainID {
for _, u := range authDomain.Users.Users {
d.SetId(u.ID)
_ = d.Set("name", u.Name)
_ = d.Set("email_id", u.Email)
_ = d.Set(UserDataSourceUserNameAttrLabel, u.Name)
_ = d.Set(UserDataSourceUserEmailAttrLabel, u.Email)
return nil
}
}
Expand Down
3 changes: 3 additions & 0 deletions newrelic/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,6 @@ func revertEscapedSingleQuote(name string) string {

return name
}

const UserDataSourceUserNameAttrLabel = "name"
const UserDataSourceUserEmailAttrLabel = "email_id"
1 change: 1 addition & 0 deletions newrelic/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ func Provider() *schema.Provider {
"newrelic_application": dataSourceNewRelicApplication(),
"newrelic_authentication_domain": dataSourceNewRelicAuthenticationDomain(),
"newrelic_cloud_account": dataSourceNewRelicCloudAccount(),
"newrelic_current_user": dataSourceNewRelicCurrentUser(),
"newrelic_entity": dataSourceNewRelicEntity(),
"newrelic_group": dataSourceNewRelicGroup(),
"newrelic_key_transaction": dataSourceNewRelicKeyTransaction(),
Expand Down
2 changes: 2 additions & 0 deletions website/docs/d/user.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ description: |-

The `newrelic_user` data source helps search for a user by their name and/or email ID, and accordingly, fetch the ID of the matching user.

-> **NOTE:** If you would only like to fetch the details of the current user (the user owning the API key which has been used to initialize the New Relic Terraform Provider to run operations) and **not** search for a specific user within an authentication domain, please head over to the documentation of the [`newrelic_current_user`](https://registry.terraform.io/providers/newrelic/newrelic/latest/docs/data-sources/user_current) data source for more details and examples to do so.

## Example Usage

The below example illustrates fetching a the ID of a user (and other arguments) using the ID of the authentication domain the user belongs to, as well as a name and/or email ID, which can be used as criteria to search for a user who matches these specified parameters.
Expand Down
41 changes: 41 additions & 0 deletions website/docs/d/user_current.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
layout: "newrelic"
page_title: "New Relic: newrelic_current_user"
sidebar_current: "docs-newrelic-datasource-current-user"
description: |-
This data source helps fetch the current user, i.e. the owning user of the credentials (API Key), using which the New Relic Terraform Provider has been initialized to perform operations.
---

# Data Source: newrelic_current_user

The `newrelic_current_user` data source helps fetch the current user, i.e. the owning user of the credentials (API Key), using which the New Relic Terraform Provider has been initialized to perform operations.

-> **NOTE:** If you would like to search for a specific user by `name` or `email_id` within a specific authentication domain, please head over to the documentation of the [`newrelic_user`](https://registry.terraform.io/providers/newrelic/newrelic/latest/docs/data-sources/user) data source for more details and examples to do so.

## Example Usage

The below example illustrates fetching the current user and associated metadata (the `name` and `email_id` of the current user) using the `newrelic_current_user` data source. The data source does not require any arguments to be specified.
```hcl
data "newrelic_current_user" "foo" {
}
output "current_user_details" {
value = {
"user_id" : data.newrelic_current_user.foo.id
"user_email_id" : data.newrelic_current_user.foo.email_id
"user_name" : data.newrelic_current_user.foo.name
}
}
```

The ID of the current user fetched may be applied in other use cases within the New Relic Terraform Provider; for instance, this may be furnished as the value of the argument [`user_id`](https://registry.terraform.io/providers/newrelic/newrelic/latest/docs/resources/api_access_key#user_id) if required, to create `USER` API Keys with the same user's credentials using the [`newrelic_api_access_key`](https://registry.terraform.io/providers/newrelic/newrelic/latest/docs/resources/api_access_key) resource.

## Argument Reference
This data source does not require any arguments to be specified.

## Attributes Reference
The following attributes are exported by this data source.
* `id` - The ID of the current user.
* `name` - The name of the current user.
* `email_id` - The email ID of the current user.

0 comments on commit 2581918

Please sign in to comment.