diff --git a/go.mod b/go.mod index e3b6094..fc0c49f 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.23 toolchain go1.23.2 require ( - github.com/dell/gopowerstore v1.16.1-0.20250109113534-39e1c58223b2 + github.com/dell/gopowerstore v1.17.1-0.20250110112425-426c7c96a018 github.com/hashicorp/terraform-plugin-docs v0.20.1 github.com/hashicorp/terraform-plugin-framework v1.13.0 github.com/hashicorp/terraform-plugin-framework-validators v0.15.0 diff --git a/go.sum b/go.sum index 709ab6b..7721ee4 100644 --- a/go.sum +++ b/go.sum @@ -43,6 +43,8 @@ github.com/dell/gopowerstore v1.16.1-0.20250103044727-b55f5083bb46 h1:EdQbU3wBHU github.com/dell/gopowerstore v1.16.1-0.20250103044727-b55f5083bb46/go.mod h1:S3Gxw34FJbKNorL/OPe/DF+0pUSkBm6E6b4ka7m6cuo= github.com/dell/gopowerstore v1.16.1-0.20250109113534-39e1c58223b2 h1:mFfuyQhhOskuwSl8SnanNZkxwDC4s6Q2umw+p0xU8Co= github.com/dell/gopowerstore v1.16.1-0.20250109113534-39e1c58223b2/go.mod h1:Ul7yw0rAboFV9sSB5Li2TkteDxL3/Ic+Q2bFdada3ZU= +github.com/dell/gopowerstore v1.17.1-0.20250110112425-426c7c96a018 h1:ugcOi8JxuqghyGvS/aZ+EGKY+7Kbn46DBlrZ5hqtwS4= +github.com/dell/gopowerstore v1.17.1-0.20250110112425-426c7c96a018/go.mod h1:Ul7yw0rAboFV9sSB5Li2TkteDxL3/Ic+Q2bFdada3ZU= github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ= github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= diff --git a/models/filesystem_snapshot_datasource.go b/models/filesystem_snapshot_datasource.go index 8c6fd5d..1dfb875 100644 --- a/models/filesystem_snapshot_datasource.go +++ b/models/filesystem_snapshot_datasource.go @@ -24,5 +24,6 @@ type FileSysteSnapshotDataSource struct { ID types.String `tfsdk:"id"` Name types.String `tfsdk:"name"` FileSystemID types.String `tfsdk:"filesystem_id"` + NasServerID types.String `tfsdk:"nas_server_id"` FileSystemSnapshots []FileSystemDatasource `tfsdk:"filesystem_snapshots"` } diff --git a/powerstore/datasource_filesystem_snapshot.go b/powerstore/datasource_filesystem_snapshot.go index 0ba60a2..f903e12 100644 --- a/powerstore/datasource_filesystem_snapshot.go +++ b/powerstore/datasource_filesystem_snapshot.go @@ -19,6 +19,7 @@ package powerstore import ( "context" + "fmt" "terraform-provider-powerstore/client" "terraform-provider-powerstore/models" @@ -29,6 +30,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-log/tflog" ) var ( @@ -63,23 +65,32 @@ func (d *fileSystemSnapshotDataSource) Schema(_ context.Context, _ datasource.Sc Optional: true, Computed: true, Validators: []validator.String{ - stringvalidator.ConflictsWith(path.MatchRoot("name"), path.MatchRoot("filesystem_id")), + stringvalidator.ConflictsWith(path.MatchRoot("name"), path.MatchRoot("filesystem_id"), path.MatchRoot("nas_server_id")), stringvalidator.LengthAtLeast(1), }, }, "name": schema.StringAttribute{ - Description: "File System Snapshot name. Conflicts with `id` and `filesystem_id`.", - MarkdownDescription: "File System Snapshot name. Conflicts with `id` and `filesystem_id`.", + Description: "File System Snapshot name. Conflicts with `id`.", + MarkdownDescription: "File System Snapshot name. Conflicts with `id`.", Optional: true, Validators: []validator.String{ stringvalidator.LengthAtLeast(1), - stringvalidator.ConflictsWith(path.MatchRoot("id"), path.MatchRoot("filesystem_id")), }, }, "filesystem_id": schema.StringAttribute{ - Description: "File System Snapshot name. Conflicts with `id` and `name`.", - MarkdownDescription: "File System Snapshot name. Conflicts with `id` and `name`.", + Description: "Parent ID of the Snapshot. Conflicts with `id` and `nas_server_id`.", + MarkdownDescription: "Parent ID of the Snapshot. Conflicts with `id` and `nas_server_id`.", + Optional: true, + Validators: []validator.String{ + stringvalidator.LengthAtLeast(1), + stringvalidator.ConflictsWith(path.MatchRoot("nas_server_id")), + }, + }, + + "nas_server_id": schema.StringAttribute{ + Description: "Nas Server ID of the Snapshot. Conflicts with `id` and `filesystem_id`.", + MarkdownDescription: "Nas Server ID of the Snapshot. Conflicts with `id` and `filesystem_id`.", Optional: true, Validators: []validator.String{ stringvalidator.LengthAtLeast(1), @@ -118,27 +129,37 @@ func (d *fileSystemSnapshotDataSource) Read(ctx context.Context, req datasource. if resp.Diagnostics.HasError() { return } - - //Read the snapshot based on snapshot id/name/filesystem id and if nothing is mentioned, then it returns all the snapshots - if state.Name.ValueString() != "" { - fileSystemSnapshot, err = d.client.PStoreClient.GetFSByName(context.Background(), state.Name.ValueString()) - fileSystemSnapshots = append(fileSystemSnapshots, fileSystemSnapshot) - } else if state.ID.ValueString() != "" { + if state.ID.ValueString() != "" { fileSystemSnapshot, err = d.client.PStoreClient.GetFsSnapshot(context.Background(), state.ID.ValueString()) + if err != nil { + resp.Diagnostics.AddError( + "Unable to Read PowerStore File System Snapshot by ID: "+state.ID.ValueString(), + err.Error(), + ) + return + } fileSystemSnapshots = append(fileSystemSnapshots, fileSystemSnapshot) - } else if state.FileSystemID.ValueString() != "" { - fileSystemSnapshots, err = d.client.PStoreClient.GetFsSnapshotsByVolumeID(context.Background(), state.FileSystemID.ValueString()) } else { - fileSystemSnapshots, err = d.client.PStoreClient.GetFsSnapshots(context.Background()) - } - - //check if there is any error while getting the fileSystemSnapshot snapshot - if err != nil { - resp.Diagnostics.AddError( - "Unable to Read PowerStore fileSystemSnapshot Snapshots", - err.Error(), - ) - return + filterMap := make(map[string]string) + filterMap["filesystem_type"] = fmt.Sprintf("eq.%s", gopowerstore.FileSystemTypeEnumSnapshot) + if state.Name.ValueString() != "" { + filterMap["name"] = fmt.Sprintf("eq.%s", state.Name.ValueString()) + } + if state.FileSystemID.ValueString() != "" { + filterMap["parent_id"] = fmt.Sprintf("eq.%s", state.FileSystemID.ValueString()) + } + if state.NasServerID.ValueString() != "" { + filterMap["nas_server_id"] = fmt.Sprintf("eq.%s", state.NasServerID.ValueString()) + } + tflog.Debug(ctx, fmt.Sprintf("PK Filter Map: %v", filterMap)) + fileSystemSnapshots, err = d.client.PStoreClient.GetFsByFilter(context.Background(), filterMap) + if err != nil { + resp.Diagnostics.AddError( + "Unable to Read PowerStore File System Snapshots", + err.Error(), + ) + return + } } state.FileSystemSnapshots = updateFileSystemState(fileSystemSnapshots) diff --git a/powerstore/datasource_filesystem_snapshot_test.go b/powerstore/datasource_filesystem_snapshot_test.go index da22c8a..a78fb44 100644 --- a/powerstore/datasource_filesystem_snapshot_test.go +++ b/powerstore/datasource_filesystem_snapshot_test.go @@ -36,16 +36,35 @@ func TestAccFileSystemSnapshotDs(t *testing.T) { ProtoV6ProviderFactories: testProviderFactory, Steps: []resource.TestStep{ { - Config: ProviderConfigForTesting + FileSystemSnapshotDataSourceparamsName, + Config: ProviderConfigForTesting + FileSystemSnapshotDataSourceParamsNasServerID, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("data.powerstore_filesystem_snapshot.test", "filesystem_snapshots.0.nas_server_id", nasServerID), + ), }, { - Config: ProviderConfigForTesting + FileSystemSnapshotDataSourceparamsID, + Config: ProviderConfigForTesting + FileSystemSnapshotDataSourceparamsFileSystemID, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttrPair("data.powerstore_filesystem_snapshot.test1", "filesystem_snapshots.0.filesystem_id", "powerstore_filesystem.test_fs_create", "id"), + ), }, { - Config: ProviderConfigForTesting + FileSystemSnapshotDataSourceparamsFileSystemID, + Config: ProviderConfigForTesting + FileSystemSnapshotDataSourceparamsName, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttrPair("data.powerstore_filesystem_snapshot.test2", "filesystem_snapshots.0.name", "powerstore_filesystem_snapshot.test", "name"), + ), }, + { + Config: ProviderConfigForTesting + FileSystemSnapshotDataSourceparamsID, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttrPair("data.powerstore_filesystem_snapshot.test3", "filesystem_snapshots.0.id", "powerstore_filesystem_snapshot.test", "id"), + ), + }, + { Config: ProviderConfigForTesting + FileSystemSnapshotDataSourceparamsAll, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttrSet("data.powerstore_filesystem_snapshot.test4", "filesystem_snapshots.#"), + ), }, { Config: ProviderConfigForTesting + FileSystemSnapshotDataSourceparamsIDAndNameNegative, @@ -60,67 +79,75 @@ func TestAccFileSystemSnapshotDs(t *testing.T) { ExpectError: regexp.MustCompile("Invalid Attribute Value Length"), }, { - Config: ProviderConfigForTesting + FileSystemSnapshotDataSourceparamsNameNegative, - ExpectError: regexp.MustCompile("Unable to Read PowerStore fileSystemSnapshot Snapshots"), + Config: ProviderConfigForTesting + FileSystemSnapshotDataSourceparamsNameNegative, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.powerstore_filesystem_snapshot.test6", "filesystem_snapshots.#", "0"), + ), }, { Config: ProviderConfigForTesting + FileSystemSnapshotDataSourceparamsIDNegative, - ExpectError: regexp.MustCompile("Unable to Read PowerStore fileSystemSnapshot Snapshots"), + ExpectError: regexp.MustCompile("Unable to Read PowerStore File System Snapshot by ID"), }, }, }) } -var FileSystemSnapshotDataSourceparamsFileSystemID = ` +var FileSystemSnapshotDataSourceParamsNasServerID = ` +data "powerstore_filesystem_snapshot" "test" { + nas_server_id = "` + nasServerID + `" +}` + +var FileSystemSnapshotDataSourceparamsFileSystemID = FileSystemSnapParamsCreate + ` data "powerstore_filesystem_snapshot" "test1" { - filesystem_id = "` + fileSystemID + `" + filesystem_id = powerstore_filesystem.test_fs_create.id + depends_on = [powerstore_filesystem.test_fs_create, powerstore_filesystem_snapshot.test] } ` -var FileSystemSnapshotDataSourceparamsName = ` -data "powerstore_filesystem_snapshot" "test1" { - name = "` + fileSystemSnapshotName + `" +var FileSystemSnapshotDataSourceparamsName = FileSystemSnapParamsCreate + ` +data "powerstore_filesystem_snapshot" "test2" { + name = powerstore_filesystem_snapshot.test.name } ` -var FileSystemSnapshotDataSourceparamsNameNegative = ` -data "powerstore_filesystem_snapshot" "test1" { - name = "invalid-name" +var FileSystemSnapshotDataSourceparamsID = FileSystemSnapParamsCreate + ` +data "powerstore_filesystem_snapshot" "test3" { + id = powerstore_filesystem_snapshot.test.id } ` -var FileSystemSnapshotDataSourceparamsID = ` -data "powerstore_filesystem_snapshot" "test1" { - id = "` + fileSystemSnapshotID + `" +var FileSystemSnapshotDataSourceparamsAll = ` +data "powerstore_filesystem_snapshot" "test4" { } ` -var FileSystemSnapshotDataSourceparamsAll = ` -data "powerstore_filesystem_snapshot" "test1" { +var FileSystemSnapshotDataSourceparamsIDAndNameNegative = ` +data "powerstore_filesystem_snapshot" "test5" { + id = "unique-id" + name = "unique-name" } ` -var FileSystemSnapshotDataSourceparamsIDNegative = ` -data "powerstore_filesystem_snapshot" "test1" { - id = "invalid-id" +var FileSystemSnapshotDataSourceparamsNameNegative = ` +data "powerstore_filesystem_snapshot" "test6" { + name = "invalid-name" } ` -var FileSystemSnapshotDataSourceparamsIDAndNameNegative = ` -data "powerstore_filesystem_snapshot" "test1" { - id = "` + fileSystemSnapshotID + `" - name = "` + fileSystemSnapshotName + `" +var FileSystemSnapshotDataSourceparamsIDNegative = ` +data "powerstore_filesystem_snapshot" "test7" { + id = "invalid-id" } ` var FileSystemSnapshotDataSourceparamsEmptyIDNegative = ` -data "powerstore_filesystem_snapshot" "test1" { +data "powerstore_filesystem_snapshot" "test8" { id = "" } ` var FileSystemSnapshotDataSourceparamsEmptyNameNegative = ` -data "powerstore_filesystem_snapshot" "test1" { +data "powerstore_filesystem_snapshot" "test9" { name = "" } ` diff --git a/powerstore/resource_filesystem_snapshot_test.go b/powerstore/resource_filesystem_snapshot_test.go index 1ccca0d..7c93f42 100644 --- a/powerstore/resource_filesystem_snapshot_test.go +++ b/powerstore/resource_filesystem_snapshot_test.go @@ -90,29 +90,32 @@ resource "powerstore_filesystem_snapshot" "test" { } ` -var FileSystemSnapParamsCreate = ` +var FileSystemSnapParamsCreate = FsParams + ` resource "powerstore_filesystem_snapshot" "test" { name = "tf_fs_snap_acc" description = "Test File System Snapshot Resource" - filesystem_id="` + fileSystemID + `" + filesystem_id=powerstore_filesystem.test_fs_create.id expiration_timestamp="2035-05-06T09:01:47Z" access_type = "Snapshot" + depends_on = [powerstore_filesystem.test_fs_create] } ` -var FileSystemSnapParamsUpdate = ` +var FileSystemSnapParamsUpdate = FsParams + ` resource "powerstore_filesystem_snapshot" "test" { name = "tf_fs_snap_acc" description = "Test File System Snapshot Resource Updated" - filesystem_id="` + fileSystemID + `" + filesystem_id=powerstore_filesystem.test_fs_create.id expiration_timestamp="2035-10-06T09:01:47Z" access_type = "Snapshot" + depends_on = [powerstore_filesystem.test_fs_create] } ` -var FileSystemSnapParamsUpdateError = ` +var FileSystemSnapParamsUpdateError = FsParams + ` resource "powerstore_filesystem_snapshot" "test" { name = "invalid" description = "Test File System Snapshot Resource Updated" filesystem_id="invalid" expiration_timestamp="2035-10-06T09:01:47Z" + depends_on = [powerstore_filesystem.test_fs_create] } `