diff --git a/azurerm/helpers/azure/resource_group.go b/azurerm/helpers/azure/resource_group.go index 9a15422613a7..7967f69f7bf8 100644 --- a/azurerm/helpers/azure/resource_group.go +++ b/azurerm/helpers/azure/resource_group.go @@ -36,6 +36,16 @@ func SchemaResourceGroupNameForDataSource() *schema.Schema { } } +func SchemaResourceGroupNameOptionalComputed() *schema.Schema { + return &schema.Schema{ + Type: schema.TypeString, + ForceNew: true, + Optional: true, + Computed: true, + ValidateFunc: validateResourceGroupName, + } +} + func validateResourceGroupName(v interface{}, k string) (warnings []string, errors []error) { value := v.(string) diff --git a/azurerm/internal/services/web/app_service_environment_resource.go b/azurerm/internal/services/web/app_service_environment_resource.go index 3632b0946cdc..21650652e52a 100644 --- a/azurerm/internal/services/web/app_service_environment_resource.go +++ b/azurerm/internal/services/web/app_service_environment_resource.go @@ -86,6 +86,9 @@ func resourceArmAppServiceEnvironment() *schema.Resource { }, false), }, + // TODO in 3.0 Make it "Required" + "resource_group_name": azure.SchemaResourceGroupNameOptionalComputed(), + "tags": tags.ForceNewSchema(), // Computed @@ -93,11 +96,6 @@ func resourceArmAppServiceEnvironment() *schema.Resource { Type: schema.TypeString, Computed: true, }, - - "resource_group_name": { - Type: schema.TypeString, - Computed: true, - }, }, } } @@ -118,10 +116,19 @@ func resourceArmAppServiceEnvironmentCreate(d *schema.ResourceData, meta interfa return err } + // TODO: Remove the implicit behavior in new major version. + // Discrepancy of resource group between ASE and Subnet is allowed. While for the sake of + // compatibility, we still allow user to use the resource group of Subnet to be the one for + // ASE implicitly. While allow user to explicitly specify the resource group, which takes higher + // precedence. resourceGroup := subnet.ResourceGroup - vnet, err := networksClient.Get(ctx, resourceGroup, subnet.VirtualNetworkName, "") + if v, ok := d.GetOk("resource_group_name"); ok { + resourceGroup = v.(string) + } + + vnet, err := networksClient.Get(ctx, subnet.ResourceGroup, subnet.VirtualNetworkName, "") if err != nil { - return fmt.Errorf("Error retrieving Virtual Network %q (Resource Group %q): %+v", subnet.VirtualNetworkName, resourceGroup, err) + return fmt.Errorf("Error retrieving Virtual Network %q (Resource Group %q): %+v", subnet.VirtualNetworkName, subnet.ResourceGroup, err) } // the App Service Environment has to be in the same location as the Virtual Network @@ -129,7 +136,7 @@ func resourceArmAppServiceEnvironmentCreate(d *schema.ResourceData, meta interfa if loc := vnet.Location; loc != nil { location = azure.NormalizeLocation(*loc) } else { - return fmt.Errorf("Error determining Location from Virtual Network %q (Resource Group %q): `location` was nil", subnet.VirtualNetworkName, resourceGroup) + return fmt.Errorf("Error determining Location from Virtual Network %q (Resource Group %q): `location` was nil", subnet.VirtualNetworkName, subnet.ResourceGroup) } existing, err := client.Get(ctx, resourceGroup, name) diff --git a/azurerm/internal/services/web/tests/app_service_environment_resource_test.go b/azurerm/internal/services/web/tests/app_service_environment_resource_test.go index e338f201084f..71769ef80e8e 100644 --- a/azurerm/internal/services/web/tests/app_service_environment_resource_test.go +++ b/azurerm/internal/services/web/tests/app_service_environment_resource_test.go @@ -120,6 +120,25 @@ func TestAccAzureRMAppServiceEnvironment_withAppServicePlan(t *testing.T) { }) } +func TestAccAzureRMAppServiceEnvironment_dedicatedResourceGroup(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_app_service_environment", "test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acceptance.PreCheck(t) }, + Providers: acceptance.SupportedProviders, + CheckDestroy: testCheckAzureRMAppServiceEnvironmentDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMAppServiceEnvironment_dedicatedResourceGroup(data), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMAppServiceEnvironmentExists(data.ResourceName), + ), + }, + data.ImportStep(), + }, + }) +} + func testCheckAzureRMAppServiceEnvironmentExists(resourceName string) resource.TestCheckFunc { return func(s *terraform.State) error { client := acceptance.AzureProvider.Meta().(*clients.Client).Web.AppServiceEnvironmentsClient @@ -234,6 +253,24 @@ resource "azurerm_app_service_plan" "test" { `, template, data.RandomInteger) } +func testAccAzureRMAppServiceEnvironment_dedicatedResourceGroup(data acceptance.TestData) string { + template := testAccAzureRMAppServiceEnvironment_template(data) + return fmt.Sprintf(` +%s + +resource "azurerm_resource_group" "test2" { + name = "acctestRG2-%[2]d" + location = "%s" +} + +resource "azurerm_app_service_environment" "test" { + name = "acctest-ase-%[2]d" + resource_group_name = azurerm_resource_group.test2.name + subnet_id = azurerm_subnet.ase.id +} +`, template, data.RandomInteger, data.Locations.Secondary) +} + func testAccAzureRMAppServiceEnvironment_template(data acceptance.TestData) string { return fmt.Sprintf(` provider "azurerm" { diff --git a/website/docs/r/app_service_environment.html.markdown b/website/docs/r/app_service_environment.html.markdown index a0518caac047..ed0cea611f3c 100644 --- a/website/docs/r/app_service_environment.html.markdown +++ b/website/docs/r/app_service_environment.html.markdown @@ -61,12 +61,14 @@ resource "azurerm_app_service_environment" "example" { * `front_end_scale_factor` - (Optional) Scale factor for front end instances. Possible values are between `5` and `15`. Defaults to `15`. +* `resource_group_name` - (Optional) The name of the Resource Group where the App Service Environment exists. Defaults to the Resource Group of the Subnet (specified by `subnet_id`). + +* `tags` - (Optional) A mapping of tags to assign to the resource. Changing this forces a new resource to be created. + ## Attribute Reference * `id` - The ID of the App Service Environment. -* `resource_group_name` - The name of the Resource Group where the App Service Environment exists. - * `location` - The location where the App Service Environment exists. ## Timeouts