From b566baa08810b93d3d7e0b40d1b3273ab2f9ebe8 Mon Sep 17 00:00:00 2001 From: Priyanshu Krishnan Date: Mon, 6 Jan 2025 23:53:50 -0500 Subject: [PATCH] added filesystem snapshot datasource --- docs/data-sources/filesystem_snapshot.md | 83 +++++++++ .../data-source.tf | 70 ++++++++ .../provider.tf | 33 ++++ .../variables.tf | 36 ++++ .../resource.tf | 10 +- models/filesystem_datasource.go | 55 ++++++ models/filesystem_snapshot_datasource.go | 28 +++ powerstore/datasource_filesystem_helper.go | 67 +++++++ powerstore/datasource_filesystem_schema.go | 168 ++++++++++++++++++ powerstore/datasource_filesystem_snapshot.go | 151 ++++++++++++++++ .../datasource_filesystem_snapshot_test.go | 126 +++++++++++++ powerstore/provider.go | 1 + powerstore/provider_test.go | 2 + 13 files changed, 825 insertions(+), 5 deletions(-) create mode 100644 docs/data-sources/filesystem_snapshot.md create mode 100644 examples/data-sources/powerstore_filesystem_snapshot/data-source.tf create mode 100644 examples/data-sources/powerstore_filesystem_snapshot/provider.tf create mode 100644 examples/data-sources/powerstore_filesystem_snapshot/variables.tf create mode 100644 models/filesystem_datasource.go create mode 100644 models/filesystem_snapshot_datasource.go create mode 100644 powerstore/datasource_filesystem_helper.go create mode 100644 powerstore/datasource_filesystem_schema.go create mode 100644 powerstore/datasource_filesystem_snapshot.go create mode 100644 powerstore/datasource_filesystem_snapshot_test.go diff --git a/docs/data-sources/filesystem_snapshot.md b/docs/data-sources/filesystem_snapshot.md new file mode 100644 index 00000000..d8723089 --- /dev/null +++ b/docs/data-sources/filesystem_snapshot.md @@ -0,0 +1,83 @@ +--- +# Copyright (c) 2024 Dell Inc., or its subsidiaries. All Rights Reserved. +# +# Licensed under the Mozilla Public License Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://mozilla.org/MPL/2.0/ +# +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +title: "powerstore_filesystem_snapshot data source" +linkTitle: "powerstore_filesystem_snapshot" +page_title: "powerstore_filesystem_snapshot Data Source - powerstore" +subcategory: "" +description: |- + This datasource is used to query the existing File System Snapshot from PowerStore array. The information fetched from this datasource can be used for getting the details for further processing in resource block. +--- + +# powerstore_filesystem_snapshot (Data Source) + +This datasource is used to query the existing File System Snapshot from PowerStore array. The information fetched from this datasource can be used for getting the details for further processing in resource block. + +## Example Usage + +{{tffile "/root/Dev/terraform-provider-powerstore/examples/data-sources/powerstore_filesystem_snapshot/data-source.tf"}} + + +## Schema + +### Optional + +- `filesystem_id` (String) File System Snapshot name. Conflicts with `id` and `name`. +- `id` (String) Unique identifier of the File System Snapshot. Conflicts with `name` and `filesystem_id`. +- `name` (String) File System Snapshot name. Conflicts with `id` and `filesystem_id`. + +### Read-Only + +- `filesystem_snapshots` (Attributes List) List of File System Snapshots. (see [below for nested schema](#nestedatt--filesystem_snapshots)) + + +### Nested Schema for `filesystem_snapshots` + +Read-Only: + +- `access_policy` (String) Access Policy +- `access_type` (String) Access Type +- `config_type` (String) Config Type +- `description` (String) Description +- `expiration_timestamp` (String) Expiration Timestamp +- `filesystem_type` (String) Filesystem Type +- `flr_attributes` (Attributes) Flr Attributes (see [below for nested schema](#nestedatt--filesystem_snapshots--flr_attributes)) +- `folder_rename_policy` (String) Folder Rename Policy +- `id` (String) ID +- `is_async_m_time_enabled` (Boolean) Is Async MTime Enabled +- `is_smb_no_notify_enabled` (Boolean) Is Smb No Notify Enabled +- `is_smb_notify_on_access_enabled` (Boolean) Is Smb Notify On Access Enabled +- `is_smb_notify_on_write_enabled` (Boolean) Is Smb Notify On Write Enabled +- `is_smb_op_locks_enabled` (Boolean) Is Smb Op Locks Enabled +- `is_smb_sync_writes_enabled` (Boolean) Is Smb Sync Writes Enabled +- `locking_policy` (String) Locking Policy +- `name` (String) Name +- `nas_server_id` (String) Nas Server ID +- `parent_id` (String) Parent ID +- `protection_policy_id` (String) Protection Policy ID +- `size_total` (Number) Size Total +- `size_used` (Number) Size Used +- `smb_notify_on_change_dir_depth` (Number) Smb Notify On Change Dir Depth + + +### Nested Schema for `filesystem_snapshots.flr_attributes` + +Read-Only: + +- `default_retention` (String) Default Retention +- `maximum_retention` (String) Maximum Retention +- `minimum_retention` (String) Minimum Retention +- `mode` (String) Mode \ No newline at end of file diff --git a/examples/data-sources/powerstore_filesystem_snapshot/data-source.tf b/examples/data-sources/powerstore_filesystem_snapshot/data-source.tf new file mode 100644 index 00000000..790a1990 --- /dev/null +++ b/examples/data-sources/powerstore_filesystem_snapshot/data-source.tf @@ -0,0 +1,70 @@ +/* +Copyright (c) 2024 Dell Inc., or its subsidiaries. All Rights Reserved. + +Licensed under the Mozilla Public License Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://mozilla.org/MPL/2.0/ + + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +# commands to run this tf file : terraform init && terraform apply --auto-approve +# This datasource reads hosts either by id or name where user can provide a value to any one of them +# If it is a empty datsource block , then it will read all the filesystem +# If id or name is provided then it reads a particular file system snapshot with that id or name +# If filesystem_id is provided then it will read all the filesystem snapshots within filesystem +# Only one of the attribute can be provided among id or name or filesystem_id + + +// create filesystem +resource "powerstore_filesystem" "test" { + name = "test_fs" + description = "testing file system" + size = 3 + nas_server_id = "" + flr_attributes = { + mode = "Enterprise" + minimum_retention = "1D" + default_retention = "1D" + maximum_retention = "infinite" + } + config_type = "General" + access_policy = "UNIX" + locking_policy = "Advisory" + folder_rename_policy = "All_Allowed" + is_smb_sync_writes_enabled = true + is_smb_no_notify_enabled = true + is_smb_op_locks_enabled = false + is_smb_notify_on_access_enabled = true + is_smb_notify_on_write_enabled = false + smb_notify_on_change_dir_depth = 12 + is_async_mtime_enabled = true + file_events_publishing_mode = "All" +} + +// create snapshot from filesystem +resource "powerstore_filesystem_snapshot" "test" { + name = "tf_fs_snap" + description = "Test File System Snapshot Resource" + filesystem_id = resource.powerstore_filesystem.test.id + expiration_timestamp = "2035-05-06T09:01:47Z" + access_type = "Snapshot" +} + +data "powerstore_filesystem_snapshot" "test1" { + name = resource.powerstore_filesystem_snapshot.test.name + # id = resource.powerstore_filesystem_snapshot.test.id + # filesystem_id= resource.powerstore_filesystem.test.id +} + + +output "fileSystemSnapshotResult" { + value = data.powerstore_filesystem_snapshot.test1.filesystem_snapshots +} diff --git a/examples/data-sources/powerstore_filesystem_snapshot/provider.tf b/examples/data-sources/powerstore_filesystem_snapshot/provider.tf new file mode 100644 index 00000000..dde25fa9 --- /dev/null +++ b/examples/data-sources/powerstore_filesystem_snapshot/provider.tf @@ -0,0 +1,33 @@ +/* +Copyright (c) 2024 Dell Inc., or its subsidiaries. All Rights Reserved. + +Licensed under the Mozilla Public License Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://mozilla.org/MPL/2.0/ + + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +terraform { + required_providers { + powerstore = { + version = "1.2.0" + source = "registry.terraform.io/dell/powerstore" + } + } +} + +provider "powerstore" { + username = var.username + password = var.password + endpoint = var.endpoint + insecure = true + timeout = var.timeout +} \ No newline at end of file diff --git a/examples/data-sources/powerstore_filesystem_snapshot/variables.tf b/examples/data-sources/powerstore_filesystem_snapshot/variables.tf new file mode 100644 index 00000000..b079f08a --- /dev/null +++ b/examples/data-sources/powerstore_filesystem_snapshot/variables.tf @@ -0,0 +1,36 @@ +/* +Copyright (c) 2024 Dell Inc., or its subsidiaries. All Rights Reserved. + +Licensed under the Mozilla Public License Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://mozilla.org/MPL/2.0/ + + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +variable "username" { + type = string + description = "Stores the username of PowerStore host." +} + +variable "password" { + type = string + description = "Stores the password of PowerStore host." +} + +variable "timeout" { + type = string + description = "Stores the timeout of PowerStore host." +} + +variable "endpoint" { + type = string + description = "Stores the endpoint of PowerStore host. eg: https://10.1.1.1/api/rest" +} \ No newline at end of file diff --git a/examples/resources/powerstore_filesystem_snapshot/resource.tf b/examples/resources/powerstore_filesystem_snapshot/resource.tf index 8db3e7c1..5c405094 100644 --- a/examples/resources/powerstore_filesystem_snapshot/resource.tf +++ b/examples/resources/powerstore_filesystem_snapshot/resource.tf @@ -26,9 +26,9 @@ limitations under the License. # To check which attributes of the file system snapshot resource can be updated, please refer Product Guide in the documentation resource "powerstore_filesystem_snapshot" "test1" { - name = "tf_fs_snap" - description = "Test File System Snapshot Resource" - filesystem_id="67608dc7-b69c-b762-0522-42848bc63a0b" - expiration_timestamp="2035-05-06T09:01:47Z" - access_type = "Snapshot" + name = "tf_fs_snap" + description = "Test File System Snapshot Resource" + filesystem_id = "67608dc7-b69c-b762-0522-42848bc63a0b" + expiration_timestamp = "2035-05-06T09:01:47Z" + access_type = "Snapshot" } \ No newline at end of file diff --git a/models/filesystem_datasource.go b/models/filesystem_datasource.go new file mode 100644 index 00000000..55ece885 --- /dev/null +++ b/models/filesystem_datasource.go @@ -0,0 +1,55 @@ +/* +Copyright (c) 2024 Dell Inc., or its subsidiaries. All Rights Reserved. + +Licensed under the Mozilla Public License Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://mozilla.org/MPL/2.0/ + + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package models + +import "github.com/hashicorp/terraform-plugin-framework/types" + +// FileSystemDatasource represents filesystem +type FileSystemDatasource struct { + AccessPolicy types.String `tfsdk:"access_policy"` + AccessType types.String `tfsdk:"access_type"` + ConfigType types.String `tfsdk:"config_type"` + Description types.String `tfsdk:"description"` + ExpirationTimestamp types.String `tfsdk:"expiration_timestamp"` + FilesystemType types.String `tfsdk:"filesystem_type"` + FlrAttributes FLRAttributesDatasource `tfsdk:"flr_attributes"` + FolderRenamePolicy types.String `tfsdk:"folder_rename_policy"` + ID types.String `tfsdk:"id"` + IsAsyncMTimeEnabled types.Bool `tfsdk:"is_async_m_time_enabled"` + IsSmbNoNotifyEnabled types.Bool `tfsdk:"is_smb_no_notify_enabled"` + IsSmbNotifyOnAccessEnabled types.Bool `tfsdk:"is_smb_notify_on_access_enabled"` + IsSmbNotifyOnWriteEnabled types.Bool `tfsdk:"is_smb_notify_on_write_enabled"` + IsSmbOpLocksEnabled types.Bool `tfsdk:"is_smb_op_locks_enabled"` + IsSmbSyncWritesEnabled types.Bool `tfsdk:"is_smb_sync_writes_enabled"` + LockingPolicy types.String `tfsdk:"locking_policy"` + Name types.String `tfsdk:"name"` + NasServerID types.String `tfsdk:"nas_server_id"` + ParentID types.String `tfsdk:"parent_id"` + ProtectionPolicyID types.String `tfsdk:"protection_policy_id"` + SizeTotal types.Int64 `tfsdk:"size_total"` + SizeUsed types.Int64 `tfsdk:"size_used"` + SmbNotifyOnChangeDirDepth types.Int64 `tfsdk:"smb_notify_on_change_dir_depth"` +} + +// FLRAttributesDatasource represents flr attributes +type FLRAttributesDatasource struct { + DefaultRetention types.String `tfsdk:"default_retention"` + MaximumRetention types.String `tfsdk:"maximum_retention"` + MinimumRetention types.String `tfsdk:"minimum_retention"` + Mode types.String `tfsdk:"mode"` +} diff --git a/models/filesystem_snapshot_datasource.go b/models/filesystem_snapshot_datasource.go new file mode 100644 index 00000000..8c6fd5d8 --- /dev/null +++ b/models/filesystem_snapshot_datasource.go @@ -0,0 +1,28 @@ +/* +Copyright (c) 2024 Dell Inc., or its subsidiaries. All Rights Reserved. + +Licensed under the Mozilla Public License Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://mozilla.org/MPL/2.0/ + + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package models + +import "github.com/hashicorp/terraform-plugin-framework/types" + +// FileSysteSnapshotDataSource is the schema for the FileSysteSnapshotDataSource datasource +type FileSysteSnapshotDataSource struct { + ID types.String `tfsdk:"id"` + Name types.String `tfsdk:"name"` + FileSystemID types.String `tfsdk:"filesystem_id"` + FileSystemSnapshots []FileSystemDatasource `tfsdk:"filesystem_snapshots"` +} diff --git a/powerstore/datasource_filesystem_helper.go b/powerstore/datasource_filesystem_helper.go new file mode 100644 index 00000000..578985db --- /dev/null +++ b/powerstore/datasource_filesystem_helper.go @@ -0,0 +1,67 @@ +/* +Copyright (c) 2024 Dell Inc., or its subsidiaries. All Rights Reserved. + +Licensed under the Mozilla Public License Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://mozilla.org/MPL/2.0/ + + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package powerstore + +import ( + "terraform-provider-powerstore/models" + + "github.com/dell/gopowerstore" + "github.com/hashicorp/terraform-plugin-framework/types" +) + +// updateFileSystemState returns the list of filesystems +func updateFileSystemState(filesystems []gopowerstore.FileSystem) (response []models.FileSystemDatasource) { + for _, filesystem := range filesystems { + response = append(response, newFileSystem(filesystem)) + } + return response +} + +// newFileSystem returns a new filesystem +func newFileSystem(input gopowerstore.FileSystem) models.FileSystemDatasource { + return models.FileSystemDatasource{ + AccessPolicy: types.StringValue(input.AccessPolicy), + AccessType: types.StringValue(input.AccessType), + ConfigType: types.StringValue(input.ConfigType), + Description: types.StringValue(input.Description), + ExpirationTimestamp: types.StringValue(input.ExpirationTimestamp), + FilesystemType: types.StringValue(string(input.FilesystemType)), + FolderRenamePolicy: types.StringValue(input.FolderRenamePolicy), + ID: types.StringValue(input.ID), + IsAsyncMTimeEnabled: types.BoolValue(input.IsAsyncMTimeEnabled), + IsSmbNoNotifyEnabled: types.BoolValue(input.IsSmbNoNotifyEnabled), + IsSmbNotifyOnAccessEnabled: types.BoolValue(input.IsSmbNotifyOnAccessEnabled), + IsSmbNotifyOnWriteEnabled: types.BoolValue(input.IsSmbNotifyOnWriteEnabled), + IsSmbOpLocksEnabled: types.BoolValue(input.IsSmbOpLocksEnabled), + IsSmbSyncWritesEnabled: types.BoolValue(input.IsSmbSyncWritesEnabled), + LockingPolicy: types.StringValue(input.LockingPolicy), + Name: types.StringValue(input.Name), + NasServerID: types.StringValue(input.NasServerID), + ParentID: types.StringValue(input.ParentID), + ProtectionPolicyID: types.StringValue(input.ProtectionPolicyID), + SizeTotal: types.Int64Value(int64(input.SizeTotal)), + SizeUsed: types.Int64Value(int64(input.SizeUsed)), + SmbNotifyOnChangeDirDepth: types.Int64Value(int64(input.SmbNotifyOnChangeDirDepth)), + FlrAttributes: models.FLRAttributesDatasource{ + DefaultRetention: types.StringValue(input.FlrCreate.DefaultRetention), + MaximumRetention: types.StringValue(input.FlrCreate.MaximumRetention), + MinimumRetention: types.StringValue(input.FlrCreate.MinimumRetention), + Mode: types.StringValue(input.FlrCreate.Mode), + }, + } +} diff --git a/powerstore/datasource_filesystem_schema.go b/powerstore/datasource_filesystem_schema.go new file mode 100644 index 00000000..70657bd4 --- /dev/null +++ b/powerstore/datasource_filesystem_schema.go @@ -0,0 +1,168 @@ +/* +Copyright (c) 2024 Dell Inc., or its subsidiaries. All Rights Reserved. + +Licensed under the Mozilla Public License Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://mozilla.org/MPL/2.0/ + + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package powerstore + +import "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + +// FileSystemDatasourceSchema is a function that returns the schema for filesystem +func FileSystemDatasourceSchema() map[string]schema.Attribute { + return map[string]schema.Attribute{ + "access_policy": schema.StringAttribute{ + MarkdownDescription: "Access Policy", + Description: "Access Policy", + Computed: true, + }, + "access_type": schema.StringAttribute{ + MarkdownDescription: "Access Type", + Description: "Access Type", + Computed: true, + }, + "config_type": schema.StringAttribute{ + MarkdownDescription: "Config Type", + Description: "Config Type", + Computed: true, + }, + "description": schema.StringAttribute{ + MarkdownDescription: "Description", + Description: "Description", + Computed: true, + }, + "expiration_timestamp": schema.StringAttribute{ + MarkdownDescription: "Expiration Timestamp", + Description: "Expiration Timestamp", + Computed: true, + }, + "filesystem_type": schema.StringAttribute{ + MarkdownDescription: "Filesystem Type", + Description: "Filesystem Type", + Computed: true, + }, + "flr_attributes": schema.SingleNestedAttribute{ + MarkdownDescription: "Flr Attributes", + Description: "Flr Attributes", + Computed: true, + Attributes: FLRAttributeSchema(), + }, + "folder_rename_policy": schema.StringAttribute{ + MarkdownDescription: "Folder Rename Policy", + Description: "Folder Rename Policy", + Computed: true, + }, + "id": schema.StringAttribute{ + MarkdownDescription: "ID", + Description: "ID", + Computed: true, + }, + "is_async_m_time_enabled": schema.BoolAttribute{ + MarkdownDescription: "Is Async MTime Enabled", + Description: "Is Async MTime Enabled", + Computed: true, + }, + "is_smb_no_notify_enabled": schema.BoolAttribute{ + MarkdownDescription: "Is Smb No Notify Enabled", + Description: "Is Smb No Notify Enabled", + Computed: true, + }, + "is_smb_notify_on_access_enabled": schema.BoolAttribute{ + MarkdownDescription: "Is Smb Notify On Access Enabled", + Description: "Is Smb Notify On Access Enabled", + Computed: true, + }, + "is_smb_notify_on_write_enabled": schema.BoolAttribute{ + MarkdownDescription: "Is Smb Notify On Write Enabled", + Description: "Is Smb Notify On Write Enabled", + Computed: true, + }, + "is_smb_op_locks_enabled": schema.BoolAttribute{ + MarkdownDescription: "Is Smb Op Locks Enabled", + Description: "Is Smb Op Locks Enabled", + Computed: true, + }, + "is_smb_sync_writes_enabled": schema.BoolAttribute{ + MarkdownDescription: "Is Smb Sync Writes Enabled", + Description: "Is Smb Sync Writes Enabled", + Computed: true, + }, + "locking_policy": schema.StringAttribute{ + MarkdownDescription: "Locking Policy", + Description: "Locking Policy", + Computed: true, + }, + "name": schema.StringAttribute{ + MarkdownDescription: "Name", + Description: "Name", + Computed: true, + }, + "nas_server_id": schema.StringAttribute{ + MarkdownDescription: "Nas Server ID", + Description: "Nas Server ID", + Computed: true, + }, + "parent_id": schema.StringAttribute{ + MarkdownDescription: "Parent ID", + Description: "Parent ID", + Computed: true, + }, + "protection_policy_id": schema.StringAttribute{ + MarkdownDescription: "Protection Policy ID", + Description: "Protection Policy ID", + Computed: true, + }, + "size_total": schema.Int64Attribute{ + MarkdownDescription: "Size Total", + Description: "Size Total", + Computed: true, + }, + "size_used": schema.Int64Attribute{ + MarkdownDescription: "Size Used", + Description: "Size Used", + Computed: true, + }, + "smb_notify_on_change_dir_depth": schema.Int64Attribute{ + MarkdownDescription: "Smb Notify On Change Dir Depth", + Description: "Smb Notify On Change Dir Depth", + Computed: true, + }, + } +} + +// FLRAttributeSchema is a function that returns the schema for FLR attributes +func FLRAttributeSchema() map[string]schema.Attribute { + return map[string]schema.Attribute{ + "default_retention": schema.StringAttribute{ + MarkdownDescription: "Default Retention", + Description: "Default Retention", + Computed: true, + }, + "maximum_retention": schema.StringAttribute{ + MarkdownDescription: "Maximum Retention", + Description: "Maximum Retention", + Computed: true, + }, + "minimum_retention": schema.StringAttribute{ + MarkdownDescription: "Minimum Retention", + Description: "Minimum Retention", + Computed: true, + }, + "mode": schema.StringAttribute{ + MarkdownDescription: "Mode", + Description: "Mode", + Computed: true, + }, + } +} diff --git a/powerstore/datasource_filesystem_snapshot.go b/powerstore/datasource_filesystem_snapshot.go new file mode 100644 index 00000000..4afe9e70 --- /dev/null +++ b/powerstore/datasource_filesystem_snapshot.go @@ -0,0 +1,151 @@ +/* +Copyright (c) 2024 Dell Inc., or its subsidiaries. All Rights Reserved. + +Licensed under the Mozilla Public License Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://mozilla.org/MPL/2.0/ + + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package powerstore + +import ( + "context" + "terraform-provider-powerstore/client" + "terraform-provider-powerstore/models" + + "github.com/dell/gopowerstore" + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" + "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-framework/path" + "github.com/hashicorp/terraform-plugin-framework/schema/validator" + "github.com/hashicorp/terraform-plugin-framework/types" +) + +var ( + _ datasource.DataSource = &fileSystemSnapshotDataSource{} + _ datasource.DataSourceWithConfigure = &fileSystemSnapshotDataSource{} +) + +// newfileSystemSnapshotDataSource returns the fileSystemSnapshot snapshot data source object +func newFileSystemSnapshotDataSource() datasource.DataSource { + return &fileSystemSnapshotDataSource{} +} + +// fileSystemSnapshotDataSource is the data source implementation +type fileSystemSnapshotDataSource struct { + client *client.Client +} + +// Metadata returns the data source type name +func (d *fileSystemSnapshotDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_filesystem_snapshot" +} + +// Schema defines the schema for the data source +func (d *fileSystemSnapshotDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { + resp.Schema = schema.Schema{ + Description: "This datasource is used to query the existing File System Snapshot from PowerStore array. The information fetched from this datasource can be used for getting the details for further processing in resource block.", + MarkdownDescription: "This datasource is used to query the existing File System Snapshot from PowerStore array. The information fetched from this datasource can be used for getting the details for further processing in resource block.", + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + Description: "Unique identifier of the File System Snapshot. Conflicts with `name` and `filesystem_id`.", + MarkdownDescription: "Unique identifier of the File System Snapshot. Conflicts with `name` and `filesystem_id`.", + Optional: true, + Computed: true, + Validators: []validator.String{ + stringvalidator.ConflictsWith(path.MatchRoot("name"), path.MatchRoot("filesystem_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`.", + 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`.", + Optional: true, + Validators: []validator.String{ + stringvalidator.LengthAtLeast(1), + }, + }, + + "filesystem_snapshots": schema.ListNestedAttribute{ + Description: "List of File System Snapshots.", + MarkdownDescription: "List of File System Snapshots.", + Computed: true, + NestedObject: schema.NestedAttributeObject{ + Attributes: FileSystemDatasourceSchema()}, + }, + }, + } + +} + +// Configure adds the provider configured client to the data source +func (d *fileSystemSnapshotDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) { + if req.ProviderData == nil { + return + } + d.client = req.ProviderData.(*client.Client) +} + +// Read refreshes the Terraform state with the latest data +func (d *fileSystemSnapshotDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var state models.FileSysteSnapshotDataSource + var fileSystemSnapshots []gopowerstore.FileSystem + var fileSystemSnapshot gopowerstore.FileSystem + var err error + + diags := req.Config.Get(ctx, &state) + resp.Diagnostics.Append(diags...) + 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() != "" { + fileSystemSnapshot, err = d.client.PStoreClient.GetFsSnapshot(context.Background(), state.ID.ValueString()) + 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 + } + + state.FileSystemSnapshots = updateFileSystemState(fileSystemSnapshots) + state.ID = types.StringValue("placeholder") + diags = resp.State.Set(ctx, state) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } +} diff --git a/powerstore/datasource_filesystem_snapshot_test.go b/powerstore/datasource_filesystem_snapshot_test.go new file mode 100644 index 00000000..f27f4f72 --- /dev/null +++ b/powerstore/datasource_filesystem_snapshot_test.go @@ -0,0 +1,126 @@ +/* +Copyright (c) 2024 Dell Inc., or its subsidiaries. All Rights Reserved. + +Licensed under the Mozilla Public License Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://mozilla.org/MPL/2.0/ + + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package powerstore + +import ( + "os" + "regexp" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +// Test to Fetch File System Snapshots +func TestAccFileSystemSnapshotDs(t *testing.T) { + if os.Getenv("TF_ACC") == "" { + t.Skip("Dont run with units tests because it will try to create the context") + } + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testProviderFactory, + Steps: []resource.TestStep{ + { + Config: ProviderConfigForTesting + FileSystemSnapshotDataSourceparamsName, + }, + { + Config: ProviderConfigForTesting + FileSystemSnapshotDataSourceparamsID, + }, + { + Config: ProviderConfigForTesting + FileSystemSnapshotDataSourceparamsFileSystemID, + }, + { + Config: ProviderConfigForTesting + FileSystemSnapshotDataSourceparamsAll, + }, + { + Config: ProviderConfigForTesting + FileSystemSnapshotDataSourceparamsIDAndNameNegative, + ExpectError: regexp.MustCompile("Invalid Attribute Combination"), + }, + { + Config: ProviderConfigForTesting + FileSystemSnapshotDataSourceparamsEmptyIDNegative, + ExpectError: regexp.MustCompile("Invalid Attribute Value Length"), + }, + { + Config: ProviderConfigForTesting + FileSystemSnapshotDataSourceparamsEmptyNameNegative, + ExpectError: regexp.MustCompile("Invalid Attribute Value Length"), + }, + { + Config: ProviderConfigForTesting + FileSystemSnapshotDataSourceparamsNameNegative, + ExpectError: regexp.MustCompile("Unable to Read PowerStore fileSystemSnapshot Snapshots"), + }, + { + Config: ProviderConfigForTesting + FileSystemSnapshotDataSourceparamsIDNegative, + ExpectError: regexp.MustCompile("Unable to Read PowerStore fileSystemSnapshot Snapshots"), + }, + }, + }) +} + +var FileSystemSnapshotDataSourceparamsFileSystemID = ` +data "powerstore_filesystem_snapshot" "test1" { + filesystem_id = "` + fileSystemID + `" +} +` + +var FileSystemSnapshotDataSourceparamsName = ` +data "powerstore_filesystem_snapshot" "test1" { + name = "` + fileSystemSnapshotName + `" +} +` + +var FileSystemSnapshotDataSourceparamsNameNegative = ` +data "powerstore_filesystem_snapshot" "test1" { + name = "invalid-name" +} +` + +var FileSystemSnapshotDataSourceparamsID = ` +data "powerstore_filesystem_snapshot" "test1" { + id = "` + fileSystemSnapshotID + `" +} +` + +var FileSystemSnapshotDataSourceparamsAll = ` +data "powerstore_filesystem_snapshot" "test1" { +} +` + +var FileSystemSnapshotDataSourceparamsIDNegative = ` +data "powerstore_filesystem_snapshot" "test1" { + id = "invalid-id" +} +` + +var FileSystemSnapshotDataSourceparamsIDAndNameNegative = ` +data "powerstore_filesystem_snapshot" "test1" { + id = "` + fileSystemSnapshotID + `" + name = "` + fileSystemSnapshotName + `" +} +` + +var FileSystemSnapshotDataSourceparamsEmptyIDNegative = ` +data "powerstore_filesystem_snapshot" "test1" { + id = "" +} +` + +var FileSystemSnapshotDataSourceparamsEmptyNameNegative = ` +data "powerstore_filesystem_snapshot" "test1" { + name = "" +} +` diff --git a/powerstore/provider.go b/powerstore/provider.go index 5a62d4f5..afaa3f6b 100644 --- a/powerstore/provider.go +++ b/powerstore/provider.go @@ -166,6 +166,7 @@ func (p *Pstoreprovider) DataSources(ctx context.Context) []func() datasource.Da newVolumeGroupSnapshotDataSource, newVolumeSnapshotDataSource, newReplicationRuleDataSource, + newFileSystemSnapshotDataSource, } } diff --git a/powerstore/provider_test.go b/powerstore/provider_test.go index 4ba1f229..c005e34f 100644 --- a/powerstore/provider_test.go +++ b/powerstore/provider_test.go @@ -57,6 +57,8 @@ var volumeSnapshotName = setDefault(os.Getenv("VOLUME_SNAPSHOT_NAME"), "tfacc_vo var nasServerID = setDefault(os.Getenv("NAS_SERVER_ID"), "tfacc_nas_server_id") var fileSystemID = setDefault(os.Getenv("FILESYSTEM_ID"), "tfacc_filesystem_id") var remoteSystemID = setDefault(os.Getenv("REMOTE_SYSTEM_ID"), "") +var fileSystemSnapshotID = setDefault(os.Getenv("FILESYSTEM_SNAPSHOT_ID"), "tfacc_filesystem_snapshot_id") +var fileSystemSnapshotName = setDefault(os.Getenv("FILESYSTEM_SNAPSHOT_NAME"), "tfacc_filesystem_snapshot_name") var ProviderConfigForTesting = ``