From b4b812d845d96fed97cc95adcd7f505decfb9180 Mon Sep 17 00:00:00 2001 From: Jeffrey Cline <20408400+WodansSon@users.noreply.github.com> Date: Fri, 18 Nov 2022 00:35:13 -0700 Subject: [PATCH 1/6] add cdn frontdoor custom domain data source --- ...cdn_frontdoor_custom_domain_data_source.go | 143 ++++++++++++++++++ ...rontdoor_custom_domain_data_source_test.go | 44 ++++++ internal/services/cdn/registration.go | 1 + .../cdn_frontdoor_custom_domain.html.markdown | 65 ++++++++ 4 files changed, 253 insertions(+) create mode 100644 internal/services/cdn/cdn_frontdoor_custom_domain_data_source.go create mode 100644 internal/services/cdn/cdn_frontdoor_custom_domain_data_source_test.go create mode 100644 website/docs/d/cdn_frontdoor_custom_domain.html.markdown diff --git a/internal/services/cdn/cdn_frontdoor_custom_domain_data_source.go b/internal/services/cdn/cdn_frontdoor_custom_domain_data_source.go new file mode 100644 index 000000000000..6e22c4fdfd07 --- /dev/null +++ b/internal/services/cdn/cdn_frontdoor_custom_domain_data_source.go @@ -0,0 +1,143 @@ +package cdn + +import ( + "fmt" + "time" + + "github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema" + "github.com/hashicorp/terraform-provider-azurerm/internal/clients" + "github.com/hashicorp/terraform-provider-azurerm/internal/services/cdn/parse" + "github.com/hashicorp/terraform-provider-azurerm/internal/services/cdn/validate" + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" + "github.com/hashicorp/terraform-provider-azurerm/internal/timeouts" + "github.com/hashicorp/terraform-provider-azurerm/utils" +) + +func dataSourceCdnFrontDoorCustomDomain() *pluginsdk.Resource { + return &pluginsdk.Resource{ + Read: dataSourceCdnFrontDoorCustomDomainRead, + + Timeouts: &pluginsdk.ResourceTimeout{ + Read: pluginsdk.DefaultTimeout(5 * time.Minute), + }, + + Schema: map[string]*pluginsdk.Schema{ + "name": { + Type: pluginsdk.TypeString, + Required: true, + ValidateFunc: validate.FrontDoorCustomDomainName, + }, + + "profile_name": { + Type: pluginsdk.TypeString, + Required: true, + ValidateFunc: validate.FrontDoorName, + }, + + "resource_group_name": commonschema.ResourceGroupNameForDataSource(), + + "cdn_frontdoor_profile_id": { + Type: pluginsdk.TypeString, + Computed: true, + }, + + "dns_zone_id": { + Type: pluginsdk.TypeString, + Computed: true, + }, + + "host_name": { + Type: pluginsdk.TypeString, + Computed: true, + }, + + "tls": { + Type: pluginsdk.TypeList, + Computed: true, + + Elem: &pluginsdk.Resource{ + Schema: map[string]*pluginsdk.Schema{ + + "certificate_type": { + Type: pluginsdk.TypeString, + Computed: true, + }, + + "minimum_tls_version": { + Type: pluginsdk.TypeString, + Computed: true, + }, + + "cdn_frontdoor_secret_id": { + Type: pluginsdk.TypeString, + Computed: true, + }, + }, + }, + }, + + "expiration_date": { + Type: pluginsdk.TypeString, + Computed: true, + }, + + "validation_token": { + Type: pluginsdk.TypeString, + Computed: true, + }, + }, + } +} + +func dataSourceCdnFrontDoorCustomDomainRead(d *pluginsdk.ResourceData, meta interface{}) error { + client := meta.(*clients.Client).Cdn.FrontDoorCustomDomainsClient + subscriptionId := meta.(*clients.Client).Account.SubscriptionId + ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) + defer cancel() + + id := parse.NewFrontDoorCustomDomainID(subscriptionId, d.Get("resource_group_name").(string), d.Get("profile_name").(string), d.Get("name").(string)) + profile := parse.NewFrontDoorProfileID(subscriptionId, d.Get("resource_group_name").(string), d.Get("profile_name").(string)) + + resp, err := client.Get(ctx, id.ResourceGroup, id.ProfileName, id.CustomDomainName) + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + return fmt.Errorf("%s was not found", id) + } + return fmt.Errorf("retrieving %s: %+v", id, err) + } + + d.SetId(id.ID()) + d.Set("name", id.CustomDomainName) + d.Set("resource_group_name", id.ResourceGroup) + d.Set("profile_name", id.ProfileName) + d.Set("cdn_frontdoor_profile_id", profile.ID()) + + if props := resp.AFDDomainProperties; props != nil { + d.Set("host_name", props.HostName) + + dnsZoneId, err := flattenDNSZoneResourceReference(props.AzureDNSZone) + if err != nil { + return fmt.Errorf("flattening `dns_zone_id`: %+v", err) + } + + if err := d.Set("dns_zone_id", dnsZoneId); err != nil { + return fmt.Errorf("setting `dns_zone_id`: %+v", err) + } + + tls, err := flattenCustomDomainAFDDomainHttpsParameters(props.TLSSettings) + if err != nil { + return fmt.Errorf("flattening `tls`: %+v", err) + } + + if err := d.Set("tls", tls); err != nil { + return fmt.Errorf("setting `tls`: %+v", err) + } + + if validationProps := props.ValidationProperties; validationProps != nil { + d.Set("expiration_date", validationProps.ExpirationDate) + d.Set("validation_token", validationProps.ValidationToken) + } + } + + return nil +} diff --git a/internal/services/cdn/cdn_frontdoor_custom_domain_data_source_test.go b/internal/services/cdn/cdn_frontdoor_custom_domain_data_source_test.go new file mode 100644 index 000000000000..383edbf0db87 --- /dev/null +++ b/internal/services/cdn/cdn_frontdoor_custom_domain_data_source_test.go @@ -0,0 +1,44 @@ +package cdn_test + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" + "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check" +) + +type CdnFrontDoorCustomDomainDataSource struct{} + +func TestAccCdnFrontDoorCustomDomainDataSource_basic(t *testing.T) { + data := acceptance.BuildTestData(t, "data.azurerm_cdn_frontdoor_custom_domain", "test") + d := CdnFrontDoorCustomDomainDataSource{} + + data.DataSourceTest(t, []acceptance.TestStep{ + { + Config: d.basic(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).Key("dns_zone_id").Exists(), + check.That(data.ResourceName).Key("host_name").Exists(), + check.That(data.ResourceName).Key("cdn_frontdoor_profile_id").Exists(), + check.That(data.ResourceName).Key("tls.0.cdn_frontdoor_secret_id").IsEmpty(), + check.That(data.ResourceName).Key("tls.0.certificate_type").Exists(), + check.That(data.ResourceName).Key("tls.0.minimum_tls_version").Exists(), + check.That(data.ResourceName).Key("expiration_date").Exists(), + check.That(data.ResourceName).Key("validation_token").Exists(), + ), + }, + }) +} + +func (CdnFrontDoorCustomDomainDataSource) basic(data acceptance.TestData) string { + return fmt.Sprintf(` +%s + +data "azurerm_cdn_frontdoor_custom_domain" "test" { + name = azurerm_cdn_frontdoor_custom_domain.test.name + profile_name = azurerm_cdn_frontdoor_profile.test.name + resource_group_name = azurerm_cdn_frontdoor_profile.test.resource_group_name +} +`, CdnFrontDoorCustomDomainResource{}.complete(data)) +} diff --git a/internal/services/cdn/registration.go b/internal/services/cdn/registration.go index c7dba37cb33f..1f5523c65625 100644 --- a/internal/services/cdn/registration.go +++ b/internal/services/cdn/registration.go @@ -32,6 +32,7 @@ func (r Registration) SupportedDataSources() map[string]*pluginsdk.Resource { "azurerm_cdn_profile": dataSourceCdnProfile(), // FrontDoor + "azurerm_cdn_frontdoor_custom_domain": dataSourceCdnFrontDoorCustomDomain(), "azurerm_cdn_frontdoor_endpoint": dataSourceCdnFrontDoorEndpoint(), "azurerm_cdn_frontdoor_firewall_policy": dataSourceCdnFrontDoorFirewallPolicy(), "azurerm_cdn_frontdoor_origin_group": dataSourceCdnFrontDoorOriginGroup(), diff --git a/website/docs/d/cdn_frontdoor_custom_domain.html.markdown b/website/docs/d/cdn_frontdoor_custom_domain.html.markdown new file mode 100644 index 000000000000..1bf71a3d0356 --- /dev/null +++ b/website/docs/d/cdn_frontdoor_custom_domain.html.markdown @@ -0,0 +1,65 @@ +--- +subcategory: "CDN" +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_cdn_frontdoor_custom_domain" +description: |- + Gets information about an existing Front Door (standard/premium) Custom Domain. +--- + +# Data Source: azurerm_cdn_frontdoor_custom_domain + +Use this data source to access information about an existing Front Door (standard/premium) Custom Domain. + +## Example Usage + +```hcl +data "azurerm_cdn_frontdoor_custom_domain" "example" { + name = azurerm_cdn_frontdoor_custom_domain.example.name + profile_name = azurerm_cdn_frontdoor_profile.example.name + resource_group_name = azurerm_cdn_frontdoor_profile.example.resource_group_name +} +``` + +## Arguments Reference + +The following arguments are supported: + +* `name` - (Required) The name of the Front Door Custom Domain. + +* `profile_name` - (Required) The name of the Front Door Profile which the Front Door Custom Domain is bound to. + +* `resource_group_name` - (Required) The name of the Resource Group where the Front Door Custom Profile exists. + +## Attributes Reference + +In addition to the Arguments listed above - the following Attributes are exported: + +* `id` - The ID of the Front Door Custom Domain. + +* `cdn_frontdoor_profile_id` - The ID of the Front Door Profile which the Front Door Custom Domain is bound to. + +* `expiration_date` - The date time that the token expires. + +* `host_name` - The host name of the domain. + +* `tls` - A `tls` block as defined below. + +* `validation_token` - The challenge used for DNS TXT record or file based validation. + +--- + +A `tls` block exports the following: + +* `cdn_frontdoor_secret_id` - The Resource ID of the Front Door Secret. + +* `certificate_type` - The SSL certificate type. + +* `minimum_tls_version` - The TLS protocol version that will be used for Https connections. + +--- + +## Timeouts + +The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions: + +* `read` - (Defaults to 5 minutes) Used when retrieving the Front Door Custom Domain. From 54653c57414e22ada122f8617a88d0ca2f9be0f3 Mon Sep 17 00:00:00 2001 From: Jeffrey Cline <20408400+WodansSon@users.noreply.github.com> Date: Fri, 18 Nov 2022 00:45:50 -0700 Subject: [PATCH 2/6] Fix documentation description for RG name --- website/docs/d/cdn_frontdoor_custom_domain.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/d/cdn_frontdoor_custom_domain.html.markdown b/website/docs/d/cdn_frontdoor_custom_domain.html.markdown index 1bf71a3d0356..bc78b19da9eb 100644 --- a/website/docs/d/cdn_frontdoor_custom_domain.html.markdown +++ b/website/docs/d/cdn_frontdoor_custom_domain.html.markdown @@ -28,7 +28,7 @@ The following arguments are supported: * `profile_name` - (Required) The name of the Front Door Profile which the Front Door Custom Domain is bound to. -* `resource_group_name` - (Required) The name of the Resource Group where the Front Door Custom Profile exists. +* `resource_group_name` - (Required) The name of the Resource Group where the Front Door Profile exists. ## Attributes Reference From f570329682158151a161f1ae245395877b3f56db Mon Sep 17 00:00:00 2001 From: Wodans Son <20408400+WodansSon@users.noreply.github.com> Date: Fri, 18 Nov 2022 21:18:42 -0700 Subject: [PATCH 3/6] Update internal/services/cdn/cdn_frontdoor_custom_domain_data_source.go Co-authored-by: Tom Harvey --- .../services/cdn/cdn_frontdoor_custom_domain_data_source.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/internal/services/cdn/cdn_frontdoor_custom_domain_data_source.go b/internal/services/cdn/cdn_frontdoor_custom_domain_data_source.go index 6e22c4fdfd07..ba011634e8d5 100644 --- a/internal/services/cdn/cdn_frontdoor_custom_domain_data_source.go +++ b/internal/services/cdn/cdn_frontdoor_custom_domain_data_source.go @@ -96,8 +96,6 @@ func dataSourceCdnFrontDoorCustomDomainRead(d *pluginsdk.ResourceData, meta inte defer cancel() id := parse.NewFrontDoorCustomDomainID(subscriptionId, d.Get("resource_group_name").(string), d.Get("profile_name").(string), d.Get("name").(string)) - profile := parse.NewFrontDoorProfileID(subscriptionId, d.Get("resource_group_name").(string), d.Get("profile_name").(string)) - resp, err := client.Get(ctx, id.ResourceGroup, id.ProfileName, id.CustomDomainName) if err != nil { if utils.ResponseWasNotFound(resp.Response) { @@ -110,7 +108,7 @@ func dataSourceCdnFrontDoorCustomDomainRead(d *pluginsdk.ResourceData, meta inte d.Set("name", id.CustomDomainName) d.Set("resource_group_name", id.ResourceGroup) d.Set("profile_name", id.ProfileName) - d.Set("cdn_frontdoor_profile_id", profile.ID()) + d.Set("cdn_frontdoor_profile_id", parse.NewFrontDoorProfileID(id.SubscriptionId, id.ResourceGroupName, id.ProfileName).ID()) if props := resp.AFDDomainProperties; props != nil { d.Set("host_name", props.HostName) From 0ad8597a2e933b22a0150874dade11ba9d0c8836 Mon Sep 17 00:00:00 2001 From: Jeffrey Cline <20408400+WodansSon@users.noreply.github.com> Date: Fri, 18 Nov 2022 21:21:07 -0700 Subject: [PATCH 4/6] Resolve PR comments --- website/docs/d/cdn_frontdoor_custom_domain.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/website/docs/d/cdn_frontdoor_custom_domain.html.markdown b/website/docs/d/cdn_frontdoor_custom_domain.html.markdown index bc78b19da9eb..34afa64973df 100644 --- a/website/docs/d/cdn_frontdoor_custom_domain.html.markdown +++ b/website/docs/d/cdn_frontdoor_custom_domain.html.markdown @@ -3,12 +3,12 @@ subcategory: "CDN" layout: "azurerm" page_title: "Azure Resource Manager: azurerm_cdn_frontdoor_custom_domain" description: |- - Gets information about an existing Front Door (standard/premium) Custom Domain. + Gets information about an existing CDN Front Door (standard/premium) Custom Domain. --- # Data Source: azurerm_cdn_frontdoor_custom_domain -Use this data source to access information about an existing Front Door (standard/premium) Custom Domain. +Use this data source to access information about an existing CDN Front Door (standard/premium) Custom Domain. ## Example Usage From 6817c2fcf9de43070f1abf28cb7e6a2aa0b8c201 Mon Sep 17 00:00:00 2001 From: Jeffrey Cline <20408400+WodansSon@users.noreply.github.com> Date: Fri, 18 Nov 2022 21:22:39 -0700 Subject: [PATCH 5/6] Revert documentation change --- website/docs/d/cdn_frontdoor_custom_domain.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/website/docs/d/cdn_frontdoor_custom_domain.html.markdown b/website/docs/d/cdn_frontdoor_custom_domain.html.markdown index 34afa64973df..bc78b19da9eb 100644 --- a/website/docs/d/cdn_frontdoor_custom_domain.html.markdown +++ b/website/docs/d/cdn_frontdoor_custom_domain.html.markdown @@ -3,12 +3,12 @@ subcategory: "CDN" layout: "azurerm" page_title: "Azure Resource Manager: azurerm_cdn_frontdoor_custom_domain" description: |- - Gets information about an existing CDN Front Door (standard/premium) Custom Domain. + Gets information about an existing Front Door (standard/premium) Custom Domain. --- # Data Source: azurerm_cdn_frontdoor_custom_domain -Use this data source to access information about an existing CDN Front Door (standard/premium) Custom Domain. +Use this data source to access information about an existing Front Door (standard/premium) Custom Domain. ## Example Usage From aa90e7a0102f5436e4db8d6f8e7185277783a9d6 Mon Sep 17 00:00:00 2001 From: Jeffrey Cline <20408400+WodansSon@users.noreply.github.com> Date: Fri, 18 Nov 2022 21:33:57 -0700 Subject: [PATCH 6/6] Fix build errors --- .../services/cdn/cdn_frontdoor_custom_domain_data_source.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/services/cdn/cdn_frontdoor_custom_domain_data_source.go b/internal/services/cdn/cdn_frontdoor_custom_domain_data_source.go index ba011634e8d5..112d61bae60e 100644 --- a/internal/services/cdn/cdn_frontdoor_custom_domain_data_source.go +++ b/internal/services/cdn/cdn_frontdoor_custom_domain_data_source.go @@ -108,7 +108,7 @@ func dataSourceCdnFrontDoorCustomDomainRead(d *pluginsdk.ResourceData, meta inte d.Set("name", id.CustomDomainName) d.Set("resource_group_name", id.ResourceGroup) d.Set("profile_name", id.ProfileName) - d.Set("cdn_frontdoor_profile_id", parse.NewFrontDoorProfileID(id.SubscriptionId, id.ResourceGroupName, id.ProfileName).ID()) + d.Set("cdn_frontdoor_profile_id", parse.NewFrontDoorProfileID(id.SubscriptionId, id.ResourceGroup, id.ProfileName).ID()) if props := resp.AFDDomainProperties; props != nil { d.Set("host_name", props.HostName)