Skip to content

Commit

Permalink
azurerm_linux_web_app azurerm_linux_web_app_slot - Documentation …
Browse files Browse the repository at this point in the history
…and test enhancements related to `virtual_network_subnet_id` argument (#17584)

* Update unit-test.yaml

* Update unit-test.yaml

* Update unit-test.yaml

* Update unit-test.yaml

* Create .gitattributes

* Create build.yml

* Create maketests.yaml

* Update maketests.yaml

* Update maketests.yaml

* enhancements

* Delete .gitattributes

* Delete build.yml

* Update unit-test.yaml

* Delete maketests.yaml

* cleanup

* make fmt

* Updated read to fix #17559

* Removed ForceNew

* make fmt

* make terrafmt

* fixed test

* make terrafmt

* Fixed typo in URL

* Fixed typo in URL

Co-authored-by: Your Name <[email protected]>
  • Loading branch information
bubbletroubles and Your Name authored Jul 28, 2022
1 parent 117cdd4 commit bc710ec
Show file tree
Hide file tree
Showing 6 changed files with 277 additions and 32 deletions.
25 changes: 21 additions & 4 deletions internal/services/appservice/linux_web_app_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/services/appservice/helpers"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/appservice/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/appservice/validate"
networkValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/network/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
Expand Down Expand Up @@ -130,9 +131,9 @@ func (r LinuxWebAppResource) Arguments() map[string]*pluginsdk.Schema {
},

"virtual_network_subnet_id": {
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: networkValidate.SubnetID,
},

"identity": commonschema.SystemAssignedUserAssignedIdentityOptional(),
Expand Down Expand Up @@ -501,11 +502,14 @@ func (r LinuxWebAppResource) Read() sdk.ResourceFunc {
KeyVaultReferenceIdentityID: utils.NormalizeNilableString(props.KeyVaultReferenceIdentity),
Enabled: utils.NormaliseNilableBool(props.Enabled),
HttpsOnly: utils.NormaliseNilableBool(props.HTTPSOnly),
VirtualNetworkSubnetID: utils.NormalizeNilableString(webApp.VirtualNetworkSubnetID),
StickySettings: helpers.FlattenStickySettings(stickySettings.SlotConfigNames),
Tags: tags.ToTypedObject(webApp.Tags),
}

if subnetId := utils.NormalizeNilableString(props.VirtualNetworkSubnetID); subnetId != "" {
state.VirtualNetworkSubnetID = subnetId
}

var healthCheckCount *int
state.AppSettings, healthCheckCount = helpers.FlattenAppSettings(appSettings)

Expand Down Expand Up @@ -665,6 +669,19 @@ func (r LinuxWebAppResource) Update() sdk.ResourceFunc {
existing.SiteConfig = siteConfig
}

if metadata.ResourceData.HasChange("virtual_network_subnet_id") {
subnetId := metadata.ResourceData.Get("virtual_network_subnet_id").(string)
if subnetId == "" {
if _, err := client.DeleteSwiftVirtualNetwork(ctx, id.ResourceGroup, id.SiteName); err != nil {
return fmt.Errorf("removing `virtual_network_subnet_id` association for %s: %+v", *id, err)
}
var empty *string
existing.SiteProperties.VirtualNetworkSubnetID = empty
} else {
existing.SiteProperties.VirtualNetworkSubnetID = utils.String(subnetId)
}
}

updateFuture, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.SiteName, existing)
if err != nil {
return fmt.Errorf("updating Linux %s: %+v", id, err)
Expand Down
123 changes: 115 additions & 8 deletions internal/services/appservice/linux_web_app_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1122,9 +1122,12 @@ func TestAccLinuxWebApp_vNetIntegration(t *testing.T) {

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.vNetIntegrationWebApp_withSubnetId(data),
Config: r.vNetIntegrationWebApp_subnet1(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("virtual_network_subnet_id").MatchesOtherKey(
check.That("azurerm_subnet.test1").Key("id"),
),
),
},
data.ImportStep(),
Expand All @@ -1144,9 +1147,22 @@ func TestAccLinuxWebApp_vNetIntegrationUpdate(t *testing.T) {
},
data.ImportStep(),
{
Config: r.vNetIntegrationWebApp_withSubnetId(data),
Config: r.vNetIntegrationWebApp_subnet1(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("virtual_network_subnet_id").MatchesOtherKey(
check.That("azurerm_subnet.test1").Key("id"),
),
),
},
data.ImportStep(),
{
Config: r.vNetIntegrationWebApp_subnet2(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("virtual_network_subnet_id").MatchesOtherKey(
check.That("azurerm_subnet.test2").Key("id"),
),
),
},
data.ImportStep(),
Expand Down Expand Up @@ -2913,8 +2929,8 @@ resource "azurerm_virtual_network" "test" {
resource_group_name = azurerm_resource_group.test.name
}
resource "azurerm_subnet" "test" {
name = "subnet"
resource "azurerm_subnet" "test1" {
name = "subnet1"
resource_group_name = azurerm_resource_group.test.name
virtual_network_name = azurerm_virtual_network.test.name
address_prefixes = ["10.0.1.0/24"]
Expand All @@ -2929,6 +2945,22 @@ resource "azurerm_subnet" "test" {
}
}
resource "azurerm_subnet" "test2" {
name = "subnet2"
resource_group_name = azurerm_resource_group.test.name
virtual_network_name = azurerm_virtual_network.test.name
address_prefixes = ["10.0.2.0/24"]
delegation {
name = "delegation"
service_delegation {
name = "Microsoft.Web/serverFarms"
actions = ["Microsoft.Network/virtualNetworks/subnets/action"]
}
}
}
resource "azurerm_linux_web_app" "test" {
name = "acctestWA-%d"
location = azurerm_resource_group.test.location
Expand All @@ -2940,7 +2972,66 @@ resource "azurerm_linux_web_app" "test" {
`, r.baseTemplate(data), data.RandomInteger, data.RandomInteger)
}

func (r LinuxWebAppResource) vNetIntegrationWebApp_withSubnetId(data acceptance.TestData) string {
func (r LinuxWebAppResource) vNetIntegrationWebApp_subnet1(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
%s
resource "azurerm_virtual_network" "test" {
name = "vnet-%d"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
}
resource "azurerm_subnet" "test1" {
name = "subnet1"
resource_group_name = azurerm_resource_group.test.name
virtual_network_name = azurerm_virtual_network.test.name
address_prefixes = ["10.0.1.0/24"]
delegation {
name = "delegation"
service_delegation {
name = "Microsoft.Web/serverFarms"
actions = ["Microsoft.Network/virtualNetworks/subnets/action"]
}
}
}
resource "azurerm_subnet" "test2" {
name = "subnet2"
resource_group_name = azurerm_resource_group.test.name
virtual_network_name = azurerm_virtual_network.test.name
address_prefixes = ["10.0.2.0/24"]
delegation {
name = "delegation"
service_delegation {
name = "Microsoft.Web/serverFarms"
actions = ["Microsoft.Network/virtualNetworks/subnets/action"]
}
}
}
resource "azurerm_linux_web_app" "test" {
name = "acctestWA-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
service_plan_id = azurerm_service_plan.test.id
virtual_network_subnet_id = azurerm_subnet.test1.id
site_config {}
}
`, r.baseTemplate(data), data.RandomInteger, data.RandomInteger)
}

func (r LinuxWebAppResource) vNetIntegrationWebApp_subnet2(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
Expand All @@ -2955,8 +3046,8 @@ resource "azurerm_virtual_network" "test" {
resource_group_name = azurerm_resource_group.test.name
}
resource "azurerm_subnet" "test" {
name = "subnet"
resource "azurerm_subnet" "test1" {
name = "subnet1"
resource_group_name = azurerm_resource_group.test.name
virtual_network_name = azurerm_virtual_network.test.name
address_prefixes = ["10.0.1.0/24"]
Expand All @@ -2971,12 +3062,28 @@ resource "azurerm_subnet" "test" {
}
}
resource "azurerm_subnet" "test2" {
name = "subnet2"
resource_group_name = azurerm_resource_group.test.name
virtual_network_name = azurerm_virtual_network.test.name
address_prefixes = ["10.0.2.0/24"]
delegation {
name = "delegation"
service_delegation {
name = "Microsoft.Web/serverFarms"
actions = ["Microsoft.Network/virtualNetworks/subnets/action"]
}
}
}
resource "azurerm_linux_web_app" "test" {
name = "acctestWA-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
service_plan_id = azurerm_service_plan.test.id
virtual_network_subnet_id = azurerm_subnet.test.id
virtual_network_subnet_id = azurerm_subnet.test2.id
site_config {}
}
Expand Down
25 changes: 21 additions & 4 deletions internal/services/appservice/linux_web_app_slot_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/services/appservice/helpers"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/appservice/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/appservice/validate"
networkValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/network/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
Expand Down Expand Up @@ -132,9 +133,9 @@ func (r LinuxWebAppSlotResource) Arguments() map[string]*pluginsdk.Schema {
},

"virtual_network_subnet_id": {
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: networkValidate.SubnetID,
},

"identity": commonschema.SystemAssignedUserAssignedIdentityOptional(),
Expand Down Expand Up @@ -451,7 +452,10 @@ func (r LinuxWebAppSlotResource) Read() sdk.ResourceFunc {
Enabled: utils.NormaliseNilableBool(props.Enabled),
HttpsOnly: utils.NormaliseNilableBool(props.HTTPSOnly),
Tags: tags.ToTypedObject(webApp.Tags),
VirtualNetworkSubnetID: utils.NormalizeNilableString(webApp.VirtualNetworkSubnetID),
}

if subnetId := utils.NormalizeNilableString(props.VirtualNetworkSubnetID); subnetId != "" {
state.VirtualNetworkSubnetID = subnetId
}

var healthCheckCount *int
Expand Down Expand Up @@ -588,6 +592,19 @@ func (r LinuxWebAppSlotResource) Update() sdk.ResourceFunc {
existing.SiteConfig = siteConfig
}

if metadata.ResourceData.HasChange("virtual_network_subnet_id") {
subnetId := metadata.ResourceData.Get("virtual_network_subnet_id").(string)
if subnetId == "" {
if _, err := client.DeleteSwiftVirtualNetworkSlot(ctx, id.ResourceGroup, id.SiteName, id.SlotName); err != nil {
return fmt.Errorf("removing `virtual_network_subnet_id` association for %s: %+v", *id, err)
}
var empty *string
existing.SiteProperties.VirtualNetworkSubnetID = empty
} else {
existing.SiteProperties.VirtualNetworkSubnetID = utils.String(subnetId)
}
}

updateFuture, err := client.CreateOrUpdateSlot(ctx, id.ResourceGroup, id.SiteName, existing, id.SlotName)
if err != nil {
return fmt.Errorf("updating Linux %s: %+v", id, err)
Expand Down
Loading

0 comments on commit bc710ec

Please sign in to comment.