From c8b8fd53b369fe0196a48112ea8c911e0a1d8595 Mon Sep 17 00:00:00 2001 From: Chris Marget Date: Wed, 25 Sep 2024 13:26:14 -0400 Subject: [PATCH 1/4] add `ValidateConfig()` method --- .../data_source_blueprint_iba_dashboards.go | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/apstra/data_source_blueprint_iba_dashboards.go b/apstra/data_source_blueprint_iba_dashboards.go index 19a14b90..05ef7cfc 100644 --- a/apstra/data_source_blueprint_iba_dashboards.go +++ b/apstra/data_source_blueprint_iba_dashboards.go @@ -4,7 +4,9 @@ import ( "context" "fmt" "github.com/Juniper/apstra-go-sdk/apstra" + "github.com/Juniper/terraform-provider-apstra/apstra/compatibility" "github.com/Juniper/terraform-provider-apstra/apstra/utils" + "github.com/hashicorp/go-version" "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/datasource" @@ -14,10 +16,13 @@ import ( ) var _ datasource.DataSourceWithConfigure = &dataSourceBlueprintIbaDashboards{} +var _ datasource.DataSourceWithValidateConfig = &dataSourceBlueprintIbaDashboards{} var _ datasourceWithSetDcBpClientFunc = &dataSourceBlueprintIbaDashboards{} +var _ datasourceWithSetClient = &dataSourceBlueprintIbaDashboards{} type dataSourceBlueprintIbaDashboards struct { getBpClientFunc func(context.Context, string) (*apstra.TwoStageL3ClosClient, error) + client *apstra.Client } func (o *dataSourceBlueprintIbaDashboards) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { @@ -47,6 +52,33 @@ func (o *dataSourceBlueprintIbaDashboards) Schema(_ context.Context, _ datasourc } } +func (o *dataSourceBlueprintIbaDashboards) ValidateConfig(ctx context.Context, req datasource.ValidateConfigRequest, resp *datasource.ValidateConfigResponse) { + var config struct { + BlueprintId types.String `tfsdk:"blueprint_id"` + Ids types.Set `tfsdk:"ids"` + } + + // Retrieve values from config. + resp.Diagnostics.Append(req.Config.Get(ctx, &config)...) + if resp.Diagnostics.HasError() { + return + } + + // cannot proceed to config + api version validation if the provider has not been configured + if o.client == nil { + return + } + + // only supported with Apstra 4.x + if !compatibility.BpIbaDashboardOk.Check(version.Must(version.NewVersion(o.client.ApiVersion()))) { + resp.Diagnostics.AddError( + "Incompatible API version", + "This data source is compatible only with Apstra "+compatibility.BpIbaDashboardOk.String(), + ) + return + } +} + func (o *dataSourceBlueprintIbaDashboards) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { var config struct { BlueprintId types.String `tfsdk:"blueprint_id"` @@ -98,3 +130,8 @@ func (o *dataSourceBlueprintIbaDashboards) Read(ctx context.Context, req datasou func (o *dataSourceBlueprintIbaDashboards) setBpClientFunc(f func(context.Context, string) (*apstra.TwoStageL3ClosClient, error)) { o.getBpClientFunc = f } + +// setClient is used for API version compatibility check only +func (o *dataSourceBlueprintIbaDashboards) setClient(client *apstra.Client) { + o.client = client +} From 44b0830addb5d834a4f557627da2d4240f788b41 Mon Sep 17 00:00:00 2001 From: Chris Marget Date: Wed, 25 Sep 2024 13:34:07 -0400 Subject: [PATCH 2/4] update version checking for iba dashboard(s) data sources --- apstra/data_source_blueprint_iba_dashboard.go | 20 ++++++------------- .../data_source_blueprint_iba_dashboards.go | 13 +----------- 2 files changed, 7 insertions(+), 26 deletions(-) diff --git a/apstra/data_source_blueprint_iba_dashboard.go b/apstra/data_source_blueprint_iba_dashboard.go index 64c2c59c..97886cf1 100644 --- a/apstra/data_source_blueprint_iba_dashboard.go +++ b/apstra/data_source_blueprint_iba_dashboard.go @@ -43,15 +43,8 @@ func (o *dataSourceBlueprintIbaDashboard) Schema(_ context.Context, _ datasource } } -func (o *dataSourceBlueprintIbaDashboard) ValidateConfig(ctx context.Context, req datasource.ValidateConfigRequest, resp *datasource.ValidateConfigResponse) { - // Retrieve values from config. - var config iba.Dashboard - resp.Diagnostics.Append(req.Config.Get(ctx, &config)...) - if resp.Diagnostics.HasError() { - return - } - - // cannot proceed to config + api version validation if the provider has not been configured +func (o *dataSourceBlueprintIbaDashboard) ValidateConfig(_ context.Context, _ datasource.ValidateConfigRequest, resp *datasource.ValidateConfigResponse) { + // cannot proceed to api version validation if the provider has not been configured if o.client == nil { return } @@ -62,7 +55,6 @@ func (o *dataSourceBlueprintIbaDashboard) ValidateConfig(ctx context.Context, re "Incompatible API version", "This data source is compatible only with Apstra "+compatibility.BpIbaDashboardOk.String(), ) - return } } @@ -127,11 +119,11 @@ func (o *dataSourceBlueprintIbaDashboard) Read(ctx context.Context, req datasour resp.Diagnostics.Append(resp.State.Set(ctx, &config)...) } +func (o *dataSourceBlueprintIbaDashboard) setBpClientFunc(f func(context.Context, string) (*apstra.TwoStageL3ClosClient, error)) { + o.getBpClientFunc = f +} + // setClient is used for API version compatibility check only func (o *dataSourceBlueprintIbaDashboard) setClient(client *apstra.Client) { o.client = client } - -func (o *dataSourceBlueprintIbaDashboard) setBpClientFunc(f func(context.Context, string) (*apstra.TwoStageL3ClosClient, error)) { - o.getBpClientFunc = f -} diff --git a/apstra/data_source_blueprint_iba_dashboards.go b/apstra/data_source_blueprint_iba_dashboards.go index 05ef7cfc..0d06a7cd 100644 --- a/apstra/data_source_blueprint_iba_dashboards.go +++ b/apstra/data_source_blueprint_iba_dashboards.go @@ -52,18 +52,7 @@ func (o *dataSourceBlueprintIbaDashboards) Schema(_ context.Context, _ datasourc } } -func (o *dataSourceBlueprintIbaDashboards) ValidateConfig(ctx context.Context, req datasource.ValidateConfigRequest, resp *datasource.ValidateConfigResponse) { - var config struct { - BlueprintId types.String `tfsdk:"blueprint_id"` - Ids types.Set `tfsdk:"ids"` - } - - // Retrieve values from config. - resp.Diagnostics.Append(req.Config.Get(ctx, &config)...) - if resp.Diagnostics.HasError() { - return - } - +func (o *dataSourceBlueprintIbaDashboards) ValidateConfig(_ context.Context, _ datasource.ValidateConfigRequest, resp *datasource.ValidateConfigResponse) { // cannot proceed to config + api version validation if the provider has not been configured if o.client == nil { return From de25da6e4c10c404c9f66c039b96e4c46f6122ee Mon Sep 17 00:00:00 2001 From: Chris Marget Date: Wed, 25 Sep 2024 13:35:35 -0400 Subject: [PATCH 3/4] remove unnecessary return from ValidateConfig --- apstra/data_source_blueprint_iba_dashboards.go | 1 - 1 file changed, 1 deletion(-) diff --git a/apstra/data_source_blueprint_iba_dashboards.go b/apstra/data_source_blueprint_iba_dashboards.go index 0d06a7cd..077a5a3d 100644 --- a/apstra/data_source_blueprint_iba_dashboards.go +++ b/apstra/data_source_blueprint_iba_dashboards.go @@ -64,7 +64,6 @@ func (o *dataSourceBlueprintIbaDashboards) ValidateConfig(_ context.Context, _ d "Incompatible API version", "This data source is compatible only with Apstra "+compatibility.BpIbaDashboardOk.String(), ) - return } } From 024f5ba97edfa1f33de4ffa63e8504d2f02854f3 Mon Sep 17 00:00:00 2001 From: Chris Marget Date: Wed, 25 Sep 2024 13:36:13 -0400 Subject: [PATCH 4/4] gofumpt --- apstra/data_source_blueprint_iba_dashboards.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/apstra/data_source_blueprint_iba_dashboards.go b/apstra/data_source_blueprint_iba_dashboards.go index 077a5a3d..8ed7929b 100644 --- a/apstra/data_source_blueprint_iba_dashboards.go +++ b/apstra/data_source_blueprint_iba_dashboards.go @@ -3,6 +3,7 @@ package tfapstra import ( "context" "fmt" + "github.com/Juniper/apstra-go-sdk/apstra" "github.com/Juniper/terraform-provider-apstra/apstra/compatibility" "github.com/Juniper/terraform-provider-apstra/apstra/utils" @@ -15,10 +16,12 @@ import ( "github.com/hashicorp/terraform-plugin-framework/types" ) -var _ datasource.DataSourceWithConfigure = &dataSourceBlueprintIbaDashboards{} -var _ datasource.DataSourceWithValidateConfig = &dataSourceBlueprintIbaDashboards{} -var _ datasourceWithSetDcBpClientFunc = &dataSourceBlueprintIbaDashboards{} -var _ datasourceWithSetClient = &dataSourceBlueprintIbaDashboards{} +var ( + _ datasource.DataSourceWithConfigure = &dataSourceBlueprintIbaDashboards{} + _ datasource.DataSourceWithValidateConfig = &dataSourceBlueprintIbaDashboards{} + _ datasourceWithSetDcBpClientFunc = &dataSourceBlueprintIbaDashboards{} + _ datasourceWithSetClient = &dataSourceBlueprintIbaDashboards{} +) type dataSourceBlueprintIbaDashboards struct { getBpClientFunc func(context.Context, string) (*apstra.TwoStageL3ClosClient, error)