Skip to content

Commit

Permalink
feat: Add delete options for aws_fsx_openzfs_file_system
Browse files Browse the repository at this point in the history
  • Loading branch information
acwwat committed May 23, 2024
1 parent 08f7629 commit 9d56ccf
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 27 deletions.
3 changes: 3 additions & 0 deletions .changelog/37651.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_fsx_openzfs_file_system: Add `delete_options` and `final_backup_tags` arguments
```
84 changes: 80 additions & 4 deletions internal/service/fsx/openzfs_file_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ func resourceOpenZFSFileSystem() *schema.Resource {
validation.StringMatch(regexache.MustCompile(`^([01]\d|2[0-3]):?([0-5]\d)$`), "must be in the format HH:MM"),
),
},
"delete_options": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice(fsx.DeleteFileSystemOpenZFSOption_Values(), false),
},
},
"deployment_type": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -127,6 +135,32 @@ func resourceOpenZFSFileSystem() *schema.Resource {
Computed: true,
ForceNew: true,
},
"final_backup_tags": {
Type: schema.TypeSet,
Optional: true,
MinItems: 1,
MaxItems: 50,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
names.AttrKey: {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.All(
validation.StringLenBetween(1, 128),
validation.StringMatch(regexache.MustCompile(`^([\p{L}\p{Z}\p{N}_.:/=+\-@]*)$`), "must be a valid tag key"),
),
},
names.AttrValue: {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.All(
validation.StringLenBetween(0, 128),
validation.StringMatch(regexache.MustCompile(`^([\p{L}\p{Z}\p{N}_.:/=+\-@]*)$`), "must be a valid tag value"),
),
},
},
},
},
names.AttrKMSKeyID: {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -534,7 +568,13 @@ func resourceOpenZFSFileSystemUpdate(ctx context.Context, d *schema.ResourceData
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).FSxConn(ctx)

if d.HasChangesExcept(names.AttrTags, names.AttrTagsAll) {
if d.HasChangesExcept(
names.AttrTags,
names.AttrTagsAll,
"delete_options",
"final_backup_tags",
"skip_final_backup",
) {
input := &fsx.UpdateFileSystemInput{
ClientRequestToken: aws.String(id.UniqueId()),
FileSystemId: aws.String(d.Id()),
Expand Down Expand Up @@ -633,13 +673,23 @@ func resourceOpenZFSFileSystemDelete(ctx context.Context, d *schema.ResourceData
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).FSxConn(ctx)

log.Printf("[DEBUG] Deleting FSx for OpenZFS File System: %s", d.Id())
_, err := conn.DeleteFileSystemWithContext(ctx, &fsx.DeleteFileSystemInput{
input := &fsx.DeleteFileSystemInput{
FileSystemId: aws.String(d.Id()),
OpenZFSConfiguration: &fsx.DeleteFileSystemOpenZFSConfiguration{
SkipFinalBackup: aws.Bool(d.Get("skip_final_backup").(bool)),
},
})
}

if v, ok := d.GetOk("delete_options"); ok {
input.OpenZFSConfiguration.Options = flex.ExpandStringSet(v.(*schema.Set))
}

if v, ok := d.GetOk("final_backup_tags"); ok {
input.OpenZFSConfiguration.FinalBackupTags = expandFinalBackupTags(v.(*schema.Set))
}

log.Printf("[DEBUG] Deleting FSx for OpenZFS File System: %s", d.Id())
_, err := conn.DeleteFileSystemWithContext(ctx, input)

if tfawserr.ErrCodeEquals(err, fsx.ErrCodeFileSystemNotFound) {
return diags
Expand Down Expand Up @@ -744,6 +794,32 @@ func expandUpdateOpenZFSVolumeConfiguration(cfg []interface{}) *fsx.UpdateOpenZF
return &out
}

func expandFinalBackupTags(cfg *schema.Set) []*fsx.Tag {
tags := []*fsx.Tag{}

for _, tag := range cfg.List() {
expandedTag := expandFinalBackupTag(tag.(map[string]interface{}))
if expandedTag != nil {
tags = append(tags, expandedTag)
}
}

return tags
}

func expandFinalBackupTag(cfg map[string]interface{}) *fsx.Tag {
out := fsx.Tag{}

if v, ok := cfg[names.AttrKey].(string); ok {
out.Key = aws.String(v)
}
if v, ok := cfg[names.AttrValue].(string); ok {
out.Value = aws.String(v)
}

return &out
}

func flattenDiskIopsConfiguration(rs *fsx.DiskIopsConfiguration) []interface{} {
if rs == nil {
return []interface{}{}
Expand Down
105 changes: 104 additions & 1 deletion internal/service/fsx/openzfs_file_system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package fsx_test
import (
"context"
"fmt"
"os"
"testing"

"github.com/YakDriver/regexache"
Expand Down Expand Up @@ -78,7 +79,7 @@ func TestAccFSxOpenZFSFileSystem_basic(t *testing.T) {
resource.TestCheckResourceAttrSet(resourceName, "root_volume_id"),
resource.TestCheckResourceAttr(resourceName, "route_table_ids.#", acctest.Ct0),
resource.TestCheckResourceAttr(resourceName, "security_group_ids.#", acctest.Ct0),
resource.TestCheckResourceAttr(resourceName, "skip_final_backup", "false"),
resource.TestCheckResourceAttr(resourceName, "skip_final_backup", "true"),
resource.TestCheckResourceAttr(resourceName, "storage_capacity", "64"),
resource.TestCheckResourceAttr(resourceName, names.AttrStorageType, fsx.StorageTypeSsd),
resource.TestCheckResourceAttr(resourceName, "subnet_ids.#", acctest.Ct1),
Expand All @@ -94,6 +95,8 @@ func TestAccFSxOpenZFSFileSystem_basic(t *testing.T) {
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"delete_options",
"final_backup_tags",
"skip_final_backup",
},
},
Expand Down Expand Up @@ -128,6 +131,8 @@ func TestAccFSxOpenZFSFileSystem_diskIops(t *testing.T) {
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
names.AttrSecurityGroupIDs,
"delete_options",
"final_backup_tags",
"skip_final_backup",
},
},
Expand Down Expand Up @@ -218,6 +223,8 @@ func TestAccFSxOpenZFSFileSystem_rootVolume(t *testing.T) {
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
names.AttrSecurityGroupIDs,
"delete_options",
"final_backup_tags",
"skip_final_backup",
},
},
Expand Down Expand Up @@ -379,6 +386,8 @@ func TestAccFSxOpenZFSFileSystem_securityGroupIDs(t *testing.T) {
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
names.AttrSecurityGroupIDs,
"delete_options",
"final_backup_tags",
"skip_final_backup",
},
},
Expand Down Expand Up @@ -420,6 +429,8 @@ func TestAccFSxOpenZFSFileSystem_tags(t *testing.T) {
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
names.AttrSecurityGroupIDs,
"delete_options",
"final_backup_tags",
"skip_final_backup",
},
},
Expand Down Expand Up @@ -474,6 +485,8 @@ func TestAccFSxOpenZFSFileSystem_copyTags(t *testing.T) {
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
names.AttrSecurityGroupIDs,
"delete_options",
"final_backup_tags",
"skip_final_backup",
},
},
Expand Down Expand Up @@ -516,6 +529,8 @@ func TestAccFSxOpenZFSFileSystem_throughput(t *testing.T) {
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
names.AttrSecurityGroupIDs,
"delete_options",
"final_backup_tags",
"skip_final_backup",
},
},
Expand Down Expand Up @@ -556,6 +571,8 @@ func TestAccFSxOpenZFSFileSystem_storageType(t *testing.T) {
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
names.AttrSecurityGroupIDs,
"delete_options",
"final_backup_tags",
"skip_final_backup",
},
},
Expand Down Expand Up @@ -588,6 +605,8 @@ func TestAccFSxOpenZFSFileSystem_weeklyMaintenanceStartTime(t *testing.T) {
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
names.AttrSecurityGroupIDs,
"delete_options",
"final_backup_tags",
"skip_final_backup",
},
},
Expand Down Expand Up @@ -628,6 +647,8 @@ func TestAccFSxOpenZFSFileSystem_automaticBackupRetentionDays(t *testing.T) {
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
names.AttrSecurityGroupIDs,
"delete_options",
"final_backup_tags",
"skip_final_backup",
},
},
Expand Down Expand Up @@ -675,6 +696,8 @@ func TestAccFSxOpenZFSFileSystem_kmsKeyID(t *testing.T) {
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
names.AttrSecurityGroupIDs,
"delete_options",
"final_backup_tags",
"skip_final_backup",
},
},
Expand Down Expand Up @@ -707,6 +730,8 @@ func TestAccFSxOpenZFSFileSystem_dailyAutomaticBackupStartTime(t *testing.T) {
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
names.AttrSecurityGroupIDs,
"delete_options",
"final_backup_tags",
"skip_final_backup",
},
},
Expand Down Expand Up @@ -747,6 +772,8 @@ func TestAccFSxOpenZFSFileSystem_throughputCapacity(t *testing.T) {
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
names.AttrSecurityGroupIDs,
"delete_options",
"final_backup_tags",
"skip_final_backup",
},
},
Expand Down Expand Up @@ -787,6 +814,8 @@ func TestAccFSxOpenZFSFileSystem_storageCapacity(t *testing.T) {
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
names.AttrSecurityGroupIDs,
"delete_options",
"final_backup_tags",
"skip_final_backup",
},
},
Expand Down Expand Up @@ -828,6 +857,8 @@ func TestAccFSxOpenZFSFileSystem_deploymentType(t *testing.T) {
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
names.AttrSecurityGroupIDs,
"delete_options",
"final_backup_tags",
"skip_final_backup",
},
},
Expand Down Expand Up @@ -909,6 +940,8 @@ func TestAccFSxOpenZFSFileSystem_multiAZ(t *testing.T) {
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"delete_options",
"final_backup_tags",
"skip_final_backup",
},
},
Expand Down Expand Up @@ -942,6 +975,8 @@ func TestAccFSxOpenZFSFileSystem_routeTableIDs(t *testing.T) {
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
names.AttrSecurityGroupIDs,
"delete_options",
"final_backup_tags",
"skip_final_backup",
},
},
Expand All @@ -966,6 +1001,52 @@ func TestAccFSxOpenZFSFileSystem_routeTableIDs(t *testing.T) {
})
}

func TestAccFSxOpenZFSFileSystem_deleteOptions(t *testing.T) {
ctx := acctest.Context(t)

if os.Getenv("FSX_CREATE_FINAL_BACKUP") != "true" {
t.Skip("Environment variable FSX_CREATE_FINAL_BACKUP is not set to true")
}

var filesystem fsx.FileSystem
resourceName := "aws_fsx_openzfs_file_system.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckPartitionHasService(t, fsx.EndpointsID) },
ErrorCheck: acctest.ErrorCheck(t, names.FSxServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckOpenZFSFileSystemDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccOpenZFSFileSystemConfig_deleteOptions(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckOpenZFSFileSystemExists(ctx, resourceName, &filesystem),
resource.TestCheckResourceAttr(resourceName, "delete_options.#", acctest.Ct1),
resource.TestCheckResourceAttr(resourceName, "delete_options.0", "DELETE_CHILD_VOLUMES_AND_SNAPSHOTS"),
resource.TestCheckResourceAttr(resourceName, "final_backup_tags.#", acctest.Ct2),
resource.TestCheckResourceAttr(resourceName, "final_backup_tags.0.key", "TFTestKVTagKey"),
resource.TestCheckResourceAttr(resourceName, "final_backup_tags.0.value", "TFTestKVTagValue"),
resource.TestCheckResourceAttr(resourceName, "final_backup_tags.1.key", "TFTestKeyOnlyTagKey"),
resource.TestCheckResourceAttr(resourceName, "final_backup_tags.1.value", ""),
resource.TestCheckResourceAttr(resourceName, "skip_final_backup", "false"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
names.AttrSecurityGroupIDs,
"delete_options",
"final_backup_tags",
"skip_final_backup",
},
},
},
})
}

func testAccCheckOpenZFSFileSystemExists(ctx context.Context, n string, v *fsx.FileSystem) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -1044,6 +1125,7 @@ func testAccOpenZFSFileSystemConfig_baseMultiAZ(rName string) string {
func testAccOpenZFSFileSystemConfig_basic(rName string) string {
return acctest.ConfigCompose(testAccOpenZFSFileSystemConfig_baseSingleAZ(rName), `
resource "aws_fsx_openzfs_file_system" "test" {
skip_final_backup = true
storage_capacity = 64
subnet_ids = aws_subnet.test[*].id
deployment_type = "SINGLE_AZ_1"
Expand Down Expand Up @@ -1659,3 +1741,24 @@ resource "aws_fsx_openzfs_file_system" "test" {
}
`, rName, n))
}

func testAccOpenZFSFileSystemConfig_deleteOptions(rName string) string {
return acctest.ConfigCompose(testAccOpenZFSFileSystemConfig_baseSingleAZ(rName), `
resource "aws_fsx_openzfs_file_system" "test" {
skip_final_backup = false
storage_capacity = 64
subnet_ids = aws_subnet.test[*].id
deployment_type = "SINGLE_AZ_1"
throughput_capacity = 64
delete_options = ["DELETE_CHILD_VOLUMES_AND_SNAPSHOTS"]
final_backup_tags {
key = "TFTestKeyOnlyTagKey"
value = ""
}
final_backup_tags {
key = "TFTestKVTagKey"
value = "TFTestKVTagValue"
}
}
`)
}
Loading

0 comments on commit 9d56ccf

Please sign in to comment.