-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth_domains): add data source to fetch auth domains (#2553)
- Loading branch information
1 parent
a9baf25
commit 9e8e8c8
Showing
6 changed files
with
167 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package newrelic | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func dataSourceNewRelicAuthenticationDomain() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataSourceNewRelicAuthenticationDomainRead, | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "The name of the authentication domain to be queried.", | ||
}, | ||
"id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "The ID of the fetched authentication domain.", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceNewRelicAuthenticationDomainRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
providerConfig := meta.(*ProviderConfig) | ||
client := providerConfig.NewClient | ||
matchingAuthenticationDomainID := "" | ||
|
||
log.Printf("[INFO] Fetching Authentication Domains") | ||
|
||
name, nameOk := d.GetOk("name") | ||
if !nameOk { | ||
return diag.FromErr(errors.New("`name` is required")) | ||
} | ||
|
||
resp, err := client.UserManagement.GetAuthenticationDomainsWithContext(ctx, "", []string{}) | ||
|
||
if resp == nil { | ||
return diag.FromErr(fmt.Errorf("failed to fetch authentication domains")) | ||
} | ||
|
||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
for _, authenticationDomain := range resp.AuthenticationDomains { | ||
if name == authenticationDomain.Name { | ||
matchingAuthenticationDomainID = authenticationDomain.ID | ||
break | ||
} | ||
} | ||
|
||
if matchingAuthenticationDomainID == "" { | ||
return diag.FromErr(fmt.Errorf("no authentication domain found with the name %s", name)) | ||
} | ||
|
||
d.SetId(matchingAuthenticationDomainID) | ||
|
||
return nil | ||
} |
60 changes: 60 additions & 0 deletions
60
newrelic/data_source_newrelic_authentication_domain_test.go
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,60 @@ | ||
//go:build integration | ||
// +build integration | ||
|
||
package newrelic | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
) | ||
|
||
func TestAccNewRelicAuthenticationDomain_Basic(t *testing.T) { | ||
resource.ParallelTest(t, resource.TestCase{ | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccNewRelicAuthenticationDomainDataSourceConfiguration("Test-Auth-Domain DO NOT DELETE"), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccNewRelicCheckAuthenticationDomainExists(t, "data.newrelic_authentication_domain.foo"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccNewRelicAuthenticationDomain_MissingError(t *testing.T) { | ||
resource.ParallelTest(t, resource.TestCase{ | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccNewRelicAuthenticationDomainDataSourceConfiguration("Invalid-Auth-Domain"), | ||
ExpectError: regexp.MustCompile(`no authentication domain found`), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccNewRelicCheckAuthenticationDomainExists(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 authentication domain") | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccNewRelicAuthenticationDomainDataSourceConfiguration(name string) string { | ||
return fmt.Sprintf(` | ||
data "newrelic_authentication_domain" "foo" { | ||
name = "%s" | ||
} | ||
`, name) | ||
} |
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,35 @@ | ||
--- | ||
layout: "newrelic" | ||
page_title: "New Relic: newrelic_authentication_domain" | ||
sidebar_current: "docs-newrelic-datasource-authentication-domain" | ||
description: |- | ||
A data source that helps fetch authentication domains seen in the New Relic One UI, matching the name specified. | ||
--- | ||
|
||
# Data Source: newrelic\_authentication\_domain | ||
|
||
Use this data source to fetch the ID of an authentication domain belonging to your account, matching the specified name. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "newrelic_authentication_domain" "foo" { | ||
name = "Test Authentication Domain" | ||
} | ||
output "foo" { | ||
value = data.newrelic_authentication_domain.foo.id | ||
} | ||
``` | ||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `name` - (Required) The name of the authentication domain to be searched for. An error is thrown, if no authentication domain is found with the specified name. | ||
|
||
## Attributes Reference | ||
|
||
The following attributes are exported: | ||
|
||
* `id` - The ID of the matching authentication domain fetched. | ||
|