Skip to content

Commit

Permalink
Merge pull request #2870 from joakimhew/feature/2848_app_service_cors…
Browse files Browse the repository at this point in the history
…_support

azurerm_app_service: Adds support for CORS settings
  • Loading branch information
tombuildsstuff authored Mar 29, 2019
2 parents 38f3ab8 + 83617f1 commit d27c3c5
Show file tree
Hide file tree
Showing 7 changed files with 414 additions and 14 deletions.
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
232 changes: 232 additions & 0 deletions azurerm/helpers/azure/app_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,235 @@ 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.(*schema.Set).List()

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([]interface{}, 0)
if s := input.AllowedOrigins; s != nil {
for _, v := range *s {
allowedOrigins = append(allowedOrigins, v)
}
}
result["allowed_origins"] = schema.NewSet(schema.HashString, 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 +579,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 +696,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)
}
70 changes: 70 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,30 @@ 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),
)},
{
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 +1275,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

0 comments on commit d27c3c5

Please sign in to comment.