From 141129adb4acca8bf5ced7fb0d80e560852b082e Mon Sep 17 00:00:00 2001 From: Sergiu Ghitea <28300158+sergiught@users.noreply.github.com> Date: Sat, 11 Mar 2023 22:21:17 +0100 Subject: [PATCH] Add custom domain data source --- docs/data-sources/custom_domain.md | 35 +++ internal/auth0/customdomain/data_source.go | 58 ++++ .../auth0/customdomain/data_source_test.go | 47 +++ internal/provider/provider.go | 1 + .../TestAccDataSourceCustomDomain.yaml | 290 ++++++++++++++++++ 5 files changed, 431 insertions(+) create mode 100644 docs/data-sources/custom_domain.md create mode 100644 internal/auth0/customdomain/data_source.go create mode 100644 internal/auth0/customdomain/data_source_test.go create mode 100644 test/data/recordings/TestAccDataSourceCustomDomain.yaml diff --git a/docs/data-sources/custom_domain.md b/docs/data-sources/custom_domain.md new file mode 100644 index 000000000..446b7252b --- /dev/null +++ b/docs/data-sources/custom_domain.md @@ -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 + +### 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)) + + +### Nested Schema for `verification` + +Read-Only: + +- `methods` (List of Map of String) + + diff --git a/internal/auth0/customdomain/data_source.go b/internal/auth0/customdomain/data_source.go new file mode 100644 index 000000000..f45ed223d --- /dev/null +++ b/internal/auth0/customdomain/data_source.go @@ -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()) +} diff --git a/internal/auth0/customdomain/data_source_test.go b/internal/auth0/customdomain/data_source_test.go new file mode 100644 index 000000000..2f265598a --- /dev/null +++ b/internal/auth0/customdomain/data_source_test.go @@ -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("data.auth0_custom_domain.test", "domain", fmt.Sprintf("%s.auth.terraform-provider-auth0.com", testName)), + resource.TestCheckResourceAttr("data.auth0_custom_domain.test", "type", "auth0_managed_certs"), + resource.TestCheckResourceAttr("data.auth0_custom_domain.test", "status", "pending_verification"), + resource.TestCheckResourceAttr("data.auth0_custom_domain.test", "origin_domain_name", ""), + resource.TestCheckResourceAttr("data.auth0_custom_domain.test", "primary", "true"), + resource.TestCheckResourceAttr("data.auth0_custom_domain.test", "verification.#", "1"), + resource.TestCheckResourceAttr("data.auth0_custom_domain.test", "custom_client_ip_header", ""), + resource.TestCheckResourceAttr("data.auth0_custom_domain.test", "tls_policy", "recommended"), + ), + }, + }, + }) +} diff --git a/internal/provider/provider.go b/internal/provider/provider.go index ff0b8012a..5ed133047 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -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(), diff --git a/test/data/recordings/TestAccDataSourceCustomDomain.yaml b/test/data/recordings/TestAccDataSourceCustomDomain.yaml new file mode 100644 index 000000000..f4734ba16 --- /dev/null +++ b/test/data/recordings/TestAccDataSourceCustomDomain.yaml @@ -0,0 +1,290 @@ +--- +version: 2 +interactions: + - id: 0 + request: + proto: HTTP/1.1 + proto_major: 1 + proto_minor: 1 + content_length: 133 + transfer_encoding: [] + trailer: {} + host: terraform-provider-auth0-dev.eu.auth0.com + remote_addr: "" + request_uri: "" + body: | + {"domain":"testaccdatasourcecustomdomain.auth.terraform-provider-auth0.com","type":"auth0_managed_certs","tls_policy":"recommended"} + form: {} + headers: + Content-Type: + - application/json + User-Agent: + - Go-Auth0-SDK/0.15.1 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/custom-domains + method: POST + response: + proto: HTTP/2.0 + proto_major: 2 + proto_minor: 0 + transfer_encoding: [] + trailer: {} + content_length: 333 + uncompressed: false + body: '{"custom_domain_id":"cd_lpxrpAKsPeWXCwAv","domain":"testaccdatasourcecustomdomain.auth.terraform-provider-auth0.com","primary":true,"status":"pending_verification","type":"auth0_managed_certs","verification":{"methods":[{"name":"cname","record":"terraform-provider-auth0-dev-cd-lpxrpakspewxcwav.edge.tenants.eu.auth0.com"}]},"tls_policy":"recommended"}' + headers: + Content-Type: + - application/json; charset=utf-8 + status: 201 Created + code: 201 + duration: 4.233335333s + - id: 1 + request: + proto: HTTP/1.1 + proto_major: 1 + proto_minor: 1 + content_length: 5 + transfer_encoding: [] + trailer: {} + host: terraform-provider-auth0-dev.eu.auth0.com + remote_addr: "" + request_uri: "" + body: | + null + form: {} + headers: + Content-Type: + - application/json + User-Agent: + - Go-Auth0-SDK/0.15.1 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/custom-domains/cd_lpxrpAKsPeWXCwAv + method: GET + response: + proto: HTTP/2.0 + proto_major: 2 + proto_minor: 0 + transfer_encoding: [] + trailer: {} + content_length: -1 + uncompressed: true + body: '{"custom_domain_id":"cd_lpxrpAKsPeWXCwAv","domain":"testaccdatasourcecustomdomain.auth.terraform-provider-auth0.com","primary":true,"status":"pending_verification","type":"auth0_managed_certs","verification":{"methods":[{"name":"cname","record":"terraform-provider-auth0-dev-cd-lpxrpakspewxcwav.edge.tenants.eu.auth0.com"}]},"tls_policy":"recommended"}' + headers: + Content-Type: + - application/json; charset=utf-8 + status: 200 OK + code: 200 + duration: 132.650458ms + - id: 2 + request: + proto: HTTP/1.1 + proto_major: 1 + proto_minor: 1 + content_length: 5 + transfer_encoding: [] + trailer: {} + host: terraform-provider-auth0-dev.eu.auth0.com + remote_addr: "" + request_uri: "" + body: | + null + form: {} + headers: + Content-Type: + - application/json + User-Agent: + - Go-Auth0-SDK/0.15.1 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/custom-domains + method: GET + response: + proto: HTTP/2.0 + proto_major: 2 + proto_minor: 0 + transfer_encoding: [] + trailer: {} + content_length: -1 + uncompressed: true + body: '[{"custom_domain_id":"cd_lpxrpAKsPeWXCwAv","domain":"testaccdatasourcecustomdomain.auth.terraform-provider-auth0.com","primary":true,"status":"pending_verification","type":"auth0_managed_certs","verification":{"methods":[{"name":"cname","record":"terraform-provider-auth0-dev-cd-lpxrpakspewxcwav.edge.tenants.eu.auth0.com"}]},"tls_policy":"recommended"}]' + headers: + Content-Type: + - application/json; charset=utf-8 + status: 200 OK + code: 200 + duration: 114.075417ms + - id: 3 + request: + proto: HTTP/1.1 + proto_major: 1 + proto_minor: 1 + content_length: 5 + transfer_encoding: [] + trailer: {} + host: terraform-provider-auth0-dev.eu.auth0.com + remote_addr: "" + request_uri: "" + body: | + null + form: {} + headers: + Content-Type: + - application/json + User-Agent: + - Go-Auth0-SDK/0.15.1 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/custom-domains + method: GET + response: + proto: HTTP/2.0 + proto_major: 2 + proto_minor: 0 + transfer_encoding: [] + trailer: {} + content_length: -1 + uncompressed: true + body: '[{"custom_domain_id":"cd_lpxrpAKsPeWXCwAv","domain":"testaccdatasourcecustomdomain.auth.terraform-provider-auth0.com","primary":true,"status":"pending_verification","type":"auth0_managed_certs","verification":{"methods":[{"name":"cname","record":"terraform-provider-auth0-dev-cd-lpxrpakspewxcwav.edge.tenants.eu.auth0.com"}]},"tls_policy":"recommended"}]' + headers: + Content-Type: + - application/json; charset=utf-8 + status: 200 OK + code: 200 + duration: 81.631667ms + - id: 4 + request: + proto: HTTP/1.1 + proto_major: 1 + proto_minor: 1 + content_length: 5 + transfer_encoding: [] + trailer: {} + host: terraform-provider-auth0-dev.eu.auth0.com + remote_addr: "" + request_uri: "" + body: | + null + form: {} + headers: + Content-Type: + - application/json + User-Agent: + - Go-Auth0-SDK/0.15.1 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/custom-domains/cd_lpxrpAKsPeWXCwAv + method: GET + response: + proto: HTTP/2.0 + proto_major: 2 + proto_minor: 0 + transfer_encoding: [] + trailer: {} + content_length: -1 + uncompressed: true + body: '{"custom_domain_id":"cd_lpxrpAKsPeWXCwAv","domain":"testaccdatasourcecustomdomain.auth.terraform-provider-auth0.com","primary":true,"status":"pending_verification","type":"auth0_managed_certs","verification":{"methods":[{"name":"cname","record":"terraform-provider-auth0-dev-cd-lpxrpakspewxcwav.edge.tenants.eu.auth0.com"}]},"tls_policy":"recommended"}' + headers: + Content-Type: + - application/json; charset=utf-8 + status: 200 OK + code: 200 + duration: 87.877958ms + - id: 5 + request: + proto: HTTP/1.1 + proto_major: 1 + proto_minor: 1 + content_length: 5 + transfer_encoding: [] + trailer: {} + host: terraform-provider-auth0-dev.eu.auth0.com + remote_addr: "" + request_uri: "" + body: | + null + form: {} + headers: + Content-Type: + - application/json + User-Agent: + - Go-Auth0-SDK/0.15.1 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/custom-domains + method: GET + response: + proto: HTTP/2.0 + proto_major: 2 + proto_minor: 0 + transfer_encoding: [] + trailer: {} + content_length: -1 + uncompressed: true + body: '[{"custom_domain_id":"cd_lpxrpAKsPeWXCwAv","domain":"testaccdatasourcecustomdomain.auth.terraform-provider-auth0.com","primary":true,"status":"pending_verification","type":"auth0_managed_certs","verification":{"methods":[{"name":"cname","record":"terraform-provider-auth0-dev-cd-lpxrpakspewxcwav.edge.tenants.eu.auth0.com"}]},"tls_policy":"recommended"}]' + headers: + Content-Type: + - application/json; charset=utf-8 + status: 200 OK + code: 200 + duration: 90.23425ms + - id: 6 + request: + proto: HTTP/1.1 + proto_major: 1 + proto_minor: 1 + content_length: 5 + transfer_encoding: [] + trailer: {} + host: terraform-provider-auth0-dev.eu.auth0.com + remote_addr: "" + request_uri: "" + body: | + null + form: {} + headers: + Content-Type: + - application/json + User-Agent: + - Go-Auth0-SDK/0.15.1 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/custom-domains + method: GET + response: + proto: HTTP/2.0 + proto_major: 2 + proto_minor: 0 + transfer_encoding: [] + trailer: {} + content_length: -1 + uncompressed: true + body: '[{"custom_domain_id":"cd_lpxrpAKsPeWXCwAv","domain":"testaccdatasourcecustomdomain.auth.terraform-provider-auth0.com","primary":true,"status":"pending_verification","type":"auth0_managed_certs","verification":{"methods":[{"name":"cname","record":"terraform-provider-auth0-dev-cd-lpxrpakspewxcwav.edge.tenants.eu.auth0.com"}]},"tls_policy":"recommended"}]' + headers: + Content-Type: + - application/json; charset=utf-8 + status: 200 OK + code: 200 + duration: 78.119791ms + - id: 7 + request: + proto: HTTP/1.1 + proto_major: 1 + proto_minor: 1 + content_length: 0 + transfer_encoding: [] + trailer: {} + host: terraform-provider-auth0-dev.eu.auth0.com + remote_addr: "" + request_uri: "" + body: "" + form: {} + headers: + Content-Type: + - application/json + User-Agent: + - Go-Auth0-SDK/0.15.1 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/custom-domains/cd_lpxrpAKsPeWXCwAv + method: DELETE + response: + proto: HTTP/2.0 + proto_major: 2 + proto_minor: 0 + transfer_encoding: [] + trailer: {} + content_length: 0 + uncompressed: false + body: "" + headers: + Content-Type: + - application/json; charset=utf-8 + status: 204 No Content + code: 204 + duration: 4.3539955s