Skip to content

Commit

Permalink
New Resource: azurerm_site_recovery_replication_recovery_plan;New D…
Browse files Browse the repository at this point in the history
…ata Source: `azurerm_site_recovery_replication_recovery_plan` (#19940)
  • Loading branch information
ziyeqf authored Jan 24, 2023
1 parent 8c5f8bb commit 34b8046
Show file tree
Hide file tree
Showing 7 changed files with 1,418 additions and 1 deletion.
5 changes: 4 additions & 1 deletion internal/services/recoveryservices/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ func (r Registration) AssociatedGitHubLabel() string {
}

func (r Registration) DataSources() []sdk.DataSource {
return []sdk.DataSource{}
return []sdk.DataSource{
SiteRecoveryReplicationRecoveryPlanDataSource{},
}
}

func (r Registration) Resources() []sdk.Resource {
return []sdk.Resource{
BackupProtectionPolicyVMWorkloadResource{},
SiteRecoveryReplicationRecoveryPlanResource{},
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
package recoveryservices

import (
"context"
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/recoveryservicessiterecovery/2022-10-01/replicationrecoveryplans"
"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 SiteRecoveryReplicationRecoveryPlanDataSource struct{}

var _ sdk.DataSource = SiteRecoveryReplicationRecoveryPlanDataSource{}

func (r SiteRecoveryReplicationRecoveryPlanDataSource) ResourceType() string {
return "azurerm_site_recovery_replication_recovery_plan"
}

func (r SiteRecoveryReplicationRecoveryPlanDataSource) ModelObject() interface{} {
return &SiteRecoveryReplicationRecoveryPlanModel{}
}

func (r SiteRecoveryReplicationRecoveryPlanDataSource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return replicationrecoveryplans.ValidateReplicationRecoveryPlanID
}

func (r SiteRecoveryReplicationRecoveryPlanDataSource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
var metaModel SiteRecoveryReplicationRecoveryPlanModel
if err := metadata.Decode(&metaModel); err != nil {
return fmt.Errorf("decoding %+v", err)
}

client := metadata.Client.RecoveryServices.ReplicationRecoveryPlansClient
subscriptionId := metadata.Client.Account.SubscriptionId

vaultId, err := replicationrecoveryplans.ParseVaultID(metaModel.RecoveryVaultId)
if err != nil {
return err
}

id := replicationrecoveryplans.NewReplicationRecoveryPlanID(subscriptionId, vaultId.ResourceGroupName, vaultId.ResourceName, metaModel.Name)

resp, err := client.Get(ctx, id)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
return metadata.MarkAsGone(id)
}
return fmt.Errorf("making Read request on site recovery replication plan %s : %+v", id.String(), err)
}

model := resp.Model
if model == nil {
return fmt.Errorf("making Read request on site recovery replication plan %s : model is nil", id.String())
}

state := SiteRecoveryReplicationRecoveryPlanModel{
Name: id.RecoveryPlanName,
RecoveryVaultId: vaultId.ID(),
}

if prop := model.Properties; prop != nil {
if prop.PrimaryFabricId != nil {
state.SourceRecoveryFabricId = handleAzureSdkForGoBug2824(*prop.PrimaryFabricId)
}
if prop.RecoveryFabricId != nil {
state.TargetRecoveryFabricId = handleAzureSdkForGoBug2824(*prop.RecoveryFabricId)
}

if group := prop.Groups; group != nil {
state.RecoveryGroup = flattenRecoveryGroups(*group)
}
}

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

}

func (r SiteRecoveryReplicationRecoveryPlanDataSource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"recovery_vault_id": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: replicationrecoveryplans.ValidateVaultID,
},
}
}

func (r SiteRecoveryReplicationRecoveryPlanDataSource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"source_recovery_fabric_id": {
Type: pluginsdk.TypeString,
Computed: true,
},

"target_recovery_fabric_id": {
Type: pluginsdk.TypeString,
Computed: true,
},

"failover_deployment_model": {
Type: pluginsdk.TypeString,
Computed: true,
},

"recovery_group": {
Type: pluginsdk.TypeSet,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"type": {
Type: pluginsdk.TypeString,
Computed: true,
},
"replicated_protected_items": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
"pre_action": {
Type: pluginsdk.TypeSet,
Computed: true,
Elem: dataSourceSiteRecoveryReplicationPlanActions(),
},
"post_action": {
Type: pluginsdk.TypeSet,
Computed: true,
Elem: dataSourceSiteRecoveryReplicationPlanActions(),
},
},
},
},
}
}

func dataSourceSiteRecoveryReplicationPlanActions() *pluginsdk.Schema {
return &pluginsdk.Schema{
Type: pluginsdk.TypeList,
Elem: &pluginsdk.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: pluginsdk.TypeString,
Computed: true,
},
"type": {
Type: pluginsdk.TypeString,
Computed: true,
},
"fail_over_directions": {
Type: pluginsdk.TypeSet,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
"fail_over_types": {
Type: pluginsdk.TypeSet,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
"runbook_id": {
Type: pluginsdk.TypeString,
Computed: true,
},
"fabric_location": {
Type: pluginsdk.TypeString,
Computed: true,
},
"manual_action_instruction": {
Type: pluginsdk.TypeString,
Computed: true,
},
"script_path": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
},
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package recoveryservices_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
)

type SiteRecoveryReplicationRecoveryPlanDataSource struct{}

func TestAccDataSourceSiteRecoveryReplicationRecoveryPlan_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_site_recovery_replication_recovery_plan", "test")
r := SiteRecoveryReplicationRecoveryPlanDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("name").Exists(),
check.That(data.ResourceName).Key("recovery_vault_id").Exists(),
check.That(data.ResourceName).Key("source_recovery_fabric_id").Exists(),
check.That(data.ResourceName).Key("target_recovery_fabric_id").Exists(),
check.That(data.ResourceName).Key("recovery_group.0.type").HasValue("Boot"),
check.That(data.ResourceName).Key("recovery_group.1.type").HasValue("Failover"),
check.That(data.ResourceName).Key("recovery_group.2.type").HasValue("Shutdown"),
),
},
})
}

func (SiteRecoveryReplicationRecoveryPlanDataSource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
data "azurerm_site_recovery_replication_recovery_plan" "test" {
name = azurerm_site_recovery_replication_recovery_plan.test.name
recovery_vault_id = azurerm_site_recovery_replication_recovery_plan.test.recovery_vault_id
}
`, SiteRecoveryReplicationRecoveryPlan{}.basic(data))
}
Loading

0 comments on commit 34b8046

Please sign in to comment.