Skip to content

Commit

Permalink
Merge pull request #8111 from njuCZ/issue_8048
Browse files Browse the repository at this point in the history
add doc and test to support function app slot vnet connetion - "azurerm_app_service_slot_virtual_network_swift_connection"
  • Loading branch information
tombuildsstuff authored Aug 24, 2020
2 parents f3f4795 + b5a902b commit 4803db4
Show file tree
Hide file tree
Showing 3 changed files with 226 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (

"github.com/Azure/azure-sdk-for-go/services/web/mgmt/2019-08-01/web"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/locks"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/network"
subnetParse "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/network/parse"
networkValidate "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/network/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/web/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/web/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
Expand Down Expand Up @@ -40,12 +40,12 @@ func resourceArmAppServiceSlotVirtualNetworkSwiftConnection() *schema.Resource {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateResourceID,
ValidateFunc: validate.AppServiceID,
},
"subnet_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: azure.ValidateResourceID,
ValidateFunc: networkValidate.SubnetID,
},
"slot_name": {
Type: schema.TypeString,
Expand All @@ -64,12 +64,11 @@ func resourceArmAppServiceSlotVirtualNetworkSwiftConnectionCreateUpdate(d *schem

appID, err := parse.AppServiceID(d.Get("app_service_id").(string))
if err != nil {
return fmt.Errorf("Error parsing Azure Resource ID %q", appID)
return fmt.Errorf("parsing app service ID %+v", err)
}

subnetID, err := subnetParse.SubnetID(d.Get("subnet_id").(string))
if err != nil {
return fmt.Errorf("Error parsing Azure Resource ID %q", subnetID)
return fmt.Errorf("parsing subnet ID %+v", err)
}

resourceGroup := appID.ResourceGroup
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
package tests

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance"
)

func TestAccAzureRMFunctionAppSlotVirtualNetworkSwiftConnection_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_app_service_slot_virtual_network_swift_connection", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMAppServiceSlotVirtualNetworkSwiftConnectionDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMFunctionAppSlotVirtualNetworkSwiftConnection_basic(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMAppServiceSlotVirtualNetworkSwiftConnectionExists(data.ResourceName),
),
},
data.ImportStep(),
},
})
}

func TestAccAzureRMFunctionAppSlotVirtualNetworkSwiftConnection_requiresImport(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_app_service_slot_virtual_network_swift_connection", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMAppServiceSlotVirtualNetworkSwiftConnectionDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMFunctionAppSlotVirtualNetworkSwiftConnection_basic(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMAppServiceVirtualNetworkSwiftConnectionExists(data.ResourceName),
),
},
data.RequiresImportErrorStep(testAccAzureRMFunctionAppSlotVirtualNetworkSwiftConnection_requiresImport),
},
})
}

func TestAccAzureRMFunctionAppSlotVirtualNetworkSwiftConnection_update(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_app_service_slot_virtual_network_swift_connection", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMAppServiceSlotVirtualNetworkSwiftConnectionDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMFunctionAppSlotVirtualNetworkSwiftConnection_basic(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMAppServiceSlotVirtualNetworkSwiftConnectionExists(data.ResourceName),
),
},
{
Config: testAccAzureRMFunctionAppSlotVirtualNetworkSwiftConnection_update(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMAppServiceSlotVirtualNetworkSwiftConnectionExists(data.ResourceName),
),
},
},
})
}

func TestAccAzureRMFunctionAppSlotVirtualNetworkSwiftConnection_disappears(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_app_service_slot_virtual_network_swift_connection", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMAppServiceSlotVirtualNetworkSwiftConnectionDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMAppServiceSlotVirtualNetworkSwiftConnection_basic(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMAppServiceSlotVirtualNetworkSwiftConnectionExists(data.ResourceName),
testCheckAzureRMAppServiceSlotVirtualNetworkSwiftConnectionDisappears(data.ResourceName),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func testAccAzureRMFunctionAppSlotVirtualNetworkSwiftConnection_base(data acceptance.TestData) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-functionapp-%d"
location = "%s"
}
resource "azurerm_virtual_network" "test" {
name = "acctest-VNET-%d"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
lifecycle {
ignore_changes = ["ddos_protection_plan"]
}
}
resource "azurerm_subnet" "test1" {
name = "acctestSubnet1"
resource_group_name = azurerm_resource_group.test.name
virtual_network_name = azurerm_virtual_network.test.name
address_prefix = "10.0.1.0/24"
delegation {
name = "acctestdelegation"
service_delegation {
name = "Microsoft.Web/serverFarms"
actions = ["Microsoft.Network/virtualNetworks/subnets/action"]
}
}
}
resource "azurerm_subnet" "test2" {
name = "acctestSubnet2"
resource_group_name = azurerm_resource_group.test.name
virtual_network_name = azurerm_virtual_network.test.name
address_prefix = "10.0.2.0/24"
delegation {
name = "acctestdelegation"
service_delegation {
name = "Microsoft.Web/serverFarms"
actions = ["Microsoft.Network/virtualNetworks/subnets/action"]
}
}
}
resource "azurerm_storage_account" "test" {
name = "acctestsa%s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
account_tier = "Standard"
account_replication_type = "LRS"
}
resource "azurerm_app_service_plan" "test" {
name = "acctest-ASP-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
sku {
tier = "Standard"
size = "S1"
}
}
resource "azurerm_function_app" "test" {
name = "acctest-FA-%d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
app_service_plan_id = azurerm_app_service_plan.test.id
storage_account_name = azurerm_storage_account.test.name
storage_account_access_key = azurerm_storage_account.test.primary_access_key
}
resource "azurerm_function_app_slot" "test-staging" {
name = "acctest-FA-%d-staging"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
app_service_plan_id = azurerm_app_service_plan.test.id
function_app_name = azurerm_function_app.test.name
storage_account_name = azurerm_storage_account.test.name
storage_account_access_key = azurerm_storage_account.test.primary_access_key
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomString, data.RandomInteger, data.RandomInteger, data.RandomInteger)
}

func testAccAzureRMFunctionAppSlotVirtualNetworkSwiftConnection_basic(data acceptance.TestData) string {
template := testAccAzureRMFunctionAppSlotVirtualNetworkSwiftConnection_base(data)
return fmt.Sprintf(`
%s
resource "azurerm_app_service_slot_virtual_network_swift_connection" "test" {
slot_name = azurerm_function_app_slot.test-staging.name
app_service_id = azurerm_function_app.test.id
subnet_id = azurerm_subnet.test1.id
}
`, template)
}

func testAccAzureRMFunctionAppSlotVirtualNetworkSwiftConnection_update(data acceptance.TestData) string {
template := testAccAzureRMFunctionAppSlotVirtualNetworkSwiftConnection_base(data)
return fmt.Sprintf(`
%s
resource "azurerm_app_service_slot_virtual_network_swift_connection" "test" {
slot_name = azurerm_function_app_slot.test-staging.name
app_service_id = azurerm_function_app.test.id
subnet_id = azurerm_subnet.test2.id
}
`, template)
}

func testAccAzureRMFunctionAppSlotVirtualNetworkSwiftConnection_requiresImport(data acceptance.TestData) string {
template := testAccAzureRMFunctionAppSlotVirtualNetworkSwiftConnection_basic(data)
return fmt.Sprintf(`
%s
resource "azurerm_app_service_slot_virtual_network_swift_connection" "import" {
slot_name = azurerm_app_service_slot_virtual_network_swift_connection.test.slot_name
app_service_id = azurerm_app_service_slot_virtual_network_swift_connection.test.app_service_id
subnet_id = azurerm_app_service_slot_virtual_network_swift_connection.test.subnet_id
}
`, template)
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ resource "azurerm_app_service_slot_virtual_network_swift_connection" "example" {

The following arguments are supported:

* `app_service_id` - (Required) The ID of the App Service to associate to the VNet. Changing this forces a new resource to be created.
* `app_service_id` - (Required) The ID of the App Service or Function App to associate to the VNet. Changing this forces a new resource to be created.

* `slot_name` - (Required) The name of the App Service Slot. Changing this forces a new resource to be created.
* `slot_name` - (Required) The name of the App Service Slot or Function App Slot. Changing this forces a new resource to be created.

* `subnet_id` - (Required) The ID of the subnet the app service will be associated to (the subnet must have a `service_delegation` configured for `Microsoft.Web/serverFarms`).

Expand Down

0 comments on commit 4803db4

Please sign in to comment.