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

Requiring Import (PR 1 of ...) #2511

Merged
merged 18 commits into from
Dec 14, 2018
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
447abec
scaffolding for feature-toggling/requiring resources to be imported
tombuildsstuff Dec 13, 2018
7342b0d
r/api_management: requiring resources to be imported
tombuildsstuff Dec 13, 2018
2147870
r/app_service: requiring that resources are imported
tombuildsstuff Dec 13, 2018
73b7358
r/app_service_custom_hostname_binding: requiring resources are imported
tombuildsstuff Dec 13, 2018
7c3b14c
making it possible to configure the feature flag via env var
tombuildsstuff Dec 13, 2018
de05f69
r/app_service_plan: requiring import
tombuildsstuff Dec 13, 2018
539a56a
r/app_service_slot: requiring import support
tombuildsstuff Dec 13, 2018
b945b17
r/application_gateway: requiring import
tombuildsstuff Dec 13, 2018
c36a2df
only checking for an existing resource creation for a new resource
tombuildsstuff Dec 13, 2018
2ba4f4e
r/application_insights: requiring import
tombuildsstuff Dec 13, 2018
07ffb93
r/application_security_group: requiring import
tombuildsstuff Dec 13, 2018
7b0e379
r/automation_account: requiring resources to be imported
tombuildsstuff Dec 13, 2018
5de879d
r/automation_credential: requiring import
tombuildsstuff Dec 13, 2018
1f51b6e
r/automation_dsc_credential: requiring import
tombuildsstuff Dec 13, 2018
b1a7e02
r/automation_dsc_nodeconfiguration: requiring import
tombuildsstuff Dec 13, 2018
4311a1a
r/automation_module: requiring import
tombuildsstuff Dec 13, 2018
9a3ce30
r/automation_runbook: requiring import
tombuildsstuff Dec 13, 2018
d8288c2
linting
tombuildsstuff Dec 14, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions azurerm/feature_flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package azurerm

import (
"os"
"strings"
)

// This file contains feature flags for functionality which will prove more challenging to implement en-mass
var requireResourcesToBeImported = strings.EqualFold(os.Getenv("ARM_PROVIDER_STRICT"), "true")
8 changes: 8 additions & 0 deletions azurerm/helpers/tf/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package tf

import "fmt"

func ImportAsExistsError(resourceName, id string) error {
msg := "A resource with the ID %q already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for %q for more information."
return fmt.Errorf(msg, id, resourceName)
}
6 changes: 6 additions & 0 deletions azurerm/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package azurerm
import (
"fmt"
"os"
"regexp"
"testing"

"github.com/Azure/go-autorest/autorest/azure"
Expand Down Expand Up @@ -98,3 +99,8 @@ func testGetAzureConfig(t *testing.T) *authentication.Config {

return config
}

func testRequiresImportError(resourceName string) *regexp.Regexp {
message := "to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for %q for more information."
return regexp.MustCompile(fmt.Sprintf(message, resourceName))
}
15 changes: 15 additions & 0 deletions azurerm/resource_arm_api_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"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/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand Down Expand Up @@ -297,6 +298,20 @@ func resourceArmApiManagementServiceCreateUpdate(d *schema.ResourceData, meta in

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)

if requireResourcesToBeImported && d.IsNewResource() {
existing, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("Error checking for presence of existing API Management Service %q (Resource Group %q): %s", name, resourceGroup, err)
}
}

if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_api_management", *existing.ID)
}
}

location := azureRMNormalizeLocation(d.Get("location").(string))
tags := d.Get("tags").(map[string]interface{})

Expand Down
51 changes: 50 additions & 1 deletion azurerm/resource_arm_api_management_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,35 @@ func TestAccAzureRMApiManagement_basic(t *testing.T) {
})
}

func TestAccAzureRMApiManagement_requiresImport(t *testing.T) {
if !requireResourcesToBeImported {
t.Skip("Skipping since resources aren't required to be imported")
return
}

resourceName := "azurerm_api_management.test"
ri := acctest.RandInt()
location := testLocation()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMApiManagementDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMApiManagement_basic(ri, location),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMApiManagementExists(resourceName),
),
},
{
Config: testAccAzureRMApiManagement_requiresImport(ri, location),
ExpectError: testRequiresImportError("azurerm_api_management"),
},
},
})
}

func TestAccAzureRMApiManagement_customProps(t *testing.T) {
resourceName := "azurerm_api_management.test"
ri := acctest.RandInt()
Expand Down Expand Up @@ -83,7 +112,7 @@ func TestAccAzureRMApiManagement_complete(t *testing.T) {
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"certificate", // not returned from API, sensitive
"certificate", // not returned from API, sensitive
"hostname_configuration.0.portal.0.certificate", // not returned from API, sensitive
"hostname_configuration.0.portal.0.certificate_password", // not returned from API, sensitive
"hostname_configuration.0.proxy.0.certificate", // not returned from API, sensitive
Expand Down Expand Up @@ -174,6 +203,26 @@ resource "azurerm_api_management" "test" {
`, rInt, location, rInt)
}

func testAccAzureRMApiManagement_requiresImport(rInt int, location string) string {
template := testAccAzureRMApiManagement_basic(rInt, location)
return fmt.Sprintf(`
%s

resource "azurerm_api_management" "import" {
name = "${azurerm_api_management.test.name}"
location = "${azurerm_api_management.test.location}"
resource_group_name = "${azurerm_api_management.test.resource_group_name}"
publisher_name = "${azurerm_api_management.test.publisher_name}"
publisher_email = "${azurerm_api_management.test.publisher_email}"

sku {
name = "Developer"
capacity = 1
}
}
`, template)
}

func testAccAzureRMApiManagement_customProps(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
Expand Down
17 changes: 16 additions & 1 deletion azurerm/resource_arm_app_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"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/utils"
)

Expand Down Expand Up @@ -188,6 +189,21 @@ func resourceArmAppServiceCreate(d *schema.ResourceData, meta interface{}) error
log.Printf("[INFO] preparing arguments for AzureRM App Service creation.")

name := d.Get("name").(string)
resGroup := d.Get("resource_group_name").(string)

if requireResourcesToBeImported && d.IsNewResource() {
existing, err := client.Get(ctx, resGroup, name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("Error checking for presence of existing App Service %q (Resource Group %q): %s", name, resGroup, err)
}
}

if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_app_service", *existing.ID)
}
}

availabilityRequest := web.ResourceNameAvailabilityRequest{
Name: utils.String(name),
Type: web.CheckNameResourceTypesMicrosoftWebsites,
Expand All @@ -201,7 +217,6 @@ func resourceArmAppServiceCreate(d *schema.ResourceData, meta interface{}) error
return fmt.Errorf("The name %q used for the App Service needs to be globally unique and isn't available: %s", name, *available.Message)
}

resGroup := d.Get("resource_group_name").(string)
location := azureRMNormalizeLocation(d.Get("location").(string))
appServicePlanId := d.Get("app_service_plan_id").(string)
enabled := d.Get("enabled").(bool)
Expand Down
14 changes: 14 additions & 0 deletions azurerm/resource_arm_app_service_custom_hostname_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/Azure/azure-sdk-for-go/services/web/mgmt/2018-02-01/web"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

Expand Down Expand Up @@ -51,6 +52,19 @@ func resourceArmAppServiceCustomHostnameBindingCreate(d *schema.ResourceData, me
azureRMLockByName(appServiceName, appServiceCustomHostnameBindingResourceName)
defer azureRMUnlockByName(appServiceName, appServiceCustomHostnameBindingResourceName)

if requireResourcesToBeImported && d.IsNewResource() {
existing, err := client.GetHostNameBinding(ctx, resourceGroup, appServiceName, hostname)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("Error checking for presence of existing Custom Hostname Binding %q (App Service %q / Resource Group %q): %s", hostname, appServiceName, resourceGroup, err)
}
}

if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_app_service_custom_hostname_binding", *existing.ID)
}
}

properties := web.HostNameBinding{
HostNameBindingProperties: &web.HostNameBindingProperties{
SiteName: utils.String(appServiceName),
Expand Down
89 changes: 60 additions & 29 deletions azurerm/resource_arm_app_service_custom_hostname_binding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,25 @@ import (
)

func TestAccAzureRMAppServiceCustomHostnameBinding(t *testing.T) {
appServiceEnvVariable := "ARM_TEST_APP_SERVICE"
appServiceEnv := os.Getenv(appServiceEnvVariable)
if appServiceEnv == "" {
t.Skipf("Skipping as %q is not specified", appServiceEnvVariable)
}

domainEnvVariable := "ARM_TEST_DOMAIN"
domainEnv := os.Getenv(domainEnvVariable)
if domainEnv == "" {
t.Skipf("Skipping as %q is not specified", domainEnvVariable)
}

// NOTE: this is a combined test rather than separate split out tests due to
// the app service name being shared (so the tests don't conflict with each other)
testCases := map[string]map[string]func(t *testing.T){
testCases := map[string]map[string]func(t *testing.T, appServiceEnv, domainEnv string){
"basic": {
"basic": testAccAzureRMAppServiceCustomHostnameBinding_basic,
"multiple": testAccAzureRMAppServiceCustomHostnameBinding_multiple,
"basic": testAccAzureRMAppServiceCustomHostnameBinding_basic,
"multiple": testAccAzureRMAppServiceCustomHostnameBinding_multiple,
"requiresImport": testAccAzureRMAppServiceCustomHostnameBinding_requiresImport,
},
}

Expand All @@ -28,26 +41,14 @@ func TestAccAzureRMAppServiceCustomHostnameBinding(t *testing.T) {
for name, tc := range m {
tc := tc
t.Run(name, func(t *testing.T) {
tc(t)
tc(t, appServiceEnv, domainEnv)
})
}
})
}
}

func testAccAzureRMAppServiceCustomHostnameBinding_basic(t *testing.T) {
appServiceEnvVariable := "ARM_TEST_APP_SERVICE"
appServiceEnv := os.Getenv(appServiceEnvVariable)
if appServiceEnv == "" {
t.Skipf("Skipping as %q is not specified", appServiceEnvVariable)
}

domainEnvVariable := "ARM_TEST_DOMAIN"
domainEnv := os.Getenv(domainEnvVariable)
if domainEnv == "" {
t.Skipf("Skipping as %q is not specified", domainEnvVariable)
}

func testAccAzureRMAppServiceCustomHostnameBinding_basic(t *testing.T, appServiceEnv, domainEnv string) {
resourceName := "azurerm_app_service_custom_hostname_binding.test"
ri := acctest.RandInt()
location := testLocation()
Expand All @@ -73,23 +74,40 @@ func testAccAzureRMAppServiceCustomHostnameBinding_basic(t *testing.T) {
})
}

func testAccAzureRMAppServiceCustomHostnameBinding_multiple(t *testing.T) {
appServiceEnvVariable := "ARM_TEST_APP_SERVICE"
appServiceEnv := os.Getenv(appServiceEnvVariable)
if appServiceEnv == "" {
t.Skipf("Skipping as %q is not specified", appServiceEnvVariable)
func testAccAzureRMAppServiceCustomHostnameBinding_requiresImport(t *testing.T, appServiceEnv, domainEnv string) {
if !requireResourcesToBeImported {
t.Skip("Skipping since resources aren't required to be imported")
return
}

domainEnvVariable := "ARM_TEST_DOMAIN"
domainEnv := os.Getenv(domainEnvVariable)
if domainEnv == "" {
t.Skipf("Skipping as %q is not specified", domainEnvVariable)
}
resourceName := "azurerm_app_service_custom_hostname_binding.test"
ri := acctest.RandInt()
location := testLocation()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMAppServiceCustomHostnameBindingDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMAppServiceCustomHostnameBinding_basicConfig(ri, location, appServiceEnv, domainEnv),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMAppServiceCustomHostnameBindingExists(resourceName),
),
},
{
Config: testAccAzureRMAppServiceCustomHostnameBinding_requiresImportConfig(ri, location, appServiceEnv, domainEnv),
ExpectError: testRequiresImportError("azurerm_app_service_custom_hostname_binding"),
},
},
})
}

func testAccAzureRMAppServiceCustomHostnameBinding_multiple(t *testing.T, appServiceEnv, domainEnv string) {
altDomainEnvVariable := "ARM_ALT_TEST_DOMAIN"
altDomainEnv := os.Getenv(altDomainEnvVariable)
if domainEnv == "" {
t.Skipf("Skipping as %q is not specified", domainEnvVariable)
if altDomainEnv == "" {
t.Skipf("Skipping as %q is not specified", altDomainEnvVariable)
}

resourceName := "azurerm_app_service_custom_hostname_binding.test"
Expand Down Expand Up @@ -200,6 +218,19 @@ resource "azurerm_app_service_custom_hostname_binding" "test" {
`, rInt, location, rInt, appServiceName, domain)
}

func testAccAzureRMAppServiceCustomHostnameBinding_requiresImportConfig(rInt int, location string, appServiceName string, domain string) string {
template := testAccAzureRMAppServiceCustomHostnameBinding_basicConfig(rInt, location, appServiceName, domain)
return fmt.Sprintf(`
%s

resource "azurerm_app_service_custom_hostname_binding" "import" {
hostname = "${azurerm_app_service_custom_hostname_binding.test.name}"
app_service_name = "${azurerm_app_service_custom_hostname_binding.test.app_service_name}"
resource_group_name = "${azurerm_app_service_custom_hostname_binding.test.resource_group_name}"
}
`, template)
}

func testAccAzureRMAppServiceCustomHostnameBinding_multipleConfig(rInt int, location, appServiceName, domain, altDomain string) string {
template := testAccAzureRMAppServiceCustomHostnameBinding_basicConfig(rInt, location, appServiceName, domain)
return fmt.Sprintf(`
Expand Down
15 changes: 15 additions & 0 deletions azurerm/resource_arm_app_service_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"regexp"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/suppress"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"

"github.com/Azure/azure-sdk-for-go/services/web/mgmt/2018-02-01/web"
"github.com/hashicorp/terraform/helper/schema"
Expand Down Expand Up @@ -151,6 +152,20 @@ func resourceArmAppServicePlanCreateUpdate(d *schema.ResourceData, meta interfac

resGroup := d.Get("resource_group_name").(string)
name := d.Get("name").(string)

if requireResourcesToBeImported && d.IsNewResource() {
existing, err := client.Get(ctx, resGroup, name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("Error checking for presence of existing App Service Plan %q (Resource Group %q): %s", name, resGroup, err)
}
}

if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_app_service_plan", *existing.ID)
}
}

location := azureRMNormalizeLocation(d.Get("location").(string))
kind := d.Get("kind").(string)
tags := d.Get("tags").(map[string]interface{})
Expand Down
Loading