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

app_service_environment: add field resource_group_name #6821

Merged
merged 2 commits into from May 13, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 10 additions & 0 deletions azurerm/helpers/azure/resource_group.go
Expand Up @@ -35,6 +35,16 @@ func SchemaResourceGroupNameForDataSource() *schema.Schema {
}
}

func SchemaResourceGroupNameOC() *schema.Schema {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have this as SchemaResourceGroupNameOptionalComputed so it's clearer purpose?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

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: Make it "Required" in next major release
magodo marked this conversation as resolved.
Show resolved Hide resolved
"resource_group_name": azure.SchemaResourceGroupNameOC(),

"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