Skip to content

Commit

Permalink
New Resource: azurerm_site_recovery_services_vault_hyperv_site
Browse files Browse the repository at this point in the history
  • Loading branch information
ziyeqf committed Feb 13, 2023
1 parent 878c611 commit ee5ec92
Show file tree
Hide file tree
Showing 41 changed files with 2,530 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
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/replicationfabrics"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

type HyperVSiteModel struct {
Name string `tfschema:"name"`
RecoveryVaultId string `tfschema:"recovery_vault_id"`
}

type HyperVSiteResource struct{}

var _ sdk.Resource = HyperVSiteResource{}

func (r HyperVSiteResource) 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: replicationfabrics.ValidateVaultID,
},
}
}

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

func (r HyperVSiteResource) ModelObject() interface{} {
return &HyperVSiteModel{}
}

func (r HyperVSiteResource) ResourceType() string {
return "azurerm_site_recovery_services_vault_hyperv_site"
}

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

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

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

id := replicationfabrics.NewReplicationFabricID(subscriptionId, vaultId.ResourceGroupName, vaultId.VaultName, metaModel.Name)

type HyperVSiteInstanceType struct {
InstanceType string `json:"instanceType"`
}

// the instance type `HyperVSite` is not exposed in Swagger, tracked on https://github.com/Azure/azure-rest-api-specs/issues/22016
parameters := replicationfabrics.FabricCreationInput{
Properties: &replicationfabrics.FabricCreationInputProperties{
CustomDetails: HyperVSiteInstanceType{
InstanceType: "HyperVSite",
},
},
}

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

metadata.SetID(id)
return nil
},
}
}

func (r HyperVSiteResource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.RecoveryServices.FabricClient
id, err := replicationfabrics.ParseReplicationFabricID(metadata.ResourceData.Id())
if err != nil {
return fmt.Errorf("parsing %s: %+v", metadata.ResourceData.Id(), err)
}

resp, err := client.Get(ctx, *id, replicationfabrics.DefaultGetOperationOptions())
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
return metadata.MarkAsGone(id)
}

return fmt.Errorf("retrieving %s: %+v", id, err)
}

if resp.Model == nil {
return fmt.Errorf("retrieving %s: `model` was nil", id)
}

state := HyperVSiteModel{
Name: id.ReplicationFabricName,
}

vaultId := replicationfabrics.NewVaultID(id.SubscriptionId, id.ResourceGroupName, id.VaultName)
state.RecoveryVaultId = vaultId.ID()

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

func (r HyperVSiteResource) Delete() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.RecoveryServices.FabricClient
id, err := replicationfabrics.ParseReplicationFabricID(metadata.ResourceData.Id())
if err != nil {
return fmt.Errorf("parsing %s: %+v", metadata.ResourceData.Id(), err)
}

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

return nil
},
}
}

func (r HyperVSiteResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return replicationfabrics.ValidateReplicationFabricID
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package recoveryservices_test

import (
"context"
"fmt"
"testing"

"github.com/hashicorp/go-azure-sdk/resource-manager/recoveryservicessiterecovery/2022-10-01/replicationfabrics"
"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 HyperVSiteResource struct{}

func TestAccSiteRecoveryHyperVSite_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_site_recovery_services_vault_hyperv_site", "test")
r := HyperVSiteResource{}

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

func (HyperVSiteResource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-recovery-%[1]d"
location = "%s"
}
resource "azurerm_recovery_services_vault" "test" {
name = "acctest-vault-%[1]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_services_vault_hyperv_site" "test" {
recovery_vault_id = azurerm_recovery_services_vault.test.id
name = "acctest-site-%[1]d"
}
`, data.RandomInteger, data.Locations.Primary)
}

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

resp, err := clients.RecoveryServices.FabricClient.Get(ctx, *id, replicationfabrics.DefaultGetOperationOptions())
if err != nil {
return nil, fmt.Errorf("reading Recovery Service Vault (%s): %+v", id.String(), err)
}

return utils.Bool(resp.Model != nil), nil
}
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{},
HyperVSiteResource{},
}
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ee5ec92

Please sign in to comment.