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

RecoveryServices - swap to hashicorp/go-azure-sdk #21344

Merged
merged 5 commits into from
Apr 11, 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 @@ -7,16 +7,17 @@ import (
"time"

"github.com/Azure/azure-sdk-for-go/services/recoveryservices/mgmt/2021-12-01/backup" // nolint: staticcheck
"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-sdk/resource-manager/recoveryservicesbackup/2021-12-01/protectioncontainers"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/recoveryservices/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/recoveryservices/validate"
storageParse "github.com/hashicorp/terraform-provider-azurerm/internal/services/storage/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func resourceBackupProtectionContainerStorageAccount() *pluginsdk.Resource {
Expand All @@ -26,7 +27,7 @@ func resourceBackupProtectionContainerStorageAccount() *pluginsdk.Resource {
Update: nil,
Delete: resourceBackupProtectionContainerStorageAccountDelete,
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.ProtectionContainerID(id)
_, err := protectioncontainers.ParseProtectionContainerID(id)
return err
}),

Expand Down Expand Up @@ -59,50 +60,48 @@ func resourceBackupProtectionContainerStorageAccount() *pluginsdk.Resource {
func resourceBackupProtectionContainerStorageAccountCreate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).RecoveryServices.BackupProtectionContainersClient
opStatusClient := meta.(*clients.Client).RecoveryServices.BackupOperationStatusesClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

resGroup := d.Get("resource_group_name").(string)
vaultName := d.Get("recovery_vault_name").(string)
storageAccountID := d.Get("storage_account_id").(string)

parsedStorageAccountID, err := storageParse.StorageAccountID(storageAccountID)
if err != nil {
return fmt.Errorf("[ERROR] Unable to parse storage_account_id '%s': %+v", storageAccountID, err)
}

containerName := fmt.Sprintf("StorageContainer;storage;%s;%s", parsedStorageAccountID.ResourceGroup, parsedStorageAccountID.Name)

id := protectioncontainers.NewProtectionContainerID(subscriptionId, d.Get("resource_group_name").(string), d.Get("recovery_vault_name").(string), "Azure", containerName)
if d.IsNewResource() {
existing, err := client.Get(ctx, vaultName, resGroup, "Azure", containerName)
existing, err := client.Get(ctx, id)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing recovery services protection container %s (Vault %s): %+v", containerName, vaultName, err)
if !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for presence of existing %s: %+v", id, err)
}
}

if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_backup_protection_container_storage", handleAzureSdkForGoBug2824(*existing.ID))
if !response.WasNotFound(existing.HttpResponse) {
return tf.ImportAsExistsError("azurerm_backup_protection_container_storage", id.ID())
}
}

parameters := backup.ProtectionContainerResource{
Properties: &backup.AzureStorageContainer{
SourceResourceID: &storageAccountID,
parameters := protectioncontainers.ProtectionContainerResource{
Properties: &protectioncontainers.AzureStorageContainer{
SourceResourceId: &storageAccountID,
FriendlyName: &parsedStorageAccountID.Name,
BackupManagementType: backup.ManagementTypeAzureStorage,
ContainerType: backup.ContainerTypeBasicProtectionContainerContainerTypeStorageContainer,
BackupManagementType: pointer.To(protectioncontainers.BackupManagementTypeAzureStorage),
},
}

resp, err := client.Register(ctx, vaultName, resGroup, "Azure", containerName, parameters)
resp, err := client.Register(ctx, id, parameters)
if err != nil {
return fmt.Errorf("registering backup protection container %s (Vault %s): %+v", containerName, vaultName, err)
return fmt.Errorf("registering %s: %+v", id, err)
}

locationURL, err := resp.Response.Location() // Operation ID found in the Location header
locationURL, err := resp.HttpResponse.Location() // Operation ID found in the Location header
if locationURL == nil || err != nil {
return fmt.Errorf("Unable to determine operation URL for protection container registration status for %s. (Vault %s): Location header missing or empty", containerName, vaultName)
return fmt.Errorf("unable to determine operation URL for %s: Location header missing or empty", id)
}

opResourceID := handleAzureSdkForGoBug2824(locationURL.Path)
Expand All @@ -113,66 +112,63 @@ func resourceBackupProtectionContainerStorageAccountCreate(d *pluginsdk.Resource
}

operationID := parsedLocation.Path["operationResults"]
if err = resourceBackupProtectionContainerStorageAccountWaitForOperation(ctx, opStatusClient, vaultName, resGroup, operationID, d); err != nil {
if err = resourceBackupProtectionContainerStorageAccountWaitForOperation(ctx, opStatusClient, id.VaultName, id.ResourceGroupName, operationID, d); err != nil {
return err
}

resp, err = client.Get(ctx, vaultName, resGroup, "Azure", containerName)
if err != nil {
return fmt.Errorf("retrieving site recovery protection container %s (Vault %s): %+v", containerName, vaultName, err)
}

d.SetId(handleAzureSdkForGoBug2824(*resp.ID))
d.SetId(handleAzureSdkForGoBug2824(id.ID()))

return resourceBackupProtectionContainerStorageAccountRead(d, meta)
}

func resourceBackupProtectionContainerStorageAccountRead(d *pluginsdk.ResourceData, meta interface{}) error {
id, err := parse.ProtectionContainerID(d.Id())
if err != nil {
return err
}

client := meta.(*clients.Client).RecoveryServices.BackupProtectionContainersClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

resp, err := client.Get(ctx, id.VaultName, id.ResourceGroup, id.BackupFabricName, id.Name)
id, err := protectioncontainers.ParseProtectionContainerID(d.Id())
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return err
}

resp, err := client.Get(ctx, *id)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
d.SetId("")
return nil
}
return fmt.Errorf("making Read request on backup protection container %s : %+v", id.String(), err)
}

d.Set("resource_group_name", id.ResourceGroup)
d.Set("resource_group_name", id.ResourceGroupName)
d.Set("recovery_vault_name", id.VaultName)

if properties, ok := resp.Properties.AsAzureStorageContainer(); ok && properties != nil {
d.Set("storage_account_id", properties.SourceResourceID)
if model := resp.Model; model != nil {
if properties, ok := model.Properties.(protectioncontainers.AzureStorageContainer); ok {
d.Set("storage_account_id", properties.SourceResourceId)
}
}

return nil
}

func resourceBackupProtectionContainerStorageAccountDelete(d *pluginsdk.ResourceData, meta interface{}) error {
id, err := parse.ProtectionContainerID(d.Id())
if err != nil {
return err
}

client := meta.(*clients.Client).RecoveryServices.BackupProtectionContainersClient
opClient := meta.(*clients.Client).RecoveryServices.BackupOperationStatusesClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

resp, err := client.Unregister(ctx, id.VaultName, id.ResourceGroup, id.BackupFabricName, id.Name)
id, err := protectioncontainers.ParseProtectionContainerID(d.Id())
if err != nil {
return err
}

resp, err := client.Unregister(ctx, *id)
if err != nil {
return fmt.Errorf("deregistering backup protection container %s (Vault %s): %+v", id.Name, id.VaultName, err)
return fmt.Errorf("deregistering %s: %+v", id, err)
}

locationURL, err := resp.Response.Location()
locationURL, err := resp.HttpResponse.Location()
if err != nil || locationURL == nil {
return fmt.Errorf("unregistering backup protection container %s : Location header missing or empty", id.String())
}
Expand All @@ -185,7 +181,7 @@ func resourceBackupProtectionContainerStorageAccountDelete(d *pluginsdk.Resource
}
operationID := parsedLocation.Path["backupOperationResults"]

if err = resourceBackupProtectionContainerStorageAccountWaitForOperation(ctx, opClient, id.VaultName, id.ResourceGroup, operationID, d); err != nil {
if err = resourceBackupProtectionContainerStorageAccountWaitForOperation(ctx, opClient, id.VaultName, id.ResourceGroupName, operationID, d); err != nil {
return err
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"fmt"
"testing"

"github.com/hashicorp/go-azure-sdk/resource-manager/recoveryservicesbackup/2021-12-01/protectioncontainers"
"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/services/recoveryservices/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)
Expand All @@ -31,16 +31,16 @@ func TestAccBackupProtectionContainerStorageAccount_basic(t *testing.T) {
}

func (t BackupProtectionContainerStorageAccountResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.ProtectionContainerID(state.ID)
id, err := protectioncontainers.ParseProtectionContainerID(state.ID)
if err != nil {
return nil, err
}
resp, err := clients.RecoveryServices.BackupProtectionContainersClient.Get(ctx, id.VaultName, id.ResourceGroup, id.BackupFabricName, id.Name)
resp, err := clients.RecoveryServices.BackupProtectionContainersClient.Get(ctx, *id)
if err != nil {
return nil, fmt.Errorf("reading site recovery protection container (%s): %+v", id.String(), err)
}

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

func (BackupProtectionContainerStorageAccountResource) basic(data acceptance.TestData) string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package recoveryservices
import (
"fmt"
"log"
"strings"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-sdk/resource-manager/recoveryservicesbackup/2021-12-01/protectionpolicies"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/recoveryservices/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func dataSourceBackupPolicyFileShare() *pluginsdk.Resource {
Expand All @@ -28,26 +28,24 @@ func dataSourceBackupPolicyFileShare() *pluginsdk.Resource {

func dataSourceBackupPolicyFileShareRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).RecoveryServices.ProtectionPoliciesClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
vaultName := d.Get("recovery_vault_name").(string)
id := protectionpolicies.NewBackupPolicyID(subscriptionId, d.Get("resource_group_name").(string), d.Get("recovery_vault_name").(string), d.Get("name").(string))

log.Printf("[DEBUG] Reading Recovery Service Policy %q (resource group %q)", name, resourceGroup)
log.Printf("[DEBUG] Reading %s", id)

protectionPolicy, err := client.Get(ctx, vaultName, resourceGroup, name)
protectionPolicy, err := client.Get(ctx, id)
if err != nil {
if utils.ResponseWasNotFound(protectionPolicy.Response) {
return fmt.Errorf("Error: Backup Policy %q (Resource Group %q) was not found", name, resourceGroup)
if response.WasNotFound(protectionPolicy.HttpResponse) {
return fmt.Errorf("%s was not found", id)
}

return fmt.Errorf("making Read request on Backup Policy %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("making Read request on%s: %+v", id, err)
}

id := strings.Replace(*protectionPolicy.ID, "Subscriptions", "subscriptions", 1)
d.SetId(id)
d.SetId(id.ID())

return nil
}
Expand Down
Loading