Skip to content

Commit

Permalink
add version checks for IBA dashboard support in 5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismarget-j committed Sep 25, 2024
1 parent b1697cc commit 0646d21
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 7 deletions.
4 changes: 3 additions & 1 deletion apstra/api_versions/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
10 changes: 10 additions & 0 deletions apstra/compatibility/constraints.go
Original file line number Diff line number Diff line change
@@ -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)
)
39 changes: 37 additions & 2 deletions apstra/data_source_blueprint_iba_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)...)
Expand Down Expand Up @@ -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
}
14 changes: 10 additions & 4 deletions apstra/data_source_blueprint_iba_dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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),
Expand All @@ -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),
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 0646d21

Please sign in to comment.