Skip to content

Commit

Permalink
CDN endpoint: Test for Premium_Verizon, optional values fix (#9902)
Browse files Browse the repository at this point in the history
Added test for "Premium_Verizon" SKU for azurerm_cdn_profile
Assigning the parameters EndpointProperties.ContentTypesToCompress, EndpointProperties.GeoFilters only when values are set for content_types_to_compress, geo_filter.
Assigning the parameter EndpointProperties.DeliveryPolicy only when SKU set to Microsoft_Standard.
Fixes #9895
  • Loading branch information
AliAllomani authored Dec 27, 2020
1 parent e0b546f commit b996893
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 12 deletions.
40 changes: 28 additions & 12 deletions azurerm/internal/services/cdn/cdn_endpoint_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,11 @@ func resourceCdnEndpointCreate(d *schema.ResourceData, meta interface{}) error {
originPath := d.Get("origin_path").(string)
probePath := d.Get("probe_path").(string)
optimizationType := d.Get("optimization_type").(string)
contentTypes := expandArmCdnEndpointContentTypesToCompress(d)
geoFilters := expandCdnEndpointGeoFilters(d)
t := d.Get("tags").(map[string]interface{})

endpoint := cdn.Endpoint{
Location: &location,
EndpointProperties: &cdn.EndpointProperties{
ContentTypesToCompress: &contentTypes,
GeoFilters: geoFilters,
IsHTTPAllowed: &httpAllowed,
IsHTTPSAllowed: &httpsAllowed,
QueryStringCachingBehavior: cdn.QueryStringCachingBehavior(cachingBehaviour),
Expand All @@ -262,6 +258,16 @@ func resourceCdnEndpointCreate(d *schema.ResourceData, meta interface{}) error {
Tags: tags.Expand(t),
}

if _, ok := d.GetOk("content_types_to_compress"); ok {
contentTypes := expandArmCdnEndpointContentTypesToCompress(d)
endpoint.EndpointProperties.ContentTypesToCompress = &contentTypes
}

if _, ok := d.GetOk("geo_filter"); ok {
geoFilters := expandCdnEndpointGeoFilters(d)
endpoint.EndpointProperties.GeoFilters = geoFilters
}

if v, ok := d.GetOk("is_compression_enabled"); ok {
endpoint.EndpointProperties.IsCompressionEnabled = utils.Bool(v.(bool))
}
Expand Down Expand Up @@ -295,10 +301,12 @@ func resourceCdnEndpointCreate(d *schema.ResourceData, meta interface{}) error {
}

if profile.Sku.Name != cdn.StandardMicrosoft && len(*deliveryPolicy.Rules) > 0 {
return fmt.Errorf("`global_delivery_policy` and `delivery_rule` are only allowed when `Standard_Microsoft` sku is used. Profile sku: %s", profile.Sku.Name)
return fmt.Errorf("`global_delivery_rule` and `delivery_rule` are only allowed when `Standard_Microsoft` sku is used. Profile sku: %s", profile.Sku.Name)
}

endpoint.EndpointProperties.DeliveryPolicy = deliveryPolicy
if profile.Sku.Name == cdn.StandardMicrosoft {
endpoint.EndpointProperties.DeliveryPolicy = deliveryPolicy
}
}

future, err := endpointsClient.Create(ctx, resourceGroup, profileName, name, endpoint)
Expand Down Expand Up @@ -342,14 +350,10 @@ func resourceCdnEndpointUpdate(d *schema.ResourceData, meta interface{}) error {
originPath := d.Get("origin_path").(string)
probePath := d.Get("probe_path").(string)
optimizationType := d.Get("optimization_type").(string)
contentTypes := expandArmCdnEndpointContentTypesToCompress(d)
geoFilters := expandCdnEndpointGeoFilters(d)
t := d.Get("tags").(map[string]interface{})

endpoint := cdn.EndpointUpdateParameters{
EndpointPropertiesUpdateParameters: &cdn.EndpointPropertiesUpdateParameters{
ContentTypesToCompress: &contentTypes,
GeoFilters: geoFilters,
IsHTTPAllowed: utils.Bool(httpAllowed),
IsHTTPSAllowed: utils.Bool(httpsAllowed),
QueryStringCachingBehavior: cdn.QueryStringCachingBehavior(cachingBehaviour),
Expand All @@ -358,6 +362,16 @@ func resourceCdnEndpointUpdate(d *schema.ResourceData, meta interface{}) error {
Tags: tags.Expand(t),
}

if _, ok := d.GetOk("content_types_to_compress"); ok {
contentTypes := expandArmCdnEndpointContentTypesToCompress(d)
endpoint.EndpointPropertiesUpdateParameters.ContentTypesToCompress = &contentTypes
}

if _, ok := d.GetOk("geo_filter"); ok {
geoFilters := expandCdnEndpointGeoFilters(d)
endpoint.EndpointPropertiesUpdateParameters.GeoFilters = geoFilters
}

if v, ok := d.GetOk("is_compression_enabled"); ok {
endpoint.EndpointPropertiesUpdateParameters.IsCompressionEnabled = utils.Bool(v.(bool))
}
Expand Down Expand Up @@ -389,10 +403,12 @@ func resourceCdnEndpointUpdate(d *schema.ResourceData, meta interface{}) error {
}

if profile.Sku.Name != cdn.StandardMicrosoft && len(*deliveryPolicy.Rules) > 0 {
return fmt.Errorf("`global_delivery_policy` and `delivery_rule` are only allowed when `Standard_Microsoft` sku is used. Profile sku: %s", profile.Sku.Name)
return fmt.Errorf("`global_delivery_rule` and `delivery_rule` are only allowed when `Standard_Microsoft` sku is used. Profile sku: %s", profile.Sku.Name)
}

endpoint.EndpointPropertiesUpdateParameters.DeliveryPolicy = deliveryPolicy
if profile.Sku.Name == cdn.StandardMicrosoft {
endpoint.EndpointPropertiesUpdateParameters.DeliveryPolicy = deliveryPolicy
}
}

future, err := endpointsClient.Update(ctx, id.ResourceGroup, id.ProfileName, id.Name, endpoint)
Expand Down
52 changes: 52 additions & 0 deletions azurerm/internal/services/cdn/cdn_endpoint_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,21 @@ func TestAccCdnEndpoint_dnsAlias(t *testing.T) {
})
}

func TestAccCdnEndpoint_PremiumVerizon(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_cdn_endpoint", "test")
r := CdnEndpointResource{}

data.ResourceTest(t, r, []resource.TestStep{
{
Config: r.PremiumVerizon(data),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func (r CdnEndpointResource) Exists(ctx context.Context, client *clients.Client, state *terraform.InstanceState) (*bool, error) {
id, err := parse.EndpointID(state.ID)
if err != nil {
Expand Down Expand Up @@ -1077,3 +1092,40 @@ resource "azurerm_dns_a_record" "test" {
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.RandomInteger)
}

func (r CdnEndpointResource) PremiumVerizon(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}
resource "azurerm_cdn_profile" "test" {
name = "acctestcdnprof%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
sku = "Premium_Verizon"
}
resource "azurerm_cdn_endpoint" "test" {
name = "acctestcdnend%d"
profile_name = azurerm_cdn_profile.test.name
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
is_http_allowed = false
is_https_allowed = true
querystring_caching_behaviour = "NotSet"
origin {
name = "acceptanceTestCdnOrigin1"
host_name = "www.contoso.com"
https_port = 443
http_port = 80
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger)
}

0 comments on commit b996893

Please sign in to comment.