diff --git a/go.mod b/go.mod index 93fab5e75..4c1cd7ec0 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/mitchellh/go-homedir v1.1.0 github.com/newrelic/go-agent/v3 v3.29.1 github.com/newrelic/go-insights v1.0.3 - github.com/newrelic/newrelic-client-go/v2 v2.23.0 + github.com/newrelic/newrelic-client-go/v2 v2.24.0 github.com/stretchr/testify v1.8.4 golang.org/x/exp v0.0.0-20240119083558-1b970713d09a ) diff --git a/go.sum b/go.sum index a7c3ebd1e..95c41bc9a 100644 --- a/go.sum +++ b/go.sum @@ -46,7 +46,7 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= -github.com/elazarl/goproxy v0.0.0-20220417044921-416226498f94 h1:VIy7cdK7ufs7ctpTFkXJHm1uP3dJSnCGSPysEICB1so= +github.com/elazarl/goproxy v0.0.0-20230731152917-f99041a5c027 h1:1L0aalTpPz7YlMxETKpmQoWMBkeiuorElZIXoNmgiPE= github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg= github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= @@ -263,8 +263,8 @@ github.com/newrelic/go-agent/v3 v3.29.1 h1:OINNRev5ImiyRq0IUYwhfTmtqQgQFYyDNQEtb github.com/newrelic/go-agent/v3 v3.29.1/go.mod h1:9utrgxlSryNqRrTvII2XBL+0lpofXbqXApvVWPpbzUg= github.com/newrelic/go-insights v1.0.3 h1:zSNp1CEZnXktzSIEsbHJk8v6ZihdPFP2WsO/fzau3OQ= github.com/newrelic/go-insights v1.0.3/go.mod h1:A20BoT8TNkqPGX2nS/Z2fYmKl3Cqa3iKZd4whzedCY4= -github.com/newrelic/newrelic-client-go/v2 v2.23.0 h1:ULqiPv4Z0QVLjMjX6uP13wSxPDL3uiKTt5U8vFsvpCQ= -github.com/newrelic/newrelic-client-go/v2 v2.23.0/go.mod h1:VPWTvEfKvnTZLunAC7fiW33y4e0srznNfN5HJH2cOp8= +github.com/newrelic/newrelic-client-go/v2 v2.24.0 h1:L4T0+wQ0P+GvxsbkNGMUW9umpBaQ2BjaTD98eyDeBCY= +github.com/newrelic/newrelic-client-go/v2 v2.24.0/go.mod h1:SO5KJuFJ/+l3lT8nOdNLTrcE9FoZ4m60kechCVb+1N4= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw= github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA= diff --git a/newrelic/data_source_newrelic_authentication_domain.go b/newrelic/data_source_newrelic_authentication_domain.go new file mode 100644 index 000000000..9cb087ae8 --- /dev/null +++ b/newrelic/data_source_newrelic_authentication_domain.go @@ -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 +} diff --git a/newrelic/data_source_newrelic_authentication_domain_test.go b/newrelic/data_source_newrelic_authentication_domain_test.go new file mode 100644 index 000000000..6b016994a --- /dev/null +++ b/newrelic/data_source_newrelic_authentication_domain_test.go @@ -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) +} diff --git a/newrelic/provider.go b/newrelic/provider.go index 85b9ad342..7895f7647 100755 --- a/newrelic/provider.go +++ b/newrelic/provider.go @@ -124,6 +124,7 @@ func Provider() *schema.Provider { "newrelic_alert_channel": dataSourceNewRelicAlertChannel(), "newrelic_alert_policy": dataSourceNewRelicAlertPolicy(), "newrelic_application": dataSourceNewRelicApplication(), + "newrelic_authentication_domain": dataSourceNewRelicAuthenticationDomain(), "newrelic_cloud_account": dataSourceNewRelicCloudAccount(), "newrelic_entity": dataSourceNewRelicEntity(), "newrelic_key_transaction": dataSourceNewRelicKeyTransaction(), diff --git a/website/docs/d/authentication_domain.html.markdown b/website/docs/d/authentication_domain.html.markdown new file mode 100644 index 000000000..ed7583e12 --- /dev/null +++ b/website/docs/d/authentication_domain.html.markdown @@ -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. +