Skip to content

Commit

Permalink
feat(auth_domains): add data source to fetch auth domains (#2553)
Browse files Browse the repository at this point in the history
  • Loading branch information
pranav-new-relic authored Feb 1, 2024
1 parent a9baf25 commit 9e8e8c8
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
6 changes: 3 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
67 changes: 67 additions & 0 deletions newrelic/data_source_newrelic_authentication_domain.go
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 newrelic/data_source_newrelic_authentication_domain_test.go
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)
}
1 change: 1 addition & 0 deletions newrelic/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
35 changes: 35 additions & 0 deletions website/docs/d/authentication_domain.html.markdown
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.

0 comments on commit 9e8e8c8

Please sign in to comment.