Skip to content

Commit

Permalink
Update FaultSet Datasource Filter (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
doriac11 authored Dec 6, 2024
1 parent d48b5d5 commit c5efd03
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 164 deletions.
38 changes: 26 additions & 12 deletions docs/data-sources/fault_set.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,26 @@ limitations under the License.
# commands to run this tf file : terraform init && terraform apply --auto-approve
# Get all fault set details present on the cluster
data "powerflex_fault_set" "example1" {
data "powerflex_fault_set" "all" {
}
# Get fault set details using fault set IDs
data "powerflex_fault_set" "example2" {
fault_set_ids = ["FaultSet_ID1", "FaultSet_ID2"]
output "fault_set_result_all" {
value = data.powerflex_fault_set.all.fault_set_details
}
# Get fault set details using fault set names
data "powerflex_fault_set" "example3" {
fault_set_names = ["FaultSet_Name1", "FaultSet_Name2"]
// If multiple filter fields are provided then it will show the intersection of all of those fields.
// If there is no intersection between the filters then an empty datasource will be returned
// For more information about how we do our datasource filtering check out our guides: https://dell.github.io/terraform-docs/docs/storage/platforms/powerflex/product_guide/examples/
data "powerflex_fault_set" "filtered" {
filter {
# protection_domain_id = ["protection_domain_id", "protection_domain_id2"]
# name = ["name", "name2"]
# id = ["id", "id2"]
}
}
output "fault_set_result" {
value = data.powerflex_fault_set.example1.fault_set_details
output "fault_set_result_filtered" {
value = data.powerflex_fault_set.filtered.fault_set_details
}
```

Expand All @@ -74,13 +79,22 @@ After the successful execution of above said block, we can see the output by exe

### Optional

- `fault_set_ids` (Set of String) List of fault set IDs
- `fault_set_names` (Set of String) List of fault set names
- `filter` (Block, Optional) (see [below for nested schema](#nestedblock--filter))

### Read-Only

- `fault_set_details` (Attributes Set) Fault set details (see [below for nested schema](#nestedatt--fault_set_details))
- `id` (String) Placeholder attribute.
- `id` (String) Placeholder for fault set datasource attribute.

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

Optional:

- `id` (Set of String) List of id
- `name` (Set of String) List of name
- `protection_domain_id` (Set of String) List of protection_domain_id


<a id="nestedatt--fault_set_details"></a>
### Nested Schema for `fault_set_details`
Expand Down
23 changes: 14 additions & 9 deletions examples/data-sources/powerflex_fault_set/data-source.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,24 @@ limitations under the License.
# commands to run this tf file : terraform init && terraform apply --auto-approve

# Get all fault set details present on the cluster
data "powerflex_fault_set" "example1" {
data "powerflex_fault_set" "all" {
}

# Get fault set details using fault set IDs
data "powerflex_fault_set" "example2" {
fault_set_ids = ["FaultSet_ID1", "FaultSet_ID2"]
output "fault_set_result_all" {
value = data.powerflex_fault_set.all.fault_set_details
}

# Get fault set details using fault set names
data "powerflex_fault_set" "example3" {
fault_set_names = ["FaultSet_Name1", "FaultSet_Name2"]
// If multiple filter fields are provided then it will show the intersection of all of those fields.
// If there is no intersection between the filters then an empty datasource will be returned
// For more information about how we do our datasource filtering check out our guides: https://dell.github.io/terraform-docs/docs/storage/platforms/powerflex/product_guide/examples/
data "powerflex_fault_set" "filtered" {
filter {
# protection_domain_id = ["protection_domain_id", "protection_domain_id2"]
# name = ["name", "name2"]
# id = ["id", "id2"]
}
}

output "fault_set_result" {
value = data.powerflex_fault_set.example1.fault_set_details
output "fault_set_result_filtered" {
value = data.powerflex_fault_set.filtered.fault_set_details
}
6 changes: 6 additions & 0 deletions powerflex/helper/fault_set_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package helper
import (
"terraform-provider-powerflex/powerflex/models"

"github.com/dell/goscaleio"
scaleiotypes "github.com/dell/goscaleio/types/v1"
"github.com/hashicorp/terraform-plugin-framework/types"
)
Expand All @@ -33,6 +34,11 @@ func UpdateFaultSetState(faultset *scaleiotypes.FaultSet, plan models.FaultSetRe
return state
}

// GetAllFaultSets returns all the fault sets
func GetAllFaultSets(system *goscaleio.System) ([]scaleiotypes.FaultSet, error) {
return system.GetAllFaultSets()
}

// GetAllFaultSetState returns the state for fault set data source
func GetAllFaultSetState(faultSet scaleiotypes.FaultSet, sdsDetails []models.SdsDataModel) (response models.FaultSet) {
response = models.FaultSet{
Expand Down
14 changes: 10 additions & 4 deletions powerflex/models/fault_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,16 @@ type FaultSetResourceModel struct {

// FaultSetDataSourceModel maps the struct to FaultSet data source schema
type FaultSetDataSourceModel struct {
FaultSetIDs types.Set `tfsdk:"fault_set_ids"`
FaultSetNames types.Set `tfsdk:"fault_set_names"`
FaultSetDetails []FaultSet `tfsdk:"fault_set_details"`
ID types.String `tfsdk:"id"`
FaultSetFilter *FaultSetFilter `tfsdk:"filter"`
FaultSetDetails []FaultSet `tfsdk:"fault_set_details"`
ID types.String `tfsdk:"id"`
}

// FaultSetFilter defines the filter for fault set
type FaultSetFilter struct {
ProtectionDomainID []types.String `tfsdk:"protection_domain_id"`
Name []types.String `tfsdk:"name"`
ID []types.String `tfsdk:"id"`
}

// FaultSet maps the struct to FaultSet schema
Expand Down
140 changes: 38 additions & 102 deletions powerflex/provider/fault_set_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,8 @@ import (

"github.com/dell/goscaleio"
scaleiotypes "github.com/dell/goscaleio/types/v1"
"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/path"
"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 @@ -105,92 +101,50 @@ func (d *faultSetDataSource) Read(ctx context.Context, req datasource.ReadReques
return
}

// Fetch Fault set details if IDs are provided
if !state.FaultSetIDs.IsNull() {
faultSetIDs := make([]string, 0)
diags.Append(state.FaultSetIDs.ElementsAs(ctx, &faultSetIDs, true)...)

for _, faultSetID := range faultSetIDs {
faultSet, err := d.system.GetFaultSetByID(faultSetID)
if err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Error in getting fault set details using id %v", faultSetID), err.Error(),
)
return
}
sdsDetails, err := d.GetSdsDetails(faultSet.ID)
if err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Error in getting SDS details connected to fault set details using id %v", faultSet.ID), err.Error(),
)
return
}
// Fetch Fault set details for all the fault sets
faultSets, err = helper.GetAllFaultSets(d.system)
if err != nil {
resp.Diagnostics.AddError(
"Error in getting Fault Set details", err.Error(),
)
return
}

var sdsStateModels []models.SdsDataModel
for index := range sdsDetails {
sdsState := getSdsState(&sdsDetails[index])
sdsStateModels = append(sdsStateModels, sdsState)
}
faultSetsModel = append(faultSetsModel, helper.GetAllFaultSetState(*faultSet, sdsStateModels))
// Filter if any are set
if state.FaultSetFilter != nil {
filtered, err := helper.GetDataSourceByValue(*state.FaultSetFilter, faultSets)
if err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Error in filtering fault sets: %v please validate the filter", state.FaultSetFilter), err.Error(),
)
return
}
} else if !state.FaultSetNames.IsNull() {
// Fetch Fault set details if names are provided
faultSetNames := make([]string, 0)
diags.Append(state.FaultSetNames.ElementsAs(ctx, &faultSetNames, true)...)

for _, faultSetName := range faultSetNames {
faultSet, err := d.system.GetFaultSetByName(faultSetName)
if err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Error in getting fault set details using name %v", faultSetName), err.Error(),
)
return
}
sdsDetails, err := d.GetSdsDetails(faultSet.ID)
if err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Error in getting SDS details connected to fault set details using id %v", faultSet.ID), err.Error(),
)
return
}

var sdsStateModels []models.SdsDataModel
for index := range sdsDetails {
sdsState := getSdsState(&sdsDetails[index])
sdsStateModels = append(sdsStateModels, sdsState)
}
faultSetsModel = append(faultSetsModel, helper.GetAllFaultSetState(*faultSet, sdsStateModels))
filteredvFaultSets := []scaleiotypes.FaultSet{}
for _, val := range filtered {
filteredvFaultSets = append(filteredvFaultSets, val.(scaleiotypes.FaultSet))
}
} else {
// Fetch Fault set details for all the fault sets
faultSets, err = d.system.GetAllFaultSets()
faultSets = filteredvFaultSets
}

for _, faultSet := range faultSets {
sdsDetails, err := d.GetSdsDetails(faultSet.ID)
if err != nil {
resp.Diagnostics.AddError(
"Error in getting Fault Set details", err.Error(),
fmt.Sprintf("Error in getting SDS details connected to fault set details using id %v", faultSet.ID), err.Error(),
)
return
}

for _, faultSet := range faultSets {
sdsDetails, err := d.GetSdsDetails(faultSet.ID)
if err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Error in getting SDS details connected to fault set details using id %v", faultSet.ID), err.Error(),
)
return
}

var sdsStateModels []models.SdsDataModel
for index := range sdsDetails {
sdsState := getSdsState(&sdsDetails[index])
sdsStateModels = append(sdsStateModels, sdsState)
}
faultSetsModel = append(faultSetsModel, helper.GetAllFaultSetState(faultSet, sdsStateModels))
var sdsStateModels []models.SdsDataModel
for index := range sdsDetails {
sdsState := getSdsState(&sdsDetails[index])
sdsStateModels = append(sdsStateModels, sdsState)
}
faultSetsModel = append(faultSetsModel, helper.GetAllFaultSetState(faultSet, sdsStateModels))
}

state.FaultSetDetails = faultSetsModel
state.ID = types.StringValue("placeholder")
state.ID = types.StringValue("fault-set-datasource-id")
diags = resp.State.Set(ctx, state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand All @@ -204,33 +158,10 @@ var FaultSetDataSourceSchema schema.Schema = schema.Schema{
MarkdownDescription: "This datasource is used to query the existing fault set from the PowerFlex 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: "Placeholder attribute.",
MarkdownDescription: "Placeholder attribute.",
Description: "Placeholder for fault set datasource attribute.",
MarkdownDescription: "Placeholder for fault set datasource attribute.",
Computed: true,
},
"fault_set_ids": schema.SetAttribute{
Description: "List of fault set IDs",
MarkdownDescription: "List of fault set IDs",
Optional: true,
ElementType: types.StringType,
Validators: []validator.Set{
setvalidator.ConflictsWith(
path.MatchRoot("fault_set_names"),
),
setvalidator.SizeAtLeast(1),
setvalidator.ValueStringsAre(stringvalidator.LengthAtLeast(1)),
},
},
"fault_set_names": schema.SetAttribute{
Description: "List of fault set names",
MarkdownDescription: "List of fault set names",
Optional: true,
ElementType: types.StringType,
Validators: []validator.Set{
setvalidator.SizeAtLeast(1),
setvalidator.ValueStringsAre(stringvalidator.LengthAtLeast(1)),
},
},
"fault_set_details": schema.SetNestedAttribute{
Description: "Fault set details",
MarkdownDescription: "Fault set details",
Expand Down Expand Up @@ -501,6 +432,11 @@ var FaultSetDataSourceSchema schema.Schema = schema.Schema{
},
},
},
Blocks: map[string]schema.Block{
"filter": schema.SingleNestedBlock{
Attributes: helper.GenerateSchemaAttributes(helper.TypeToMap(models.FaultSetFilter{})),
},
},
}

// FaultSetLinksSchema specifies the schema for fault set links
Expand Down
Loading

0 comments on commit c5efd03

Please sign in to comment.