Skip to content

Commit

Permalink
Merge pull request #485 from Juniper/484-update-configlet-code-to-use…
Browse files Browse the repository at this point in the history
…-current-enum-and-json-marshalerunmarshaler-pattern

**Breaking Change** - Update Configlet structs
  • Loading branch information
chrismarget-j authored Jan 24, 2025
2 parents 05d1915 + 27a77ad commit f264c5c
Show file tree
Hide file tree
Showing 17 changed files with 368 additions and 694 deletions.
72 changes: 10 additions & 62 deletions apstra/api_blueprints.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Juniper Networks, Inc., 2022-2024.
// Copyright (c) Juniper Networks, Inc., 2022-2025.
// All rights reserved.
// SPDX-License-Identifier: Apache-2.0

Expand All @@ -13,6 +13,8 @@ import (
"net/http"
"net/url"
"time"

"github.com/Juniper/apstra-go-sdk/apstra/enum"
)

const (
Expand All @@ -33,16 +35,6 @@ const (
cablingMapMaxWaitSec = 30
)

const (
RefDesignTwoStageL3Clos = RefDesign(iota)
RefDesignFreeform
RefDesignUnknown = "unknown reference design '%s'"

refDesignTwoStageL3Clos = refDesign("two_stage_l3clos")
refDesignFreeform = refDesign("freeform")
refDesignUnknown = refDesign("unknown reference design %d")
)

type BlueprintRequestFabricAddressingPolicy struct {
SpineSuperspineLinks AddressingScheme
SpineLeafLinks AddressingScheme
Expand All @@ -69,42 +61,6 @@ type rawBlueprintRequestFabricAddressingPolicy struct {
FabricL3Mtu *uint16 `json:"fabric_l3_mtu,omitempty"`
}

type (
RefDesign int
refDesign string
)

func (o RefDesign) String() string {
switch o {
case RefDesignTwoStageL3Clos:
return string(refDesignTwoStageL3Clos)
case RefDesignFreeform:
return string(refDesignFreeform)
default:
return fmt.Sprintf(string(refDesignUnknown), o)
}
}

func (o *RefDesign) FromString(s string) error {
i, err := refDesign(s).parse()
if err != nil {
return err
}
*o = i
return nil
}

func (o refDesign) parse() (RefDesign, error) {
switch o {
case refDesignTwoStageL3Clos:
return RefDesignTwoStageL3Clos, nil
case refDesignFreeform:
return RefDesignFreeform, nil
default:
return 0, fmt.Errorf(RefDesignUnknown, o)
}
}

type getBluePrintsResponse struct {
Items []rawBlueprintStatus `json:"items"`
}
Expand All @@ -123,7 +79,7 @@ type Blueprint struct {
client *Client
Id ObjectId
Version int
Design RefDesign
Design enum.RefDesign
LastModifiedAt time.Time
Label string
Relationships map[string]json.RawMessage
Expand All @@ -136,7 +92,7 @@ type Blueprint struct {
type rawBlueprint struct {
Id ObjectId `json:"id"`
Version int `json:"version"`
Design refDesign `json:"design"`
Design enum.RefDesign `json:"design"`
LastModifiedAt time.Time `json:"last_modified_at"`
Label string `json:"label"`
Relationships map[string]json.RawMessage `json:"relationships"`
Expand All @@ -147,15 +103,11 @@ type rawBlueprint struct {
}

func (o *rawBlueprint) polish() (*Blueprint, error) {
design, err := o.Design.parse()
if err != nil {
return nil, err
}
return &Blueprint{
client: nil,
Id: o.Id,
Version: o.Version,
Design: design,
Design: o.Design,
LastModifiedAt: o.LastModifiedAt,
Label: o.Label,
Relationships: o.Relationships,
Expand Down Expand Up @@ -209,7 +161,7 @@ type BlueprintStatus struct {
Id ObjectId `json:"id"`
Label string `json:"label"`
Status string `json:"status"`
Design RefDesign `json:"design"`
Design enum.RefDesign `json:"design"`
HasUncommittedChanges bool `json:"has_uncommitted_changes"`
Version int `json:"version"`
LastModifiedAt time.Time `json:"last_modified_at"`
Expand All @@ -234,7 +186,7 @@ type rawBlueprintStatus struct {
Id ObjectId `json:"id"`
Label string `json:"label"`
Status string `json:"status"`
Design refDesign `json:"design"`
Design enum.RefDesign `json:"design"`
HasUncommittedChanges bool `json:"has_uncommitted_changes"`
Version int `json:"version"`
LastModifiedAt time.Time `json:"last_modified_at"`
Expand Down Expand Up @@ -263,15 +215,11 @@ type rawBlueprintStatus struct {
}

func (o *rawBlueprintStatus) polish() (*BlueprintStatus, error) {
design, err := o.Design.parse()
if err != nil {
return nil, err
}
return &BlueprintStatus{
Id: o.Id,
Label: o.Label,
Status: o.Status,
Design: design,
Design: o.Design,
HasUncommittedChanges: o.HasUncommittedChanges,
Version: o.Version,
LastModifiedAt: o.LastModifiedAt,
Expand All @@ -294,7 +242,7 @@ func (o *rawBlueprintStatus) polish() (*BlueprintStatus, error) {
}

type CreateBlueprintFromTemplateRequest struct {
RefDesign RefDesign
RefDesign enum.RefDesign
Label string
TemplateId ObjectId
FabricSettings *FabricSettings
Expand Down
36 changes: 19 additions & 17 deletions apstra/api_blueprints_integration_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Juniper Networks, Inc., 2022-2024.
// Copyright (c) Juniper Networks, Inc., 2022-2025.
// All rights reserved.
// SPDX-License-Identifier: Apache-2.0

Expand Down Expand Up @@ -77,7 +77,7 @@ func TestCreateDeleteBlueprint(t *testing.T) {
t.Parallel()

req := CreateBlueprintFromTemplateRequest{
RefDesign: RefDesignTwoStageL3Clos,
RefDesign: enum.RefDesignDatacenter,
Label: randString(10, "hex"),
TemplateId: "L2_Virtual_EVPN",
FabricSettings: &FabricSettings{
Expand Down Expand Up @@ -327,14 +327,14 @@ func TestCreateDeleteEvpnBlueprint(t *testing.T) {
testCases := map[string]testCase{
"simple": {
req: CreateBlueprintFromTemplateRequest{
RefDesign: RefDesignTwoStageL3Clos,
RefDesign: enum.RefDesignDatacenter,
Label: randString(5, "hex"),
TemplateId: "L2_Virtual_EVPN",
},
},
"4.1.1_and_later": {
req: CreateBlueprintFromTemplateRequest{
RefDesign: RefDesignTwoStageL3Clos,
RefDesign: enum.RefDesignDatacenter,
Label: randString(5, "hex"),
TemplateId: "L2_Virtual_EVPN",
FabricSettings: &FabricSettings{
Expand All @@ -345,7 +345,7 @@ func TestCreateDeleteEvpnBlueprint(t *testing.T) {
},
"4.2.0_specific_test": {
req: CreateBlueprintFromTemplateRequest{
RefDesign: RefDesignTwoStageL3Clos,
RefDesign: enum.RefDesignDatacenter,
Label: randString(5, "hex"),
TemplateId: "L2_Virtual_EVPN",
FabricSettings: &FabricSettings{
Expand All @@ -357,7 +357,7 @@ func TestCreateDeleteEvpnBlueprint(t *testing.T) {
},
"lots_of_values": {
req: CreateBlueprintFromTemplateRequest{
RefDesign: RefDesignTwoStageL3Clos,
RefDesign: enum.RefDesignDatacenter,
Label: randString(5, "hex"),
TemplateId: "L2_Virtual_EVPN",
FabricSettings: &FabricSettings{
Expand Down Expand Up @@ -392,7 +392,7 @@ func TestCreateDeleteEvpnBlueprint(t *testing.T) {
},
"different_values": {
req: CreateBlueprintFromTemplateRequest{
RefDesign: RefDesignTwoStageL3Clos,
RefDesign: enum.RefDesignDatacenter,
Label: randString(5, "hex"),
TemplateId: "L2_Virtual_EVPN",
FabricSettings: &FabricSettings{
Expand Down Expand Up @@ -541,14 +541,14 @@ func TestCreateDeleteIpFabricBlueprint(t *testing.T) {
testCases := map[string]testCase{
"simple": {
req: CreateBlueprintFromTemplateRequest{
RefDesign: RefDesignTwoStageL3Clos,
RefDesign: enum.RefDesignDatacenter,
Label: randString(5, "hex"),
TemplateId: "L2_Virtual",
},
},
"4.1.1_and_later": {
req: CreateBlueprintFromTemplateRequest{
RefDesign: RefDesignTwoStageL3Clos,
RefDesign: enum.RefDesignDatacenter,
Label: randString(5, "hex"),
TemplateId: "L2_Virtual",
FabricSettings: &FabricSettings{
Expand All @@ -559,7 +559,7 @@ func TestCreateDeleteIpFabricBlueprint(t *testing.T) {
},
"4.2.0_specific_test": {
req: CreateBlueprintFromTemplateRequest{
RefDesign: RefDesignTwoStageL3Clos,
RefDesign: enum.RefDesignDatacenter,
Label: randString(5, "hex"),
TemplateId: "L2_Virtual",
FabricSettings: &FabricSettings{
Expand All @@ -571,7 +571,7 @@ func TestCreateDeleteIpFabricBlueprint(t *testing.T) {
},
"lots_of_values": {
req: CreateBlueprintFromTemplateRequest{
RefDesign: RefDesignTwoStageL3Clos,
RefDesign: enum.RefDesignDatacenter,
Label: randString(5, "hex"),
TemplateId: "L2_Virtual",
FabricSettings: &FabricSettings{
Expand Down Expand Up @@ -606,7 +606,7 @@ func TestCreateDeleteIpFabricBlueprint(t *testing.T) {
},
"different_values": {
req: CreateBlueprintFromTemplateRequest{
RefDesign: RefDesignTwoStageL3Clos,
RefDesign: enum.RefDesignDatacenter,
Label: randString(5, "hex"),
TemplateId: "L2_Virtual",
FabricSettings: &FabricSettings{
Expand Down Expand Up @@ -747,10 +747,12 @@ func TestCreateDeleteBlueprintWithRoutingLimits(t *testing.T) {
clients, err := getTestClients(ctx, t)
require.NoError(t, err)

blueprintRequest := CreateBlueprintFromTemplateRequest{
RefDesign: RefDesignTwoStageL3Clos,
Label: randString(5, "hex"),
TemplateId: "L2_Virtual",
blueprintRequest := func() CreateBlueprintFromTemplateRequest {
return CreateBlueprintFromTemplateRequest{
RefDesign: enum.RefDesignDatacenter,
Label: randString(5, "hex"),
TemplateId: "L2_Virtual",
}
}

type testCase struct {
Expand Down Expand Up @@ -795,7 +797,7 @@ func TestCreateDeleteBlueprintWithRoutingLimits(t *testing.T) {
tCase := tCase

t.Run(tCase.name, func(t *testing.T) {
bpr := blueprintRequest
bpr := blueprintRequest()
bpr.FabricSettings = &tCase.fabricSettings
t.Logf("testing CreateBlueprintFromTemplate() against %s %s (%s)", client.clientType, clientName, client.client.ApiVersion())
id, err := client.client.CreateBlueprintFromTemplate(ctx, &bpr)
Expand Down
Loading

0 comments on commit f264c5c

Please sign in to comment.