Skip to content

Commit

Permalink
add version checks for IBA Widget compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismarget-j committed Sep 26, 2024
1 parent 3730466 commit db57dc4
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 16 deletions.
1 change: 1 addition & 0 deletions apstra/compatibility/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ import (
var (
BpIbaDashboardOk = versionconstraints.New(apiversions.LtApstra500)
BpIbaProbeOk = versionconstraints.New(apiversions.LtApstra500)
BpIbaWidgetOk = versionconstraints.New(apiversions.LtApstra500)
)
36 changes: 33 additions & 3 deletions apstra/data_source_blueprint_iba_widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,27 @@ 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/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 = &dataSourceBlueprintIbaWidget{}
var _ datasourceWithSetDcBpClientFunc = &dataSourceBlueprintIbaWidget{}
var (
_ datasource.DataSourceWithConfigure = &dataSourceBlueprintIbaWidget{}
_ datasource.DataSourceWithValidateConfig = &dataSourceBlueprintIbaWidget{}
_ datasourceWithSetDcBpClientFunc = &dataSourceBlueprintIbaWidget{}
_ datasourceWithSetClient = &dataSourceBlueprintIbaWidget{}
)

type dataSourceBlueprintIbaWidget struct {
getBpClientFunc func(context.Context, string) (*apstra.TwoStageL3ClosClient, error)
client *apstra.Client
}

func (o *dataSourceBlueprintIbaWidget) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand All @@ -30,11 +38,28 @@ func (o *dataSourceBlueprintIbaWidget) Schema(_ context.Context, _ datasource.Sc
resp.Schema = schema.Schema{
MarkdownDescription: docCategoryRefDesignAny + "This data source provides details of a specific IBA Widget in a Blueprint." +
"\n\n" +
"At least one optional attribute is required.",
"At least one optional attribute is required.\n\n" +
"*Note: Compatible only with Apstra " + compatibility.BpIbaWidgetOk.String() + "*",

Attributes: iba.Widget{}.DataSourceAttributes(),
}
}

func (o *dataSourceBlueprintIbaWidget) 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
}

// only supported with Apstra 4.x
if !compatibility.BpIbaWidgetOk.Check(version.Must(version.NewVersion(o.client.ApiVersion()))) {
resp.Diagnostics.AddError(
"Incompatible API version",
"This data source is compatible only with Apstra "+compatibility.BpIbaWidgetOk.String(),
)
}
}

func (o *dataSourceBlueprintIbaWidget) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var config iba.Widget
resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)
Expand Down Expand Up @@ -99,3 +124,8 @@ func (o *dataSourceBlueprintIbaWidget) Read(ctx context.Context, req datasource.
func (o *dataSourceBlueprintIbaWidget) setBpClientFunc(f func(context.Context, string) (*apstra.TwoStageL3ClosClient, error)) {
o.getBpClientFunc = f
}

// setClient is used for API version compatibility check only
func (o *dataSourceBlueprintIbaWidget) setClient(client *apstra.Client) {
o.client = client
}
11 changes: 10 additions & 1 deletion apstra/data_source_blueprint_iba_widget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package tfapstra
import (
"context"
"fmt"
"testing"

"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"
"testing"
)

const (
Expand All @@ -27,6 +30,12 @@ const (
func TestAccDataSourceIbaWidget(t *testing.T) {
ctx := context.Background()

client := testutils.GetTestClient(t, ctx)
clientVersion := version.Must(version.NewVersion(client.ApiVersion()))
if !compatibility.BpIbaWidgetOk.Check(clientVersion) {
t.Skipf("skipping due to version constraint %s", compatibility.BpIbaWidgetOk)
}

bpClient := testutils.MakeOrFindBlueprint(t, ctx, "BPA", testutils.BlueprintA)

// Set up Widgets
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ 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"
"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"
Expand All @@ -13,11 +16,16 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types"
)

var _ datasource.DataSourceWithConfigure = &dataSourceBlueprintIbaWidgets{}
var _ datasourceWithSetDcBpClientFunc = &dataSourceBlueprintIbaWidgets{}
var (
_ datasource.DataSourceWithConfigure = &dataSourceBlueprintIbaWidgets{}
_ datasource.DataSourceWithValidateConfig = &dataSourceBlueprintIbaWidgets{}
_ datasourceWithSetDcBpClientFunc = &dataSourceBlueprintIbaWidgets{}
_ datasourceWithSetClient = &dataSourceBlueprintIbaWidgets{}
)

type dataSourceBlueprintIbaWidgets struct {
getBpClientFunc func(context.Context, string) (*apstra.TwoStageL3ClosClient, error)
client *apstra.Client
}

func (o *dataSourceBlueprintIbaWidgets) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand All @@ -30,7 +38,9 @@ func (o *dataSourceBlueprintIbaWidgets) Configure(ctx context.Context, req datas

func (o *dataSourceBlueprintIbaWidgets) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: docCategoryRefDesignAny + "This data source returns the IDs of the IBA Widgets in a Blueprint.",
MarkdownDescription: docCategoryRefDesignAny + "This data source returns the IDs of the IBA Widgets in a Blueprint." +
"*Note: Compatible only with Apstra " + compatibility.BpIbaWidgetOk.String() + "*",

Attributes: map[string]schema.Attribute{
"blueprint_id": schema.StringAttribute{
MarkdownDescription: "Apstra Blueprint ID. " +
Expand All @@ -47,6 +57,21 @@ func (o *dataSourceBlueprintIbaWidgets) Schema(_ context.Context, _ datasource.S
}
}

func (o *dataSourceBlueprintIbaWidgets) 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
}

// only supported with Apstra 4.x
if !compatibility.BpIbaWidgetOk.Check(version.Must(version.NewVersion(o.client.ApiVersion()))) {
resp.Diagnostics.AddError(
"Incompatible API version",
"This data source is compatible only with Apstra "+compatibility.BpIbaWidgetOk.String(),
)
}
}

func (o *dataSourceBlueprintIbaWidgets) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var config struct {
BlueprintId types.String `tfsdk:"blueprint_id"`
Expand Down Expand Up @@ -98,3 +123,8 @@ func (o *dataSourceBlueprintIbaWidgets) Read(ctx context.Context, req datasource
func (o *dataSourceBlueprintIbaWidgets) setBpClientFunc(f func(context.Context, string) (*apstra.TwoStageL3ClosClient, error)) {
o.getBpClientFunc = f
}

// setClient is used for API version compatibility check only
func (o *dataSourceBlueprintIbaWidgets) setClient(client *apstra.Client) {
o.client = client
}
2 changes: 1 addition & 1 deletion apstra/resource_blueprint_iba_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (o *resourceBlueprintIbaDashboard) ValidateConfig(_ context.Context, _ reso
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(),
"This resource is compatible only with Apstra "+compatibility.BpIbaDashboardOk.String(),
)
}
}
Expand Down
40 changes: 35 additions & 5 deletions apstra/resource_blueprint_iba_widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,27 @@ 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/iba"
"github.com/Juniper/terraform-provider-apstra/apstra/utils"
"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)

var _ resource.ResourceWithConfigure = &resourceBlueprintIbaWidget{}
var _ resourceWithSetDcBpClientFunc = &resourceBlueprintIbaWidget{}
var (
_ resource.ResourceWithConfigure = &resourceBlueprintIbaWidget{}
_ resource.ResourceWithValidateConfig = &resourceBlueprintIbaWidget{}
_ resourceWithSetDcBpClientFunc = &resourceBlueprintIbaWidget{}
_ resourceWithSetClient = &resourceBlueprintIbaWidget{}
)

type resourceBlueprintIbaWidget struct {
getBpClientFunc func(context.Context, string) (*apstra.TwoStageL3ClosClient, error)
client *apstra.Client
}

func (o *resourceBlueprintIbaWidget) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
Expand All @@ -28,8 +36,25 @@ func (o *resourceBlueprintIbaWidget) Configure(ctx context.Context, req resource

func (o *resourceBlueprintIbaWidget) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: docCategoryRefDesignAny + "This resource creates an IBA Widget.",
Attributes: iba.Widget{}.ResourceAttributes(),
MarkdownDescription: docCategoryRefDesignAny + "This resource creates an IBA Widget.\n\n" +
"*Note: Compatible only with Apstra " + compatibility.BpIbaWidgetOk.String() + "*",

Attributes: iba.Widget{}.ResourceAttributes(),
}
}

func (o *resourceBlueprintIbaWidget) ValidateConfig(_ context.Context, _ resource.ValidateConfigRequest, resp *resource.ValidateConfigResponse) {
// cannot proceed to api version validation if the provider has not been configured
if o.client == nil {
return
}

// only supported with Apstra 4.x
if !compatibility.BpIbaWidgetOk.Check(version.Must(version.NewVersion(o.client.ApiVersion()))) {
resp.Diagnostics.AddError(
"Incompatible API version",
"This resource is compatible only with Apstra "+compatibility.BpIbaWidgetOk.String(),
)
}
}

Expand Down Expand Up @@ -93,7 +118,7 @@ func (o *resourceBlueprintIbaWidget) Read(ctx context.Context, req resource.Read
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("Failed to Read IBA Dashboard", err.Error())
resp.Diagnostics.AddError("Failed to Read IBA Widget", err.Error())
return
}

Expand Down Expand Up @@ -171,3 +196,8 @@ func (o *resourceBlueprintIbaWidget) Delete(ctx context.Context, req resource.De
func (o *resourceBlueprintIbaWidget) setBpClientFunc(f func(context.Context, string) (*apstra.TwoStageL3ClosClient, error)) {
o.getBpClientFunc = f
}

// setClient is used for API version compatibility check only
func (o *resourceBlueprintIbaWidget) setClient(client *apstra.Client) {
o.client = client
}
12 changes: 11 additions & 1 deletion apstra/resource_blueprint_iba_widget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package tfapstra
import (
"context"
"fmt"
"testing"

"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"
"testing"
)

const (
Expand Down Expand Up @@ -36,6 +39,13 @@ resource "apstra_blueprint_iba_widget" "w_device_health_high_cpu" {

func TestAccResourceWidget(t *testing.T) {
ctx := context.Background()

client := testutils.GetTestClient(t, ctx)
clientVersion := version.Must(version.NewVersion(client.ApiVersion()))
if !compatibility.BpIbaWidgetOk.Check(clientVersion) {
t.Skipf("skipping due to version constraint %s", compatibility.BpIbaWidgetOk)
}

bpClient := testutils.MakeOrFindBlueprint(t, ctx, "widget", testutils.BlueprintA)

n1 := "Widget1"
Expand Down
3 changes: 3 additions & 0 deletions docs/data-sources/blueprint_iba_widget.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ subcategory: "Reference Design: Shared"
description: |-
This data source provides details of a specific IBA Widget in a Blueprint.
At least one optional attribute is required.
Note: Compatible only with Apstra <5.0.0
---

# apstra_blueprint_iba_widget (Data Source)
Expand All @@ -12,6 +13,8 @@ This data source provides details of a specific IBA Widget in a Blueprint.

At least one optional attribute is required.

*Note: Compatible only with Apstra <5.0.0*


## Example Usage

Expand Down
4 changes: 2 additions & 2 deletions docs/data-sources/blueprint_iba_widgets.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
page_title: "apstra_blueprint_iba_widgets Data Source - terraform-provider-apstra"
subcategory: "Reference Design: Shared"
description: |-
This data source returns the IDs of the IBA Widgets in a Blueprint.
This data source returns the IDs of the IBA Widgets in a Blueprint.Note: Compatible only with Apstra <5.0.0
---

# apstra_blueprint_iba_widgets (Data Source)

This data source returns the IDs of the IBA Widgets in a Blueprint.
This data source returns the IDs of the IBA Widgets in a Blueprint.*Note: Compatible only with Apstra <5.0.0*


## Example Usage
Expand Down
3 changes: 3 additions & 0 deletions docs/resources/blueprint_iba_widget.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ page_title: "apstra_blueprint_iba_widget Resource - terraform-provider-apstra"
subcategory: "Reference Design: Shared"
description: |-
This resource creates an IBA Widget.
Note: Compatible only with Apstra <5.0.0
---

# apstra_blueprint_iba_widget (Resource)

This resource creates an IBA Widget.

*Note: Compatible only with Apstra <5.0.0*


## Example Usage

Expand Down

0 comments on commit db57dc4

Please sign in to comment.