Skip to content

Commit

Permalink
Merge pull request #428 from Juniper/426-eliminate-bad-idea-logicalde…
Browse files Browse the repository at this point in the history
…viceportroleflags

Replace `LogicalDeviecPortRoleFlags` with `LogicalDevicePortRoles`
  • Loading branch information
chrismarget-j authored Oct 7, 2024
2 parents a48e304 + bf98283 commit 5e6f8f2
Show file tree
Hide file tree
Showing 9 changed files with 385 additions and 353 deletions.
10 changes: 6 additions & 4 deletions apstra/api_design_interface_maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ type InterfaceMapInterfaceSetting struct {

type InterfaceMapInterface struct {
Name string
Roles LogicalDevicePortRoleFlags
Roles LogicalDevicePortRoles
Mapping InterfaceMapMapping
ActiveState InterfaceStateActive
Position int
Expand All @@ -188,7 +188,7 @@ type InterfaceMapInterface struct {
func (o *InterfaceMapInterface) raw() *rawInterfaceMapInterface {
return &rawInterfaceMapInterface{
Name: o.Name,
Roles: o.Roles.raw(),
Roles: o.Roles.Strings(),
Mapping: *o.Mapping.raw(),
State: o.ActiveState.raw(),
Setting: o.Setting,
Expand All @@ -199,7 +199,7 @@ func (o *InterfaceMapInterface) raw() *rawInterfaceMapInterface {

type rawInterfaceMapInterface struct {
Name string `json:"name"`
Roles logicalDevicePortRoles `json:"roles"`
Roles []string `json:"roles"`
Mapping rawInterfaceMapping `json:"mapping"`
State rawInterfaceState `json:"state"`
Setting InterfaceMapInterfaceSetting `json:"setting"`
Expand All @@ -208,10 +208,12 @@ type rawInterfaceMapInterface struct {
}

func (o *rawInterfaceMapInterface) polish() (*InterfaceMapInterface, error) {
roles, err := o.Roles.parse()
var roles LogicalDevicePortRoles
err := roles.FromStrings(o.Roles)
if err != nil {
return nil, err
}

state, err := o.State.polish()
if err != nil {
return nil, err
Expand Down
166 changes: 166 additions & 0 deletions apstra/api_design_interface_maps_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
// Copyright (c) Juniper Networks, Inc., 2022-2024.
// All rights reserved.
// SPDX-License-Identifier: Apache-2.0

// Copyright (c) Juniper Networks, Inc., 2024-2024.
// All rights reserved.
// SPDX-License-Identifier: Apache-2.0

//go:build integration

package apstra

import (
"context"
"log"
"math/rand"
"testing"

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

func TestInterfaceSettingParam(t *testing.T) {
expected := `{\"global\":{\"breakout\":false,\"fpc\":0,\"pic\":0,\"port\":0,\"speed\":\"100g\"},\"interface\":{\"speed\":\"\"}}`
test := InterfaceSettingParam{
Global: struct {
Breakout bool `json:"breakout"`
Fpc int `json:"fpc"`
Pic int `json:"pic"`
Port int `json:"port"`
Speed string `json:"speed"`
}{
Breakout: false,
Fpc: 0,
Pic: 0,
Port: 0,
Speed: "100g",
},
Interface: struct {
Speed string `json:"speed"`
}{},
}
result := test.String()
if result != expected {
t.Fatalf("expected '%s', got '%s'", expected, result)
}
}

func TestListGetAllInterfaceMaps(t *testing.T) {
clients, err := getTestClients(context.Background(), t)
if err != nil {
t.Fatal(err)
}
for clientName, client := range clients {
log.Printf("testing listAllInterfaceMapIds() against %s %s (%s)", client.clientType, clientName, client.client.ApiVersion())
iMapIds, err := client.client.listAllInterfaceMapIds(context.TODO())
if err != nil {
t.Fatal(err)
}

if len(iMapIds) == 0 {
t.Fatal("we should have gotten some interface maps here")
}

log.Println("all interface maps IDs: ", iMapIds)

log.Printf("testing getInterfaceMap() against %s %s (%s)", client.clientType, clientName, client.client.ApiVersion())
iMap, err := client.client.getInterfaceMap(context.TODO(), iMapIds[rand.Intn(len(iMapIds))])
if err != nil {
t.Fatal(err)
}
log.Println("random interface map: ", iMap)
}
}

func TestCreateInterfaceMap(t *testing.T) {
clients, err := getTestClients(context.Background(), t)
if err != nil {
t.Fatal(err)
}

for clientName, client := range clients {

ldId := ObjectId("AOS-1x1-1")
dpId := ObjectId("Generic_Server_1RU_1x1G")
label := "label-" + randString(10, "hex")

newMapInfo := InterfaceMapData{
LogicalDeviceId: ldId,
DeviceProfileId: dpId,
Label: label,
Interfaces: []InterfaceMapInterface{
{
Name: "eth0",
Roles: LogicalDevicePortRoles{enum.PortRoleLeaf, enum.PortRoleAccess},
Mapping: InterfaceMapMapping{
DPPortId: 1,
DPTransformId: 1,
DPInterfaceId: 1,
LDPanel: 1,
LDPort: 1,
},
ActiveState: true,
Position: 1,
Speed: "1G",
Setting: InterfaceMapInterfaceSetting{Param: ""},
},
},
}

log.Printf("testing createInterfaceMap() against %s %s (%s)", client.clientType, clientName, client.client.ApiVersion())
mapId, err := client.client.createInterfaceMap(context.TODO(), &newMapInfo)
require.NoError(t, err)

log.Printf("testing getInterfaceMap() against %s %s (%s)", client.clientType, clientName, client.client.ApiVersion())
asCreated, err := client.client.GetInterfaceMap(context.TODO(), mapId)
require.NoError(t, err)
require.NotNil(t, asCreated)
require.Equal(t, mapId, asCreated.Id)
require.Equal(t, newMapInfo.LogicalDeviceId, asCreated.Data.LogicalDeviceId)
require.Equal(t, newMapInfo.DeviceProfileId, asCreated.Data.DeviceProfileId)
require.Equal(t, newMapInfo.Label, asCreated.Data.Label)
require.Equal(t, asCreated.Data.Interfaces, newMapInfo.Interfaces)

for i := range asCreated.Data.Interfaces {
newMapInfo.Interfaces[i].Roles.Sort()
asCreated.Data.Interfaces[i].Roles.Sort()

require.Equal(t, newMapInfo.Interfaces[i].Name, asCreated.Data.Interfaces[i].Name)
require.Equal(t, newMapInfo.Interfaces[i].Roles, asCreated.Data.Interfaces[i].Roles)
require.Equal(t, newMapInfo.Interfaces[i].ActiveState, asCreated.Data.Interfaces[i].ActiveState)
require.Equal(t, newMapInfo.Interfaces[i].Setting.Param, asCreated.Data.Interfaces[i].Setting.Param)
require.Equal(t, newMapInfo.Interfaces[i].Position, asCreated.Data.Interfaces[i].Position)
require.Equal(t, newMapInfo.Interfaces[i].Speed.BitsPerSecond(), asCreated.Data.Interfaces[i].Speed.BitsPerSecond())
require.Equal(t, newMapInfo.Interfaces[i].Mapping.DPInterfaceId, asCreated.Data.Interfaces[i].Mapping.DPInterfaceId)
require.Equal(t, newMapInfo.Interfaces[i].Mapping.DPPortId, asCreated.Data.Interfaces[i].Mapping.DPPortId)
require.Equal(t, newMapInfo.Interfaces[i].Mapping.DPTransformId, asCreated.Data.Interfaces[i].Mapping.DPTransformId)
require.Equal(t, newMapInfo.Interfaces[i].Mapping.LDPanel, asCreated.Data.Interfaces[i].Mapping.LDPanel)
require.Equal(t, newMapInfo.Interfaces[i].Mapping.LDPort, asCreated.Data.Interfaces[i].Mapping.LDPort)
}

log.Println("new interface map: ", mapId)

log.Printf("testing deleteInterfaceMap() against %s %s (%s)", client.clientType, clientName, client.client.ApiVersion())
err = client.client.deleteInterfaceMap(context.TODO(), mapId)
require.NoError(t, err)
}
}

func TestGetInterfaceMapByName(t *testing.T) {
clients, err := getTestClients(context.Background(), t)
if err != nil {
t.Fatal(err)
}

desired := "Juniper_QFX5120-32C_Junos__AOS-32x100-1"

for clientName, client := range clients {
log.Printf("testing getInterfaceMapByName(%s) against %s %s (%s)", desired, client.clientType, clientName, client.client.ApiVersion())
interfaceMap, err := client.client.GetInterfaceMapByName(context.Background(), desired)
if err != nil {
t.Fatal(err)
}
log.Printf("%s <---> %s", interfaceMap.Data.LogicalDeviceId, interfaceMap.Data.DeviceProfileId)
}
}
Loading

0 comments on commit 5e6f8f2

Please sign in to comment.