-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(usermanagement): new data source to fetch current user details #…
- Loading branch information
1 parent
a4fa318
commit b22561f
Showing
7 changed files
with
162 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" { | ||
}`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|