Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to Filter OnPrem Datasource #106

Merged
merged 3 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/data-sources/datasources.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ data "squaredup_datasources" "sample_data" {
### Optional

- `data_source_name` (String) The name of the data source. If not specified, all data sources will be returned.
- `on_prem` (Boolean) If true, only on-prem data sources will be returned.

### Read-Only

Expand Down
1 change: 1 addition & 0 deletions docs/resources/datasource.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ resource "squaredup_datasource" "ado_datasource" {

- `agent_group_id` (String) The ID of the agent group to which the data source should connect to (on-prem data sources only)
- `config` (String, Sensitive) Sensitive configuration for the data source. Needs to be a valid JSON
- `on_prem` (Boolean) Whether the data source is an on-prem data source

### Read-Only

Expand Down
37 changes: 20 additions & 17 deletions internal/provider/client_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"
)

func (c *SquaredUpClient) GetLatestDataSources(filterDisplayName string) ([]LatestDataSource, error) {
func (c *SquaredUpClient) GetLatestDataSources(filterDisplayName string, onPrem *bool) ([]LatestDataSource, error) {
req, err := http.NewRequest("GET", c.baseURL+"/api/plugins/latest", nil)
if err != nil {
return nil, err
Expand All @@ -24,26 +24,29 @@ func (c *SquaredUpClient) GetLatestDataSources(filterDisplayName string) ([]Late
return nil, err
}

if filterDisplayName != "" {
filteredPlugins := []LatestDataSource{}
for _, plugin := range plugins {
if plugin.DisplayName == filterDisplayName {
filteredPlugins = append(filteredPlugins, plugin)
}
filteredPlugins := []LatestDataSource{}
for _, plugin := range plugins {
if filterDisplayName != "" && plugin.DisplayName != filterDisplayName {
continue
}

if len(filteredPlugins) == 0 {
return nil, fmt.Errorf("No plugins found with display name: %s", filterDisplayName)
if onPrem != nil && plugin.OnPrem != *onPrem {
continue
}

return filteredPlugins, nil
filteredPlugins = append(filteredPlugins, plugin)
}

return plugins, nil
if len(filteredPlugins) == 0 {
return nil, fmt.Errorf("no plugins found with the given filter")
}

return filteredPlugins, nil

}

func (c *SquaredUpClient) GenerateDataSourcePayload(displayName string, name string, pluginConfig map[string]interface{}, agentGroupId string) (map[string]interface{}, error) {
plugins, err := c.GetLatestDataSources(name)
func (c *SquaredUpClient) GenerateDataSourcePayload(displayName string, name string, onPrem *bool, pluginConfig map[string]interface{}, agentGroupId string) (map[string]interface{}, error) {
plugins, err := c.GetLatestDataSources(name, onPrem)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -79,8 +82,8 @@ func (c *SquaredUpClient) GenerateDataSourcePayload(displayName string, name str
return DataSourcePayload, nil
}

func (c *SquaredUpClient) AddDataSource(displayName string, name string, pluginConfig map[string]interface{}, agentGroupId string) (*DataSource, error) {
DataSourcePayload, err := c.GenerateDataSourcePayload(displayName, name, pluginConfig, agentGroupId)
func (c *SquaredUpClient) AddDataSource(displayName string, name string, onPrem *bool, pluginConfig map[string]interface{}, agentGroupId string) (*DataSource, error) {
DataSourcePayload, err := c.GenerateDataSourcePayload(displayName, name, onPrem, pluginConfig, agentGroupId)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -129,8 +132,8 @@ func (c *SquaredUpClient) GetDataSource(dataSourceId string) (*DataSource, error
return &dataSource, nil
}

func (c *SquaredUpClient) UpdateDataSource(dataSourceId string, displayName string, name string, pluginConfig map[string]interface{}, agentGroupId string) error {
DataSourcePayload, err := c.GenerateDataSourcePayload(displayName, name, pluginConfig, agentGroupId)
func (c *SquaredUpClient) UpdateDataSource(dataSourceId string, displayName string, name string, onPrem *bool, pluginConfig map[string]interface{}, agentGroupId string) error {
DataSourcePayload, err := c.GenerateDataSourcePayload(displayName, name, onPrem, pluginConfig, agentGroupId)
if err != nil {
return err
}
Expand Down
7 changes: 6 additions & 1 deletion internal/provider/data_source_data_sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type squaredupLatestDataSource struct {
type squaredupDataSourceModel struct {
Plugins []squaredupPluginModel `tfsdk:"plugins"`
DataSourceName types.String `tfsdk:"data_source_name"`
OnPrem types.Bool `tfsdk:"on_prem"`
}

type squaredupPluginModel struct {
Expand All @@ -46,6 +47,10 @@ func (d *squaredupLatestDataSource) Schema(_ context.Context, _ datasource.Schem
Optional: true,
MarkdownDescription: "The name of the data source. If not specified, all data sources will be returned.",
},
"on_prem": schema.BoolAttribute{
Optional: true,
MarkdownDescription: "If true, only on-prem data sources will be returned.",
},
"plugins": schema.ListNestedAttribute{
Computed: true,
NestedObject: schema.NestedAttributeObject{
Expand All @@ -70,7 +75,7 @@ func (d *squaredupLatestDataSource) Read(ctx context.Context, req datasource.Rea
return
}

plugins, err := d.client.GetLatestDataSources(state.DataSourceName.ValueString())
plugins, err := d.client.GetLatestDataSources(state.DataSourceName.ValueString(), state.OnPrem.ValueBoolPointer())
if err != nil {
resp.Diagnostics.AddError(
"Error making API request to fetch latest Data Sources",
Expand Down
3 changes: 2 additions & 1 deletion internal/provider/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ type DataSource struct {
DisplayName string `json:"displayName"`
ID string `json:"id,omitempty"`
Plugin struct {
Name string `json:"name"`
Name string `json:"name"`
OnPrem bool `json:"onPrem"`
} `json:"plugin"`
AgentGroupID string `json:"agentGroupId,omitempty"`
}
Expand Down
11 changes: 11 additions & 0 deletions internal/provider/resource_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func (r *dataSourceResource) Metadata(_ context.Context, req resource.MetadataRe

type dataSource struct {
DisplayName types.String `tfsdk:"display_name"`
OnPrem types.Bool `tfsdk:"on_prem"`
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"data_source_name"`
Config types.String `tfsdk:"config"`
Expand All @@ -61,6 +62,11 @@ func (r *dataSourceResource) Schema(_ context.Context, _ resource.SchemaRequest,
MarkdownDescription: "Display name of the data source",
Required: true,
},
"on_prem": schema.BoolAttribute{
MarkdownDescription: "Whether the data source is an on-prem data source",
Optional: true,
Computed: true,
},
"config": schema.StringAttribute{
MarkdownDescription: "Sensitive configuration for the data source. Needs to be a valid JSON",
Optional: true,
Expand Down Expand Up @@ -120,6 +126,7 @@ func (r *dataSourceResource) Create(ctx context.Context, req resource.CreateRequ
newDataSource, err := r.client.AddDataSource(
plan.DisplayName.ValueString(),
plan.Name.ValueString(),
plan.OnPrem.ValueBoolPointer(),
plugin_config,
plan.AgentGroupID.ValueString(),
)
Expand All @@ -133,6 +140,7 @@ func (r *dataSourceResource) Create(ctx context.Context, req resource.CreateRequ

state := dataSource{
DisplayName: types.StringValue(newDataSource.DisplayName),
OnPrem: types.BoolPointerValue(&newDataSource.Plugin.OnPrem),
Name: types.StringValue(newDataSource.Plugin.Name),
AgentGroupID: types.StringValue(newDataSource.AgentGroupID),
ID: types.StringValue(newDataSource.ID),
Expand Down Expand Up @@ -168,6 +176,7 @@ func (r *dataSourceResource) Read(ctx context.Context, req resource.ReadRequest,
}

state.DisplayName = types.StringValue(readDataSource.DisplayName)
state.OnPrem = types.BoolValue(readDataSource.Plugin.OnPrem)
state.Name = types.StringValue(readDataSource.Plugin.Name)
state.AgentGroupID = types.StringValue(readDataSource.AgentGroupID)
state.ID = types.StringValue(readDataSource.ID)
Expand Down Expand Up @@ -214,6 +223,7 @@ func (r *dataSourceResource) Update(ctx context.Context, req resource.UpdateRequ
state.ID.ValueString(),
plan.DisplayName.ValueString(),
plan.Name.ValueString(),
plan.OnPrem.ValueBoolPointer(),
plugin_config,
plan.AgentGroupID.ValueString(),
)
Expand All @@ -236,6 +246,7 @@ func (r *dataSourceResource) Update(ctx context.Context, req resource.UpdateRequ

state = dataSource{
DisplayName: types.StringValue(getDataSource.DisplayName),
OnPrem: types.BoolPointerValue(&getDataSource.Plugin.OnPrem),
Name: types.StringValue(getDataSource.Plugin.Name),
AgentGroupID: types.StringValue(getDataSource.AgentGroupID),
ID: types.StringValue(getDataSource.ID),
Expand Down
1 change: 1 addition & 0 deletions internal/provider/resource_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ resource "squaredup_datasource" "sample_data_source" {
resource.TestCheckResourceAttr("squaredup_datasource.sample_data_source", "display_name", "Sample Data - DataSource Test - "+uuid),
resource.TestCheckResourceAttrSet("squaredup_datasource.sample_data_source", "id"),
resource.TestCheckResourceAttrSet("squaredup_datasource.sample_data_source", "last_updated"),
resource.TestCheckResourceAttr("squaredup_datasource.sample_data_source", "on_prem", "false"),
),
},
//Update DataSource Test
Expand Down
Loading