Skip to content

Commit

Permalink
Add custom domain data source
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught committed Mar 11, 2023
1 parent 76cd907 commit 85c4966
Show file tree
Hide file tree
Showing 5 changed files with 431 additions and 0 deletions.
35 changes: 35 additions & 0 deletions docs/data-sources/custom_domain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
page_title: "Data Source: auth0_custom_domain"
description: |-
Data source to retrieve the custom domain configuration.
---

# Data Source: auth0_custom_domain

Data source to retrieve the custom domain configuration.



<!-- schema generated by tfplugindocs -->
## Schema

### Read-Only

- `custom_client_ip_header` (String) The HTTP header to fetch the client's IP address. Cannot be set on auth0_managed domains.
- `domain` (String) Name of the custom domain.
- `id` (String) The ID of this resource.
- `origin_domain_name` (String) Once the configuration status is `ready`, the DNS name of the Auth0 origin server that handles traffic for the custom domain.
- `primary` (Boolean) Indicates whether this is a primary domain.
- `status` (String) Configuration status for the custom domain. Options include `disabled`, `pending`, `pending_verification`, and `ready`.
- `tls_policy` (String) TLS policy for the custom domain. Available options are: `compatible` or `recommended`. Compatible includes TLS 1.0, 1.1, 1.2, and recommended only includes TLS 1.2. Cannot be set on self_managed domains.
- `type` (String) Provisioning type for the custom domain. Options include `auth0_managed_certs` and `self_managed_certs`.
- `verification` (List of Object) Configuration settings for verification. (see [below for nested schema](#nestedatt--verification))

<a id="nestedatt--verification"></a>
### Nested Schema for `verification`

Read-Only:

- `methods` (List of Map of String)


58 changes: 58 additions & 0 deletions internal/auth0/customdomain/data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package customdomain

import (
"context"

"github.com/auth0/go-auth0/management"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

internalSchema "github.com/auth0/terraform-provider-auth0/internal/schema"
)

// NewDataSource will return a new auth0_custom_domain data source.
func NewDataSource() *schema.Resource {
return &schema.Resource{
ReadContext: readCustomDomainForDataSource,
Description: "Data source to retrieve the custom domain configuration.",
Schema: dataSourceSchema(),
}
}

func dataSourceSchema() map[string]*schema.Schema {
return internalSchema.TransformResourceToDataSource(NewResource().Schema)
}

func readCustomDomainForDataSource(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
api := meta.(*management.Management)

customDomains, err := api.CustomDomain.List()
if err != nil {
return diag.FromErr(err)
}

// At the moment there can only ever
// be one custom domain configured.
customDomain := customDomains[0]

data.SetId(customDomain.GetID())

result := multierror.Append(
data.Set("domain", customDomain.GetDomain()),
data.Set("type", customDomain.GetType()),
data.Set("primary", customDomain.GetPrimary()),
data.Set("status", customDomain.GetStatus()),
data.Set("origin_domain_name", customDomain.GetOriginDomainName()),
data.Set("custom_client_ip_header", customDomain.GetCustomClientIPHeader()),
data.Set("tls_policy", customDomain.GetTLSPolicy()),
)

if customDomain.Verification != nil {
result = multierror.Append(result, data.Set("verification", []map[string]interface{}{
{"methods": customDomain.Verification.Methods},
}))
}

return diag.FromErr(result.ErrorOrNil())
}
47 changes: 47 additions & 0 deletions internal/auth0/customdomain/data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package customdomain_test

import (
"fmt"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

"github.com/auth0/terraform-provider-auth0/internal/acctest"
"github.com/auth0/terraform-provider-auth0/internal/template"
)

const testAccDataSourceCustomDomain = `
resource "auth0_custom_domain" "my_custom_domain" {
domain = "{{.testName}}.auth.terraform-provider-auth0.com"
type = "auth0_managed_certs"
tls_policy = "recommended"
}
data "auth0_custom_domain" "test" {
depends_on = [ auth0_custom_domain.my_custom_domain ]
}
`

func TestAccDataSourceCustomDomain(t *testing.T) {
testName := strings.ToLower(t.Name())

acctest.Test(t, resource.TestCase{
PreventPostDestroyRefresh: true,
Steps: []resource.TestStep{
{
Config: template.ParseTestName(testAccDataSourceCustomDomain, testName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_custom_domain.my_custom_domain", "domain", fmt.Sprintf("%s.auth.terraform-provider-auth0.com", testName)),
resource.TestCheckResourceAttr("auth0_custom_domain.my_custom_domain", "type", "auth0_managed_certs"),
resource.TestCheckResourceAttr("auth0_custom_domain.my_custom_domain", "status", "pending_verification"),
resource.TestCheckResourceAttr("auth0_custom_domain.my_custom_domain", "origin_domain_name", ""),
resource.TestCheckResourceAttr("auth0_custom_domain.my_custom_domain", "primary", "true"),
resource.TestCheckResourceAttr("auth0_custom_domain.my_custom_domain", "verification.#", "1"),
resource.TestCheckResourceAttr("auth0_custom_domain.my_custom_domain", "custom_client_ip_header", ""),
resource.TestCheckResourceAttr("auth0_custom_domain.my_custom_domain", "tls_policy", "recommended"),
),
},
},
})
}
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func New() *schema.Provider {
"auth0_client": client.NewDataSource(),
"auth0_global_client": client.NewGlobalDataSource(),
"auth0_connection": connection.NewDataSource(),
"auth0_custom_domain": customdomain.NewDataSource(),
"auth0_organization": organization.NewDataSource(),
"auth0_resource_server": resourceserver.NewDataSource(),
"auth0_role": role.NewDataSource(),
Expand Down
Loading

0 comments on commit 85c4966

Please sign in to comment.