Skip to content

Commit aa8d350

Browse files
committed
SDC Datasource Filter
1 parent 3e1e44f commit aa8d350

File tree

9 files changed

+182
-170
lines changed

9 files changed

+182
-170
lines changed

docs/data-sources/sdc.md

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ description: |-
2626

2727
This datasource is used to query the existing Storage Data Clients from the PowerFlex array. The information fetched from this datasource can be used for getting the details / for further processing in resource block.
2828

29-
> **Note:** Only one of `name` and `id` can be provided at a time.
30-
3129
## Example Usage
3230

3331
```terraform
@@ -49,21 +47,34 @@ limitations under the License.
4947
*/
5048
5149
# commands to run this tf file : terraform init && terraform apply --auto-approve
52-
# Reads SDC either by name or by id , if provided
53-
# If both name and id is not provided , then it reads all the SDC
54-
# id and name can't be given together to fetch the SDC .
55-
# id can't be empty
56-
57-
data "powerflex_sdc" "selected" {
58-
#id = "e3ce1fb500000000"
59-
name = "sdc_01"
50+
51+
data "powerflex_sdc" "all" {
52+
6053
}
6154
62-
# # Returns all sdcs matching criteria
55+
# Returns all sdcs
6356
output "allsdcresult" {
64-
value = data.powerflex_sdc.selected
57+
value = data.powerflex_sdc.all
6558
}
66-
# # -----------------------------------------------------------------------------------
59+
60+
data "powerflex_sdc" "filtered" {
61+
filter {
62+
# id = ["ID1", "ID2"]
63+
# system_id = ["systemID", "systemID2"]
64+
# sdc_ip = ["SCDIP1", "SCDIP2"]
65+
# sdc_approved = false
66+
# on_vmware = false
67+
# sdc_guid = ["SdcGUID1", "SdcGUID2"]
68+
# mdm_connection_state = ["MdmConnectionState1", "MdmConnectionState2"]
69+
# name = ["Name1", "Name2"]
70+
}
71+
}
72+
73+
# Returns filtered sdcs matching criteria
74+
output "filteredsdcresult" {
75+
value = data.powerflex_sdc.filtered
76+
}
77+
# -----------------------------------------------------------------------------------
6778
```
6879

6980
After the successful execution of above said block, We can see the output by executing `terraform output` command. Also, we can fetch information via the variable: `data.powerflex_sdc.selected.attribute_name` where attribute_name is the attribute which user wants to fetch.
@@ -73,13 +84,28 @@ After the successful execution of above said block, We can see the output by exe
7384

7485
### Optional
7586

76-
- `id` (String) ID of the SDC to fetch. Conflicts with `name`
77-
- `name` (String) Name of the SDC to fetch. Conflicts with `id`
87+
- `filter` (Block, Optional) (see [below for nested schema](#nestedblock--filter))
7888

7989
### Read-Only
8090

91+
- `id` (String) ID placeholder for sdc datasource
8192
- `sdcs` (Attributes List) List of fetched SDCs. (see [below for nested schema](#nestedatt--sdcs))
8293

94+
<a id="nestedblock--filter"></a>
95+
### Nested Schema for `filter`
96+
97+
Optional:
98+
99+
- `id` (Set of String) List of id
100+
- `mdm_connection_state` (Set of String) List of mdm_connection_state
101+
- `name` (Set of String) List of name
102+
- `on_vmware` (Boolean) Value for on_vmware
103+
- `sdc_approved` (Boolean) Value for sdc_approved
104+
- `sdc_guid` (Set of String) List of sdc_guid
105+
- `sdc_ip` (Set of String) List of sdc_ip
106+
- `system_id` (Set of String) List of system_id
107+
108+
83109
<a id="nestedatt--sdcs"></a>
84110
### Nested Schema for `sdcs`
85111

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

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,32 @@ limitations under the License.
1616
*/
1717

1818
# commands to run this tf file : terraform init && terraform apply --auto-approve
19-
# Reads SDC either by name or by id , if provided
20-
# If both name and id is not provided , then it reads all the SDC
21-
# id and name can't be given together to fetch the SDC .
22-
# id can't be empty
23-
24-
data "powerflex_sdc" "selected" {
25-
#id = "e3ce1fb500000000"
26-
name = "sdc_01"
19+
20+
data "powerflex_sdc" "all" {
21+
2722
}
2823

29-
# # Returns all sdcs matching criteria
24+
# Returns all sdcs
3025
output "allsdcresult" {
31-
value = data.powerflex_sdc.selected
26+
value = data.powerflex_sdc.all
27+
}
28+
29+
data "powerflex_sdc" "filtered" {
30+
filter {
31+
# id = ["ID1", "ID2"]
32+
# system_id = ["systemID", "systemID2"]
33+
# sdc_ip = ["SCDIP1", "SCDIP2"]
34+
# sdc_approved = false
35+
# on_vmware = false
36+
# sdc_guid = ["SdcGUID1", "SdcGUID2"]
37+
# mdm_connection_state = ["MdmConnectionState1", "MdmConnectionState2"]
38+
# name = ["Name1", "Name2"]
39+
}
40+
}
41+
42+
# Returns filtered sdcs matching criteria
43+
output "filteredsdcresult" {
44+
value = data.powerflex_sdc.filtered
3245
}
33-
# # -----------------------------------------------------------------------------------
46+
# -----------------------------------------------------------------------------------
3447

powerflex/helper/sdc_helper.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func GetFilteredSdcState(sdcs *[]models.SdcModel, method string, name string, id
6666
}
6767

6868
// GetAllSdcState - function to return all sdc result from goscaleio.
69-
func GetAllSdcState(ctx context.Context, client goscaleio.Client, sdcs []goscaleio_types.Sdc) *[]models.SdcModel {
69+
func GetAllSdcState(ctx context.Context, client goscaleio.Client, sdcs []goscaleio_types.Sdc) []models.SdcModel {
7070
response := []models.SdcModel{}
7171
for _, sdcValue := range sdcs {
7272
sdcState := models.SdcModel{
@@ -90,7 +90,7 @@ func GetAllSdcState(ctx context.Context, client goscaleio.Client, sdcs []goscale
9090
response = append(response, sdcState)
9191
}
9292

93-
return &response
93+
return response
9494
}
9595

9696
// GetSDCDetailType returns the SDC Detail type

powerflex/models/sdc.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ import (
2323

2424
// SdcDataSourceModel - for returning result to terraform.
2525
type SdcDataSourceModel struct {
26-
ID types.String `tfsdk:"id"`
27-
Sdcs []SdcModel `tfsdk:"sdcs"`
28-
Name types.String `tfsdk:"name"`
26+
ID types.String `tfsdk:"id"`
27+
Sdcs []SdcModel `tfsdk:"sdcs"`
28+
SdcFilter *SdcFilter `tfsdk:"filter"`
2929
}
3030

3131
// SdcModel - MODEL for SDC data returned by goscaleio.
@@ -41,6 +41,18 @@ type SdcModel struct {
4141
Links []SdcLinkModel `tfsdk:"links"`
4242
}
4343

44+
// SdcFilter - MODEL for SDC filter parameters.
45+
type SdcFilter struct {
46+
ID []types.String `tfsdk:"id"`
47+
SystemID []types.String `tfsdk:"system_id"`
48+
SdcIP []types.String `tfsdk:"sdc_ip"`
49+
SdcApproved types.Bool `tfsdk:"sdc_approved"`
50+
OnVMWare types.Bool `tfsdk:"on_vmware"`
51+
SdcGUID []types.String `tfsdk:"sdc_guid"`
52+
MdmConnectionState []types.String `tfsdk:"mdm_connection_state"`
53+
Name []types.String `tfsdk:"name"`
54+
}
55+
4456
// SdcLinkModel - MODEL for SDC Links data returned by goscaleio.
4557
type SdcLinkModel struct {
4658
Rel types.String `tfsdk:"rel"`

powerflex/provider/sdc_datasource.go

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ package provider
1919

2020
import (
2121
"context"
22+
"fmt"
2223
"strings"
2324

2425
"terraform-provider-powerflex/powerflex/helper"
2526
"terraform-provider-powerflex/powerflex/models"
2627

2728
"github.com/dell/goscaleio"
29+
scaleiotypes "github.com/dell/goscaleio/types/v1"
2830
"github.com/hashicorp/terraform-plugin-framework/datasource"
2931
"github.com/hashicorp/terraform-plugin-framework/types"
3032
"github.com/hashicorp/terraform-plugin-log/tflog"
@@ -111,40 +113,24 @@ func (d *sdcDataSource) Read(ctx context.Context, req datasource.ReadRequest, re
111113
}
112114
sdcs = sdcs[:n]
113115
}
114-
115-
// Set state
116-
searchFilter := helper.SdcFilterType.All
117-
if !state.Name.IsNull() {
118-
searchFilter = helper.SdcFilterType.ByName
119-
}
120-
if !state.ID.IsNull() {
121-
searchFilter = helper.SdcFilterType.ByID
122-
}
123-
124-
allSdcWithStats := helper.GetAllSdcState(ctx, *d.client, sdcs)
125-
126-
if len(*allSdcWithStats) == 0 {
127-
resp.Diagnostics.AddError("SDCs are not installed on the PowerFlex cluster.",
128-
"SDCs are not installed on the PowerFlex cluster.",
129-
)
130-
return
131-
}
132-
133-
if searchFilter == helper.SdcFilterType.All {
134-
state.Sdcs = *allSdcWithStats
135-
} else {
136-
filterResult := helper.GetFilteredSdcState(allSdcWithStats, searchFilter, state.Name.ValueString(), state.ID.ValueString())
137-
if len(*filterResult) == 0 {
138-
resp.Diagnostics.AddError("Couldn't find SDC.",
139-
"Couldn't find SDC.",
116+
// Set state for filters
117+
if state.SdcFilter != nil {
118+
filtered, err := helper.GetDataSourceByValue(*state.SdcFilter, sdcs)
119+
if err != nil {
120+
resp.Diagnostics.AddError(
121+
fmt.Sprintf("Error in filtering sdcs: %v please validate the filter", state.SdcFilter), err.Error(),
140122
)
141123
return
142124
}
143-
state.Sdcs = *filterResult
125+
filteredSdc := []scaleiotypes.Sdc{}
126+
for _, val := range filtered {
127+
filteredSdc = append(filteredSdc, val.(scaleiotypes.Sdc))
128+
}
129+
sdcs = filteredSdc
144130
}
131+
state.Sdcs = helper.GetAllSdcState(ctx, *d.client, sdcs)
145132

146-
state.Name = types.StringValue(state.Name.ValueString())
147-
state.ID = types.StringValue(state.ID.ValueString())
133+
state.ID = types.StringValue("sdc_datasource_id")
148134

149135
diags = resp.State.Set(ctx, state)
150136
resp.Diagnostics.Append(diags...)

powerflex/provider/sdc_datasource_schema.go

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@ limitations under the License.
1818
package provider
1919

2020
import (
21+
"terraform-provider-powerflex/powerflex/helper"
2122
"terraform-provider-powerflex/powerflex/models"
2223

23-
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
2424
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
25-
"github.com/hashicorp/terraform-plugin-framework/path"
26-
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
2725
)
2826

2927
// SDCDataSourceScheme is variable for schematic for SDC Data Source
@@ -32,27 +30,9 @@ var SDCDataSourceScheme schema.Schema = schema.Schema{
3230
MarkdownDescription: "This datasource is used to query the existing Storage Data Clients from the PowerFlex array. The information fetched from this datasource can be used for getting the details / for further processing in resource block.",
3331
Attributes: map[string]schema.Attribute{
3432
"id": schema.StringAttribute{
35-
Description: "ID of the SDC to fetch." +
36-
" Conflicts with 'name'",
37-
MarkdownDescription: "ID of the SDC to fetch." +
38-
" Conflicts with `name`",
39-
Optional: true,
40-
Computed: true,
41-
Validators: []validator.String{
42-
stringvalidator.ConflictsWith(path.MatchRoot("name")),
43-
stringvalidator.LengthAtLeast(1),
44-
},
45-
},
46-
"name": schema.StringAttribute{
47-
Description: "Name of the SDC to fetch." +
48-
" Conflicts with 'id'",
49-
MarkdownDescription: "Name of the SDC to fetch." +
50-
" Conflicts with `id`",
51-
Optional: true,
52-
Computed: true,
53-
Validators: []validator.String{
54-
stringvalidator.LengthAtLeast(1),
55-
},
33+
Description: "ID placeholder for sdc datasource",
34+
MarkdownDescription: "ID placeholder for sdc datasource",
35+
Computed: true,
5636
},
5737
"sdcs": schema.ListNestedAttribute{
5838
Description: "List of fetched SDCs.",
@@ -111,4 +91,9 @@ var SDCDataSourceScheme schema.Schema = schema.Schema{
11191
},
11292
},
11393
},
94+
Blocks: map[string]schema.Block{
95+
"filter": schema.SingleNestedBlock{
96+
Attributes: helper.GenerateSchemaAttributes(helper.TypeToMap(models.SdcFilter{})),
97+
},
98+
},
11499
}

0 commit comments

Comments
 (0)