From df50acd7952cc4c2b7213ce91b45b1fcfd902056 Mon Sep 17 00:00:00 2001 From: Tom Harvey Date: Wed, 26 Sep 2018 08:19:25 +1000 Subject: [PATCH] App Service Custom Hostname Binding: support for multiple bindings (#1970) before this change: ``` $ ARM_TEST_APP_SERVICE=tomdevacctest12345 ARM_TEST_DOMAIN=REDACTED ARM_ALT_TEST_DOMAIN=REDACTED acctests azurerm TestAccAzureRMAppServiceCustomHostnameBinding === RUN TestAccAzureRMAppServiceCustomHostnameBinding === RUN TestAccAzureRMAppServiceCustomHostnameBinding/basic === RUN TestAccAzureRMAppServiceCustomHostnameBinding/basic/basic === RUN TestAccAzureRMAppServiceCustomHostnameBinding/basic/import --- PASS: TestAccAzureRMAppServiceCustomHostnameBinding (299.70s) --- PASS: TestAccAzureRMAppServiceCustomHostnameBinding/basic (299.70s) --- PASS: TestAccAzureRMAppServiceCustomHostnameBinding/basic/basic (135.97s) --- PASS: TestAccAzureRMAppServiceCustomHostnameBinding/basic/import (163.72s) === RUN TestAccAzureRMAppServiceCustomHostnameBinding_multiple --- FAIL: TestAccAzureRMAppServiceCustomHostnameBinding_multiple (112.01s) testing.go:573: Error destroying resource! WARNING: Dangling resources may exist. The full state and error is shown below. Error: Error applying: 1 error(s) occurred: * azurerm_app_service_custom_hostname_binding.test2 (destroy): 1 error(s) occurred: * azurerm_app_service_custom_hostname_binding.test2: web.AppsClient#DeleteHostNameBinding: Failure sending request: StatusCode=409 -- Original Error: autorest/azure: Service returned an error. Status= ``` after this change: ``` $ ARM_TEST_APP_SERVICE=tomdevacctest12345 ARM_TEST_DOMAIN=REDACTED ARM_ALT_TEST_DOMAIN=REDACTED acctests azurerm TestAccAzureRMAppServiceCustomHostnameBinding_multiple === RUN TestAccAzureRMAppServiceCustomHostnameBinding_multiple --- PASS: TestAccAzureRMAppServiceCustomHostnameBinding_multiple (157.72s) PASS ok github.com/terraform-providers/terraform-provider-azurerm/azurerm 158.148s ``` --- ...t_arm_app_service_hostname_binding_test.go | 5 +- ...arm_app_service_custom_hostname_binding.go | 8 +++ ...pp_service_custom_hostname_binding_test.go | 52 +++++++++++++++++++ 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/azurerm/import_arm_app_service_hostname_binding_test.go b/azurerm/import_arm_app_service_hostname_binding_test.go index 9e51fcf61401..fbe801567956 100644 --- a/azurerm/import_arm_app_service_hostname_binding_test.go +++ b/azurerm/import_arm_app_service_hostname_binding_test.go @@ -13,8 +13,9 @@ func TestAccAzureRMAppServiceCustomHostnameBinding(t *testing.T) { // the app service name being shared (so the tests don't conflict with each other) testCases := map[string]map[string]func(t *testing.T){ "basic": { - "basic": testAccAzureRMAppServiceCustomHostnameBinding_basic, - "import": testAccAzureRMAppServiceCustomHostnameBinding_import, + "basic": testAccAzureRMAppServiceCustomHostnameBinding_basic, + "multiple": testAccAzureRMAppServiceCustomHostnameBinding_multiple, + "import": testAccAzureRMAppServiceCustomHostnameBinding_import, }, } diff --git a/azurerm/resource_arm_app_service_custom_hostname_binding.go b/azurerm/resource_arm_app_service_custom_hostname_binding.go index 62ead0e706f0..3fe71b48b61b 100644 --- a/azurerm/resource_arm_app_service_custom_hostname_binding.go +++ b/azurerm/resource_arm_app_service_custom_hostname_binding.go @@ -9,6 +9,8 @@ import ( "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" ) +var appServiceCustomHostnameBindingResourceName = "azurerm_app_service_custom_hostname_binding" + func resourceArmAppServiceCustomHostnameBinding() *schema.Resource { return &schema.Resource{ Create: resourceArmAppServiceCustomHostnameBindingCreate, @@ -46,6 +48,9 @@ func resourceArmAppServiceCustomHostnameBindingCreate(d *schema.ResourceData, me appServiceName := d.Get("app_service_name").(string) hostname := d.Get("hostname").(string) + azureRMLockByName(appServiceName, appServiceCustomHostnameBindingResourceName) + defer azureRMUnlockByName(appServiceName, appServiceCustomHostnameBindingResourceName) + properties := web.HostNameBinding{ HostNameBindingProperties: &web.HostNameBindingProperties{ SiteName: utils.String(appServiceName), @@ -110,6 +115,9 @@ func resourceArmAppServiceCustomHostnameBindingDelete(d *schema.ResourceData, me appServiceName := id.Path["sites"] hostname := id.Path["hostNameBindings"] + azureRMLockByName(appServiceName, appServiceCustomHostnameBindingResourceName) + defer azureRMUnlockByName(appServiceName, appServiceCustomHostnameBindingResourceName) + log.Printf("[DEBUG] Deleting App Service Hostname Binding %q (App Service %q / Resource Group %q)", hostname, appServiceName, resGroup) ctx := meta.(*ArmClient).StopContext diff --git a/azurerm/resource_arm_app_service_custom_hostname_binding_test.go b/azurerm/resource_arm_app_service_custom_hostname_binding_test.go index 7ab2b0c6fbbb..57f8a84554b7 100644 --- a/azurerm/resource_arm_app_service_custom_hostname_binding_test.go +++ b/azurerm/resource_arm_app_service_custom_hostname_binding_test.go @@ -45,6 +45,45 @@ func testAccAzureRMAppServiceCustomHostnameBinding_basic(t *testing.T) { }) } +func testAccAzureRMAppServiceCustomHostnameBinding_multiple(t *testing.T) { + appServiceEnvVariable := "ARM_TEST_APP_SERVICE" + appServiceEnv := os.Getenv(appServiceEnvVariable) + if appServiceEnv == "" { + t.Skipf("Skipping as %q is not specified", appServiceEnvVariable) + } + + domainEnvVariable := "ARM_TEST_DOMAIN" + domainEnv := os.Getenv(domainEnvVariable) + if domainEnv == "" { + t.Skipf("Skipping as %q is not specified", domainEnvVariable) + } + + altDomainEnvVariable := "ARM_ALT_TEST_DOMAIN" + altDomainEnv := os.Getenv(altDomainEnvVariable) + if domainEnv == "" { + t.Skipf("Skipping as %q is not specified", domainEnvVariable) + } + + resourceName := "azurerm_app_service_custom_hostname_binding.test" + ri := acctest.RandInt() + location := testLocation() + config := testAccAzureRMAppServiceCustomHostnameBinding_multipleConfig(ri, location, appServiceEnv, domainEnv, altDomainEnv) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMAppServiceCustomHostnameBindingDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMAppServiceCustomHostnameBindingExists(resourceName), + ), + }, + }, + }) +} + func testCheckAzureRMAppServiceCustomHostnameBindingDestroy(s *terraform.State) error { client := testAccProvider.Meta().(*ArmClient).appServicesClient @@ -132,3 +171,16 @@ resource "azurerm_app_service_custom_hostname_binding" "test" { } `, rInt, location, rInt, appServiceName, domain) } + +func testAccAzureRMAppServiceCustomHostnameBinding_multipleConfig(rInt int, location, appServiceName, domain, altDomain string) string { + template := testAccAzureRMAppServiceCustomHostnameBinding_basicConfig(rInt, location, appServiceName, domain) + return fmt.Sprintf(` +%s + +resource "azurerm_app_service_custom_hostname_binding" "test2" { + hostname = "%s" + app_service_name = "${azurerm_app_service.test.name}" + resource_group_name = "${azurerm_resource_group.test.name}" +} +`, template, altDomain) +}