Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writable Snapshot DataSource Added #225

Merged
merged 7 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions docs/data-sources/writable_snapshot.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
---
# 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: "powerscale_writable_snapshot data source"
linkTitle: "powerscale_writable_snapshot"
page_title: "powerscale_writable_snapshot Data Source - terraform-provider-powerscale"
subcategory: ""
description: |-

---

# powerscale_writable_snapshot (Data Source)



## Example Usage

```terraform
/*
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.
*/

# PowerScale Writable Snapshots allows you to get a list of Writable Snapshots or a Writable Snapshot by its Path which can be added in filters.

# Returns a list of PowerScale Writable Snapshots
data "powerscale_writable_snapshot" "all_snaps" {
}

# Returns a the PowerScale Writable Snapshot with given Path
data "powerscale_writable_snapshot" "test" {
filter {
path = "/ifs/path/to/writable/snap"
limit = 5
}
}

# Output value of above block by executing 'terraform output' command.
# The user can use the fetched information by the variable data.powerscale_writable_snapshot.all_snaps.writable
# output "powerscale_writable_snapshot" {
# value = data.powerscale_writable_snapshot.all_snaps.writable
# }

# After the successful execution of above said block, We can see the output value by executing 'terraform output' command.
```

<!-- schema generated by tfplugindocs -->
## Schema

### Optional

- `filter` (Block, Optional) (see [below for nested schema](#nestedblock--filter))
- `id` (String) Path of the writable snapshot.

### Read-Only

- `writable` (Attributes List) (see [below for nested schema](#nestedatt--writable))

<a id="nestedblock--filter"></a>
### Nested Schema for `filter`

Optional:

- `dir` (String) The direction of the sort.
- `limit` (Number) Return no more than this many results at once (see resume).
- `path` (String) Only list writable snapshots matching this path.
- `resume` (String) Continue returning results from previous call using this token (token should come from the previous call, resume cannot be used with other options).
- `sort` (String) The field that will be used for sorting. Choices are path, src name, src path, created, size and state. Default is created.
- `state` (String) Only list writable snapshots matching this state.


<a id="nestedatt--writable"></a>
### Nested Schema for `writable`

Read-Only:

- `created` (Number) The Unix Epoch time the writable snapshot was created.
- `dst_path` (String) The user supplied /ifs path of writable snapshot.
- `id` (Number) The system ID given to the writable snapshot. This is useful for debugging.
- `log_size` (Number) The sum in bytes of logical size of files in this writable snapshot.
- `phys_size` (Number) The amount of storage in bytes used to store this writable snapshot.
- `src_id` (Number) The system ID of the user supplied source snapshot. This is useful for debugging.
- `src_path` (String) The /ifs path of user supplied source snapshot. This will be null for writable snapshots pending delete.
- `src_snap` (String) The user supplied source snapshot name or ID. This will be null for writable snapshots pending delete.
- `state` (String) Writable Snapshot state.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
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.
*/

# PowerScale Writable Snapshots allows you to get a list of Writable Snapshots or a Writable Snapshot by its Path which can be added in filters.

# Returns a list of PowerScale Writable Snapshots
data "powerscale_writable_snapshot" "all_snaps" {
}

# Returns a the PowerScale Writable Snapshot with given Path
data "powerscale_writable_snapshot" "test" {
filter {
path = "/ifs/path/to/writable/snap"
limit = 5
}
}

# Output value of above block by executing 'terraform output' command.
# The user can use the fetched information by the variable data.powerscale_writable_snapshot.all_snaps.writable
# output "powerscale_writable_snapshot" {
# value = data.powerscale_writable_snapshot.all_snaps.writable
# }

# After the successful execution of above said block, We can see the output value by executing 'terraform output' command.
31 changes: 31 additions & 0 deletions examples/data-sources/powerscale_writable_snapshot/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
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 {
powerscale = {
source = "registry.terraform.io/dell/powerscale"
}
}
}

provider "powerscale" {
username = var.username
password = var.password
endpoint = var.endpoint
insecure = var.insecure
}
3 changes: 3 additions & 0 deletions powerscale/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,4 +461,7 @@ const (

// ReadSnapshotRestoreJobReportErrorMsg specifies error details occurred while reading snapshot restore job reports.
ReadSnapshotRestoreJobReportErrorMsg = "Could not read snapshot restore job reports "

// ListWritableSnapshotMsg specifies error details occurred while listing writable snapshot.
ListWritableSnapshotMsg = "Could not list Writable Snapshot "
)
2 changes: 1 addition & 1 deletion powerscale/helper/file_system_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ func UpdateFileSystemAccessControl(ctx context.Context, client *client.Client, d
if err != nil {
errStr := constants.SetFileSystemACLErrorMsg + "Error Updating AccessControl for the filesystem with error: "
message := GetErrorString(err, errStr)
return fmt.Errorf(message)
return fmt.Errorf("%s", message)
}
}
return nil
Expand Down
8 changes: 4 additions & 4 deletions powerscale/helper/s3_key_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package helper
import (
"context"
powerscale "dell/powerscale-go-client"
"fmt"
"errors"
"terraform-provider-powerscale/client"
"terraform-provider-powerscale/powerscale/models"
)
Expand All @@ -39,7 +39,7 @@ func GenerateS3Key(ctx context.Context, c *client.Client, state models.S3KeyReso
param = param.V10S3Key(eket)
response, _, err := param.Execute()
if err != nil {
err = fmt.Errorf(GetErrorString(err, "s3 key generate error: "))
err = errors.New(GetErrorString(err, "s3 key generate error: "))
}
return response, err
}
Expand All @@ -52,7 +52,7 @@ func GetS3Key(ctx context.Context, c *client.Client, state models.S3KeyResourceD
}
response, _, err := param.Execute()
if err != nil {
err = fmt.Errorf(GetErrorString(err, "s3 key get error: "))
err = errors.New(GetErrorString(err, "s3 key get error: "))
}
return response, err
}
Expand All @@ -65,7 +65,7 @@ func DeleteS3Key(ctx context.Context, c *client.Client, state models.S3KeyResour
}
_, err := param.Execute()
if err != nil {
err = fmt.Errorf(GetErrorString(err, "s3 key delete error: "))
err = errors.New(GetErrorString(err, "s3 key delete error: "))
}
return err
}
4 changes: 2 additions & 2 deletions powerscale/helper/s3_zone_settings_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package helper
import (
"context"
powerscale "dell/powerscale-go-client"
"fmt"
"errors"
"terraform-provider-powerscale/client"
"terraform-provider-powerscale/powerscale/models"
)
Expand Down Expand Up @@ -49,7 +49,7 @@ func UpdateS3ZoneSetting(ctx context.Context, client *client.Client, state model
updateParam = updateParam.V10S3SettingsZone(toUpdate)
_, err = updateParam.Execute()
if err != nil {
err = fmt.Errorf(GetErrorString(err, "s3 zone setting update error: "))
err = errors.New(GetErrorString(err, "s3 zone setting update error: "))
}
return err
}
Expand Down
46 changes: 46 additions & 0 deletions powerscale/helper/writable_snapshot_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package helper
import (
"context"
powerscale "dell/powerscale-go-client"
"errors"
"strconv"
"terraform-provider-powerscale/client"
"terraform-provider-powerscale/powerscale/models"
Expand All @@ -45,6 +46,31 @@ func DeleteWritableSnapshot(ctx context.Context, client *client.Client, path str
return err
}

// GetAllWritableSnapshots returns the full list of writable snapshots.
func GetAllWritableSnapshots(ctx context.Context, client *client.Client, state *models.WritablesnapshotModel) (*powerscale.V14SnapshotWritable, error) {
writablesnapshots := client.PscaleOpenAPIClient.SnapshotApi.ListSnapshotv14SnapshotWritable(ctx)

if state.WritableSnapshotFilter != nil {
if !state.WritableSnapshotFilter.Sort.IsNull() {
writablesnapshots = writablesnapshots.Sort(state.WritableSnapshotFilter.Sort.ValueString())
}
if !state.WritableSnapshotFilter.State.IsNull() {
writablesnapshots = writablesnapshots.State(state.WritableSnapshotFilter.State.ValueString())
}
if !state.WritableSnapshotFilter.Limit.IsNull() {
writablesnapshots = writablesnapshots.Limit(int32(state.WritableSnapshotFilter.Limit.ValueInt64()))
}
if !state.WritableSnapshotFilter.Dir.IsNull() {
writablesnapshots = writablesnapshots.Dir(state.WritableSnapshotFilter.Dir.ValueString())
}
if !state.WritableSnapshotFilter.Resume.IsNull() {
writablesnapshots = writablesnapshots.Resume(state.WritableSnapshotFilter.Resume.ValueString())
}
}
resp, _, err := writablesnapshots.Execute()
return resp, err
rounak-adhikary marked this conversation as resolved.
Show resolved Hide resolved
}

// UpdateWritableSnapshotState updates the state parameters based on the fetched computed values from the API.
func UpdateWritableSnapshotState(state *models.WritableSnapshot, fetchedState *powerscale.Createv14SnapshotWritableItemResponse) {
state.DstPath = types.StringValue(fetchedState.DstPath)
Expand All @@ -55,3 +81,23 @@ func UpdateWritableSnapshotState(state *models.WritableSnapshot, fetchedState *p
state.State = types.StringValue(fetchedState.State)
state.SnapName = types.StringValue(fetchedState.SrcSnap)
}

// NewWritableSnapshotDataSource creates the writable snapshot data source.
func NewWritableSnapshotDataSource(ctx context.Context, writableSnapshot []powerscale.Createv14SnapshotWritableItemResponse) (*models.WritablesnapshotModel, error) {
var err error
dsWritable := make([]models.WritableSnapshotDataSource, len(writableSnapshot))
for i := range writableSnapshot {
var item models.WritableSnapshotDataSource
ierr := CopyFields(ctx, &writableSnapshot[i], &item)
err = errors.Join(err, ierr)
dsWritable[i] = item
}
if err != nil {
return nil, err
}
ret := models.WritablesnapshotModel{
ID: types.StringValue("Writable_snapshot Datasource"),
Writable: dsWritable,
}
return &ret, nil
}
21 changes: 0 additions & 21 deletions powerscale/models/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,3 @@ type SnapshotDetailModel struct {
// The name of the snapshot pointed to if this is an alias.
TargetName types.String `tfsdk:"target_name"`
}

// WritableSnapshot defines the writable snapshot.
type WritableSnapshot struct {
// The system ID given to the snapshot. This is useful for tracking the status of delete pending snapshots.
ID types.Int32 `tfsdk:"id"`

// The destination path of the snapshot.
DstPath types.String `tfsdk:"dst_path"`

// The source snapshot ID.
SrcSnap types.String `tfsdk:"snap_id"`

// The source snapshot name.
SnapName types.String `tfsdk:"snap_name"`

// The source path.
SrcPath types.String `tfsdk:"src_path"`

// Snapshot state.
State types.String `tfsdk:"state"`
}
Loading
Loading