From 412909f13ceb6151102525bf64fe9a76d3c818b7 Mon Sep 17 00:00:00 2001 From: Shaswot Subedi Date: Wed, 15 Jan 2025 12:04:18 +0000 Subject: [PATCH 1/3] add onprem as optional bool value --- docs/data-sources/datasources.md | 1 + docs/resources/datasource.md | 1 + internal/provider/client_data_source.go | 31 ++++++++++++++----- internal/provider/data_source_data_sources.go | 7 ++++- internal/provider/resource_data_source.go | 10 ++++++ 5 files changed, 41 insertions(+), 9 deletions(-) diff --git a/docs/data-sources/datasources.md b/docs/data-sources/datasources.md index 9697a07..c9cc7fc 100644 --- a/docs/data-sources/datasources.md +++ b/docs/data-sources/datasources.md @@ -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 diff --git a/docs/resources/datasource.md b/docs/resources/datasource.md index a3f0a92..36bab55 100644 --- a/docs/resources/datasource.md +++ b/docs/resources/datasource.md @@ -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 diff --git a/internal/provider/client_data_source.go b/internal/provider/client_data_source.go index 7c655d0..f63af4a 100644 --- a/internal/provider/client_data_source.go +++ b/internal/provider/client_data_source.go @@ -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 @@ -33,7 +33,22 @@ func (c *SquaredUpClient) GetLatestDataSources(filterDisplayName string) ([]Late } if len(filteredPlugins) == 0 { - return nil, fmt.Errorf("No plugins found with display name: %s", filterDisplayName) + return nil, fmt.Errorf("no plugins found with display name: %s", filterDisplayName) + } + + return filteredPlugins, nil + } + + if *onPrem { + filteredPlugins := []LatestDataSource{} + for _, plugin := range plugins { + if plugin.OnPrem { + filteredPlugins = append(filteredPlugins, plugin) + } + } + + if len(filteredPlugins) == 0 { + return nil, fmt.Errorf("no on-prem plugins found") } return filteredPlugins, nil @@ -42,8 +57,8 @@ func (c *SquaredUpClient) GetLatestDataSources(filterDisplayName string) ([]Late return plugins, 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 } @@ -79,8 +94,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 } @@ -129,8 +144,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 } diff --git a/internal/provider/data_source_data_sources.go b/internal/provider/data_source_data_sources.go index e07d171..d9cbfca 100644 --- a/internal/provider/data_source_data_sources.go +++ b/internal/provider/data_source_data_sources.go @@ -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 { @@ -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{ @@ -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", diff --git a/internal/provider/resource_data_source.go b/internal/provider/resource_data_source.go index 1281e64..1a68b14 100644 --- a/internal/provider/resource_data_source.go +++ b/internal/provider/resource_data_source.go @@ -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"` @@ -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, @@ -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(), ) @@ -133,6 +140,7 @@ func (r *dataSourceResource) Create(ctx context.Context, req resource.CreateRequ state := dataSource{ DisplayName: types.StringValue(newDataSource.DisplayName), + OnPrem: types.BoolPointerValue(plan.OnPrem.ValueBoolPointer()), Name: types.StringValue(newDataSource.Plugin.Name), AgentGroupID: types.StringValue(newDataSource.AgentGroupID), ID: types.StringValue(newDataSource.ID), @@ -214,6 +222,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(), ) @@ -236,6 +245,7 @@ func (r *dataSourceResource) Update(ctx context.Context, req resource.UpdateRequ state = dataSource{ DisplayName: types.StringValue(getDataSource.DisplayName), + OnPrem: types.BoolPointerValue(plan.OnPrem.ValueBoolPointer()), Name: types.StringValue(getDataSource.Plugin.Name), AgentGroupID: types.StringValue(getDataSource.AgentGroupID), ID: types.StringValue(getDataSource.ID), From 662712c4265e203b4b0713a11bfbddc8df801933 Mon Sep 17 00:00:00 2001 From: Shaswot Subedi Date: Wed, 15 Jan 2025 12:28:15 +0000 Subject: [PATCH 2/3] change filtering function --- internal/provider/client_data_source.go | 34 ++++++++----------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/internal/provider/client_data_source.go b/internal/provider/client_data_source.go index f63af4a..7680e7c 100644 --- a/internal/provider/client_data_source.go +++ b/internal/provider/client_data_source.go @@ -24,37 +24,25 @@ func (c *SquaredUpClient) GetLatestDataSources(filterDisplayName string, onPrem 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) } - if *onPrem { - filteredPlugins := []LatestDataSource{} - for _, plugin := range plugins { - if plugin.OnPrem { - filteredPlugins = append(filteredPlugins, plugin) - } - } - - if len(filteredPlugins) == 0 { - return nil, fmt.Errorf("no on-prem plugins found") - } - - return filteredPlugins, nil + if len(filteredPlugins) == 0 { + return nil, fmt.Errorf("no plugins found with the given filter") } - return plugins, nil + return filteredPlugins, nil + } func (c *SquaredUpClient) GenerateDataSourcePayload(displayName string, name string, onPrem *bool, pluginConfig map[string]interface{}, agentGroupId string) (map[string]interface{}, error) { From 27ea26e518bd7f0e1ffd278beae7dda608fab60b Mon Sep 17 00:00:00 2001 From: Shaswot Subedi Date: Wed, 15 Jan 2025 15:03:20 +0000 Subject: [PATCH 3/3] fix import --- internal/provider/models.go | 3 ++- internal/provider/resource_data_source.go | 5 +++-- internal/provider/resource_data_source_test.go | 1 + 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/internal/provider/models.go b/internal/provider/models.go index c8400c1..d533be4 100644 --- a/internal/provider/models.go +++ b/internal/provider/models.go @@ -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"` } diff --git a/internal/provider/resource_data_source.go b/internal/provider/resource_data_source.go index 1a68b14..6a08131 100644 --- a/internal/provider/resource_data_source.go +++ b/internal/provider/resource_data_source.go @@ -140,7 +140,7 @@ func (r *dataSourceResource) Create(ctx context.Context, req resource.CreateRequ state := dataSource{ DisplayName: types.StringValue(newDataSource.DisplayName), - OnPrem: types.BoolPointerValue(plan.OnPrem.ValueBoolPointer()), + OnPrem: types.BoolPointerValue(&newDataSource.Plugin.OnPrem), Name: types.StringValue(newDataSource.Plugin.Name), AgentGroupID: types.StringValue(newDataSource.AgentGroupID), ID: types.StringValue(newDataSource.ID), @@ -176,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) @@ -245,7 +246,7 @@ func (r *dataSourceResource) Update(ctx context.Context, req resource.UpdateRequ state = dataSource{ DisplayName: types.StringValue(getDataSource.DisplayName), - OnPrem: types.BoolPointerValue(plan.OnPrem.ValueBoolPointer()), + OnPrem: types.BoolPointerValue(&getDataSource.Plugin.OnPrem), Name: types.StringValue(getDataSource.Plugin.Name), AgentGroupID: types.StringValue(getDataSource.AgentGroupID), ID: types.StringValue(getDataSource.ID), diff --git a/internal/provider/resource_data_source_test.go b/internal/provider/resource_data_source_test.go index 1526184..7df054a 100644 --- a/internal/provider/resource_data_source_test.go +++ b/internal/provider/resource_data_source_test.go @@ -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