Skip to content

Commit

Permalink
OAS-5111 | update backup policy
Browse files Browse the repository at this point in the history
  • Loading branch information
lakhansamani committed Sep 6, 2022
1 parent aeb596d commit 0f28b60
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 18 deletions.
1 change: 1 addition & 0 deletions docs/resources/backup_policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ resource "oasis_backup_policy" "my_backup_policy" {

### Optional

- `additional_region_ids` (List of String) Backup Policy Additional Region Identifiers where backup should be cloned
- `description` (String) Backup Policy Resource Backup Policy Description field
- `is_paused` (Boolean) Backup Policy Resource Backup Policy Is Paused field
- `locked` (Boolean) Backup Policy Resource Backup Policy Locked field
Expand Down
41 changes: 41 additions & 0 deletions internal/resource_backup_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ const (
hourlySchedule = "Hourly"
dailySchedule = "Daily"
monthlySchedule = "Monthly"

// Additional region identifiers where backup should be cloned
backupPolicyAdditionalRegionIDs = "additional_region_ids"
)

// resourceBackupPolicy defines a BackupPolicy oasis resource.
Expand Down Expand Up @@ -133,6 +136,13 @@ func resourceBackupPolicy() *schema.Resource {
Description: "Backup Policy Resource Backup Policy Email Notification field",
Required: true,
},
backupPolicyAdditionalRegionIDs: {
Type: schema.TypeList,
Description: "Backup Policy Additional Region Identifiers where backup should be cloned",
Optional: true,
MinItems: 1,
Elem: &schema.Schema{Type: schema.TypeString},
},
backupPolicyScheduleFieldName: {
Type: schema.TypeList,
Description: "Backup Policy Resource Backup Policy Schedule field",
Expand Down Expand Up @@ -320,6 +330,14 @@ func resourceBackupPolicyUpdate(ctx context.Context, d *schema.ResourceData, m i
if d.HasChange(backupPolicyScheduleFieldName) {
policy.Schedule = expandBackupPolicySchedule(d.Get(backupPolicyScheduleFieldName).([]interface{}))
}

// if v, ok := d.GetOk(backupPolicyAdditionalRegionIDs); ok {
// additionalRegionIDs, err := expandAdditionalRegionList(v.([]interface{}))
// if err != nil {
// return diag.FromErr(err)
// }
// policy.AdditionalRegionIds = additionalRegionIDs
// }
// Make sure we are sending the right schedule. The coming back schedule can contain invalid
// field items for different schedule. We make sure here that the right one is sent after an
// update. This check can be removed once terraform allows conflict checks for list items.
Expand Down Expand Up @@ -395,6 +413,7 @@ func flattenBackupPolicyResource(policy *backup.BackupPolicy) map[string]interfa
backupPolictEmailNotificationFieldName: policy.GetEmailNotification(),
backupPolicyScheduleFieldName: schedule,
backupPolicyLockedFieldName: policy.GetLocked(),
backupPolicyAdditionalRegionIDs: policy.GetAdditionalRegionIds(),
}
if policy.GetRetentionPeriod() != nil {
seconds := policy.GetRetentionPeriod().GetSeconds()
Expand Down Expand Up @@ -499,6 +518,13 @@ func expandBackupPolicyResource(d *schema.ResourceData) (*backup.BackupPolicy, e
if v, ok := d.GetOk(backupPolicyLockedFieldName); ok {
ret.Locked = v.(bool)
}
if v, ok := d.GetOk(backupPolicyAdditionalRegionIDs); ok {
additionalRegionIDs, err := expandAdditionalRegionList(v.([]interface{}))
if err != nil {
return ret, err
}
ret.AdditionalRegionIds = additionalRegionIDs
}
return ret, nil
}

Expand Down Expand Up @@ -669,3 +695,18 @@ func resourceBackupPolicyDelete(ctx context.Context, d *schema.ResourceData, m i
d.SetId("") // called automatically, but added to be explicit
return nil
}

// expandAdditionalRegionList creates a string list of items from an interface slice. It also
// verifies if a given string item is empty or not. In case it's empty, an error is thrown.
func expandAdditionalRegionList(list []interface{}) ([]string, error) {
additionalRegionIDs := []string{}
for _, v := range list {
if v, ok := v.(string); ok {
if v == "" {
continue
}
additionalRegionIDs = append(additionalRegionIDs, v)
}
}
return additionalRegionIDs, nil
}
36 changes: 20 additions & 16 deletions internal/resource_backup_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ import (

func TestFlattenBackupPolicy(t *testing.T) {
policy := &backup.BackupPolicy{
Name: "test-policy",
Description: "test-description",
DeploymentId: "123456",
IsPaused: true,
Upload: true,
RetentionPeriod: types.DurationProto(200 * time.Hour),
EmailNotification: "None",
Locked: true,
Name: "test-policy",
Description: "test-description",
DeploymentId: "123456",
IsPaused: true,
Upload: true,
RetentionPeriod: types.DurationProto(200 * time.Hour),
EmailNotification: "None",
Locked: true,
AdditionalRegionIds: []string{"aks-westeurope"},
}

expected := map[string]interface{}{
Expand All @@ -52,6 +53,7 @@ func TestFlattenBackupPolicy(t *testing.T) {
backupPolicyRetentionPeriodFieldName: 200,
backupPolictEmailNotificationFieldName: "None",
backupPolicyLockedFieldName: true,
backupPolicyAdditionalRegionIDs: []string{"aks-westeurope"},
}

t.Run("with hourly schedule", func(tt *testing.T) {
Expand Down Expand Up @@ -168,16 +170,18 @@ func TestExpandBackupPolicy(t *testing.T) {
backupPolicyRetentionPeriodFieldName: 200,
backupPolictEmailNotificationFieldName: "None",
backupPolicyLockedFieldName: true,
backupPolicyAdditionalRegionIDs: []interface{}{"aks-westeurope"},
}
expected := &backup.BackupPolicy{
Name: "test-policy",
Description: "test-description",
DeploymentId: "123456",
IsPaused: true,
Upload: true,
RetentionPeriod: types.DurationProto(200 * time.Hour),
EmailNotification: "None",
Locked: true,
Name: "test-policy",
Description: "test-description",
DeploymentId: "123456",
IsPaused: true,
Upload: true,
RetentionPeriod: types.DurationProto(200 * time.Hour),
EmailNotification: "None",
Locked: true,
AdditionalRegionIds: []string{"aks-westeurope"},
}
t.Run("test hourly schedule", func(tt *testing.T) {
rawSchedule := []interface{}{
Expand Down
2 changes: 0 additions & 2 deletions internal/resource_iam_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ func TestAccResourceIAMGroup(t *testing.T) {
orgID, err := FetchOrganizationID()
require.NoError(t, err)

fmt.Println(orgID)

name := acctest.RandString(5)

resource.Test(t, resource.TestCase{
Expand Down

0 comments on commit 0f28b60

Please sign in to comment.