Skip to content
17 changes: 9 additions & 8 deletions internal/services/cdn/cdn_frontdoor_route_resource.go
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you replace the utils.String() etc with pointer.To()?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/Azure/azure-sdk-for-go/services/cdn/mgmt/2021-06-01/cdn" // nolint: staticcheck
"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/locks"
Expand Down Expand Up @@ -62,10 +63,10 @@ func resourceCdnFrontDoorRoute() *pluginsdk.Resource {

// NOTE: These are not sent to the API, they are only here so Terraform
// can provision/destroy the resources in the correct order.
// Made this field optional to address comments in Issue #29063
"cdn_frontdoor_origin_ids": {
Type: pluginsdk.TypeList,
Required: true,

Optional: true,
Elem: &pluginsdk.Schema{
Comment on lines +69 to 70
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've seen both ways in the code, but it seems most of the time there's no new line before Elem:

Suggested change
Optional: true,
Elem: &pluginsdk.Schema{
Optional: true,
Elem: &pluginsdk.Schema{

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Type: pluginsdk.TypeString,
ValidateFunc: validate.FrontDoorOriginID,
Expand Down Expand Up @@ -290,7 +291,7 @@ func resourceCdnFrontDoorRouteCreate(d *pluginsdk.ResourceData, meta interface{}
}

if originPath := d.Get("cdn_frontdoor_origin_path").(string); originPath != "" {
props.RouteProperties.OriginPath = utils.String(originPath)
props.RouteProperties.OriginPath = pointer.To(originPath)
}

future, err := client.Create(ctx, id.ResourceGroup, id.ProfileName, id.AfdEndpointName, id.RouteName, props)
Expand Down Expand Up @@ -346,7 +347,7 @@ func resourceCdnFrontDoorRouteRead(d *pluginsdk.ResourceData, meta interface{})
// NOTE: These are not sent to the API, they are only here so Terraform
// can provision/destroy the resources in the correct order.
if originIds := d.Get("cdn_frontdoor_origin_ids").([]interface{}); len(originIds) > 0 {
d.Set("cdn_frontdoor_origin_ids", utils.ExpandStringSlice(originIds))
d.Set("cdn_frontdoor_origin_ids", originIds)
}

d.Set("name", id.RouteName)
Expand Down Expand Up @@ -495,7 +496,7 @@ func resourceCdnFrontDoorRouteUpdate(d *pluginsdk.ResourceData, meta interface{}

originPath := d.Get("cdn_frontdoor_origin_path").(string)
if originPath != "" {
updateProps.OriginPath = utils.String(originPath)
updateProps.OriginPath = pointer.To(originPath)
}
}

Expand Down Expand Up @@ -532,7 +533,7 @@ func resourceCdnFrontDoorRouteUpdate(d *pluginsdk.ResourceData, meta interface{}
// NOTE: These are not sent to the API, they are only here so Terraform
// can provision/destroy the resources in the correct order.
if originIds := d.Get("cdn_frontdoor_origin_ids").([]interface{}); len(originIds) > 0 {
d.Set("cdn_frontdoor_origin_ids", utils.ExpandStringSlice(originIds))
d.Set("cdn_frontdoor_origin_ids", originIds)
}

return resourceCdnFrontDoorRouteRead(d, meta)
Expand Down Expand Up @@ -582,7 +583,7 @@ func expandRuleSetReferenceArray(input []interface{}) *[]cdn.ResourceReference {

for _, item := range input {
results = append(results, cdn.ResourceReference{
ID: utils.String(item.(string)),
ID: pointer.To(item.(string)),
})
}

Expand All @@ -604,7 +605,7 @@ func expandCdnFrontdoorRouteCacheConfiguration(input []interface{}) *cdn.AfdRout

cacheConfiguration := &cdn.AfdRouteCacheConfiguration{
CompressionSettings: &cdn.CompressionSettings{
IsCompressionEnabled: utils.Bool(compressionEnabled),
IsCompressionEnabled: pointer.To(compressionEnabled),
},
QueryParameters: expandStringSliceToCsvFormat(v["query_strings"].([]interface{})),
QueryStringCachingBehavior: queryStringCachingBehaviorValue,
Expand Down
63 changes: 63 additions & 0 deletions internal/services/cdn/cdn_frontdoor_route_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,34 @@ func TestAccCdnFrontDoorRoute_basic(t *testing.T) {
})
}

func TestAccCdnFrontDoorRoute_basicDependsOn(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_cdn_frontdoor_route", "test")
r := CdnFrontDoorRouteResource{}
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basicDependsOn(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep("cdn_frontdoor_origin_group_id"),
})
}

func TestAccCdnFrontDoorRoute_basicDependsOnAndField(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_cdn_frontdoor_route", "test")
r := CdnFrontDoorRouteResource{}
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basicDependsOnAndField(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep("cdn_frontdoor_origin_group_id", "cdn_frontdoor_origin_ids"),
})
}

func TestAccCdnFrontDoorRoute_requiresImport(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_cdn_frontdoor_route", "test")
r := CdnFrontDoorRouteResource{}
Expand Down Expand Up @@ -197,6 +225,41 @@ resource "azurerm_cdn_frontdoor_route" "test" {
`, template, data.RandomInteger)
}

func (r CdnFrontDoorRouteResource) basicDependsOn(data acceptance.TestData) string {
template := r.template(data)
return fmt.Sprintf(`
%s

resource "azurerm_cdn_frontdoor_route" "test" {
name = "accTestRoute-%d"
cdn_frontdoor_endpoint_id = azurerm_cdn_frontdoor_endpoint.test.id
cdn_frontdoor_origin_group_id = azurerm_cdn_frontdoor_origin_group.test.id
patterns_to_match = ["/*"]
supported_protocols = ["Http", "Https"]

depends_on = [azurerm_cdn_frontdoor_origin.test]
}
`, template, data.RandomInteger)
}

func (r CdnFrontDoorRouteResource) basicDependsOnAndField(data acceptance.TestData) string {
template := r.template(data)
return fmt.Sprintf(`
%s

resource "azurerm_cdn_frontdoor_route" "test" {
name = "accTestRoute-%d"
cdn_frontdoor_endpoint_id = azurerm_cdn_frontdoor_endpoint.test.id
cdn_frontdoor_origin_group_id = azurerm_cdn_frontdoor_origin_group.test.id
cdn_frontdoor_origin_ids = [azurerm_cdn_frontdoor_origin.test.id]
patterns_to_match = ["/*"]
supported_protocols = ["Http", "Https"]

depends_on = [azurerm_cdn_frontdoor_origin.test]
}
`, template, data.RandomInteger)
}

func (r CdnFrontDoorRouteResource) requiresImport(data acceptance.TestData) string {
config := r.basic(data)
return fmt.Sprintf(`
Expand Down
14 changes: 10 additions & 4 deletions website/docs/r/cdn_frontdoor_route.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ description: |-

Manages a Front Door (standard/premium) Route.

!> **Note:** The `azurerm_cdn_frontdoor_route` resource must **explicitly** reference its associated `azurerm_cdn_frontdoor_origin` resource(s). This can be achieved either by using a `depends_on` meta-argument that points to the `azurerm_cdn_frontdoor_origin` resource(s), or by specifying the `azurerm_cdn_frontdoor_origin` IDs via the `cdn_frontdoor_origin_ids` field.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd like to save the "!>" notes for cases where costly or irreversible damage could occur. The "~>" is a good choice for advising users how to avoid minor errors.

Suggested change
!> **Note:** The `azurerm_cdn_frontdoor_route` resource must **explicitly** reference its associated `azurerm_cdn_frontdoor_origin` resource(s). This can be achieved either by using a `depends_on` meta-argument that points to the `azurerm_cdn_frontdoor_origin` resource(s), or by specifying the `azurerm_cdn_frontdoor_origin` IDs via the `cdn_frontdoor_origin_ids` field.
~> **Note:** The `azurerm_cdn_frontdoor_route` resource must **explicitly** reference its associated `azurerm_cdn_frontdoor_origin` resource(s). This can be achieved either by using a `depends_on` meta-argument that points to the `azurerm_cdn_frontdoor_origin` resource(s), or by specifying the `azurerm_cdn_frontdoor_origin` IDs via the `cdn_frontdoor_origin_ids` field.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.


## Example Usage

```hcl
Expand Down Expand Up @@ -134,16 +136,20 @@ The following arguments are supported:

* `cdn_frontdoor_origin_group_id` - (Required) The resource ID of the Front Door Origin Group where this Front Door Route should be created.

* `cdn_frontdoor_origin_ids` - (Required) One or more Front Door Origin resource IDs that this Front Door Route will link to.

* `forwarding_protocol` - (Optional) The Protocol that will be use when forwarding traffic to backends. Possible values are `HttpOnly`, `HttpsOnly` or `MatchRequest`. Defaults to `MatchRequest`.

* `patterns_to_match` - (Required) The route patterns of the rule.

* `supported_protocols` - (Required) One or more Protocols supported by this Front Door Route. Possible values are `Http` or `Https`.

~> **Note:** If `https_redirect_enabled` is set to `true` the `supported_protocols` field must contain both `Http` and `Https` values.

* `cdn_frontdoor_origin_ids` - (Optional) One or more Front Door Origin resource IDs for this Front Door Route.

~> **Note:** The `cdn_frontdoor_origin_ids` field is not sent to the Azure API, it exists solely to ensure Terraform can manage the correct `provisioning` and `destruction` order of related resources. When importing an existing `azurerm_cdn_frontdoor_route` resource, you will need to manually add the `cdn_frontdoor_origin_ids` field to your configuration after the resource has been successfully imported.

~> **Note:** If the `cdn_frontdoor_origin_ids` field is not defined in the configuration, you **must** use a `depends_on` meta-argument that references the corresponding `azurerm_cdn_frontdoor_origin` resource(s) for the route. When importing an existing `azurerm_cdn_frontdoor_route` resource from Azure, you will need to manually add the `depends_on` meta-argument to your configuration after the resource has been successfully imported.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could these two notes be condensed and combined?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.


* `forwarding_protocol` - (Optional) The Protocol that will be use when forwarding traffic to backends. Possible values are `HttpOnly`, `HttpsOnly` or `MatchRequest`. Defaults to `MatchRequest`.

* `cache` - (Optional) A `cache` block as defined below.

~> **Note:** To disable caching, do not provide the `cache` block in the configuration file.
Expand Down
Loading