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_replication_recovery_plan;New Data Source: azurerm_site_recovery_replication_recovery_plan #19940

Merged
merged 20 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from 11 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
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
1 change: 1 addition & 0 deletions internal/services/recoveryservices/resourceids.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ package recoveryservices
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=BackupPolicy -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.RecoveryServices/vaults/vault1/backupPolicies/policy1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=ProtectedItem -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.RecoveryServices/vaults/vault1/backupFabrics/Azure/protectionContainers/container1/protectedItems/protectedItem1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=Vault -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.RecoveryServices/vaults/vault1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=ReplicationRecoverPlan -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.RecoveryServices/vaults/vault1/replicationRecoveryPlans/plan1
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
package recoveryservices

import (
"context"
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"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/services/recoveryservices/validate"

"github.com/hashicorp/go-azure-helpers/lang/response"
"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

id := replicationrecoveryplans.NewReplicationRecoveryPlanID(subscriptionId, metaModel.ResourceGroupName, metaModel.RecoveryVaultName, 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,
ResourceGroupName: id.ResourceGroupName,
RecoveryVaultName: id.ResourceName,
}

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,
},

"resource_group_name": commonschema.ResourceGroupName(),

"recovery_vault_name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validate.RecoveryServicesVaultName,
},
}
}

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{
"group_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,
},
"action_detail_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,44 @@
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("resource_group_name").Exists(),
check.That(data.ResourceName).Key("recovery_vault_name").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.group_type").HasValue("Boot"),
check.That(data.ResourceName).Key("recovery_group.1.group_type").HasValue("Failover"),
check.That(data.ResourceName).Key("recovery_group.2.group_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
resource_group_name = azurerm_site_recovery_replication_recovery_plan.test.resource_group_name
recovery_vault_name = azurerm_site_recovery_replication_recovery_plan.test.recovery_vault_name
}
`, SiteRecoveryReplicationRecoveryPlan{}.basic(data))
}
Loading