Skip to content

Commit

Permalink
Updated to remove ForceNew (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
bubbletroubles authored Jul 14, 2022
1 parent 26d2a49 commit 520f6c7
Show file tree
Hide file tree
Showing 6 changed files with 261 additions and 28 deletions.
20 changes: 17 additions & 3 deletions internal/services/appservice/linux_function_app_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/services/appservice/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/appservice/validate"
kvValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/keyvault/validate"
networkValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/network/validate"
storageValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/storage/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
Expand Down Expand Up @@ -246,9 +247,9 @@ func (r LinuxFunctionAppResource) Arguments() map[string]*pluginsdk.Schema {
"tags": tags.Schema(),

"virtual_network_subnet_id": {
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: networkValidate.SubnetID,
},
}
}
Expand Down Expand Up @@ -717,6 +718,19 @@ func (r LinuxFunctionAppResource) Update() sdk.ResourceFunc {
existing.SiteProperties.HTTPSOnly = utils.Bool(state.HttpsOnly)
}

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)
}
}

if metadata.ResourceData.HasChange("client_certificate_enabled") {
existing.SiteProperties.ClientCertEnabled = utils.Bool(state.ClientCertEnabled)
}
Expand Down
125 changes: 115 additions & 10 deletions internal/services/appservice/linux_function_app_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1199,11 +1199,11 @@ func TestAccLinuxFunctionApp_vNetIntegration(t *testing.T) {

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.vNetIntegration_withSubnetId(data, SkuStandardPlan),
Config: r.vNetIntegration_subnet1(data, SkuStandardPlan),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("virtual_network_subnet_id").MatchesOtherKey(
check.That("azurerm_subnet.test").Key("id"),
check.That("azurerm_subnet.test1").Key("id"),
),
),
},
Expand All @@ -1224,11 +1224,21 @@ func TestAccLinuxFunctionApp_vNetIntegrationUpdate(t *testing.T) {
},
data.ImportStep(),
{
Config: r.vNetIntegration_withSubnetId(data, SkuStandardPlan),
Config: r.vNetIntegration_subnet1(data, SkuStandardPlan),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("virtual_network_subnet_id").MatchesOtherKey(
check.That("azurerm_subnet.test").Key("id"),
check.That("azurerm_subnet.test1").Key("id"),
),
),
},
data.ImportStep(),
{
Config: r.vNetIntegration_subnet2(data, SkuStandardPlan),
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"),
),
),
},
Expand Down Expand Up @@ -3202,8 +3212,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 @@ -3218,6 +3228,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_function_app" "test" {
name = "acctest-LFA-%d"
location = azurerm_resource_group.test.location
Expand All @@ -3235,7 +3261,7 @@ resource "azurerm_linux_function_app" "test" {
`, r.template(data, planSku), data.RandomInteger, data.RandomInteger)
}

func (r LinuxFunctionAppResource) vNetIntegration_withSubnetId(data acceptance.TestData, planSku string) string {
func (r LinuxFunctionAppResource) vNetIntegration_subnet1(data acceptance.TestData, planSku string) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
Expand All @@ -3250,8 +3276,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 @@ -3266,12 +3292,91 @@ 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_function_app" "test" {
name = "acctest-LFA-%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
storage_account_name = azurerm_storage_account.test.name
storage_account_access_key = azurerm_storage_account.test.primary_access_key
site_config {}
}
`, r.template(data, planSku), data.RandomInteger, data.RandomInteger)
}

func (r LinuxFunctionAppResource) vNetIntegration_subnet2(data acceptance.TestData, planSku string) 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_function_app" "test" {
name = "acctest-LFA-%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
storage_account_name = azurerm_storage_account.test.name
storage_account_access_key = azurerm_storage_account.test.primary_access_key
Expand Down
20 changes: 17 additions & 3 deletions internal/services/appservice/linux_function_app_slot_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/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/appservice/validate"
kvValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/keyvault/validate"
networkValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/network/validate"
storageValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/storage/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
Expand Down Expand Up @@ -227,9 +228,9 @@ func (r LinuxFunctionAppSlotResource) Arguments() map[string]*pluginsdk.Schema {
"tags": tags.Schema(),

"virtual_network_subnet_id": {
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: networkValidate.SubnetID,
},
}
}
Expand Down Expand Up @@ -711,6 +712,19 @@ func (r LinuxFunctionAppSlotResource) Update() sdk.ResourceFunc {
existing.Tags = tags.FromTypedObject(state.Tags)
}

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)
}
}

storageString := state.StorageAccountName
if !state.StorageUsesMSI {
if state.StorageKeyVaultSecretID != "" {
Expand Down
Loading

0 comments on commit 520f6c7

Please sign in to comment.