Skip to content

Commit

Permalink
Address latest PR comments...
Browse files Browse the repository at this point in the history
  • Loading branch information
WodansSon committed Dec 13, 2024
1 parent 2b75a3d commit 10953b0
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 94 deletions.
7 changes: 4 additions & 3 deletions internal/services/cdn/cdn_frontdoor_custom_domain_resource.go
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-sdk/resource-manager/cdn/2024-02-01/profiles"
dnsValidate "github.com/hashicorp/go-azure-sdk/resource-manager/dns/2018-05-01/zones"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand Down Expand Up @@ -139,12 +140,12 @@ func resourceCdnFrontDoorCustomDomainCreate(d *pluginsdk.ResourceData, meta inte
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()

profileId, err := parse.FrontDoorProfileID(d.Get("cdn_frontdoor_profile_id").(string))
profileId, err := profiles.ParseProfileID(d.Get("cdn_frontdoor_profile_id").(string))
if err != nil {
return err
}

id := parse.NewFrontDoorCustomDomainID(profileId.SubscriptionId, profileId.ResourceGroup, profileId.ProfileName, d.Get("name").(string))
id := parse.NewFrontDoorCustomDomainID(profileId.SubscriptionId, profileId.ResourceGroupName, profileId.ProfileName, d.Get("name").(string))

existing, err := client.Get(ctx, id.ResourceGroup, id.ProfileName, id.CustomDomainName)
if err != nil {
Expand Down Expand Up @@ -212,7 +213,7 @@ func resourceCdnFrontDoorCustomDomainRead(d *pluginsdk.ResourceData, meta interf
}

d.Set("name", id.CustomDomainName)
d.Set("cdn_frontdoor_profile_id", parse.NewFrontDoorProfileID(id.SubscriptionId, id.ResourceGroup, id.ProfileName).ID())
d.Set("cdn_frontdoor_profile_id", profiles.NewProfileID(id.SubscriptionId, id.ResourceGroup, id.ProfileName).ID())

if props := resp.AFDDomainProperties; props != nil {
d.Set("host_name", props.HostName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ func dataSourceCdnFrontDoorFirewallPolicyRead(d *pluginsdk.ResourceData, meta in
return fmt.Errorf("retrieving %s: %+v", id, err)
}

if model := result.Model; model != nil {
d.SetId(id.ID())
d.SetId(id.ID())

if model := result.Model; model != nil {
d.Set("name", id.FrontDoorWebApplicationFirewallPolicyName)
d.Set("resource_group_name", id.ResourceGroupName)
if sku := model.Sku; sku != nil {
Expand Down
84 changes: 34 additions & 50 deletions internal/services/cdn/cdn_frontdoor_firewall_policy_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
waf "github.com/hashicorp/go-azure-sdk/resource-manager/frontdoor/2024-02-01/webapplicationfirewallpolicies"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"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/tf/validation"
Expand All @@ -32,7 +31,7 @@ func resourceCdnFrontDoorFirewallPolicy() *pluginsdk.Resource {
Delete: resourceCdnFrontDoorFirewallPolicyDelete,

Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.FrontDoorFirewallPolicyID(id)
_, err := waf.ParseFrontDoorWebApplicationFirewallPolicyID(id)
return err
}),

Expand Down Expand Up @@ -503,8 +502,6 @@ func resourceCdnFrontDoorFirewallPolicyCreate(d *pluginsdk.ResourceData, meta in
return fmt.Errorf("the 'managed_rule' field is only supported with the 'Premium_AzureFrontDoor' sku, got %q", sku)
}

t := d.Get("tags").(map[string]interface{})

payload := waf.WebApplicationFirewallPolicy{
Location: pointer.To(location.Normalize("Global")),
Sku: &waf.Sku{
Expand All @@ -518,7 +515,7 @@ func resourceCdnFrontDoorFirewallPolicyCreate(d *pluginsdk.ResourceData, meta in
},
CustomRules: expandCdnFrontDoorFirewallCustomRules(customRules),
},
Tags: tags.Expand(t),
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
}

if managedRules != nil {
Expand Down Expand Up @@ -626,13 +623,12 @@ func resourceCdnFrontDoorFirewallPolicyUpdate(d *pluginsdk.ResourceData, meta in
}

if d.HasChange("tags") {
t := d.Get("tags").(map[string]interface{})
model.Tags = tags.Expand(t)
model.Tags = tags.Expand(d.Get("tags").(map[string]interface{}))
}

model.Properties = pointer.To(props)

err = client.PoliciesCreateOrUpdateThenPoll(ctx, pointer.From(id), pointer.From(model))
err = client.PoliciesCreateOrUpdateThenPoll(ctx, *id, *model)
if err != nil {
return fmt.Errorf("updating %s: %+v", *id, err)
}
Expand All @@ -650,77 +646,65 @@ func resourceCdnFrontDoorFirewallPolicyRead(d *pluginsdk.ResourceData, meta inte
return err
}

result, err := client.PoliciesGet(ctx, pointer.From(id))
result, err := client.PoliciesGet(ctx, *id)
if err != nil {
return fmt.Errorf("retrieving %s: %+v", *id, err)
}

model := result.Model

if model == nil {
return fmt.Errorf("retrieving %s: 'model' was nil", *id)
}

if model.Properties == nil {
return fmt.Errorf("retrieving %s: 'model.Properties' was nil", *id)
}

d.Set("name", id.FrontDoorWebApplicationFirewallPolicyName)
d.Set("resource_group_name", id.ResourceGroupName)

if sku := model.Sku; sku != nil {
d.Set("sku_name", string(pointer.From(sku.Name)))
}

if props := model.Properties; props != nil {
if policy := props.PolicySettings; policy != nil {
d.Set("enabled", pointer.From(policy.EnabledState) == waf.PolicyEnabledStateEnabled)
d.Set("mode", string(pointer.From(policy.Mode)))
d.Set("request_body_check_enabled", pointer.From(policy.RequestBodyCheck) == waf.PolicyRequestBodyCheckEnabled)
d.Set("redirect_url", policy.RedirectURL)
d.Set("custom_block_response_status_code", int(pointer.From(policy.CustomBlockResponseStatusCode)))
d.Set("custom_block_response_body", policy.CustomBlockResponseBody)
}

if err := d.Set("custom_rule", flattenCdnFrontDoorFirewallCustomRules(props.CustomRules)); err != nil {
return fmt.Errorf("flattening 'custom_rule': %+v", err)
if model := result.Model; model != nil {
if sku := model.Sku; sku != nil {
d.Set("sku_name", string(pointer.From(sku.Name)))
}

if err := d.Set("frontend_endpoint_ids", flattenFrontendEndpointLinkSlice(props.FrontendEndpointLinks)); err != nil {
return fmt.Errorf("flattening 'frontend_endpoint_ids': %+v", err)
if props := model.Properties; props != nil {
if err := d.Set("custom_rule", flattenCdnFrontDoorFirewallCustomRules(props.CustomRules)); err != nil {
return fmt.Errorf("flattening 'custom_rule': %+v", err)
}

if err := d.Set("frontend_endpoint_ids", flattenFrontendEndpointLinkSlice(props.FrontendEndpointLinks)); err != nil {
return fmt.Errorf("flattening 'frontend_endpoint_ids': %+v", err)
}

if err := d.Set("managed_rule", flattenCdnFrontDoorFirewallManagedRules(props.ManagedRules)); err != nil {
return fmt.Errorf("flattening 'managed_rule': %+v", err)
}

if policy := props.PolicySettings; policy != nil {
d.Set("enabled", pointer.From(policy.EnabledState) == waf.PolicyEnabledStateEnabled)
d.Set("mode", string(pointer.From(policy.Mode)))
d.Set("request_body_check_enabled", pointer.From(policy.RequestBodyCheck) == waf.PolicyRequestBodyCheckEnabled)
d.Set("redirect_url", policy.RedirectURL)
d.Set("custom_block_response_status_code", int(pointer.From(policy.CustomBlockResponseStatusCode)))
d.Set("custom_block_response_body", policy.CustomBlockResponseBody)
}
}

if err := d.Set("managed_rule", flattenCdnFrontDoorFirewallManagedRules(props.ManagedRules)); err != nil {
return fmt.Errorf("flattening 'managed_rule': %+v", err)
if err := tags.FlattenAndSet(d, model.Tags); err != nil {
return err
}
}

if err := tags.FlattenAndSet(d, model.Tags); err != nil {
return err
}

return nil
}

func resourceCdnFrontDoorFirewallPolicyDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Cdn.FrontDoorLegacyFirewallPoliciesClient
client := meta.(*clients.Client).Cdn.FrontDoorFirewallPoliciesClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.FrontDoorFirewallPolicyID(d.Id())
id, err := waf.ParseFrontDoorWebApplicationFirewallPolicyID(d.Id())
if err != nil {
return err
}

future, err := client.Delete(ctx, id.ResourceGroup, id.FrontDoorWebApplicationFirewallPolicyName)
err = client.PoliciesDeleteThenPoll(ctx, *id)
if err != nil {
return fmt.Errorf("deleting %s: %+v", *id, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for the deletion of %s: %+v", *id, err)
}

return nil
}

Expand Down
31 changes: 0 additions & 31 deletions internal/services/cdn/cdn_frontdoor_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strings"

"github.com/Azure/azure-sdk-for-go/services/cdn/mgmt/2021-06-01/cdn" // nolint: staticcheck
"github.com/hashicorp/go-azure-helpers/lang/pointer"
dnsValidate "github.com/hashicorp/go-azure-sdk/resource-manager/dns/2018-05-01/zones"
waf "github.com/hashicorp/go-azure-sdk/resource-manager/frontdoor/2024-02-01/webapplicationfirewallpolicies"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand Down Expand Up @@ -109,36 +108,6 @@ func flattenHttpsRedirectToBool(input cdn.HTTPSRedirect) bool {
return input == cdn.HTTPSRedirectEnabled
}

func expandFrontDoorTagsPointer(tagMap map[string]interface{}) *map[string]string {
t := make(map[string]string)

for k, v := range tagMap {
tagKey := k
tagValue := v
t[tagKey] = tagValue.(string)
}

return pointer.To(t)
}

func flattenFrontDoorTags(tagMap *map[string]string) map[string]interface{} {
if tagMap == nil {
return make(map[string]interface{}, 0)
}

t := make(map[string]interface{}, len(*tagMap))

for k, v := range *tagMap {
if v == "" {
continue
}

t[k] = v
}

return t
}

func flattenTransformSlice(input *[]waf.TransformType) []interface{} {
result := make([]interface{}, 0)
if input == nil || len(*input) == 0 {
Expand Down
6 changes: 3 additions & 3 deletions internal/services/cdn/cdn_frontdoor_origin_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,10 @@ func resourceCdnFrontDoorOriginUpdate(d *pluginsdk.ResourceData, meta interface{
profileResp, err := profileClient.Get(ctx, profileId)
if err != nil {
if response.WasNotFound(profileResp.HttpResponse) {
d.SetId("")
return nil
return fmt.Errorf("retrieving parent %s: not found", profileId)
}
return fmt.Errorf("retrieving %s: %+v", profileId, err)

return fmt.Errorf("retrieving parent %s: %+v", profileId, err)
}

profileModel := profileResp.Model
Expand Down
8 changes: 6 additions & 2 deletions internal/services/cdn/cdn_frontdoor_profile_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/cdn/2024-02-01/profiles"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/cdn/validate"
Expand Down Expand Up @@ -70,8 +71,9 @@ func dataSourceCdnFrontDoorProfileRead(d *pluginsdk.ResourceData, meta interface
return fmt.Errorf("retrieving %s: %+v", id, err)
}

d.SetId(id.ID())

if model := resp.Model; model != nil {
d.SetId(id.ID())
d.Set("name", id.ProfileName)
d.Set("resource_group_name", id.ResourceGroupName)

Expand All @@ -87,7 +89,9 @@ func dataSourceCdnFrontDoorProfileRead(d *pluginsdk.ResourceData, meta interface
d.Set("resource_guid", pointer.From(props.FrontDoorId))
}

d.Set("tags", flattenFrontDoorTags(model.Tags))
if err := tags.FlattenAndSet(d, model.Tags); err != nil {
return err
}
}

return nil
Expand Down
9 changes: 6 additions & 3 deletions internal/services/cdn/cdn_frontdoor_profile_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/cdn/2024-02-01/profiles"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand Down Expand Up @@ -103,7 +104,7 @@ func resourceCdnFrontDoorProfileCreate(d *pluginsdk.ResourceData, meta interface
Sku: profiles.Sku{
Name: pointer.To(profiles.SkuName(d.Get("sku_name").(string))),
},
Tags: expandFrontDoorTagsPointer(d.Get("tags").(map[string]interface{})),
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
}

err = client.CreateThenPoll(ctx, id, props)
Expand Down Expand Up @@ -150,7 +151,9 @@ func resourceCdnFrontDoorProfileRead(d *pluginsdk.ResourceData, meta interface{}
d.Set("resource_guid", pointer.From(props.FrontDoorId))
}

d.Set("tags", flattenFrontDoorTags(model.Tags))
if err := tags.FlattenAndSet(d, model.Tags); err != nil {
return err
}
}

return nil
Expand All @@ -167,7 +170,7 @@ func resourceCdnFrontDoorProfileUpdate(d *pluginsdk.ResourceData, meta interface
}

props := profiles.ProfileUpdateParameters{
Tags: expandFrontDoorTagsPointer(d.Get("tags").(map[string]interface{})),
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
Properties: &profiles.ProfilePropertiesUpdateParameters{},
}

Expand Down

0 comments on commit 10953b0

Please sign in to comment.