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

CDN endpoint: Test for Premium_Verizon, optional values fix #9902

Merged
merged 5 commits into from
Dec 27, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
68 changes: 46 additions & 22 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 @@ -294,11 +300,13 @@ func resourceCdnEndpointCreate(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Error expanding `global_delivery_rule` or `delivery_rule`: %s", err)
}

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)
}
if deliveryPolicy != nil {
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)
}

endpoint.EndpointProperties.DeliveryPolicy = deliveryPolicy
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 @@ -388,11 +402,13 @@ func resourceCdnEndpointUpdate(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Error expanding `global_delivery_rule` or `delivery_rule`: %s", err)
}

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)
}
if deliveryPolicy != nil {
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)
}

endpoint.EndpointPropertiesUpdateParameters.DeliveryPolicy = deliveryPolicy
endpoint.EndpointPropertiesUpdateParameters.DeliveryPolicy = deliveryPolicy
}
}

future, err := endpointsClient.Update(ctx, id.ResourceGroup, id.ProfileName, id.Name, endpoint)
Expand Down Expand Up @@ -453,14 +469,18 @@ func resourceCdnEndpointRead(d *schema.ResourceData, meta interface{}) error {
}
}

contentTypes := flattenAzureRMCdnEndpointContentTypes(props.ContentTypesToCompress)
if err := d.Set("content_types_to_compress", contentTypes); err != nil {
return fmt.Errorf("Error setting `content_types_to_compress`: %+v", err)
if _, ok := d.GetOk("content_types_to_compress"); ok {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think we need this check? or does the API return a default value?

contentTypes := flattenAzureRMCdnEndpointContentTypes(props.ContentTypesToCompress)
if err := d.Set("content_types_to_compress", contentTypes); err != nil {
return fmt.Errorf("Error setting `content_types_to_compress`: %+v", err)
}
}

geoFilters := flattenCdnEndpointGeoFilters(props.GeoFilters)
if err := d.Set("geo_filter", geoFilters); err != nil {
return fmt.Errorf("Error setting `geo_filter`: %+v", err)
if _, ok := d.GetOk("geo_filter"); ok {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think we need this check? or does the API return a default value?

geoFilters := flattenCdnEndpointGeoFilters(props.GeoFilters)
if err := d.Set("geo_filter", geoFilters); err != nil {
return fmt.Errorf("Error setting `geo_filter`: %+v", err)
}
}

origins := flattenAzureRMCdnEndpointOrigin(props.Origins)
Expand Down Expand Up @@ -662,6 +682,10 @@ func flattenAzureRMCdnEndpointOrigin(input *[]cdn.DeepCreatedOrigin) []interface
}

func expandArmCdnEndpointDeliveryPolicy(globalRulesRaw []interface{}, deliveryRulesRaw []interface{}) (*cdn.EndpointPropertiesUpdateParametersDeliveryPolicy, error) {
if len(globalRulesRaw) == 0 && len(deliveryRulesRaw) == 0 {
return nil, nil
}

deliveryRules := make([]cdn.DeliveryRule, 0)
deliveryPolicy := cdn.EndpointPropertiesUpdateParametersDeliveryPolicy{
Description: utils.String(""),
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)
}