Skip to content

Commit

Permalink
filter node by source id
Browse files Browse the repository at this point in the history
  • Loading branch information
shaswot77 committed Apr 17, 2024
1 parent 1b5a1b9 commit a836976
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 11 deletions.
10 changes: 6 additions & 4 deletions docs/data-sources/nodes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
```

Expand All @@ -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

Expand All @@ -59,4 +59,6 @@ Read-Only:

- `display_name` (String)
- `id` (String)
- `source_id` (String)
- `source_name` (String)
- `type` (String)
7 changes: 3 additions & 4 deletions examples/data-sources/squaredup_nodes/data-source.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
13 changes: 11 additions & 2 deletions internal/provider/client_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
}
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
}

Expand Down
21 changes: 20 additions & 1 deletion internal/provider/data_source_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ 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"`
}

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) {
Expand All @@ -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},
},
},
},
Expand All @@ -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,
Expand Down Expand Up @@ -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",
Expand All @@ -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)
}
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a836976

Please sign in to comment.