Skip to content

Commit

Permalink
Add Filter for DS (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
Krunal-Thakkar authored Nov 19, 2024
1 parent 57ecf1b commit 10ff838
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 15 deletions.
58 changes: 43 additions & 15 deletions powerscale/provider/nfs_alias_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ package provider
import (
"context"
"fmt"
"strings"
"terraform-provider-powerscale/client"
"terraform-provider-powerscale/powerscale/constants"
"terraform-provider-powerscale/powerscale/helper"
"terraform-provider-powerscale/powerscale/models"

"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
"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/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
Expand Down Expand Up @@ -110,16 +114,25 @@ func (d *NfsAliasDataSource) Schema(_ context.Context, _ datasource.SchemaReques
MarkdownDescription: "IDs to filter nfs Aliases.",
Optional: true,
ElementType: types.StringType,
Validators: []validator.Set{
setvalidator.ValueStringsAre(stringvalidator.LengthAtLeast(1)),
},
},
"sort": schema.StringAttribute{
Description: "The field that will be used for sorting.",
MarkdownDescription: "The field that will be used for sorting.",
Optional: true,
Validators: []validator.String{
stringvalidator.LengthAtLeast(1),
},
},
"zone": schema.StringAttribute{
Description: "Specifies which access zone to use.",
MarkdownDescription: "Specifies which access zone to use.",
Optional: true,
Validators: []validator.String{
stringvalidator.LengthAtLeast(1),
},
},
"limit": schema.Int64Attribute{
Description: "Return no more than this many results at once (see resume).",
Expand All @@ -130,6 +143,9 @@ func (d *NfsAliasDataSource) Schema(_ context.Context, _ datasource.SchemaReques
Description: "The direction of the sort.",
MarkdownDescription: "The direction of the sort.",
Optional: true,
Validators: []validator.String{
stringvalidator.LengthAtLeast(1),
},
},
"check": schema.BoolAttribute{
Description: "Check for conflicts when listing Aliases.",
Expand Down Expand Up @@ -185,26 +201,38 @@ func (d *NfsAliasDataSource) Read(ctx context.Context, req datasource.ReadReques
var paths []types.String
if aliasPlan.NfsAliasesFilter != nil {
exportsIDs = aliasPlan.NfsAliasesFilter.IDs
}
filteredAliases, err := helper.FilterAliases(paths, exportsIDs, *totalNfsAliases)
if err != nil {
errStr := constants.ListNfsAliasErrorMsg + "with error: "
message := helper.GetErrorString(err, errStr)
resp.Diagnostics.AddError("Error filtering nfs alias",
message)
return
}

for _, export := range filteredAliases {
entity := models.NfsAliasDatasourceEntity{}
err := helper.CopyFields(ctx, export, &entity)
filteredAliases, err := helper.FilterAliases(paths, exportsIDs, *totalNfsAliases)
if err != nil {
resp.Diagnostics.AddError("Error reading nfs aliases datasource plan",
fmt.Sprintf("Could not list nfs aliases with error: %s", err.Error()))
errStr := constants.ListNfsAliasErrorMsg + "with error: "
message := helper.GetErrorString(err, errStr)
resp.Diagnostics.AddError("Error filtering nfs alias",
message)
return
}
aliasState.NfsAliases = append(aliasState.NfsAliases, entity)

var validAliases []string
for _, export := range filteredAliases {
entity := models.NfsAliasDatasourceEntity{}
err := helper.CopyFields(ctx, export, &entity)
if err != nil {
resp.Diagnostics.AddError("Error reading nfs aliases datasource plan",
fmt.Sprintf("Could not list nfs aliases with error: %s", err.Error()))
return
}
aliasState.NfsAliases = append(aliasState.NfsAliases, entity)

validAliases = append(validAliases, entity.ID.ValueString())
}

if len(aliasState.NfsAliases) < len(aliasPlan.NfsAliasesFilter.IDs) {
resp.Diagnostics.AddError(
"Error one or more of the filtered NFS Alias id is not a valid powerscale NFS Alias.",
fmt.Sprintf("Valid NFS Aliases: [%v], filtered list: [%v]", strings.Join(validAliases, " , "), aliasPlan.NfsAliasesFilter.IDs),
)
}
}

aliasState.ID = types.StringValue("nfs_alias")
aliasState.NfsAliasesFilter = aliasPlan.NfsAliasesFilter
resp.Diagnostics.Append(resp.State.Set(ctx, &aliasState)...)
Expand Down
12 changes: 12 additions & 0 deletions powerscale/provider/snapshot_schedule_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ import (
"terraform-provider-powerscale/powerscale/helper"
"terraform-provider-powerscale/powerscale/models"

"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
"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/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
Expand Down Expand Up @@ -134,11 +137,17 @@ func (d *SnapshotScheduleDataSource) Schema(_ context.Context, _ datasource.Sche
MarkdownDescription: "Names to filter snapshot schedules.",
Optional: true,
ElementType: types.StringType,
Validators: []validator.Set{
setvalidator.ValueStringsAre(stringvalidator.LengthAtLeast(1)),
},
},
"sort": schema.StringAttribute{
Description: "The field that will be used for sorting. Choices are id, name, path, pattern, schedule, duration, alias, next_run, and next_snapshot. Default is id.",
MarkdownDescription: "The field that will be used for sorting. Choices are id, name, path, pattern, schedule, duration, alias, next_run, and next_snapshot. Default is id.",
Optional: true,
Validators: []validator.String{
stringvalidator.LengthAtLeast(1),
},
},
"limit": schema.Int64Attribute{
Description: "Return no more than this many results at once.",
Expand All @@ -149,6 +158,9 @@ func (d *SnapshotScheduleDataSource) Schema(_ context.Context, _ datasource.Sche
Description: "The direction of the sort.Supported Values:ASC , DESC",
MarkdownDescription: "The direction of the sort.Supported Values:ASC , DESC",
Optional: true,
Validators: []validator.String{
stringvalidator.LengthAtLeast(1),
},
},
},
},
Expand Down
20 changes: 20 additions & 0 deletions powerscale/provider/subnet_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ package provider
import (
"context"
"fmt"
"strings"
"terraform-provider-powerscale/client"
"terraform-provider-powerscale/powerscale/constants"
"terraform-provider-powerscale/powerscale/helper"
"terraform-provider-powerscale/powerscale/models"

"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
"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/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
Expand Down Expand Up @@ -178,11 +182,17 @@ func (d *SubnetDataSource) Schema(ctx context.Context, req datasource.SchemaRequ
MarkdownDescription: "List of subnet name.",
Optional: true,
ElementType: types.StringType,
Validators: []validator.Set{
setvalidator.ValueStringsAre(stringvalidator.LengthAtLeast(1)),
},
},
"groupnet_name": schema.StringAttribute{
Description: "Specifies which groupnet to query.",
MarkdownDescription: "Specifies which groupnet to query.",
Optional: true,
Validators: []validator.String{
stringvalidator.LengthAtLeast(1),
},
},
},
},
Expand Down Expand Up @@ -232,6 +242,7 @@ func (d *SubnetDataSource) Read(ctx context.Context, req datasource.ReadRequest,
return
}

var validSubnets []string
for _, subnet := range *subnets {
entity := models.V12GroupnetSubnetExtended{}
err := helper.CopyFields(ctx, subnet, &entity)
Expand All @@ -240,8 +251,17 @@ func (d *SubnetDataSource) Read(ctx context.Context, req datasource.ReadRequest,
fmt.Sprintf("Could not list subnets with error: %s", err.Error()))
return
}
validSubnets = append(validSubnets, entity.Name.ValueString())
subnetState.Subnets = append(subnetState.Subnets, entity)
}

if subnetPlan.SubnetFilter != nil && len(subnetState.Subnets) < len(subnetPlan.SubnetFilter.Names) {
resp.Diagnostics.AddError(
"Error one or more of the filtered subnet name is not a valid powerscale Subnet.",
fmt.Sprintf("Valid Subnets: [%v], filtered list: [%v]", strings.Join(validSubnets, " , "), subnetPlan.SubnetFilter.Names),
)
}

subnetState.ID = types.StringValue("Subnet-id")
subnetState.SubnetFilter = subnetPlan.SubnetFilter
resp.Diagnostics.Append(resp.State.Set(ctx, &subnetState)...)
Expand Down

0 comments on commit 10ff838

Please sign in to comment.