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

azurerm_app_service: Adds support for CORS settings #2870

Merged
2 changes: 1 addition & 1 deletion azurerm/data_source_app_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func dataSourceArmAppService() *schema.Resource {
Computed: true,
},

"site_config": azure.SchemaAppServiceSiteConfig(),
"site_config": azure.SchemaAppServiceDataSourceSiteConfig(),

"client_affinity_enabled": {
Type: schema.TypeBool,
Expand Down
253 changes: 253 additions & 0 deletions azurerm/helpers/azure/app_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,29 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func SchemaAppServiceCorsSettings() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"allowed_origins": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"support_credentials": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
},
}
}

func SchemaAppServiceSiteConfig() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Expand Down Expand Up @@ -222,11 +245,233 @@ func SchemaAppServiceSiteConfig() *schema.Schema {
Type: schema.TypeString,
Optional: true,
},

"cors": {
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"allowed_origins": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"support_credentials": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
},
},
},
},
}
}

func SchemaAppServiceDataSourceSiteConfig() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"always_on": {
Type: schema.TypeBool,
Computed: true,
},

"app_command_line": {
Type: schema.TypeString,
Computed: true,
},

"default_documents": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},

"dotnet_framework_version": {
Type: schema.TypeString,
Computed: true,
},

"http2_enabled": {
Type: schema.TypeBool,
Computed: true,
},

"ip_restriction": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ip_address": {
Type: schema.TypeString,
Computed: true,
},
"subnet_mask": {
Type: schema.TypeString,
Computed: true,
},
},
},
},

"java_version": {
Type: schema.TypeString,
Computed: true,
},

"java_container": {
Type: schema.TypeString,
Computed: true,
},

"java_container_version": {
Type: schema.TypeString,
Computed: true,
},

"local_mysql_enabled": {
Type: schema.TypeBool,
Computed: true,
},

"managed_pipeline_mode": {
Type: schema.TypeString,
Computed: true,
},

"php_version": {
Type: schema.TypeString,
Computed: true,
},

"python_version": {
Type: schema.TypeString,
Computed: true,
},

"remote_debugging_enabled": {
Type: schema.TypeBool,
Computed: true,
},

"remote_debugging_version": {
Type: schema.TypeString,
Computed: true,
},

"scm_type": {
Type: schema.TypeString,
Computed: true,
},

"use_32_bit_worker_process": {
Type: schema.TypeBool,
Computed: true,
},

"websockets_enabled": {
Type: schema.TypeBool,
Computed: true,
},

"ftps_state": {
Type: schema.TypeString,
Computed: true,
},

"linux_fx_version": {
Type: schema.TypeString,
Computed: true,
},

"min_tls_version": {
Type: schema.TypeString,
Computed: true,
},

"virtual_network_name": {
Type: schema.TypeString,
Computed: true,
},

"cors": {
Type: schema.TypeList,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"allowed_origins": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"support_credentials": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},
},
},
}
}

func ExpandAppServiceCorsSettings(input interface{}) web.CorsSettings {
settings := input.([]interface{})
corsSettings := web.CorsSettings{}

if len(settings) == 0 {
return corsSettings
}

setting := settings[0].(map[string]interface{})

if v, ok := setting["allowed_origins"]; ok {
input := v.([]interface{})

allowedOrigins := make([]string, 0)
for _, param := range input {
allowedOrigins = append(allowedOrigins, param.(string))
}

corsSettings.AllowedOrigins = &allowedOrigins
}

if v, ok := setting["support_credentials"]; ok {
corsSettings.SupportCredentials = utils.Bool(v.(bool))
}

return corsSettings
}

func FlattenAppServiceCorsSettings(input *web.CorsSettings) []interface{} {
results := make([]interface{}, 0)
if input == nil {
return results
}

result := make(map[string]interface{})

allowedOrigins := make([]string, 0)
if s := input.AllowedOrigins; s != nil {
allowedOrigins = *s
}
result["allowed_origins"] = allowedOrigins

if input.SupportCredentials != nil {
result["support_credentials"] = *input.SupportCredentials
}

return append(results, result)
}

func ExpandAppServiceSiteConfig(input interface{}) web.SiteConfig {
configs := input.([]interface{})
siteConfig := web.SiteConfig{}
Expand Down Expand Up @@ -355,6 +600,12 @@ func ExpandAppServiceSiteConfig(input interface{}) web.SiteConfig {
siteConfig.VnetName = utils.String(v.(string))
}

if v, ok := config["cors"]; ok {
corsSettings := v.(interface{})
expand := ExpandAppServiceCorsSettings(corsSettings)
siteConfig.Cors = &expand
}

return siteConfig
}

Expand Down Expand Up @@ -466,5 +717,7 @@ func FlattenAppServiceSiteConfig(input *web.SiteConfig) []interface{} {
result["ftps_state"] = string(input.FtpsState)
result["min_tls_version"] = string(input.MinTLSVersion)

result["cors"] = FlattenAppServiceCorsSettings(input.Cors)

return append(results, result)
}
73 changes: 73 additions & 0 deletions azurerm/resource_arm_app_service_slot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,33 @@ func TestAccAzureRMAppServiceSlot_connectionStrings(t *testing.T) {
})
}

func TestAccAzureRMAppServiceSlot_corsSettings(t *testing.T) {
resourceName := "azurerm_app_service.test"
ri := tf.AccRandTimeInt()
location := testLocation()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMAppServiceSlotDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMAppServiceSlot_corsSettings(ri, location),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMAppServiceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "site_config.0.cors.#", "1"),
resource.TestCheckResourceAttr(resourceName, "site_config.0.cors.0.support_credentials", "true"),
resource.TestCheckResourceAttr(resourceName, "site_config.0.cors.0.allowed_origins.#", "3"),
)},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAzureRMAppServiceSlot_defaultDocuments(t *testing.T) {
resourceName := "azurerm_app_service_slot.test"
ri := tf.AccRandTimeInt()
Expand Down Expand Up @@ -1251,6 +1278,52 @@ resource "azurerm_app_service_slot" "test" {
`, rInt, location, rInt, rInt, rInt)
}

func testAccAzureRMAppServiceSlot_corsSettings(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_app_service_plan" "test" {
name = "acctestASP-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"

sku {
tier = "Standard"
size = "S1"
}
}

resource "azurerm_app_service" "test" {
name = "acctestAS-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
app_service_plan_id = "${azurerm_app_service_plan.test.id}"
}

resource "azurerm_app_service_slot" "test" {
name = "acctestASSlot-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
app_service_plan_id = "${azurerm_app_service_plan.test.id}"
app_service_name = "${azurerm_app_service.test.name}"

site_config {
cors {
allowed_origins = [
"http://www.contoso.com",
"www.contoso.com",
"contoso.com"
]
support_credentials = true
}
}
}
`, rInt, location, rInt, rInt, rInt)
}

func testAccAzureRMAppServiceSlot_defaultDocuments(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
Expand Down
Loading