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

New Resource:azurerm_site_recovery_hyperv_replication_policy #20454

Merged
merged 6 commits into from
Feb 20, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ resource "azurerm_recovery_services_vault" "test" {
resource_group_name = azurerm_resource_group.test.name
sku = "Standard"

cross_region_restore_enabled = %v
cross_region_restore_enabled = %t

soft_delete_enabled = false
}
Expand Down
1 change: 1 addition & 0 deletions internal/services/recoveryservices/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (r Registration) Resources() []sdk.Resource {
return []sdk.Resource{
BackupProtectionPolicyVMWorkloadResource{},
SiteRecoveryReplicationRecoveryPlanResource{},
ReplicationPolicyHyperVResource{},
HyperVSiteResource{},
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
package recoveryservices

import (
"context"
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/recoveryservices/2022-10-01/vaults"
"github.com/hashicorp/go-azure-sdk/resource-manager/recoveryservicessiterecovery/2022-10-01/replicationpolicies"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
)

type ReplicationPolicyHyperVModel struct {
Name string `tfschema:"name"`
RecoveryVaultId string `tfschema:"recovery_vault_id"`
RecoveryPointRetentionInHours int64 `tfschema:"recovery_point_retention_in_hours"`
ApplicationConsistentSnapshotFrequencyInHours int64 `tfschema:"application_consistent_snapshot_frequency_in_hours"`
CopyFrequency int64 `tfschema:"replication_interval_in_seconds"`
}

type ReplicationPolicyHyperVResource struct{}

var _ sdk.ResourceWithUpdate = ReplicationPolicyHyperVResource{}

func (r ReplicationPolicyHyperVResource) Arguments() map[string]*schema.Schema {
return map[string]*schema.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"recovery_vault_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: vaults.ValidateVaultID,
},
"recovery_point_retention_in_hours": {
Type: pluginsdk.TypeInt,
Required: true,
ValidateFunc: validation.IntBetween(0, 24),
},
"application_consistent_snapshot_frequency_in_hours": {
Type: pluginsdk.TypeInt,
Required: true,
ValidateFunc: validation.IntBetween(0, 12),
},
"replication_interval_in_seconds": {
Type: pluginsdk.TypeInt,
Required: true,
ValidateFunc: validation.IntInSlice([]int{30, 300}),
},
}
}

func (r ReplicationPolicyHyperVResource) Attributes() map[string]*schema.Schema {
return map[string]*schema.Schema{}
}

func (r ReplicationPolicyHyperVResource) ModelObject() interface{} {
return &ReplicationPolicyHyperVModel{}
}

func (r ReplicationPolicyHyperVResource) ResourceType() string {
return "azurerm_site_recovery_hyperv_replication_policy"
}

func (r ReplicationPolicyHyperVResource) Create() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
var plan ReplicationPolicyHyperVModel
if err := metadata.Decode(&plan); err != nil {
return fmt.Errorf("decoding %+v", err)
}

client := metadata.Client.RecoveryServices.ReplicationPoliciesClient

parsedVaultId, err := vaults.ParseVaultID(plan.RecoveryVaultId)
if err != nil {
return fmt.Errorf("parsing %s: %+v", plan.RecoveryVaultId, err)
}

id := replicationpolicies.NewReplicationPolicyID(parsedVaultId.SubscriptionId, parsedVaultId.ResourceGroupName, parsedVaultId.VaultName, plan.Name)

existing, err := client.Get(ctx, id)
if err != nil {
// NOTE: Bad Request due to https://github.com/Azure/azure-rest-api-specs/issues/12759
if !response.WasNotFound(existing.HttpResponse) && !wasBadRequestWithNotExist(existing.HttpResponse, err) {
return fmt.Errorf("checking presence %s: %+v", plan.Name, err)
}
}

if existing.Model != nil {
return metadata.ResourceRequiresImport(r.ResourceType(), id)
}

parameters := replicationpolicies.CreatePolicyInput{
Properties: &replicationpolicies.CreatePolicyInputProperties{
ProviderSpecificInput: &replicationpolicies.HyperVReplicaAzurePolicyInput{
RecoveryPointHistoryDuration: &plan.RecoveryPointRetentionInHours,
ApplicationConsistentSnapshotFrequencyInHours: &plan.ApplicationConsistentSnapshotFrequencyInHours,
ReplicationInterval: &plan.CopyFrequency,
},
},
}
err = client.CreateThenPoll(ctx, id, parameters)
if err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
}

metadata.SetID(id)

return nil
},
}
}

func (r ReplicationPolicyHyperVResource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
id, err := replicationpolicies.ParseReplicationPolicyID(metadata.ResourceData.Id())
if err != nil {
return err
}

client := metadata.Client.RecoveryServices.ReplicationPoliciesClient

resp, err := client.Get(ctx, *id)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
return metadata.MarkAsGone(id)
}
return fmt.Errorf("retrieving %s : %+v", *id, err)
}

vaultId := vaults.NewVaultID(id.SubscriptionId, id.ResourceGroupName, id.VaultName)

state := ReplicationPolicyHyperVModel{
Name: id.ReplicationPolicyName,
RecoveryVaultId: vaultId.ID(),
}

if model := resp.Model; model != nil {
if detail, isA2A := expandHyperVToAzurePolicyDetail(resp.Model); isA2A {
if detail.ApplicationConsistentSnapshotFrequencyInHours != nil {
state.ApplicationConsistentSnapshotFrequencyInHours = *detail.ApplicationConsistentSnapshotFrequencyInHours
}
if detail.ReplicationInterval != nil {
state.CopyFrequency = *detail.ReplicationInterval
}
if detail.RecoveryPointHistoryDurationInHours != nil {
state.RecoveryPointRetentionInHours = *detail.RecoveryPointHistoryDurationInHours
}

}
}
return metadata.Encode(&state)
},
}
}

func (r ReplicationPolicyHyperVResource) Update() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
var model ReplicationPolicyHyperVModel
if err := metadata.Decode(&model); err != nil {
return fmt.Errorf("decoding %+v", err)
}

id, err := replicationpolicies.ParseReplicationPolicyID(metadata.ResourceData.Id())
if err != nil {
return err
}

client := metadata.Client.RecoveryServices.ReplicationPoliciesClient

parameters := replicationpolicies.UpdatePolicyInput{
Properties: &replicationpolicies.UpdatePolicyInputProperties{
ReplicationProviderSettings: &replicationpolicies.HyperVReplicaAzurePolicyInput{
RecoveryPointHistoryDuration: &model.RecoveryPointRetentionInHours,
ApplicationConsistentSnapshotFrequencyInHours: &model.ApplicationConsistentSnapshotFrequencyInHours,
ReplicationInterval: &model.CopyFrequency,
},
},
}

err = client.UpdateThenPoll(ctx, *id, parameters)
if err != nil {
return fmt.Errorf("updating %s: %+v", id, err)
}

return nil
},
}
}

func (r ReplicationPolicyHyperVResource) Delete() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
id, err := replicationpolicies.ParseReplicationPolicyID(metadata.ResourceData.Id())
if err != nil {
return err
}

client := metadata.Client.RecoveryServices.ReplicationPoliciesClient

err = client.DeleteThenPoll(ctx, *id)
if err != nil {
return fmt.Errorf("deleting %s : %+v", id, err)
}

return nil
},
}
}

func (r ReplicationPolicyHyperVResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return replicationpolicies.ValidateReplicationPolicyID
}

func expandHyperVToAzurePolicyDetail(input *replicationpolicies.Policy) (out *replicationpolicies.HyperVReplicaAzurePolicyDetails, isA2A bool) {
if input.Properties == nil {
return nil, false
}
if input.Properties.ProviderSpecificDetails == nil {
return nil, false
}
detail, isA2A := input.Properties.ProviderSpecificDetails.(replicationpolicies.HyperVReplicaAzurePolicyDetails)
if isA2A {
out = &detail
}
return
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package recoveryservices_test

import (
"context"
"fmt"
"testing"

"github.com/hashicorp/go-azure-sdk/resource-manager/recoveryservicessiterecovery/2022-10-01/replicationpolicies"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

type SiteRecoveryReplicationPolicyHyperVResource struct{}

func TestAccSiteRecoveryReplicationPolicyHyperV_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_site_recovery_hyperv_replication_policy", "test")
r := SiteRecoveryReplicationPolicyHyperVResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data, 2, 1, 300),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccSiteRecoveryReplicationPolicyHyperV_noSnapshots(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_site_recovery_hyperv_replication_policy", "test")
r := SiteRecoveryReplicationPolicyHyperVResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data, 2, 0, 30),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("recovery_point_retention_in_hours").HasValue("2"),
check.That(data.ResourceName).Key("application_consistent_snapshot_frequency_in_hours").HasValue("0"),
check.That(data.ResourceName).Key("replication_interval_in_seconds").HasValue("30"),
),
},
data.ImportStep(),
})
}

func (SiteRecoveryReplicationPolicyHyperVResource) basic(data acceptance.TestData, retentionInHours int, snapshotFrequencyInHours int, replicationInterval int) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-recovery-%d"
location = "%s"
}

resource "azurerm_recovery_services_vault" "test" {
name = "acctest-vault-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
sku = "Standard"

soft_delete_enabled = false
}

resource "azurerm_site_recovery_hyperv_replication_policy" "test" {
recovery_vault_id = azurerm_recovery_services_vault.test.id
name = "acctest-policy-%d"
recovery_point_retention_in_hours = %d
application_consistent_snapshot_frequency_in_hours = %d
replication_interval_in_seconds = %d
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, retentionInHours, snapshotFrequencyInHours, replicationInterval)
}

func (t SiteRecoveryReplicationPolicyHyperVResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := replicationpolicies.ParseReplicationPolicyID(state.ID)
if err != nil {
return nil, err
}

resp, err := clients.RecoveryServices.ReplicationPoliciesClient.Get(ctx, *id)
if err != nil {
return nil, fmt.Errorf("retrieving %s: %+v", *id, err)
}

return utils.Bool(resp.Model != nil), nil
}
Loading