-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(user_management): addition of a resource to manage users (#2575)
- Loading branch information
1 parent
42026bf
commit 26c5b49
Showing
11 changed files
with
660 additions
and
9 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
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,100 @@ | ||
package newrelic | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func dataSourceNewRelicUser() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataSourceNewRelicUserRead, | ||
Schema: map[string]*schema.Schema{ | ||
"authentication_domain_id": { | ||
Type: schema.TypeString, | ||
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"}, | ||
}, | ||
"email_id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
Description: "The email ID of the user to be queried.", | ||
AtLeastOneOf: []string{"name", "email_id"}, | ||
}, | ||
"id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "The ID of the fetched user.", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceNewRelicUserRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
providerConfig := meta.(*ProviderConfig) | ||
client := providerConfig.NewClient | ||
|
||
log.Printf("[INFO] Fetching Users") | ||
|
||
name, nameOk := d.GetOk("name") | ||
email, emailOk := d.GetOk("email_id") | ||
authDomainID, authDomainIDOk := d.GetOk("authentication_domain_id") | ||
|
||
nameQuery := "" | ||
emailQuery := "" | ||
|
||
if nameOk && name != "" { | ||
nameQuery = name.(string) | ||
} | ||
|
||
if emailOk && email != "" { | ||
emailQuery = email.(string) | ||
} | ||
|
||
if !authDomainIDOk { | ||
return diag.FromErr(fmt.Errorf("'authentication_domain_id' is required")) | ||
} | ||
|
||
authenticationDomainID := authDomainID.(string) | ||
|
||
resp, err := client.UserManagement.UserManagementGetUsersWithContext( | ||
ctx, | ||
[]string{authenticationDomainID}, | ||
[]string{}, | ||
nameQuery, | ||
emailQuery, | ||
) | ||
|
||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
if resp == nil { | ||
return diag.FromErr(fmt.Errorf("failed to fetch users")) | ||
} | ||
|
||
for _, authDomain := range resp.AuthenticationDomains { | ||
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) | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
return diag.FromErr(fmt.Errorf("no user found with the specified parameters")) | ||
|
||
} |
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,85 @@ | ||
//go:build integration | ||
// +build integration | ||
|
||
package newrelic | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
) | ||
|
||
var authenticationDomainName = "Test-Auth-Domain DO NOT DELETE" | ||
var existingUserEmail = strings.ReplaceAll(userEmailPrefix, "#", "integration") | ||
var existingUserName = "Integration Test User 1 DO NOT DELETE" | ||
|
||
func TestAccNewRelicUserDataSource_Basic(t *testing.T) { | ||
resource.ParallelTest(t, resource.TestCase{ | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccNewRelicUserDataSourceConfiguration(authenticationDomainName, existingUserEmail, ""), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccNewRelicCheckUserDataSourceExists(t, "data.newrelic_user.foo"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccNewRelicUserDataSource_EmailAndName(t *testing.T) { | ||
resource.ParallelTest(t, resource.TestCase{ | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccNewRelicUserDataSourceConfiguration(authenticationDomainName, existingUserEmail, existingUserName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccNewRelicCheckUserDataSourceExists(t, "data.newrelic_user.foo"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccNewRelicUserDataSource_MissingError(t *testing.T) { | ||
resource.ParallelTest(t, resource.TestCase{ | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccNewRelicUserDataSourceConfiguration(authenticationDomainName, "", fmt.Sprintf("%s-Invalid", existingUserName)), | ||
ExpectError: regexp.MustCompile(`no user found with the specified parameters`), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccNewRelicCheckUserDataSourceExists(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 testAccNewRelicUserDataSourceConfiguration(authenticationDomainName string, userEmailID string, userName string) string { | ||
return fmt.Sprintf(` | ||
data "newrelic_authentication_domain" "foo" { | ||
name = "%s" | ||
} | ||
data "newrelic_user" "foo" { | ||
authentication_domain_id = data.newrelic_authentication_domain.foo.id | ||
email_id = "%s" | ||
name = "%s" | ||
} | ||
`, authenticationDomainName, userEmailID, userName) | ||
} |
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
Oops, something went wrong.