Skip to content

Commit

Permalink
Merge pull request #2664 from terraform-providers/f/requiring-imports
Browse files Browse the repository at this point in the history
Requiring Imports (PR 5 of ...)
  • Loading branch information
tombuildsstuff authored Jan 17, 2019
2 parents 3b32b8b + a563394 commit 6af93a3
Show file tree
Hide file tree
Showing 10 changed files with 417 additions and 38 deletions.
17 changes: 16 additions & 1 deletion azurerm/resource_arm_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"

"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-06-01/compute"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"

"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
Expand Down Expand Up @@ -164,8 +165,22 @@ func resourceArmImageCreateUpdate(d *schema.ResourceData, meta interface{}) erro
log.Printf("[INFO] preparing arguments for AzureRM Image creation.")

name := d.Get("name").(string)
location := azureRMNormalizeLocation(d.Get("location").(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 Image %q (Resource Group %q): %s", name, resGroup, err)
}
}

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

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

properties := compute.ImageProperties{}
Expand Down
69 changes: 69 additions & 0 deletions azurerm/resource_arm_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,48 @@ func TestAccAzureRMImage_standaloneImage(t *testing.T) {
})
}

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

ri := tf.AccRandTimeInt()
resourceGroup := fmt.Sprintf("acctestRG-%d", ri)
userName := "testadmin"
password := "Password1234!"
hostName := fmt.Sprintf("tftestcustomimagesrc%d", ri)
sshPort := "22"
location := testLocation()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMImageDestroy,
Steps: []resource.TestStep{
{
//need to create a vm and then reference it in the image creation
Config: testAccAzureRMImage_standaloneImage_setup(ri, userName, password, hostName, location),
Destroy: false,
Check: resource.ComposeTestCheckFunc(
testCheckAzureVMExists("azurerm_virtual_machine.testsource", true),
testGeneralizeVMImage(resourceGroup, "testsource", userName, password, hostName, sshPort, location),
),
},
{
Config: testAccAzureRMImage_standaloneImage_provision(ri, userName, password, hostName, location),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMImageExists("azurerm_image.test", true),
),
},
{
Config: testAccAzureRMImage_standaloneImage_requiresImport(ri, userName, password, hostName, location),
ExpectError: testRequiresImportError("azurerm_image"),
},
},
})
}

func TestAccAzureRMImage_customImageVMFromVHD(t *testing.T) {
ri := tf.AccRandTimeInt()
resourceGroup := fmt.Sprintf("acctestRG-%d", ri)
Expand Down Expand Up @@ -575,6 +617,33 @@ resource "azurerm_image" "test" {
`, rInt, location, rInt, rInt, rInt, hostName, rInt, rInt, userName, password)
}

func testAccAzureRMImage_standaloneImage_requiresImport(rInt int, userName string, password string, hostName string, location string) string {
template := testAccAzureRMImage_standaloneImage_provision(rInt, userName, password, hostName, location)
return fmt.Sprintf(`
%s
resource "azurerm_image" "import" {
name = "${azurerm_image.test.name}"
location = "${azurerm_image.test.location}"
resource_group_name = "${azurerm_image.test.resource_group_name}"
os_disk {
os_type = "Linux"
os_state = "Generalized"
blob_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd"
size_gb = 30
caching = "None"
}
tags {
environment = "Dev"
cost-center = "Ops"
}
}
`, template)
}

func testAccAzureRMImage_customImage_fromVHD_setup(rInt int, userName string, password string, hostName string, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
Expand Down
16 changes: 15 additions & 1 deletion azurerm/resource_arm_iothub.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/response"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/suppress"
"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 @@ -260,10 +261,23 @@ func resourceArmIotHubCreateUpdate(d *schema.ResourceData, meta interface{}) err

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 IoTHub %q (Resource Group %q): %s", name, resourceGroup, err)
}
}

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

res, err := client.CheckNameAvailability(ctx, devices.OperationInputs{
Name: &name,
})

if err != nil {
return fmt.Errorf("An error occurred checking if the IoTHub name was unique: %+v", err)
}
Expand Down
25 changes: 20 additions & 5 deletions azurerm/resource_arm_iothub_consumer_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"

"github.com/hashicorp/terraform/helper/schema"
"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 @@ -54,17 +55,30 @@ func resourceArmIotHubConsumerGroupCreate(d *schema.ResourceData, meta interface
endpointName := d.Get("eventhub_endpoint_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 Consumer Group %q (Endpoint %q / IoTHub %q / Resource Group %q): %s", name, endpointName, iotHubName, resourceGroup, err)
}
}

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

if _, err := client.CreateEventHubConsumerGroup(ctx, resourceGroup, iotHubName, endpointName, name); err != nil {
return err
return fmt.Errorf("Error creating Consumer Group %q (Endpoint %q / IoTHub %q / Resource Group %q): %+v", name, endpointName, iotHubName, resourceGroup, err)
}

read, err := client.GetEventHubConsumerGroup(ctx, resourceGroup, iotHubName, endpointName, name)
if err != nil {
return err
return fmt.Errorf("Error retrieving Consumer Group %q (Endpoint %q / IoTHub %q / Resource Group %q): %+v", name, endpointName, iotHubName, resourceGroup, err)
}

if read.ID == nil {
return fmt.Errorf("Cannot read IoTHub Consumer Group %q (Resource Group %q) ID", name, resourceGroup)
return fmt.Errorf("Cannot read ID for Consumer Group %q (Endpoint %q / IoTHub %q / Resource Group %q): %+v", name, endpointName, iotHubName, resourceGroup, err)
}

d.SetId(*read.ID)
Expand All @@ -91,7 +105,8 @@ func resourceArmIotHubConsumerGroupRead(d *schema.ResourceData, meta interface{}
d.SetId("")
return nil
}
return fmt.Errorf("Error making Read request on IoTHub Consumer Group %s: %+v", name, err)

return fmt.Errorf("Error making read request for Consumer Group %q (Endpoint %q / IoTHub %q / Resource Group %q): %+v", name, endpointName, iotHubName, resourceGroup, err)
}

d.Set("name", name)
Expand Down Expand Up @@ -119,7 +134,7 @@ func resourceArmIotHubConsumerGroupDelete(d *schema.ResourceData, meta interface

if err != nil {
if !utils.ResponseWasNotFound(resp) {
return fmt.Errorf("Error issuing delete request for IoTHub Consumer Group %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("Error deleting Consumer Group %q (Endpoint %q / IoTHub %q / Resource Group %q): %+v", name, endpointName, iotHubName, resourceGroup, err)
}
}

Expand Down
55 changes: 50 additions & 5 deletions azurerm/resource_arm_iothub_consumer_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ func TestAccAzureRMIotHubConsumerGroup_events(t *testing.T) {
testCheckAzureRMIotHubConsumerGroupExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "eventhub_endpoint_name", "events"),
),
}, {
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
Expand All @@ -35,6 +36,36 @@ func TestAccAzureRMIotHubConsumerGroup_events(t *testing.T) {
})
}

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

resourceName := "azurerm_iothub_consumer_group.test"
rInt := tf.AccRandTimeInt()
location := testLocation()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMIotHubConsumerGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMIotHubConsumerGroup_basic(rInt, location, "events"),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMIotHubConsumerGroupExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "eventhub_endpoint_name", "events"),
),
},
{
Config: testAccAzureRMIotHubConsumerGroup_requiresImport(rInt, location, "events"),
ExpectError: testRequiresImportError("azurerm_iothub_consumer_group"),
},
},
})
}

func TestAccAzureRMIotHubConsumerGroup_operationsMonitoringEvents(t *testing.T) {
resourceName := "azurerm_iothub_consumer_group.test"
rInt := tf.AccRandTimeInt()
Expand Down Expand Up @@ -118,15 +149,15 @@ func testCheckAzureRMIotHubConsumerGroupExists(resourceName string) resource.Tes

func testAccAzureRMIotHubConsumerGroup_basic(rInt int, location, eventName string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "foo" {
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}
resource "azurerm_iothub" "test" {
name = "acctestIoTHub-%d"
resource_group_name = "${azurerm_resource_group.foo.name}"
location = "${azurerm_resource_group.foo.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "${azurerm_resource_group.test.location}"
sku {
name = "B1"
Expand All @@ -143,7 +174,21 @@ resource "azurerm_iothub_consumer_group" "test" {
name = "test"
iothub_name = "${azurerm_iothub.test.name}"
eventhub_endpoint_name = "%s"
resource_group_name = "${azurerm_resource_group.foo.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
}
`, rInt, location, rInt, eventName)
}

func testAccAzureRMIotHubConsumerGroup_requiresImport(rInt int, location, eventName string) string {
template := testAccAzureRMIotHubConsumerGroup_basic(rInt, location, eventName)
return fmt.Sprintf(`
%s
resource "azurerm_iothub_consumer_group" "import" {
name = "${azurerm_iothub_consumer_group.test.name}"
iothub_name = "${azurerm_iothub_consumer_group.test.iothub_name}"
eventhub_endpoint_name = "${azurerm_iothub_consumer_group.test.eventhub_endpoint_name}"
resource_group_name = "${azurerm_iothub_consumer_group.test.resource_group_name}"
}
`, template)
}
Loading

0 comments on commit 6af93a3

Please sign in to comment.