Skip to content

Commit cc729f7

Browse files
committed
Update FaultSet Datasource Filter
1 parent 3e1e44f commit cc729f7

File tree

6 files changed

+180
-170
lines changed

6 files changed

+180
-170
lines changed

docs/data-sources/fault_set.md

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,25 @@ limitations under the License.
4848
4949
# commands to run this tf file : terraform init && terraform apply --auto-approve
5050
51-
# Get all fault set details present on the cluster
52-
data "powerflex_fault_set" "example1" {
53-
}
51+
# # Get all fault set details present on the cluster
52+
# data "powerflex_fault_set" "all" {
53+
# }
5454
55-
# Get fault set details using fault set IDs
56-
data "powerflex_fault_set" "example2" {
57-
fault_set_ids = ["FaultSet_ID1", "FaultSet_ID2"]
58-
}
55+
# output "fault_set_result_all" {
56+
# value = data.powerflex_fault_set.all.fault_set_details
57+
# }
5958
60-
# Get fault set details using fault set names
61-
data "powerflex_fault_set" "example3" {
62-
fault_set_names = ["FaultSet_Name1", "FaultSet_Name2"]
59+
# Get fault set details using fault set IDs
60+
data "powerflex_fault_set" "filtered" {
61+
filter {
62+
# protection_domain_id = ["protection_domain_id", "protection_domain_id2"]
63+
# name = ["name", "name2"]
64+
# id = ["id", "id2"]
65+
}
6366
}
6467
65-
output "fault_set_result" {
66-
value = data.powerflex_fault_set.example1.fault_set_details
68+
output "fault_set_result_filtered" {
69+
value = data.powerflex_fault_set.filtered.fault_set_details
6770
}
6871
```
6972

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

7578
### Optional
7679

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

8082
### Read-Only
8183

8284
- `fault_set_details` (Attributes Set) Fault set details (see [below for nested schema](#nestedatt--fault_set_details))
83-
- `id` (String) Placeholder attribute.
85+
- `id` (String) Placeholder for fault set datasource attribute.
86+
87+
<a id="nestedblock--filter"></a>
88+
### Nested Schema for `filter`
89+
90+
Optional:
91+
92+
- `id` (Set of String) List of id
93+
- `name` (Set of String) List of name
94+
- `protection_domain_id` (Set of String) List of protection_domain_id
95+
8496

8597
<a id="nestedatt--fault_set_details"></a>
8698
### Nested Schema for `fault_set_details`

examples/data-sources/powerflex_fault_set/data-source.tf

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,23 @@ limitations under the License.
1717

1818
# commands to run this tf file : terraform init && terraform apply --auto-approve
1919

20-
# Get all fault set details present on the cluster
21-
data "powerflex_fault_set" "example1" {
22-
}
20+
# # Get all fault set details present on the cluster
21+
# data "powerflex_fault_set" "all" {
22+
# }
2323

24-
# Get fault set details using fault set IDs
25-
data "powerflex_fault_set" "example2" {
26-
fault_set_ids = ["FaultSet_ID1", "FaultSet_ID2"]
27-
}
24+
# output "fault_set_result_all" {
25+
# value = data.powerflex_fault_set.all.fault_set_details
26+
# }
2827

29-
# Get fault set details using fault set names
30-
data "powerflex_fault_set" "example3" {
31-
fault_set_names = ["FaultSet_Name1", "FaultSet_Name2"]
28+
# Get fault set details using fault set IDs
29+
data "powerflex_fault_set" "filtered" {
30+
filter {
31+
# protection_domain_id = ["protection_domain_id", "protection_domain_id2"]
32+
# name = ["name", "name2"]
33+
# id = ["id", "id2"]
34+
}
3235
}
3336

34-
output "fault_set_result" {
35-
value = data.powerflex_fault_set.example1.fault_set_details
37+
output "fault_set_result_filtered" {
38+
value = data.powerflex_fault_set.filtered.fault_set_details
3639
}

powerflex/helper/fault_set_helper.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package helper
2020
import (
2121
"terraform-provider-powerflex/powerflex/models"
2222

23+
"github.com/dell/goscaleio"
2324
scaleiotypes "github.com/dell/goscaleio/types/v1"
2425
"github.com/hashicorp/terraform-plugin-framework/types"
2526
)
@@ -33,6 +34,11 @@ func UpdateFaultSetState(faultset *scaleiotypes.FaultSet, plan models.FaultSetRe
3334
return state
3435
}
3536

37+
// GetAllFaultSets returns all the fault sets
38+
func GetAllFaultSets(system *goscaleio.System) ([]scaleiotypes.FaultSet, error) {
39+
return system.GetAllFaultSets()
40+
}
41+
3642
// GetAllFaultSetState returns the state for fault set data source
3743
func GetAllFaultSetState(faultSet scaleiotypes.FaultSet, sdsDetails []models.SdsDataModel) (response models.FaultSet) {
3844
response = models.FaultSet{

powerflex/models/fault_set.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,16 @@ type FaultSetResourceModel struct {
3030

3131
// FaultSetDataSourceModel maps the struct to FaultSet data source schema
3232
type FaultSetDataSourceModel struct {
33-
FaultSetIDs types.Set `tfsdk:"fault_set_ids"`
34-
FaultSetNames types.Set `tfsdk:"fault_set_names"`
35-
FaultSetDetails []FaultSet `tfsdk:"fault_set_details"`
36-
ID types.String `tfsdk:"id"`
33+
FaultSetFilter *FaultSetFilter `tfsdk:"filter"`
34+
FaultSetDetails []FaultSet `tfsdk:"fault_set_details"`
35+
ID types.String `tfsdk:"id"`
36+
}
37+
38+
// FaultSetFilter defines the filter for fault set
39+
type FaultSetFilter struct {
40+
ProtectionDomainID []types.String `tfsdk:"protection_domain_id"`
41+
Name []types.String `tfsdk:"name"`
42+
ID []types.String `tfsdk:"id"`
3743
}
3844

3945
// FaultSet maps the struct to FaultSet schema

powerflex/provider/fault_set_datasource.go

Lines changed: 38 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,8 @@ import (
2525

2626
"github.com/dell/goscaleio"
2727
scaleiotypes "github.com/dell/goscaleio/types/v1"
28-
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
29-
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
3028
"github.com/hashicorp/terraform-plugin-framework/datasource"
3129
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
32-
"github.com/hashicorp/terraform-plugin-framework/path"
33-
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
3430
"github.com/hashicorp/terraform-plugin-framework/types"
3531
"github.com/hashicorp/terraform-plugin-log/tflog"
3632
)
@@ -105,92 +101,50 @@ func (d *faultSetDataSource) Read(ctx context.Context, req datasource.ReadReques
105101
return
106102
}
107103

108-
// Fetch Fault set details if IDs are provided
109-
if !state.FaultSetIDs.IsNull() {
110-
faultSetIDs := make([]string, 0)
111-
diags.Append(state.FaultSetIDs.ElementsAs(ctx, &faultSetIDs, true)...)
112-
113-
for _, faultSetID := range faultSetIDs {
114-
faultSet, err := d.system.GetFaultSetByID(faultSetID)
115-
if err != nil {
116-
resp.Diagnostics.AddError(
117-
fmt.Sprintf("Error in getting fault set details using id %v", faultSetID), err.Error(),
118-
)
119-
return
120-
}
121-
sdsDetails, err := d.GetSdsDetails(faultSet.ID)
122-
if err != nil {
123-
resp.Diagnostics.AddError(
124-
fmt.Sprintf("Error in getting SDS details connected to fault set details using id %v", faultSet.ID), err.Error(),
125-
)
126-
return
127-
}
104+
// Fetch Fault set details for all the fault sets
105+
faultSets, err = helper.GetAllFaultSets(d.system)
106+
if err != nil {
107+
resp.Diagnostics.AddError(
108+
"Error in getting Fault Set details", err.Error(),
109+
)
110+
return
111+
}
128112

129-
var sdsStateModels []models.SdsDataModel
130-
for index := range sdsDetails {
131-
sdsState := getSdsState(&sdsDetails[index])
132-
sdsStateModels = append(sdsStateModels, sdsState)
133-
}
134-
faultSetsModel = append(faultSetsModel, helper.GetAllFaultSetState(*faultSet, sdsStateModels))
113+
// Filter if any are set
114+
if state.FaultSetFilter != nil {
115+
filtered, err := helper.GetDataSourceByValue(*state.FaultSetFilter, faultSets)
116+
if err != nil {
117+
resp.Diagnostics.AddError(
118+
fmt.Sprintf("Error in filtering fault sets: %v please validate the filter", state.FaultSetFilter), err.Error(),
119+
)
120+
return
135121
}
136-
} else if !state.FaultSetNames.IsNull() {
137-
// Fetch Fault set details if names are provided
138-
faultSetNames := make([]string, 0)
139-
diags.Append(state.FaultSetNames.ElementsAs(ctx, &faultSetNames, true)...)
140-
141-
for _, faultSetName := range faultSetNames {
142-
faultSet, err := d.system.GetFaultSetByName(faultSetName)
143-
if err != nil {
144-
resp.Diagnostics.AddError(
145-
fmt.Sprintf("Error in getting fault set details using name %v", faultSetName), err.Error(),
146-
)
147-
return
148-
}
149-
sdsDetails, err := d.GetSdsDetails(faultSet.ID)
150-
if err != nil {
151-
resp.Diagnostics.AddError(
152-
fmt.Sprintf("Error in getting SDS details connected to fault set details using id %v", faultSet.ID), err.Error(),
153-
)
154-
return
155-
}
156-
157-
var sdsStateModels []models.SdsDataModel
158-
for index := range sdsDetails {
159-
sdsState := getSdsState(&sdsDetails[index])
160-
sdsStateModels = append(sdsStateModels, sdsState)
161-
}
162-
faultSetsModel = append(faultSetsModel, helper.GetAllFaultSetState(*faultSet, sdsStateModels))
122+
filteredvFaultSets := []scaleiotypes.FaultSet{}
123+
for _, val := range filtered {
124+
filteredvFaultSets = append(filteredvFaultSets, val.(scaleiotypes.FaultSet))
163125
}
164-
} else {
165-
// Fetch Fault set details for all the fault sets
166-
faultSets, err = d.system.GetAllFaultSets()
126+
faultSets = filteredvFaultSets
127+
}
128+
129+
for _, faultSet := range faultSets {
130+
sdsDetails, err := d.GetSdsDetails(faultSet.ID)
167131
if err != nil {
168132
resp.Diagnostics.AddError(
169-
"Error in getting Fault Set details", err.Error(),
133+
fmt.Sprintf("Error in getting SDS details connected to fault set details using id %v", faultSet.ID), err.Error(),
170134
)
171135
return
172136
}
173137

174-
for _, faultSet := range faultSets {
175-
sdsDetails, err := d.GetSdsDetails(faultSet.ID)
176-
if err != nil {
177-
resp.Diagnostics.AddError(
178-
fmt.Sprintf("Error in getting SDS details connected to fault set details using id %v", faultSet.ID), err.Error(),
179-
)
180-
return
181-
}
182-
183-
var sdsStateModels []models.SdsDataModel
184-
for index := range sdsDetails {
185-
sdsState := getSdsState(&sdsDetails[index])
186-
sdsStateModels = append(sdsStateModels, sdsState)
187-
}
188-
faultSetsModel = append(faultSetsModel, helper.GetAllFaultSetState(faultSet, sdsStateModels))
138+
var sdsStateModels []models.SdsDataModel
139+
for index := range sdsDetails {
140+
sdsState := getSdsState(&sdsDetails[index])
141+
sdsStateModels = append(sdsStateModels, sdsState)
189142
}
143+
faultSetsModel = append(faultSetsModel, helper.GetAllFaultSetState(faultSet, sdsStateModels))
190144
}
191145

192146
state.FaultSetDetails = faultSetsModel
193-
state.ID = types.StringValue("placeholder")
147+
state.ID = types.StringValue("fault-set-datasource-id")
194148
diags = resp.State.Set(ctx, state)
195149
resp.Diagnostics.Append(diags...)
196150
if resp.Diagnostics.HasError() {
@@ -204,33 +158,10 @@ var FaultSetDataSourceSchema schema.Schema = schema.Schema{
204158
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.",
205159
Attributes: map[string]schema.Attribute{
206160
"id": schema.StringAttribute{
207-
Description: "Placeholder attribute.",
208-
MarkdownDescription: "Placeholder attribute.",
161+
Description: "Placeholder for fault set datasource attribute.",
162+
MarkdownDescription: "Placeholder for fault set datasource attribute.",
209163
Computed: true,
210164
},
211-
"fault_set_ids": schema.SetAttribute{
212-
Description: "List of fault set IDs",
213-
MarkdownDescription: "List of fault set IDs",
214-
Optional: true,
215-
ElementType: types.StringType,
216-
Validators: []validator.Set{
217-
setvalidator.ConflictsWith(
218-
path.MatchRoot("fault_set_names"),
219-
),
220-
setvalidator.SizeAtLeast(1),
221-
setvalidator.ValueStringsAre(stringvalidator.LengthAtLeast(1)),
222-
},
223-
},
224-
"fault_set_names": schema.SetAttribute{
225-
Description: "List of fault set names",
226-
MarkdownDescription: "List of fault set names",
227-
Optional: true,
228-
ElementType: types.StringType,
229-
Validators: []validator.Set{
230-
setvalidator.SizeAtLeast(1),
231-
setvalidator.ValueStringsAre(stringvalidator.LengthAtLeast(1)),
232-
},
233-
},
234165
"fault_set_details": schema.SetNestedAttribute{
235166
Description: "Fault set details",
236167
MarkdownDescription: "Fault set details",
@@ -501,6 +432,11 @@ var FaultSetDataSourceSchema schema.Schema = schema.Schema{
501432
},
502433
},
503434
},
435+
Blocks: map[string]schema.Block{
436+
"filter": schema.SingleNestedBlock{
437+
Attributes: helper.GenerateSchemaAttributes(helper.TypeToMap(models.FaultSetFilter{})),
438+
},
439+
},
504440
}
505441

506442
// FaultSetLinksSchema specifies the schema for fault set links

0 commit comments

Comments
 (0)