Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rosetta update #12

Merged
merged 6 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 5 additions & 17 deletions apstra/data_source_blueprints.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package tfapstra

import (
"github.com/Juniper/apstra-go-sdk/apstra"
"context"
"fmt"
"github.com/Juniper/apstra-go-sdk/apstra"
"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
Expand All @@ -12,11 +12,7 @@ import (
_ "github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
)

const (
minimumFreeFormVersion = "4.1.1"
twoStageL3ClosRefDesignUiName = "datacenter"
"terraform-provider-apstra/apstra/utils"
)

var _ datasource.DataSourceWithConfigure = &dataSourceBlueprints{}
Expand Down Expand Up @@ -50,7 +46,7 @@ func (o *dataSourceBlueprints) Schema(_ context.Context, _ datasource.SchemaRequ
MarkdownDescription: "Optional filter to select only Blueprints matching the specified Reference Design.",
Optional: true,
Validators: []validator.String{stringvalidator.OneOf(
twoStageL3ClosRefDesignUiName,
utils.StringersToFriendlyString(apstra.RefDesignDatacenter),
apstra.RefDesignFreeform.String(),
)},
},
Expand Down Expand Up @@ -104,22 +100,14 @@ func (o *dataSourceBlueprints) Read(ctx context.Context, req datasource.ReadRequ
return
}
} else {
var refDesign string
// substitute UI name for API name
switch config.RefDesign.ValueString() {
case twoStageL3ClosRefDesignUiName:
refDesign = apstra.RefDesignDatacenter.String()
default:
refDesign = config.RefDesign.ValueString()
}

bpStatuses, err := o.client.GetAllBlueprintStatus(ctx)
if err != nil {
resp.Diagnostics.AddError("error retrieving Blueprint statuses", err.Error())
return
}

for _, bpStatus := range bpStatuses {
if bpStatus.Design.String() == refDesign {
if utils.StringersToFriendlyString(bpStatus.Design) == config.RefDesign.ValueString() {
ids = append(ids, bpStatus.Id)
}
}
Expand Down
80 changes: 51 additions & 29 deletions apstra/utils/rosetta.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import (
)

const (
JunOSTopLevelHierarchical = "top_level_hierarchical"
JunOSTopLevelSetDelete = "top_level_set_delete"
JunOSInterfaceLevelHierarchical = "interface_level_hierarchical"
JunOSInterfaceLevelSet = "interface_level_set"
JunOSInterfaceLevelDelete = "interface_level_delete"
junOSTopLevelHierarchical = "top_level_hierarchical"
junOSTopLevelSetDelete = "top_level_set_delete"
junOSInterfaceLevelHierarchical = "interface_level_hierarchical"
junOSInterfaceLevelSet = "interface_level_set"
junOSInterfaceLevelDelete = "interface_level_delete"

AsnAllocationSingle = "single"
AsnAllocationUnique = "unique"
asnAllocationUnique = "unique"

OverlayControlProtocolEvpn = "evpn"
OverlayControlProtocolStatic = "static"
overlayControlProtocolStatic = "static"

refDesignDataCenter = "datacenter"
)

type StringerWithFromString interface {
Expand All @@ -42,6 +42,8 @@ func StringersToFriendlyString(in ...fmt.Stringer) string {
return configletSectionToFriendlyString(in[0].(apstra.ConfigletSection), in[1:]...)
case apstra.OverlayControlProtocol:
return overlayControlProtocolToFriendlyString(in[0].(apstra.OverlayControlProtocol))
case apstra.RefDesign:
return refDesignToFriendlyString(in[0].(apstra.RefDesign))
}

return in[0].String()
Expand All @@ -66,17 +68,17 @@ func ApiStringerFromFriendlyString(target StringerWithFromString, in ...string)
return configletSectionFromFriendlyString(target.(*apstra.ConfigletSection), in...)
case *apstra.OverlayControlProtocol:
return overlayControlProtocolFromFriendlyString(target.(*apstra.OverlayControlProtocol), in...)
case *apstra.RefDesign:
return refDesignFromFriendlyString(target.(*apstra.RefDesign), in...)
}

return target.FromString(in[0])
}

func asnAllocationSchemeToFriendlyString(in apstra.AsnAllocationScheme) string {
switch in {
case apstra.AsnAllocationSchemeSingle:
return AsnAllocationSingle
case apstra.AsnAllocationSchemeDistinct:
return AsnAllocationUnique
return asnAllocationUnique
}

return in.String()
Expand All @@ -96,15 +98,15 @@ func configletSectionToFriendlyString(in apstra.ConfigletSection, additionalInfo
case apstra.PlatformOSJunos:
switch in {
case apstra.ConfigletSectionSystem:
return JunOSTopLevelHierarchical
return junOSTopLevelHierarchical
case apstra.ConfigletSectionSetBasedSystem:
return JunOSTopLevelSetDelete
return junOSTopLevelSetDelete
case apstra.ConfigletSectionSetBasedInterface:
return JunOSInterfaceLevelSet
return junOSInterfaceLevelSet
case apstra.ConfigletSectionDeleteBasedInterface:
return JunOSInterfaceLevelDelete
return junOSInterfaceLevelDelete
case apstra.ConfigletSectionInterface:
return JunOSInterfaceLevelHierarchical
return junOSInterfaceLevelHierarchical
}
}

Expand All @@ -113,10 +115,17 @@ func configletSectionToFriendlyString(in apstra.ConfigletSection, additionalInfo

func overlayControlProtocolToFriendlyString(in apstra.OverlayControlProtocol) string {
switch in {
case apstra.OverlayControlProtocolEvpn:
return OverlayControlProtocolEvpn
case apstra.OverlayControlProtocolNone:
return OverlayControlProtocolStatic
return overlayControlProtocolStatic
}

return in.String()
}

func refDesignToFriendlyString(in apstra.RefDesign) string {
switch in {
case apstra.RefDesignDatacenter:
return refDesignDataCenter
}

return in.String()
Expand All @@ -128,7 +137,7 @@ func asnAllocationSchemeFromFriendlyString(target *apstra.AsnAllocationScheme, i
}

switch in[0] {
case AsnAllocationUnique:
case asnAllocationUnique:
*target = apstra.AsnAllocationSchemeDistinct
default:
return target.FromString(in[0])
Expand All @@ -153,15 +162,15 @@ func configletSectionFromFriendlyString(target *apstra.ConfigletSection, in ...s
}

switch section {
case JunOSTopLevelHierarchical:
case junOSTopLevelHierarchical:
*target = apstra.ConfigletSectionSystem
case JunOSInterfaceLevelHierarchical:
case junOSInterfaceLevelHierarchical:
*target = apstra.ConfigletSectionInterface
case JunOSTopLevelSetDelete:
case junOSTopLevelSetDelete:
*target = apstra.ConfigletSectionSetBasedSystem
case JunOSInterfaceLevelDelete:
case junOSInterfaceLevelDelete:
*target = apstra.ConfigletSectionDeleteBasedInterface
case JunOSInterfaceLevelSet:
case junOSInterfaceLevelSet:
*target = apstra.ConfigletSectionSetBasedInterface
default:
return target.FromString(section)
Expand All @@ -176,13 +185,26 @@ func overlayControlProtocolFromFriendlyString(target *apstra.OverlayControlProto
}

switch in[0] {
case OverlayControlProtocolEvpn:
*target = apstra.OverlayControlProtocolEvpn
case OverlayControlProtocolStatic:
case overlayControlProtocolStatic:
*target = apstra.OverlayControlProtocolNone
default:
return target.FromString(in[0])
}

return nil
}

func refDesignFromFriendlyString(target *apstra.RefDesign, in ...string) error {
if len(in) == 0 {
return target.FromString("")
}

switch in[0] {
case refDesignDataCenter:
*target = apstra.RefDesignDatacenter
default:
return target.FromString(in[0])
}

return nil
}
10 changes: 10 additions & 0 deletions apstra/utils/rosetta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ func TestRosetta(t *testing.T) {

{string: "static", stringers: []fmt.Stringer{apstra.OverlayControlProtocolNone}},
{string: "evpn", stringers: []fmt.Stringer{apstra.OverlayControlProtocolEvpn}},

{string: "datacenter", stringers: []fmt.Stringer{apstra.RefDesignDatacenter}},
{string: "freeform", stringers: []fmt.Stringer{apstra.RefDesignFreeform}},
}

for i, tc := range testCases {
Expand All @@ -48,6 +51,13 @@ func TestRosetta(t *testing.T) {
case apstra.OverlayControlProtocol:
x := apstra.OverlayControlProtocol(-1)
target = &x
case apstra.RefDesign:
x := apstra.RefDesign(-1)
target = &x
}

if target == nil {
t.Fatalf("missing case above - target is nil")
}

// stringsWithContext is the []string sent to the rosetta function to populate target
Expand Down