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

azurerm_recovery_services_protected_vm can now be used when VM's are in a different resource group #2287

Merged
merged 2 commits into from
Nov 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions azurerm/resource_arm_recovery_services_protected_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ func resourceArmRecoveryServicesProtectedVmCreateUpdate(d *schema.ResourceData,
return fmt.Errorf("[ERROR] parsed source_vm_id '%s' doesn't contain 'virtualMachines'", vmId)
}

protectedItemName := fmt.Sprintf("VM;iaasvmcontainerv2;%s;%s", resourceGroup, vmName)
containerName := fmt.Sprintf("iaasvmcontainer;iaasvmcontainerv2;%s;%s", resourceGroup, vmName)
protectedItemName := fmt.Sprintf("VM;iaasvmcontainerv2;%s;%s", parsedVmId.ResourceGroup, vmName)
containerName := fmt.Sprintf("iaasvmcontainer;iaasvmcontainerv2;%s;%s", parsedVmId.ResourceGroup, vmName)

log.Printf("[DEBUG] Creating/updating Recovery Service Protected VM %s (resource group %q)", protectedItemName, resourceGroup)

Expand Down
96 changes: 91 additions & 5 deletions azurerm/resource_arm_recovery_services_protected_vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,35 @@ func TestAccAzureRMRecoveryServicesProtectedVm_basic(t *testing.T) {
})
}

func TestAccAzureRMRecoveryServicesProtectedVm_separateResourceGroups(t *testing.T) {
resourceName := "azurerm_recovery_services_protected_vm.test"
ri := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMRecoveryServicesProtectedVmDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMRecoveryServicesProtectedVm_separateResourceGroups(ri, testLocation()),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMRecoveryServicesProtectedVmExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "resource_group_name"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{ //vault cannot be deleted unless we unregister all backups
Config: testAccAzureRMRecoveryServicesProtectedVm_additionalVault(ri, testLocation()),
Check: resource.ComposeTestCheckFunc(),
},
},
})
}

func testCheckAzureRMRecoveryServicesProtectedVmDestroy(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "azurerm_recovery_services_protected_vm" {
Expand All @@ -49,10 +78,19 @@ func testCheckAzureRMRecoveryServicesProtectedVmDestroy(s *terraform.State) erro

resourceGroup := rs.Primary.Attributes["resource_group_name"]
vaultName := rs.Primary.Attributes["recovery_vault_name"]
vmName := rs.Primary.Attributes["source_vm_name"]
vmId := rs.Primary.Attributes["source_vm_id"]

parsedVmId, err := azure.ParseAzureResourceID(vmId)
if err != nil {
return fmt.Errorf("[ERROR] Unable to parse source_vm_id '%s': %+v", vmId, err)
}
vmName, hasName := parsedVmId.Path["virtualMachines"]
if !hasName {
return fmt.Errorf("[ERROR] parsed source_vm_id '%s' doesn't contain 'virtualMachines'", vmId)
}

protectedItemName := fmt.Sprintf("VM;iaasvmcontainerv2;%s;%s", resourceGroup, vmName)
containerName := fmt.Sprintf("iaasvmcontainer;iaasvmcontainerv2;%s;%s", resourceGroup, vmName)
protectedItemName := fmt.Sprintf("VM;iaasvmcontainerv2;%s;%s", parsedVmId.ResourceGroup, vmName)
containerName := fmt.Sprintf("iaasvmcontainer;iaasvmcontainerv2;%s;%s", parsedVmId.ResourceGroup, vmName)

client := testAccProvider.Meta().(*ArmClient).recoveryServicesProtectedItemsClient
ctx := testAccProvider.Meta().(*ArmClient).StopContext
Expand Down Expand Up @@ -98,8 +136,8 @@ func testCheckAzureRMRecoveryServicesProtectedVmExists(resourceName string) reso
return fmt.Errorf("[ERROR] parsed source_vm_id '%s' doesn't contain 'virtualMachines'", vmId)
}

protectedItemName := fmt.Sprintf("VM;iaasvmcontainerv2;%s;%s", resourceGroup, vmName)
containerName := fmt.Sprintf("iaasvmcontainer;iaasvmcontainerv2;%s;%s", resourceGroup, vmName)
protectedItemName := fmt.Sprintf("VM;iaasvmcontainerv2;%s;%s", parsedVmId.ResourceGroup, vmName)
containerName := fmt.Sprintf("iaasvmcontainer;iaasvmcontainerv2;%s;%s", parsedVmId.ResourceGroup, vmName)

client := testAccProvider.Meta().(*ArmClient).recoveryServicesProtectedItemsClient
ctx := testAccProvider.Meta().(*ArmClient).StopContext
Expand Down Expand Up @@ -259,3 +297,51 @@ resource "azurerm_recovery_services_protected_vm" "test" {

`, testAccAzureRMRecoveryServicesProtectedVm_base(rInt, location))
}

func testAccAzureRMRecoveryServicesProtectedVm_additionalVault(rInt int, location string) string {
return fmt.Sprintf(`
%[1]s

resource "azurerm_resource_group" "test2" {
name = "acctestRG2-%[2]d"
location = "%[3]s"
}

resource "azurerm_recovery_services_vault" "test2" {
name = "acctest2-%[2]d"
location = "${azurerm_resource_group.test2.location}"
resource_group_name = "${azurerm_resource_group.test2.name}"
sku = "Standard"
}

resource "azurerm_recovery_services_protection_policy_vm" "test2" {
name = "acctest2-%[2]d"
resource_group_name = "${azurerm_resource_group.test2.name}"
recovery_vault_name = "${azurerm_recovery_services_vault.test2.name}"

backup = {
frequency = "Daily"
time = "23:00"
}

retention_daily = {
count = 10
}
}

`, testAccAzureRMRecoveryServicesProtectedVm_base(rInt, location), rInt, location)
}

func testAccAzureRMRecoveryServicesProtectedVm_separateResourceGroups(rInt int, location string) string {
return fmt.Sprintf(`
%s

resource "azurerm_recovery_services_protected_vm" "test" {
resource_group_name = "${azurerm_resource_group.test2.name}"
recovery_vault_name = "${azurerm_recovery_services_vault.test2.name}"
backup_policy_id = "${azurerm_recovery_services_protection_policy_vm.test2.id}"
source_vm_id = "${azurerm_virtual_machine.test.id}"
}

`, testAccAzureRMRecoveryServicesProtectedVm_additionalVault(rInt, location))
}
2 changes: 0 additions & 2 deletions website/docs/r/recovery_services_protected_vm.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ resource "azurerm_recovery_services_protected_vm" "example" {

The following arguments are supported:

* `location` - (Required) Specifies the supported Azure location where the resource exists. Changing this forces a new resource to be created.

* `resource_group_name` - (Required) The name of the resource group in which to create the Recovery Services Protected VM. Changing this forces a new resource to be created.

* `recovery_vault_name` - (Required) Specifies the name of the Recovery Services Vault to use. Changing this forces a new resource to be created.
Expand Down