Skip to content

Commit

Permalink
add GetVirtualNetworkByName()
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismarget-j committed Nov 15, 2023
1 parent 77abd5f commit 3dd68e0
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
9 changes: 9 additions & 0 deletions apstra/two_stage_l3_clos_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,15 @@ func (o *TwoStageL3ClosClient) GetVirtualNetwork(ctx context.Context, vnId Objec
return raw.polish()
}

// GetVirtualNetworkByName returns *VirtualNetwork representing the given VN name
func (o *TwoStageL3ClosClient) GetVirtualNetworkByName(ctx context.Context, name string) (*VirtualNetwork, error) {
raw, err := o.getVirtualNetworkByName(ctx, name)
if err != nil {
return nil, err
}
return raw.polish()
}

// GetAllVirtualNetworks return map[ObjectId]VirtualNetwork representing all
// virtual networks configured in Apstra. NOTE: the underlying API call DOES NOT
// RETURN the SVI information, so each map entry will have a nil slice at it's
Expand Down
32 changes: 32 additions & 0 deletions apstra/two_stage_l3_clos_virtual_networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,38 @@ func (o *TwoStageL3ClosClient) getVirtualNetwork(ctx context.Context, vnId Objec
return response, nil
}

func (o *TwoStageL3ClosClient) getVirtualNetworkByName(ctx context.Context, name string) (*rawVirtualNetwork, error) {
rawVns, err := o.getAllVirtualNetworks(ctx)
if err != nil {
return nil, err
}

var found int
var rawVn rawVirtualNetwork
for i := range rawVns {
if rawVns[i].Label == name {
found++
rawVn = rawVns[i]
}
}

switch found {
case 0:
return nil, ClientErr{
errType: ErrNotfound,
err: fmt.Errorf("virtual network with label %q not found", name),
}
case 1:
// re-fetch is required here because data is missing when we "get all".
return o.getVirtualNetwork(ctx, rawVn.Id)
default:
return nil, ClientErr{
errType: ErrMultipleMatch,
err: fmt.Errorf("found %d virtual networks with label %q", found, name),
}
}
}

func (o *TwoStageL3ClosClient) getAllVirtualNetworks(ctx context.Context) (map[ObjectId]rawVirtualNetwork, error) {
var response struct {
VirtualNetworks map[ObjectId]rawVirtualNetwork `json:"virtual_networks"`
Expand Down
9 changes: 9 additions & 0 deletions apstra/two_stage_l3_clos_virtual_networks_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,15 @@ func TestCreateUpdateDeleteVirtualNetwork(t *testing.T) {
}
compareVirtualNetworkData(t, &createData, getById.Data, false)

getByName, err := bpClient.GetVirtualNetworkByName(ctx, getById.Data.Label)
if err != nil {
t.Fatal(err)
}
if vnId != getByName.Id {
t.Fatalf("Virtual Network ID mismatch: %q vs. %q", vnId, getByName.Id)
}
compareVirtualNetworkData(t, &createData, getByName.Data, false)

newVlan := Vlan(100)
createData.ReservedVlanId = &newVlan
createData.Label = randString(10, "hex")
Expand Down

0 comments on commit 3dd68e0

Please sign in to comment.