Skip to content

Commit

Permalink
azurerm_cdn_endpoint - fix API error when given an empty `origin_host…
Browse files Browse the repository at this point in the history
…_header` (#11852)

Confirmed with service team, the request payload can't contain an empty string for the origin_host_header, otherwise, the service will raise following error:

{
  "error": {
    "code": "BadRequest",
    "message": "OriginHostHeader \"\" is invalid. It must be a valid domain name, IP version 4, or IP version 6."
  }
}
Also update the document of this property to remove the line about default, since that is not true.

Meanwhile, this should fix some of the acctest failures.

Fixes #8870, #11107.
  • Loading branch information
magodo committed May 25, 2021
1 parent 6aaeaea commit 2292f59
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
12 changes: 8 additions & 4 deletions azurerm/internal/services/cdn/cdn_endpoint_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ func resourceCdnEndpointCreate(d *schema.ResourceData, meta interface{}) error {
httpAllowed := d.Get("is_http_allowed").(bool)
httpsAllowed := d.Get("is_https_allowed").(bool)
cachingBehaviour := d.Get("querystring_caching_behaviour").(string)
originHostHeader := d.Get("origin_host_header").(string)
originPath := d.Get("origin_path").(string)
probePath := d.Get("probe_path").(string)
optimizationType := d.Get("optimization_type").(string)
Expand All @@ -250,11 +249,14 @@ func resourceCdnEndpointCreate(d *schema.ResourceData, meta interface{}) error {
IsHTTPAllowed: &httpAllowed,
IsHTTPSAllowed: &httpsAllowed,
QueryStringCachingBehavior: cdn.QueryStringCachingBehavior(cachingBehaviour),
OriginHostHeader: utils.String(originHostHeader),
},
Tags: tags.Expand(t),
}

if v, ok := d.GetOk("origin_host_header"); ok {
endpoint.EndpointProperties.OriginHostHeader = utils.String(v.(string))
}

if _, ok := d.GetOk("content_types_to_compress"); ok {
contentTypes := expandArmCdnEndpointContentTypesToCompress(d)
endpoint.EndpointProperties.ContentTypesToCompress = &contentTypes
Expand Down Expand Up @@ -343,7 +345,6 @@ func resourceCdnEndpointUpdate(d *schema.ResourceData, meta interface{}) error {
httpAllowed := d.Get("is_http_allowed").(bool)
httpsAllowed := d.Get("is_https_allowed").(bool)
cachingBehaviour := d.Get("querystring_caching_behaviour").(string)
hostHeader := d.Get("origin_host_header").(string)
originPath := d.Get("origin_path").(string)
probePath := d.Get("probe_path").(string)
optimizationType := d.Get("optimization_type").(string)
Expand All @@ -354,11 +355,14 @@ func resourceCdnEndpointUpdate(d *schema.ResourceData, meta interface{}) error {
IsHTTPAllowed: utils.Bool(httpAllowed),
IsHTTPSAllowed: utils.Bool(httpsAllowed),
QueryStringCachingBehavior: cdn.QueryStringCachingBehavior(cachingBehaviour),
OriginHostHeader: utils.String(hostHeader),
},
Tags: tags.Expand(t),
}

if v, ok := d.GetOk("origin_host_header"); ok {
endpoint.EndpointPropertiesUpdateParameters.OriginHostHeader = utils.String(v.(string))
}

if _, ok := d.GetOk("content_types_to_compress"); ok {
contentTypes := expandArmCdnEndpointContentTypesToCompress(d)
endpoint.EndpointPropertiesUpdateParameters.ContentTypesToCompress = &contentTypes
Expand Down
7 changes: 0 additions & 7 deletions azurerm/internal/services/cdn/cdn_endpoint_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,6 @@ func TestAccCdnEndpoint_updateHostHeader(t *testing.T) {
),
},
data.ImportStep(),
{
Config: r.hostHeader(data, ""),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.hostHeader(data, "www.example2.com"),
Check: resource.ComposeTestCheckFunc(
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/cdn_endpoint.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ The following arguments are supported:

* `origin` - (Required) The set of origins of the CDN endpoint. When multiple origins exist, the first origin will be used as primary and rest will be used as failover options. Each `origin` block supports fields documented below.

* `origin_host_header` - (Optional) The host header CDN provider will send along with content requests to origins. Defaults to the host name of the origin.
* `origin_host_header` - (Optional) The host header CDN provider will send along with content requests to origins.

* `origin_path` - (Optional) The path used at for origin requests.

Expand Down

0 comments on commit 2292f59

Please sign in to comment.