Skip to content

Commit

Permalink
Merge pull request #6821 from magodo/ase_resgroup
Browse files Browse the repository at this point in the history
  • Loading branch information
jackofallops committed May 13, 2020
2 parents e69f9b6 + 10cc0fd commit a685a47
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 10 deletions.
10 changes: 10 additions & 0 deletions azurerm/helpers/azure/resource_group.go
Expand Up @@ -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)

Expand Down
23 changes: 15 additions & 8 deletions azurerm/internal/services/web/app_service_environment_resource.go
Expand Up @@ -86,18 +86,16 @@ func resourceArmAppServiceEnvironment() *schema.Resource {
}, false),
},

// TODO in 3.0 Make it "Required"
"resource_group_name": azure.SchemaResourceGroupNameOptionalComputed(),

"tags": tags.ForceNewSchema(),

// Computed
"location": {
Type: schema.TypeString,
Computed: true,
},

"resource_group_name": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand All @@ -118,18 +116,27 @@ 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
var location string
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)
Expand Down
Expand Up @@ -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
Expand Down Expand Up @@ -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" {
Expand Down
6 changes: 4 additions & 2 deletions website/docs/r/app_service_environment.html.markdown
Expand Up @@ -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
Expand Down

0 comments on commit a685a47

Please sign in to comment.