Skip to content

Commit

Permalink
SDC Datasource Filter
Browse files Browse the repository at this point in the history
  • Loading branch information
doriac11 committed Dec 6, 2024
1 parent c5efd03 commit e7d09b8
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 170 deletions.
59 changes: 44 additions & 15 deletions docs/data-sources/sdc.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ description: |-

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.

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

```terraform
Expand All @@ -49,21 +47,37 @@ limitations under the License.
*/
# commands to run this tf file : terraform init && terraform apply --auto-approve
# Reads SDC either by name or by id , if provided
# If both name and id is not provided , then it reads all the SDC
# id and name can't be given together to fetch the SDC .
# id can't be empty
data "powerflex_sdc" "selected" {
#id = "e3ce1fb500000000"
name = "sdc_01"
data "powerflex_sdc" "all" {
}
# # Returns all sdcs matching criteria
# Returns all sdcs
output "allsdcresult" {
value = data.powerflex_sdc.selected
value = data.powerflex_sdc.all
}
# # -----------------------------------------------------------------------------------
// 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_sdc" "filtered" {
filter {
# id = ["ID1", "ID2"]
# system_id = ["systemID", "systemID2"]
# sdc_ip = ["SCDIP1", "SCDIP2"]
# sdc_approved = false
# on_vmware = false
# sdc_guid = ["SdcGUID1", "SdcGUID2"]
# mdm_connection_state = ["MdmConnectionState1", "MdmConnectionState2"]
# name = ["Name1", "Name2"]
}
}
# Returns filtered sdcs matching criteria
output "filteredsdcresult" {
value = data.powerflex_sdc.filtered
}
# -----------------------------------------------------------------------------------
```

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.
Expand All @@ -73,13 +87,28 @@ After the successful execution of above said block, We can see the output by exe

### Optional

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

### Read-Only

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

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

Optional:

- `id` (Set of String) List of id
- `mdm_connection_state` (Set of String) List of mdm_connection_state
- `name` (Set of String) List of name
- `on_vmware` (Boolean) Value for on_vmware
- `sdc_approved` (Boolean) Value for sdc_approved
- `sdc_guid` (Set of String) List of sdc_guid
- `sdc_ip` (Set of String) List of sdc_ip
- `system_id` (Set of String) List of system_id


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

Expand Down
38 changes: 27 additions & 11 deletions examples/data-sources/powerflex_sdc/data-source.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,35 @@ limitations under the License.
*/

# commands to run this tf file : terraform init && terraform apply --auto-approve
# Reads SDC either by name or by id , if provided
# If both name and id is not provided , then it reads all the SDC
# id and name can't be given together to fetch the SDC .
# id can't be empty

data "powerflex_sdc" "selected" {
#id = "e3ce1fb500000000"
name = "sdc_01"

data "powerflex_sdc" "all" {

}

# # Returns all sdcs matching criteria
# Returns all sdcs
output "allsdcresult" {
value = data.powerflex_sdc.selected
value = data.powerflex_sdc.all
}

// 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_sdc" "filtered" {
filter {
# id = ["ID1", "ID2"]
# system_id = ["systemID", "systemID2"]
# sdc_ip = ["SCDIP1", "SCDIP2"]
# sdc_approved = false
# on_vmware = false
# sdc_guid = ["SdcGUID1", "SdcGUID2"]
# mdm_connection_state = ["MdmConnectionState1", "MdmConnectionState2"]
# name = ["Name1", "Name2"]
}
}

# Returns filtered sdcs matching criteria
output "filteredsdcresult" {
value = data.powerflex_sdc.filtered
}
# # -----------------------------------------------------------------------------------
# -----------------------------------------------------------------------------------

4 changes: 2 additions & 2 deletions powerflex/helper/sdc_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func GetFilteredSdcState(sdcs *[]models.SdcModel, method string, name string, id
}

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

return &response
return response
}

// GetSDCDetailType returns the SDC Detail type
Expand Down
18 changes: 15 additions & 3 deletions powerflex/models/sdc.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (

// SdcDataSourceModel - for returning result to terraform.
type SdcDataSourceModel struct {
ID types.String `tfsdk:"id"`
Sdcs []SdcModel `tfsdk:"sdcs"`
Name types.String `tfsdk:"name"`
ID types.String `tfsdk:"id"`
Sdcs []SdcModel `tfsdk:"sdcs"`
SdcFilter *SdcFilter `tfsdk:"filter"`
}

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

// SdcFilter - MODEL for SDC filter parameters.
type SdcFilter struct {
ID []types.String `tfsdk:"id"`
SystemID []types.String `tfsdk:"system_id"`
SdcIP []types.String `tfsdk:"sdc_ip"`
SdcApproved types.Bool `tfsdk:"sdc_approved"`
OnVMWare types.Bool `tfsdk:"on_vmware"`
SdcGUID []types.String `tfsdk:"sdc_guid"`
MdmConnectionState []types.String `tfsdk:"mdm_connection_state"`
Name []types.String `tfsdk:"name"`
}

// SdcLinkModel - MODEL for SDC Links data returned by goscaleio.
type SdcLinkModel struct {
Rel types.String `tfsdk:"rel"`
Expand Down
44 changes: 15 additions & 29 deletions powerflex/provider/sdc_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ package provider

import (
"context"
"fmt"
"strings"

"terraform-provider-powerflex/powerflex/helper"
"terraform-provider-powerflex/powerflex/models"

"github.com/dell/goscaleio"
scaleiotypes "github.com/dell/goscaleio/types/v1"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
Expand Down Expand Up @@ -111,40 +113,24 @@ func (d *sdcDataSource) Read(ctx context.Context, req datasource.ReadRequest, re
}
sdcs = sdcs[:n]
}

// Set state
searchFilter := helper.SdcFilterType.All
if !state.Name.IsNull() {
searchFilter = helper.SdcFilterType.ByName
}
if !state.ID.IsNull() {
searchFilter = helper.SdcFilterType.ByID
}

allSdcWithStats := helper.GetAllSdcState(ctx, *d.client, sdcs)

if len(*allSdcWithStats) == 0 {
resp.Diagnostics.AddError("SDCs are not installed on the PowerFlex cluster.",
"SDCs are not installed on the PowerFlex cluster.",
)
return
}

if searchFilter == helper.SdcFilterType.All {
state.Sdcs = *allSdcWithStats
} else {
filterResult := helper.GetFilteredSdcState(allSdcWithStats, searchFilter, state.Name.ValueString(), state.ID.ValueString())
if len(*filterResult) == 0 {
resp.Diagnostics.AddError("Couldn't find SDC.",
"Couldn't find SDC.",
// Set state for filters
if state.SdcFilter != nil {
filtered, err := helper.GetDataSourceByValue(*state.SdcFilter, sdcs)
if err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Error in filtering sdcs: %v please validate the filter", state.SdcFilter), err.Error(),
)
return
}
state.Sdcs = *filterResult
filteredSdc := []scaleiotypes.Sdc{}
for _, val := range filtered {
filteredSdc = append(filteredSdc, val.(scaleiotypes.Sdc))
}
sdcs = filteredSdc
}
state.Sdcs = helper.GetAllSdcState(ctx, *d.client, sdcs)

state.Name = types.StringValue(state.Name.ValueString())
state.ID = types.StringValue(state.ID.ValueString())
state.ID = types.StringValue("sdc_datasource_id")

diags = resp.State.Set(ctx, state)
resp.Diagnostics.Append(diags...)
Expand Down
33 changes: 9 additions & 24 deletions powerflex/provider/sdc_datasource_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ limitations under the License.
package provider

import (
"terraform-provider-powerflex/powerflex/helper"
"terraform-provider-powerflex/powerflex/models"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

// SDCDataSourceScheme is variable for schematic for SDC Data Source
Expand All @@ -32,27 +30,9 @@ var SDCDataSourceScheme schema.Schema = schema.Schema{
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.",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "ID of the SDC to fetch." +
" Conflicts with 'name'",
MarkdownDescription: "ID of the SDC to fetch." +
" Conflicts with `name`",
Optional: true,
Computed: true,
Validators: []validator.String{
stringvalidator.ConflictsWith(path.MatchRoot("name")),
stringvalidator.LengthAtLeast(1),
},
},
"name": schema.StringAttribute{
Description: "Name of the SDC to fetch." +
" Conflicts with 'id'",
MarkdownDescription: "Name of the SDC to fetch." +
" Conflicts with `id`",
Optional: true,
Computed: true,
Validators: []validator.String{
stringvalidator.LengthAtLeast(1),
},
Description: "ID placeholder for sdc datasource",
MarkdownDescription: "ID placeholder for sdc datasource",
Computed: true,
},
"sdcs": schema.ListNestedAttribute{
Description: "List of fetched SDCs.",
Expand Down Expand Up @@ -111,4 +91,9 @@ var SDCDataSourceScheme schema.Schema = schema.Schema{
},
},
},
Blocks: map[string]schema.Block{
"filter": schema.SingleNestedBlock{
Attributes: helper.GenerateSchemaAttributes(helper.TypeToMap(models.SdcFilter{})),
},
},
}
Loading

0 comments on commit e7d09b8

Please sign in to comment.