From a8369769242cef02fb7875c5447132daaf30f0c8 Mon Sep 17 00:00:00 2001 From: Shaswot Subedi Date: Wed, 17 Apr 2024 13:23:45 +0100 Subject: [PATCH] filter node by source id --- docs/data-sources/nodes.md | 10 +++++---- .../squaredup_nodes/data-source.tf | 7 +++---- internal/provider/client_node.go | 13 ++++++++++-- internal/provider/data_source_node.go | 21 ++++++++++++++++++- internal/provider/models.go | 2 ++ 5 files changed, 42 insertions(+), 11 deletions(-) diff --git a/docs/data-sources/nodes.md b/docs/data-sources/nodes.md index 6051ad7..2ae13da 100644 --- a/docs/data-sources/nodes.md +++ b/docs/data-sources/nodes.md @@ -22,17 +22,16 @@ resource "squaredup_datasource" "sample_data_source" { data_source_name = data.squaredup_datasources.sample_data.plugins[0].display_name } -data "squaredup_nodes" "acommon_node" { +data "squaredup_nodes" "acommon_node_by_name" { depends_on = [squaredup_datasource.sample_data_source] data_source_id = squaredup_datasource.sample_data_source.id node_name = "account-common-lambda" } -data "squaredup_nodes" "s3_node" { +data "squaredup_nodes" "acommon_node_by_id" { depends_on = [squaredup_datasource.sample_data_source] data_source_id = squaredup_datasource.sample_data_source.id - node_name = "account-common-s3" - allow_no_data = true + node_source_id = "sample-server-2" } ``` @@ -47,6 +46,7 @@ data "squaredup_nodes" "s3_node" { - `allow_no_data` (Boolean) If true, the data source will return an empty list if its unable to find the node. - `node_name` (String) Node Name +- `node_source_id` (String) Node Source ID ### Read-Only @@ -59,4 +59,6 @@ Read-Only: - `display_name` (String) - `id` (String) +- `source_id` (String) - `source_name` (String) +- `type` (String) diff --git a/examples/data-sources/squaredup_nodes/data-source.tf b/examples/data-sources/squaredup_nodes/data-source.tf index 601151a..c5b8484 100644 --- a/examples/data-sources/squaredup_nodes/data-source.tf +++ b/examples/data-sources/squaredup_nodes/data-source.tf @@ -7,15 +7,14 @@ resource "squaredup_datasource" "sample_data_source" { data_source_name = data.squaredup_datasources.sample_data.plugins[0].display_name } -data "squaredup_nodes" "acommon_node" { +data "squaredup_nodes" "acommon_node_by_name" { depends_on = [squaredup_datasource.sample_data_source] data_source_id = squaredup_datasource.sample_data_source.id node_name = "account-common-lambda" } -data "squaredup_nodes" "s3_node" { +data "squaredup_nodes" "acommon_node_by_id" { depends_on = [squaredup_datasource.sample_data_source] data_source_id = squaredup_datasource.sample_data_source.id - node_name = "account-common-s3" - allow_no_data = true + node_source_id = "sample-server-2" } diff --git a/internal/provider/client_node.go b/internal/provider/client_node.go index 45993c0..efd9574 100644 --- a/internal/provider/client_node.go +++ b/internal/provider/client_node.go @@ -11,9 +11,10 @@ import ( const maxRetries = 10 const retryDelaySeconds = 30 -func (c *SquaredUpClient) GetNodes(dataSourceId string, nodeName string, allowNull bool) ([]GremlinQueryResult, error) { +func (c *SquaredUpClient) GetNodes(dataSourceId string, nodeName string, nodeSourceId string, allowNull bool) ([]GremlinQueryResult, error) { var gremlinQueryResults []GremlinQueryResult for attempt := 1; attempt <= maxRetries; attempt++ { + errMessage := fmt.Sprintf("no nodes found with name: %s in data source: %s. attempted to search for it %d times", nodeName, dataSourceId, attempt) rb := map[string]interface{}{ "gremlinQuery": "g.V().has('__configId', '" + dataSourceId + "').has('name', '" + nodeName + "').hasNot('__canonicalType').valueMap(true)", } @@ -22,6 +23,14 @@ func (c *SquaredUpClient) GetNodes(dataSourceId string, nodeName string, allowNu rb = map[string]interface{}{ "gremlinQuery": "g.V().has('__configId', '" + dataSourceId + "').hasNot('__canonicalType').valueMap(true)", } + errMessage = fmt.Sprintf("failed to get nodes from data source: %s. attempted to search for it %d times", dataSourceId, attempt) + } + + if nodeSourceId != "" { + rb = map[string]interface{}{ + "gremlinQuery": "g.V().has('__configId', '" + dataSourceId + "').has('sourceId', '" + nodeSourceId + "').hasNot('__canonicalType').valueMap(true)", + } + errMessage = fmt.Sprintf("no nodes found with source id: %s in data source: %s. attempted to search for it %d times", nodeSourceId, dataSourceId, attempt) } reqBody, err := json.Marshal(rb) @@ -55,7 +64,7 @@ func (c *SquaredUpClient) GetNodes(dataSourceId string, nodeName string, allowNu continue } if !allowNull { - return nil, fmt.Errorf("no nodes found with name: %s in data source: %s. attempted to search for it %d times", nodeName, dataSourceId, attempt) + return nil, fmt.Errorf(errMessage) } } diff --git a/internal/provider/data_source_node.go b/internal/provider/data_source_node.go index ed78da4..5f36827 100644 --- a/internal/provider/data_source_node.go +++ b/internal/provider/data_source_node.go @@ -26,6 +26,7 @@ type squaredupNodesResponse struct { NodeProperties []squaredupNodesProperties `tfsdk:"node_properties"` DataSourceID types.String `tfsdk:"data_source_id"` NodeName types.String `tfsdk:"node_name"` + NodeSourceID types.String `tfsdk:"node_source_id"` AllowNoData types.Bool `tfsdk:"allow_no_data"` } @@ -33,6 +34,8 @@ type squaredupNodesProperties struct { ID types.String `tfsdk:"id"` SourceName types.String `tfsdk:"source_name"` DisplayName types.String `tfsdk:"display_name"` + SourceID types.String `tfsdk:"source_id"` + Type types.String `tfsdk:"type"` } func (d *squaredupNodes) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { @@ -50,6 +53,8 @@ func (d *squaredupNodes) Schema(_ context.Context, req datasource.SchemaRequest, "id": schema.StringAttribute{Computed: true}, "source_name": schema.StringAttribute{Computed: true}, "display_name": schema.StringAttribute{Computed: true}, + "source_id": schema.StringAttribute{Computed: true}, + "type": schema.StringAttribute{Computed: true}, }, }, }, @@ -61,6 +66,10 @@ func (d *squaredupNodes) Schema(_ context.Context, req datasource.SchemaRequest, Description: "Node Name", Optional: true, }, + "node_source_id": schema.StringAttribute{ + Description: "Node Source ID", + Optional: true, + }, "allow_no_data": schema.BoolAttribute{ Description: "If true, the data source will return an empty list if its unable to find the node.", Optional: true, @@ -93,7 +102,15 @@ func (d *squaredupNodes) Read(ctx context.Context, req datasource.ReadRequest, r return } - nodes, err := d.client.GetNodes(state.DataSourceID.ValueString(), state.NodeName.ValueString(), state.AllowNoData.ValueBool()) + if state.NodeName.ValueString() != "" && state.NodeSourceID.ValueString() != "" { + resp.Diagnostics.AddError( + "Invalid Configuration", + "Both node_name and node_source_id cannot be used at the same time", + ) + return + } + + nodes, err := d.client.GetNodes(state.DataSourceID.ValueString(), state.NodeName.ValueString(), state.NodeSourceID.ValueString(), state.AllowNoData.ValueBool()) if err != nil { resp.Diagnostics.AddError( "Unable to Retrieve Nodes", @@ -108,6 +125,8 @@ func (d *squaredupNodes) Read(ctx context.Context, req datasource.ReadRequest, r ID: types.StringValue(node.ID), SourceName: types.StringValue(node.SourceName[0]), DisplayName: types.StringValue(node.DisplayName[0]), + SourceID: types.StringValue(node.SourceID[0]), + Type: types.StringValue(node.Type[0]), } NodeProperties = append(NodeProperties, nodeProperties) } diff --git a/internal/provider/models.go b/internal/provider/models.go index 4478c99..f82aad2 100644 --- a/internal/provider/models.go +++ b/internal/provider/models.go @@ -72,6 +72,8 @@ type GremlinQueryResult struct { ID string `json:"id"` SourceName []string `json:"sourceName"` DisplayName []string `json:"name"` + SourceID []string `json:"sourceId"` + Type []string `json:"type"` } type DashboardShare struct {