From 3dd68e0e6702689879eb8a35b35e0870ed51ffe1 Mon Sep 17 00:00:00 2001 From: Chris Marget Date: Wed, 15 Nov 2023 15:12:41 -0500 Subject: [PATCH] add `GetVirtualNetworkByName()` --- apstra/two_stage_l3_clos_client.go | 9 ++++++ apstra/two_stage_l3_clos_virtual_networks.go | 32 +++++++++++++++++++ ..._clos_virtual_networks_integration_test.go | 9 ++++++ 3 files changed, 50 insertions(+) diff --git a/apstra/two_stage_l3_clos_client.go b/apstra/two_stage_l3_clos_client.go index 136aebd0..bd39eb52 100644 --- a/apstra/two_stage_l3_clos_client.go +++ b/apstra/two_stage_l3_clos_client.go @@ -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 diff --git a/apstra/two_stage_l3_clos_virtual_networks.go b/apstra/two_stage_l3_clos_virtual_networks.go index 84bceefe..76f17693 100644 --- a/apstra/two_stage_l3_clos_virtual_networks.go +++ b/apstra/two_stage_l3_clos_virtual_networks.go @@ -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"` diff --git a/apstra/two_stage_l3_clos_virtual_networks_integration_test.go b/apstra/two_stage_l3_clos_virtual_networks_integration_test.go index 9535b466..318d6136 100644 --- a/apstra/two_stage_l3_clos_virtual_networks_integration_test.go +++ b/apstra/two_stage_l3_clos_virtual_networks_integration_test.go @@ -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")