Skip to content

Commit

Permalink
Add an error to the return of SetUserData() (and the private method…
Browse files Browse the repository at this point in the history
…s it calls)
  • Loading branch information
chrismarget-j committed Aug 21, 2023
1 parent d3b282c commit 36d5baa
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
25 changes: 20 additions & 5 deletions apstra/two_stage_l3_clos_connectivity_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (o *ConnectivityTemplate) SetIds() error {

// SetUserData builds the top level `user_data` struct. It tries to lay the
// primitive "sausages" out sensibly.
func (o *ConnectivityTemplate) SetUserData() {
func (o *ConnectivityTemplate) SetUserData() error {
o.UserData = &ConnectivityTemplatePrimitiveUserData{
IsSausage: true,
Positions: make(map[ObjectId][]float64),
Expand All @@ -147,12 +147,18 @@ func (o *ConnectivityTemplate) SetUserData() {
var width int
for _, subpolicy := range o.Subpolicies {
xPosition := float64(xInitialPosition + (width * xSpacing))
additionalPositions, subpolicyWidth := subpolicy.positions(xPosition, yInitialPosition)
additionalPositions, subpolicyWidth, err := subpolicy.positions(xPosition, yInitialPosition)
if err != nil {
return err
}

mergePositionMaps(&o.UserData.Positions, &additionalPositions)
if subpolicyWidth > width {
width += subpolicyWidth
}
}

return nil
}

type rawConnectivityTemplate struct {
Expand Down Expand Up @@ -246,19 +252,28 @@ type ConnectivityTemplatePrimitive struct {

// positions returns position data suitable for Web UI layout and an integer
// which indicates the maximum width in "sausages" of the subpolicy tree.
func (o *ConnectivityTemplatePrimitive) positions(x, y float64) (map[ObjectId][]float64, int) {
func (o *ConnectivityTemplatePrimitive) positions(x, y float64) (map[ObjectId][]float64, int, error) {
if o.Id == nil {
return nil, 0, errors.New("attempt to define connectivity template primitive position without setting ID")
}

var width int
positions := make(map[ObjectId][]float64)
positions[*o.Id] = []float64{x, y, 1}
for _, subpolicy := range o.Subpolicies {
additionalPositions, subpolicyWidth := subpolicy.positions(x+float64(width*xSpacing), y+ySpacing)
additionalPositions, subpolicyWidth, err := subpolicy.positions(x+float64(width*xSpacing), y+ySpacing)
if err != nil {
return nil, 0, err
}

mergePositionMaps(&positions, &additionalPositions)
width += subpolicyWidth
}
if width == 0 {
width++
}
return positions, width

return positions, width, nil
}

// rawPipeline returns []rawConnectivityTemplatePolicy consisting of:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ func TestAssignClearCtToInterface(t *testing.T) {
t.Fatal(err)
}

ct.SetUserData()
err = ct.SetUserData()
if err != nil {
t.Fatal(err)
}

log.Printf("testing CreateConnectivityTemplate() against %s %s (%s)", client.clientType, clientName, client.client.ApiVersion())
err = bpClient.CreateConnectivityTemplate(ctx, &ct)
Expand Down
18 changes: 15 additions & 3 deletions apstra/two_stage_l3_clos_connectivity_template_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,11 @@ func TestCreateGetUpdateDeleteCT(t *testing.T) {
if err != nil {
t.Fatal(err)
}
tc.ct.SetUserData()

err = tc.ct.SetUserData()
if err != nil {
t.Fatal(err)
}

log.Printf("testing CreateConnectivityTemplate(%d) against %s %s (%s)", i, client.clientType, clientName, client.client.ApiVersion())
err = bpClient.CreateConnectivityTemplate(ctx, tc.ct)
Expand Down Expand Up @@ -265,7 +269,11 @@ func TestCreateGetUpdateDeleteCT(t *testing.T) {
if err != nil {
t.Fatal(err)
}
u.SetUserData()

err = u.SetUserData()
if err != nil {
t.Fatal(err)
}

log.Printf("testing UpdateConnectivityTemplate(%d) against %s %s (%s)", i, client.clientType, clientName, client.client.ApiVersion())
err = bpClient.UpdateConnectivityTemplate(ctx, u)
Expand Down Expand Up @@ -443,7 +451,11 @@ func TestCtLayout(t *testing.T) {
if err != nil {
t.Fatal(err)
}
ct.SetUserData()

err = ct.SetUserData()
if err != nil {
t.Fatal(err)
}

err = bpClient.CreateConnectivityTemplate(ctx, &ct)
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion apstra/two_stage_l3_clos_connectivity_template_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,11 @@ func TestBgpOverL3Connectivity(t *testing.T) {
Label: "BGP over L3 connectivity",
Description: "this is the description",
}
ct.SetUserData()

err := ct.SetUserData()
if err != nil {
t.Fatal(err)
}

raw, err := ct.raw()
if err != nil {
Expand Down

0 comments on commit 36d5baa

Please sign in to comment.