Skip to content

Commit

Permalink
Add StateChangeConf
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-henglu committed Jun 22, 2021
1 parent b7653cd commit cb4841a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dataprotection

import (
"context"
"fmt"
"log"
"time"
Expand All @@ -16,6 +17,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/dataprotection/validate"
postgresParse "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/postgres/parse"
postgresValidate "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/postgres/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/pluginsdk"
azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
Expand Down Expand Up @@ -136,6 +138,18 @@ func resourceDataProtectionBackupInstancePostgreSQLCreateUpdate(d *schema.Resour
return fmt.Errorf("waiting for creation/update of the DataProtection BackupInstance (%q): %+v", id, err)
}

stateConf := &pluginsdk.StateChangeConf{
Pending: []string{string(dataprotection.ConfiguringProtection), string(dataprotection.UpdatingProtection)},
Target: []string{string(dataprotection.ProtectionConfigured)},
Refresh: policyProtectionStateRefreshFunc(ctx, client, id),
MinTimeout: 1 * time.Minute,
Timeout: d.Timeout(pluginsdk.TimeoutCreate),
}

if _, err = stateConf.WaitForState(); err != nil {
return fmt.Errorf("waiting for BackupInstance(%q) policy protection to be completed: %+v", id, err)
}

d.SetId(id.ID())
return resourceDataProtectionBackupInstancePostgreSQLRead(d, meta)
}
Expand Down Expand Up @@ -197,3 +211,14 @@ func resourceDataProtectionBackupInstancePostgreSQLDelete(d *schema.ResourceData
}
return nil
}

func policyProtectionStateRefreshFunc(ctx context.Context, client *dataprotection.BackupInstancesClient, id parse.BackupInstanceId) pluginsdk.StateRefreshFunc {
return func() (interface{}, string, error) {
res, err := client.Get(ctx, id.BackupVaultName, id.ResourceGroup, id.Name)
if err != nil {
return nil, "", fmt.Errorf("retrieving DataProtection BackupInstance (%q): %+v", id, err)
}

return res, string(res.Properties.ProtectionStatus.Status), nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"testing"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
Expand Down Expand Up @@ -71,16 +70,14 @@ func TestAccDataProtectionBackupInstancePostgreSQL_update(t *testing.T) {
},
data.ImportStep(),
{
PreConfig: func() { time.Sleep(3 * time.Minute) },
Config: r.complete(data),
Config: r.complete(data),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
PreConfig: func() { time.Sleep(3 * time.Minute) },
Config: r.basic(data),
Config: r.basic(data),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
Expand Down

0 comments on commit cb4841a

Please sign in to comment.