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_protection_policy_vm: add timezone property #2404

Merged
merged 2 commits into from
Nov 30, 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
6 changes: 3 additions & 3 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ import (
"github.com/Azure/azure-sdk-for-go/services/preview/security/mgmt/2017-08-01-preview/security"
"github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/2015-05-01-preview/sql"
MsSql "github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/2017-10-01-preview/sql"
"github.com/Azure/azure-sdk-for-go/services/recoveryservices/mgmt/2016-06-01/backup"
"github.com/Azure/azure-sdk-for-go/services/recoveryservices/mgmt/2016-06-01/recoveryservices"
"github.com/Azure/azure-sdk-for-go/services/recoveryservices/mgmt/2017-07-01/backup"
"github.com/Azure/azure-sdk-for-go/services/redis/mgmt/2018-03-01/redis"
"github.com/Azure/azure-sdk-for-go/services/relay/mgmt/2017-04-01/relay"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2016-06-01/subscriptions"
Expand Down Expand Up @@ -256,7 +256,7 @@ type ArmClient struct {

// Recovery Services
recoveryServicesVaultsClient recoveryservices.VaultsClient
recoveryServicesProtectedItemsClient backup.ProtectedItemsClient
recoveryServicesProtectedItemsClient backup.ProtectedItemsGroupClient
recoveryServicesProtectionPoliciesClient backup.ProtectionPoliciesClient

// Relay
Expand Down Expand Up @@ -963,7 +963,7 @@ func (c *ArmClient) registerRecoveryServiceClients(endpoint, subscriptionId stri
c.configureClient(&vaultsClient.Client, auth)
c.recoveryServicesVaultsClient = vaultsClient

protectedItemsClient := backup.NewProtectedItemsClientWithBaseURI(endpoint, subscriptionId)
protectedItemsClient := backup.NewProtectedItemsGroupClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&protectedItemsClient.Client, auth)
c.recoveryServicesProtectedItemsClient = protectedItemsClient

Expand Down
9 changes: 5 additions & 4 deletions azurerm/resource_arm_recovery_services_protected_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/services/recoveryservices/mgmt/2016-06-01/backup"
"github.com/Azure/azure-sdk-for-go/services/recoveryservices/mgmt/2017-07-01/backup"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
Expand Down Expand Up @@ -92,7 +92,7 @@ func resourceArmRecoveryServicesProtectedVmCreateUpdate(d *schema.ResourceData,
Properties: &backup.AzureIaaSComputeVMProtectedItem{
PolicyID: &policyId,
ProtectedItemType: backup.ProtectedItemTypeMicrosoftClassicComputevirtualMachines,
WorkloadType: backup.VM,
WorkloadType: backup.DataSourceTypeVM,
SourceResourceID: utils.String(vmId),
FriendlyName: utils.String(vmName),
VirtualMachineID: utils.String(vmId),
Expand Down Expand Up @@ -187,7 +187,7 @@ func resourceArmRecoveryServicesProtectedVmDelete(d *schema.ResourceData, meta i
return nil
}

func resourceArmRecoveryServicesProtectedVmWaitForState(client backup.ProtectedItemsClient, ctx context.Context, found bool, vaultName, resourceGroup, containerName, protectedItemName string) (backup.ProtectedItemResource, error) {
func resourceArmRecoveryServicesProtectedVmWaitForState(client backup.ProtectedItemsGroupClient, ctx context.Context, found bool, vaultName, resourceGroup, containerName, protectedItemName string) (backup.ProtectedItemResource, error) {
state := &resource.StateChangeConf{
Timeout: 30 * time.Minute,
MinTimeout: 30 * time.Second,
Expand Down Expand Up @@ -217,7 +217,8 @@ func resourceArmRecoveryServicesProtectedVmWaitForState(client backup.ProtectedI

resp, err := state.WaitForState()
if err != nil {
return resp.(backup.ProtectedItemResource), fmt.Errorf("Error waiting for the Recovery Service Protected VM %q to be %t (Resource Group %q) to provision: %+v", protectedItemName, found, resourceGroup, err)
i, _ := resp.(backup.ProtectedItemResource)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imo we should be raising this error, since i won't be valid if it's non-nil?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am raising it as part of the errorf? the cast is done like this because sometimes resp ends up being nil 🤷‍♀️

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, I was referring to the casting error that's a _?

Copy link
Collaborator Author

@katbyte katbyte Nov 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the reasoning behind this is if resp is nil

i := resp.(backup.ProtectedItemResource)

fails.

however if you use the ok cast, even if its _ for unused

i, _ := resp.(backup.ProtectedItemResource)

the cast doesn't not fail on a nil value and I becomes the zero value for (backup.ProtectedItemResource): https://play.golang.org/p/cDZrRXP6C27

return i, fmt.Errorf("Error waiting for the Recovery Service Protected VM %q to be %t (Resource Group %q) to provision: %+v", protectedItemName, found, resourceGroup, err)
}

return resp.(backup.ProtectedItemResource), nil
Expand Down
33 changes: 21 additions & 12 deletions azurerm/resource_arm_recovery_services_protection_policy_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/services/recoveryservices/mgmt/2016-06-01/backup"
"github.com/Azure/azure-sdk-for-go/services/recoveryservices/mgmt/2017-07-01/backup"

"github.com/Azure/go-autorest/autorest/date"
"github.com/hashicorp/terraform/helper/resource"
Expand Down Expand Up @@ -55,6 +55,12 @@ func resourceArmRecoveryServicesProtectionPolicyVm() *schema.Resource {
),
},

"timezone": {
Type: schema.TypeString,
Optional: true,
Default: "UTC",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we apply some validation to this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice, however I don't know what of the many different timezone formats is valid here (there are at least 2 different sets of valid values it could be, maybe more)

},

"backup": {
Type: schema.TypeList,
MaxItems: 1,
Expand Down Expand Up @@ -156,11 +162,11 @@ func resourceArmRecoveryServicesProtectionPolicyVm() *schema.Resource {
Type: schema.TypeString,
DiffSuppressFunc: suppress.CaseDifference,
ValidateFunc: validation.StringInSlice([]string{
string(backup.First),
string(backup.Second),
string(backup.Third),
string(backup.Fourth),
string(backup.Last),
string(backup.WeekOfMonthFirst),
string(backup.WeekOfMonthSecond),
string(backup.WeekOfMonthThird),
string(backup.WeekOfMonthFourth),
string(backup.WeekOfMonthLast),
}, true),
},
},
Expand Down Expand Up @@ -210,11 +216,11 @@ func resourceArmRecoveryServicesProtectionPolicyVm() *schema.Resource {
Type: schema.TypeString,
DiffSuppressFunc: suppress.CaseDifference,
ValidateFunc: validation.StringInSlice([]string{
string(backup.First),
string(backup.Second),
string(backup.Third),
string(backup.Fourth),
string(backup.Last),
string(backup.WeekOfMonthFirst),
string(backup.WeekOfMonthSecond),
string(backup.WeekOfMonthThird),
string(backup.WeekOfMonthFourth),
string(backup.WeekOfMonthLast),
}, true),
},
},
Expand Down Expand Up @@ -291,6 +297,7 @@ func resourceArmRecoveryServicesProtectionPolicyVmCreateUpdate(d *schema.Resourc
policy := backup.ProtectionPolicyResource{
Tags: expandTags(tags),
Properties: &backup.AzureIaaSVMProtectionPolicy{
TimeZone: utils.String(d.Get("timezone").(string)),
BackupManagementType: backup.BackupManagementTypeAzureIaasVM,
SchedulePolicy: expandArmRecoveryServicesProtectionPolicySchedule(d, times),
RetentionPolicy: &backup.LongTermRetentionPolicy{ //SimpleRetentionPolicy only has duration property ¯\_(ツ)_/¯
Expand Down Expand Up @@ -347,8 +354,10 @@ func resourceArmRecoveryServicesProtectionPolicyVmRead(d *schema.ResourceData, m
d.Set("recovery_vault_name", vaultName)

if properties, ok := resp.Properties.AsAzureIaaSVMProtectionPolicy(); ok && properties != nil {
if schedule, ok := properties.SchedulePolicy.AsSimpleSchedulePolicy(); ok && schedule != nil {

d.Set("timezone", properties.TimeZone)

if schedule, ok := properties.SchedulePolicy.AsSimpleSchedulePolicy(); ok && schedule != nil {
if err := d.Set("backup", flattenArmRecoveryServicesProtectionPolicySchedule(schedule)); err != nil {
return fmt.Errorf("Error setting `backup`: %+v", err)
}
Expand Down
14 changes: 6 additions & 8 deletions examples/recoveryservice/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ resource "azurerm_recovery_services_vault" "example" {
sku = "Standard"
}

resource "azurerm_recovery_services_protection_policy_vm" "example" {
resource "azurerm_recovery_services_protection_policy_vm" "simple" {
name = "tfex-policy-simple"
resource_group_name = "${azurerm_resource_group.rg.name}"
recovery_vault_name = "${azurerm_recovery_services_vault.example.name}"
Expand All @@ -42,21 +42,19 @@ resource "azurerm_recovery_services_protection_policy_vm" "example" {
}
}

resource "azurerm_recovery_services_protection_policy_vm" "example" {
resource "azurerm_recovery_services_protection_policy_vm" "advanced" {
name = "tfex-policy-advanced"
resource_group_name = "${azurerm_resource_group.rg.name}"
recovery_vault_name = "${azurerm_recovery_services_vault.example.name}"

timezone = "UTC"

backup = {
frequency = "Daily"
frequency = "Weekly"
time = "23:00"
weekdays = ["Monday", "Wednesday"]
}

retention_daily = {
count = 10
}

retention_weekly = {
weekdays = ["Monday", "Wednesday"]
count = 52
Expand All @@ -79,6 +77,6 @@ resource "azurerm_recovery_services_protection_policy_vm" "example" {
resource "azurerm_recovery_services_protected_vm" "example" {
resource_group_name = "${azurerm_resource_group.rg.name}"
recovery_vault_name = "${azurerm_recovery_services_vault.example.name}"
source_vm_name = "${module.vm.vm-name}"
source_vm_id = "${module.vm.vm-id}"
backup_policy_id = "${azurerm_recovery_services_protection_policy_vm.simple.id}"
}

This file was deleted.

Loading