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

fix scope of name availability check in ASE hosted app services #7157

Merged
merged 1 commit into from Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion azurerm/internal/services/web/resource_arm_app_service.go
Expand Up @@ -192,6 +192,7 @@ func resourceArmAppService() *schema.Resource {

func resourceArmAppServiceCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Web.AppServicesClient
aspClient := meta.(*clients.Client).Web.AppServicePlansClient
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()

Expand All @@ -217,6 +218,19 @@ func resourceArmAppServiceCreate(d *schema.ResourceData, meta interface{}) error
Name: utils.String(name),
Type: web.CheckNameResourceTypesMicrosoftWebsites,
}

appServicePlanId := d.Get("app_service_plan_id").(string)
aspID, err := ParseAppServicePlanID(appServicePlanId)
if err != nil {
return err
}
// Check if App Service Plan is part of ASE
// If so, the name needs updating to <app name>.<ASE name>.appserviceenvironment.net and FQDN setting true for name availability check
aspDetails, _ := aspClient.Get(ctx, aspID.ResourceGroup, aspID.Name)
if aspDetails.HostingEnvironmentProfile != nil {
availabilityRequest.Name = utils.String(fmt.Sprintf("%s.%s.appserviceenvironment.net", name, aspID.Name))
availabilityRequest.IsFqdn = utils.Bool(true)
}
available, err := client.CheckNameAvailability(ctx, availabilityRequest)
if err != nil {
return fmt.Errorf("Error checking if the name %q was available: %+v", name, err)
Expand All @@ -227,7 +241,6 @@ func resourceArmAppServiceCreate(d *schema.ResourceData, meta interface{}) error
}

location := azure.NormalizeLocation(d.Get("location").(string))
appServicePlanId := d.Get("app_service_plan_id").(string)
enabled := d.Get("enabled").(bool)
httpsOnly := d.Get("https_only").(bool)
t := d.Get("tags").(map[string]interface{})
Expand Down
Expand Up @@ -1670,6 +1670,24 @@ func TestAccAzureRMAppService_basicWindowsContainer(t *testing.T) {
})
}

func TestAccAzureRMAppService_aseScopeNameCheck(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_app_service", "test")
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMAppServiceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMAppService_inAppServiceEnvironment(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMAppServiceExists(data.ResourceName),
),
},
data.ImportStep(),
},
})
}

func testCheckAzureRMAppServiceDestroy(s *terraform.State) error {
client := acceptance.AzureProvider.Meta().(*clients.Client).Web.AppServicesClient
ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext
Expand Down Expand Up @@ -4371,3 +4389,63 @@ resource "azurerm_app_service" "test" {
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger)
}

func testAccAzureRMAppService_inAppServiceEnvironment(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_virtual_network" "test" {
name = "acctest-vnet-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
address_space = ["10.0.0.0/16"]
}

resource "azurerm_subnet" "ase" {
name = "asesubnet"
resource_group_name = azurerm_resource_group.test.name
virtual_network_name = azurerm_virtual_network.test.name
address_prefix = "10.0.1.0/24"
}

resource "azurerm_subnet" "gateway" {
name = "gatewaysubnet"
resource_group_name = azurerm_resource_group.test.name
virtual_network_name = azurerm_virtual_network.test.name
address_prefix = "10.0.2.0/24"
}

resource "azurerm_app_service_environment" "test" {
name = "acctest-ase-%d"
subnet_id = azurerm_subnet.ase.id
}

resource "azurerm_app_service_plan" "test" {
name = "acctest-ASP-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
app_service_environment_id = azurerm_app_service_environment.test.id

sku {
tier = "Isolated"
size = "I1"
capacity = 1
}
}

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
}

`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.RandomInteger)
}