From 0646d2188da69546bf3d689783230e68d7e1d41a Mon Sep 17 00:00:00 2001 From: Chris Marget Date: Wed, 25 Sep 2024 12:28:15 -0400 Subject: [PATCH] add version checks for IBA dashboard support in 5.x --- apstra/api_versions/versions.go | 4 +- apstra/compatibility/constraints.go | 10 +++++ apstra/data_source_blueprint_iba_dashboard.go | 39 ++++++++++++++++++- ...ata_source_blueprint_iba_dashboard_test.go | 14 +++++-- go.mod | 1 + go.sum | 2 + 6 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 apstra/compatibility/constraints.go diff --git a/apstra/api_versions/versions.go b/apstra/api_versions/versions.go index 13266ead..fcf85adc 100644 --- a/apstra/api_versions/versions.go +++ b/apstra/api_versions/versions.go @@ -11,6 +11,8 @@ const ( Apstra421 = "4.2.1" Apstra4211 = "4.2.1.1" Apstra422 = "4.2.2" + Apstra500 = "5.0.0" - Le420 = "<=" + Apstra420 + Le420 = "<=" + Apstra420 + LtApstra500 = "<" + Apstra500 ) diff --git a/apstra/compatibility/constraints.go b/apstra/compatibility/constraints.go new file mode 100644 index 00000000..ea72a6ab --- /dev/null +++ b/apstra/compatibility/constraints.go @@ -0,0 +1,10 @@ +package compatibility + +import ( + apiversions "github.com/Juniper/terraform-provider-apstra/apstra/api_versions" + "github.com/chrismarget-j/version-constraints" +) + +var ( + BpIbaDashboardOk = versionconstraints.New(apiversions.LtApstra500) +) diff --git a/apstra/data_source_blueprint_iba_dashboard.go b/apstra/data_source_blueprint_iba_dashboard.go index f1c25470..6b596df4 100644 --- a/apstra/data_source_blueprint_iba_dashboard.go +++ b/apstra/data_source_blueprint_iba_dashboard.go @@ -4,17 +4,24 @@ 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/iba" "github.com/Juniper/terraform-provider-apstra/apstra/utils" + "github.com/hashicorp/go-version" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/path" ) -var _ datasource.DataSourceWithConfigure = &dataSourceBlueprintIbaDashboard{} -var _ datasourceWithSetDcBpClientFunc = &dataSourceBlueprintIbaDashboard{} +var ( + _ datasource.DataSourceWithConfigure = &dataSourceBlueprintIbaDashboard{} + _ datasource.DataSourceWithValidateConfig = &dataSourceBlueprintIbaDashboard{} + _ datasourceWithSetDcBpClientFunc = &dataSourceBlueprintIbaDashboard{} + _ datasourceWithSetClient = &dataSourceBlueprintIbaDashboard{} // needed for API version compatibility check only +) type dataSourceBlueprintIbaDashboard struct { + client *apstra.Client getBpClientFunc func(context.Context, string) (*apstra.TwoStageL3ClosClient, error) } @@ -35,6 +42,29 @@ 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 + 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 *dataSourceBlueprintIbaDashboard) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { var config iba.Dashboard resp.Diagnostics.Append(req.Config.Get(ctx, &config)...) @@ -96,6 +126,11 @@ func (o *dataSourceBlueprintIbaDashboard) Read(ctx context.Context, req datasour resp.Diagnostics.Append(resp.State.Set(ctx, &config)...) } +// 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_dashboard_test.go b/apstra/data_source_blueprint_iba_dashboard_test.go index 129ff639..3fe12f6a 100644 --- a/apstra/data_source_blueprint_iba_dashboard_test.go +++ b/apstra/data_source_blueprint_iba_dashboard_test.go @@ -4,7 +4,9 @@ import ( "context" "fmt" "github.com/Juniper/apstra-go-sdk/apstra" + "github.com/Juniper/terraform-provider-apstra/apstra/compatibility" testutils "github.com/Juniper/terraform-provider-apstra/apstra/test_utils" + "github.com/hashicorp/go-version" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/stretchr/testify/require" "testing" @@ -29,6 +31,12 @@ const ( func TestAccDataSourceIbaDashboard(t *testing.T) { ctx := context.Background() + client := testutils.GetTestClient(t, ctx) + clientVersion := version.Must(version.NewVersion(client.ApiVersion())) + if !compatibility.BpIbaDashboardOk.Check(clientVersion) { + t.Skipf("skipping due to version constraint %s", compatibility.BpIbaDashboardOk) + } + bpClient := testutils.MakeOrFindBlueprint(t, ctx, "BPA", testutils.BlueprintA) // Set up Widgets @@ -50,8 +58,7 @@ func TestAccDataSourceIbaDashboard(t *testing.T) { Steps: []resource.TestStep{ // Read by ID { - Config: insecureProviderConfigHCL + fmt.Sprintf(dataSourceBlueprintIbaDashboardTemplateByIdHCL, - bpClient.Id().String(), id.String()), + Config: insecureProviderConfigHCL + fmt.Sprintf(dataSourceBlueprintIbaDashboardTemplateByIdHCL, bpClient.Id().String(), id.String()), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr("data.apstra_blueprint_iba_dashboard.test", "id", id.String()), resource.TestCheckResourceAttr("data.apstra_blueprint_iba_dashboard.test", "name", dashboardData.Label), @@ -61,8 +68,7 @@ func TestAccDataSourceIbaDashboard(t *testing.T) { }, // Read by Name { - Config: insecureProviderConfigHCL + fmt.Sprintf(dataSourceBlueprintIbaDashboardTemplateByNameHCL, - bpClient.Id().String(), dashboardData.Label), + Config: insecureProviderConfigHCL + fmt.Sprintf(dataSourceBlueprintIbaDashboardTemplateByNameHCL, bpClient.Id().String(), dashboardData.Label), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr("data.apstra_blueprint_iba_dashboard.test", "id", id.String()), resource.TestCheckResourceAttr("data.apstra_blueprint_iba_dashboard.test", "name", dashboardData.Label), diff --git a/go.mod b/go.mod index f29070b9..ac455c3f 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/IBM/netaddr v1.5.0 github.com/Juniper/apstra-go-sdk v0.0.0-20240920145043-b30ce0dd776c github.com/chrismarget-j/go-licenses v0.0.0-20240224210557-f22f3e06d3d4 + github.com/chrismarget-j/version-constraints v0.0.0-20240925155624-26771a0a6820 github.com/google/go-cmp v0.6.0 github.com/hashicorp/go-version v1.7.0 github.com/hashicorp/hcl/v2 v2.20.1 diff --git a/go.sum b/go.sum index d2f55567..160fa7c6 100644 --- a/go.sum +++ b/go.sum @@ -63,6 +63,8 @@ github.com/bufbuild/protocompile v0.4.0/go.mod h1:3v93+mbWn/v3xzN+31nwkJfrEpAUwp github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/chrismarget-j/go-licenses v0.0.0-20240224210557-f22f3e06d3d4 h1:nFogSDBo0cmCwO2JX2gl+2onPk4Fw63VreG+HX9U5n0= github.com/chrismarget-j/go-licenses v0.0.0-20240224210557-f22f3e06d3d4/go.mod h1:RxJksUf3IJIwpKhrrGOUOVD7qQQj1tlJtXYhmsyLVlE= +github.com/chrismarget-j/version-constraints v0.0.0-20240925155624-26771a0a6820 h1:ca2TIBMAj9Czul/4WUN0sLWqn1Wmicm81Ke0m6HR8+s= +github.com/chrismarget-j/version-constraints v0.0.0-20240925155624-26771a0a6820/go.mod h1:lD9yQKzccrnkg2Xx/9kKzrlW9OWP61bRRLjec0l4Wok= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cloudflare/circl v1.3.9 h1:QFrlgFYf2Qpi8bSpVPK1HBvWpx16v/1TZivyo7pGuBE= github.com/cloudflare/circl v1.3.9/go.mod h1:PDRU+oXvdD7KCtgKxW95M5Z8BpSCJXQORiZFnBQS5QU=