Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dependencies:] azurerm_cdn_frontdoor_rule, azurerm_cdn_frontdoor_ruleset and cdn_frontdoor_rule_actions - upgrade resources to 2024-02-01 API to enable support for the JS Challenge field #28308

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
236 changes: 117 additions & 119 deletions internal/services/cdn/cdn_frontdoor_rule_resource.go

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions internal/services/cdn/cdn_frontdoor_rule_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
"regexp"
"testing"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/cdn/2024-02-01/rules"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"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/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)
Expand Down Expand Up @@ -436,15 +437,15 @@ func TestAccCdnFrontDoorRule_urlFilenameConditionOperatorError(t *testing.T) {
}

func (r CdnFrontDoorRuleResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.FrontDoorRuleID(state.ID)
id, err := rules.ParseRuleID(state.ID)
if err != nil {
return nil, err
}

client := clients.Cdn.FrontDoorRulesClient
resp, err := client.Get(ctx, id.ResourceGroup, id.ProfileName, id.RuleSetName, id.RuleName)
result, err := client.Get(ctx, *id)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
if response.WasNotFound(result.HttpResponse) {
return utils.Bool(false), nil
}
return nil, fmt.Errorf("retrieving %s: %+v", id, err)
Expand Down
4 changes: 3 additions & 1 deletion internal/services/cdn/cdn_frontdoor_rule_set_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/go-azure-sdk/resource-manager/cdn/2024-02-01/rulesets"
"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"
Expand Down Expand Up @@ -61,7 +62,8 @@ func resourceCdnFrontDoorRuleSetCreate(d *pluginsdk.ResourceData, meta interface
return err
}

id := parse.NewFrontDoorRuleSetID(profile.SubscriptionId, profile.ResourceGroup, profile.ProfileName, d.Get("name").(string))
// func NewRuleSetID(subscriptionId string, resourceGroupName string, profileName string, ruleSetName string)
id := rulesets.NewRuleSetID(profile.SubscriptionId, profile.ResourceGroup, profile.ProfileName, d.Get("name").(string))
if d.IsNewResource() {
existing, err := client.Get(ctx, id.ResourceGroup, id.ProfileName, id.RuleSetName)
if err != nil {
Expand Down
24 changes: 16 additions & 8 deletions internal/services/cdn/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
cdnFrontDoorSdk "github.com/Azure/azure-sdk-for-go/services/cdn/mgmt/2021-06-01/cdn" // nolint: staticcheck
"github.com/Azure/azure-sdk-for-go/services/frontdoor/mgmt/2020-11-01/frontdoor" // nolint: staticcheck
"github.com/hashicorp/go-azure-sdk/resource-manager/cdn/2024-02-01/profiles"
"github.com/hashicorp/go-azure-sdk/resource-manager/cdn/2024-02-01/rules"
"github.com/hashicorp/go-azure-sdk/resource-manager/cdn/2024-02-01/rulesets"
"github.com/hashicorp/go-azure-sdk/resource-manager/cdn/2024-02-01/securitypolicies"
waf "github.com/hashicorp/go-azure-sdk/resource-manager/frontdoor/2024-02-01/webapplicationfirewallpolicies"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
Expand All @@ -22,10 +24,10 @@ type Client struct {
FrontDoorCustomDomainsClient *cdnFrontDoorSdk.AFDCustomDomainsClient
FrontDoorSecurityPoliciesClient *securitypolicies.SecurityPoliciesClient
FrontDoorRoutesClient *cdnFrontDoorSdk.RoutesClient
FrontDoorRulesClient *cdnFrontDoorSdk.RulesClient
FrontDoorRulesClient *rules.RulesClient
FrontDoorProfilesClient *profiles.ProfilesClient
FrontDoorSecretsClient *cdnFrontDoorSdk.SecretsClient
FrontDoorRuleSetsClient *cdnFrontDoorSdk.RuleSetsClient
FrontDoorRuleSetsClient *rulesets.RuleSetsClient
FrontDoorLegacyFirewallPoliciesClient *frontdoor.PoliciesClient
FrontDoorFirewallPoliciesClient *waf.WebApplicationFirewallPoliciesClient
CustomDomainsClient *cdnSdk.CustomDomainsClient
Expand Down Expand Up @@ -61,8 +63,11 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
frontDoorRoutesClient := cdnFrontDoorSdk.NewRoutesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&frontDoorRoutesClient.Client, o.ResourceManagerAuthorizer)

frontDoorRulesClient := cdnFrontDoorSdk.NewRulesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&frontDoorRulesClient.Client, o.ResourceManagerAuthorizer)
frontDoorRulesClient, err := rules.NewRulesClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building RulesClient: %+v", err)
}
o.Configure(frontDoorRulesClient.Client, o.Authorizers.ResourceManager)

frontDoorProfilesClient, err := profiles.NewProfilesClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
Expand All @@ -73,8 +78,11 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
frontDoorPolicySecretsClient := cdnFrontDoorSdk.NewSecretsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&frontDoorPolicySecretsClient.Client, o.ResourceManagerAuthorizer)

frontDoorRuleSetsClient := cdnFrontDoorSdk.NewRuleSetsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&frontDoorRuleSetsClient.Client, o.ResourceManagerAuthorizer)
frontDoorRuleSetsClient, err := rulesets.NewRuleSetsClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building RuleSet: %+v", err)
}
o.Configure(frontDoorRulesClient.Client, o.Authorizers.ResourceManager)

customDomainsClient := cdnSdk.NewCustomDomainsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&customDomainsClient.Client, o.ResourceManagerAuthorizer)
Expand All @@ -92,10 +100,10 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
FrontDoorCustomDomainsClient: &frontDoorCustomDomainsClient,
FrontDoorSecurityPoliciesClient: frontDoorSecurityPoliciesClient,
FrontDoorRoutesClient: &frontDoorRoutesClient,
FrontDoorRulesClient: &frontDoorRulesClient,
FrontDoorRulesClient: frontDoorRulesClient,
FrontDoorProfilesClient: frontDoorProfilesClient,
FrontDoorSecretsClient: &frontDoorPolicySecretsClient,
FrontDoorRuleSetsClient: &frontDoorRuleSetsClient,
FrontDoorRuleSetsClient: frontDoorRuleSetsClient,
FrontDoorLegacyFirewallPoliciesClient: &frontDoorLegacyFirewallPoliciesClient,
FrontDoorFirewallPoliciesClient: &frontDoorFirewallPoliciesClient,
CustomDomainsClient: &customDomainsClient,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package frontdoordeliveryruleactiondiscriminator

import (
"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-sdk/resource-manager/cdn/2024-02-01/rules"
)

func AsDeliveryRuleCacheExpirationAction(input rules.DeliveryRuleAction) (*rules.DeliveryRuleCacheExpirationAction, bool) {
if input.DeliveryRuleAction().Name != rules.DeliveryRuleActionNameRouteConfigurationOverride {
return nil, false
}

cacheExpiration := rules.DeliveryRuleCacheExpirationAction{}
return pointer.To(cacheExpiration), true
}

func AsDeliveryRuleRouteConfigurationOverrideAction(input rules.DeliveryRuleAction) (*rules.DeliveryRuleRouteConfigurationOverrideAction, bool) {
if input.DeliveryRuleAction().Name != rules.DeliveryRuleActionNameRouteConfigurationOverride {
return nil, false
}

routeConfigurationOverride := rules.DeliveryRuleRouteConfigurationOverrideAction{}
return pointer.To(routeConfigurationOverride), true
}

func AsDeliveryRuleResponseHeaderAction(input rules.DeliveryRuleAction) (*rules.DeliveryRuleResponseHeaderAction, bool) {
if input.DeliveryRuleAction().Name != rules.DeliveryRuleActionNameModifyResponseHeader {
return nil, false
}

responseHeader := rules.DeliveryRuleResponseHeaderAction{}
return pointer.To(responseHeader), true
}

func AsDeliveryRuleRequestHeaderAction(input rules.DeliveryRuleAction) (*rules.DeliveryRuleRequestHeaderAction, bool) {
if input.DeliveryRuleAction().Name != rules.DeliveryRuleActionNameModifyRequestHeader {
return nil, false
}

requestHeader := rules.DeliveryRuleRequestHeaderAction{}
return pointer.To(requestHeader), true
}

func AsDeliveryRuleUrlRewriteAction(input rules.DeliveryRuleAction) (*rules.URLRewriteAction, bool) {
if input.DeliveryRuleAction().Name != rules.DeliveryRuleActionNameURLRewrite {
return nil, false
}

urlRewrite := rules.URLRewriteAction{}
return pointer.To(urlRewrite), true
}

func AsDeliveryRuleUrlRedirectAction(input rules.DeliveryRuleAction) (*rules.URLRedirectAction, bool) {
if input.DeliveryRuleAction().Name != rules.DeliveryRuleActionNameURLRedirect {
return nil, false
}

urlRedirect := rules.URLRedirectAction{}
return pointer.To(urlRedirect), true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package frontdoordeliveryruleconditiondiscriminator

import (
"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-sdk/resource-manager/cdn/2024-02-01/rules"
)

type FrontDoorDeliveryRuleCondition interface {
Evaluate(input string, Parameters map[string]interface{}) bool
}

func AsDeliveryRuleCookiesCondition(input rules.DeliveryRuleCondition) (*rules.CookiesMatchConditionParameters, bool) {
if input.DeliveryRuleCondition().Name != rules.MatchVariableCookies {
return nil, false
}

cookieParameter := rules.CookiesMatchConditionParameters{}
return pointer.To(cookieParameter), true
}

func AsDeliveryRuleIsDeviceCondition(input rules.DeliveryRuleCondition) (*rules.IsDeviceMatchConditionParameters, bool) {
if input.DeliveryRuleCondition().Name != rules.MatchVariableIsDevice {
return nil, false
}

deviceParameter := rules.IsDeviceMatchConditionParameters{}
return pointer.To(deviceParameter), true
}

func AsDeliveryRuleHTTPVersionCondition(input rules.DeliveryRuleCondition) (*rules.HTTPVersionMatchConditionParameters, bool) {
if input.DeliveryRuleCondition().Name != rules.MatchVariableHTTPVersion {
return nil, false
}

deviceParameter := rules.HTTPVersionMatchConditionParameters{}
return pointer.To(deviceParameter), true
}

func AsDeliveryRulePostArgsCondition(input rules.DeliveryRuleCondition) (*rules.PostArgsMatchConditionParameters, bool) {
if input.DeliveryRuleCondition().Name != rules.MatchVariablePostArgs {
return nil, false
}

postArgParameter := rules.PostArgsMatchConditionParameters{}
return pointer.To(postArgParameter), true
}

func AsDeliveryRuleQueryStringCondition(input rules.DeliveryRuleCondition) (*rules.QueryStringMatchConditionParameters, bool) {
if input.DeliveryRuleCondition().Name != rules.MatchVariableQueryString {
return nil, false
}

postArgParameter := rules.QueryStringMatchConditionParameters{}
return pointer.To(postArgParameter), true
}

func AsDeliveryRuleRemoteAddressCondition(input rules.DeliveryRuleCondition) (*rules.RemoteAddressMatchConditionParameters, bool) {
if input.DeliveryRuleCondition().Name != rules.MatchVariableRemoteAddress {
return nil, false
}

remoteAddressParameter := rules.RemoteAddressMatchConditionParameters{}
return pointer.To(remoteAddressParameter), true
}

func AsDeliveryRuleRequestBodyCondition(input rules.DeliveryRuleCondition) (*rules.RequestBodyMatchConditionParameters, bool) {
if input.DeliveryRuleCondition().Name != rules.MatchVariableRequestBody {
return nil, false
}

requestBodyParameter := rules.RequestBodyMatchConditionParameters{}
return pointer.To(requestBodyParameter), true
}

func AsDeliveryRuleRequestHeaderCondition(input rules.DeliveryRuleCondition) (*rules.RequestHeaderMatchConditionParameters, bool) {
if input.DeliveryRuleCondition().Name != rules.MatchVariableRequestHeader {
return nil, false
}

requestHeaderParameter := rules.RequestHeaderMatchConditionParameters{}
return pointer.To(requestHeaderParameter), true
}

func AsDeliveryRuleRequestMethodCondition(input rules.DeliveryRuleCondition) (*rules.RequestMethodMatchConditionParameters, bool) {
if input.DeliveryRuleCondition().Name != rules.MatchVariableRequestMethod {
return nil, false
}

requestMethodParameter := rules.RequestMethodMatchConditionParameters{}
return pointer.To(requestMethodParameter), true
}

func AsDeliveryRuleRequestSchemeCondition(input rules.DeliveryRuleCondition) (*rules.RequestSchemeMatchConditionParameters, bool) {
if input.DeliveryRuleCondition().Name != rules.MatchVariableRequestScheme {
return nil, false
}

requestSchemeParameter := rules.RequestSchemeMatchConditionParameters{}
return pointer.To(requestSchemeParameter), true
}

func AsDeliveryRuleRequestUriCondition(input rules.DeliveryRuleCondition) (*rules.RequestUriMatchConditionParameters, bool) {
if input.DeliveryRuleCondition().Name != rules.MatchVariableRequestUri {
return nil, false
}

requestUriParameter := rules.RequestUriMatchConditionParameters{}
return pointer.To(requestUriParameter), true
}

func AsDeliveryRuleURLFileExtensionCondition(input rules.DeliveryRuleCondition) (*rules.URLFileExtensionMatchConditionParameters, bool) {
if input.DeliveryRuleCondition().Name != rules.MatchVariableURLFileExtension {
return nil, false
}

urlFileExtensionParameter := rules.URLFileExtensionMatchConditionParameters{}
return pointer.To(urlFileExtensionParameter), true
}

func AsDeliveryRuleURLFileNameCondition(input rules.DeliveryRuleCondition) (*rules.URLFileNameMatchConditionParameters, bool) {
if input.DeliveryRuleCondition().Name != rules.MatchVariableURLFileName {
return nil, false
}

urlFileNameParameter := rules.URLFileNameMatchConditionParameters{}
return pointer.To(urlFileNameParameter), true
}

func AsDeliveryRuleURLPathCondition(input rules.DeliveryRuleCondition) (*rules.URLPathMatchConditionParameters, bool) {
if input.DeliveryRuleCondition().Name != rules.MatchVariableURLPath {
return nil, false
}

urlPathParameter := rules.URLPathMatchConditionParameters{}
return pointer.To(urlPathParameter), true
}

func AsDeliveryRuleSslProtocolCondition(input rules.DeliveryRuleCondition) (*rules.SslProtocolMatchConditionParameters, bool) {
if input.DeliveryRuleCondition().Name != rules.MatchVariableSslProtocol {
return nil, false
}

sslProtocolParameter := rules.SslProtocolMatchConditionParameters{}
return pointer.To(sslProtocolParameter), true
}

func AsDeliveryRuleSocketAddrCondition(input rules.DeliveryRuleCondition) (*rules.SocketAddrMatchConditionParameters, bool) {
if input.DeliveryRuleCondition().Name != rules.MatchVariableSocketAddr {
return nil, false
}

socketAddrParameter := rules.SocketAddrMatchConditionParameters{}
return pointer.To(socketAddrParameter), true
}

func AsDeliveryRuleClientPortCondition(input rules.DeliveryRuleCondition) (*rules.ClientPortMatchConditionParameters, bool) {
// The input here has the data...
if input.DeliveryRuleCondition().Name != rules.MatchVariableClientPort {
return nil, false
}

// need to pull the values out here...
clientPortParameter := rules.ClientPortMatchConditionParameters{}
return pointer.To(clientPortParameter), true
}

func AsDeliveryRuleServerPortCondition(input rules.DeliveryRuleCondition) (*rules.ServerPortMatchConditionParameters, bool) {
if input.DeliveryRuleCondition().Name != rules.MatchVariableServerPort {
return nil, false
}

serverPortParameter := rules.ServerPortMatchConditionParameters{}
return pointer.To(serverPortParameter), true
}

func AsDeliveryRuleHostNameCondition(input rules.DeliveryRuleCondition) (*rules.HostNameMatchConditionParameters, bool) {
if input.DeliveryRuleCondition().Name != rules.MatchVariableHostName {
return nil, false
}

hostNametParameter := rules.HostNameMatchConditionParameters{}
return pointer.To(hostNametParameter), true
}
Loading
Loading