Skip to content

Commit

Permalink
introduce GetRoutingPolicyByName()
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismarget-j committed Jul 19, 2023
1 parent adc5b8c commit 38b1387
Show file tree
Hide file tree
Showing 3 changed files with 44 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 @@ -368,6 +368,15 @@ func (o *TwoStageL3ClosClient) GetRoutingPolicy(ctx context.Context, id ObjectId
return raw.polish()
}

func (o *TwoStageL3ClosClient) GetRoutingPolicyByName(ctx context.Context, desired string) (*DcRoutingPolicy, error) {
raw, err := o.getRoutingPolicyByName(ctx, desired)
if err != nil {
return nil, err
}

return raw.polish()
}

// GetDefaultRoutingPolicy returns *DcRoutingPolicy representing the
// "default_immutable" routing policy attached to the blueprint.
func (o *TwoStageL3ClosClient) GetDefaultRoutingPolicy(ctx context.Context) (*DcRoutingPolicy, error) {
Expand Down
25 changes: 25 additions & 0 deletions apstra/two_stage_l3_clos_routing_policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,31 @@ func (o *TwoStageL3ClosClient) getRoutingPolicy(ctx context.Context, id ObjectId
return response, nil
}

func (o *TwoStageL3ClosClient) getRoutingPolicyByName(ctx context.Context, desired string) (*rawDcRoutingPolicy, error) {
policies, err := o.getAllRoutingPolicies(ctx)
if err != nil {
return nil, err
}

policyIdx := -1
for i, policy := range policies {
if policy.Label != desired {
continue
}

if policyIdx >= 0 {
return nil, ApstraClientErr{
errType: ErrMultipleMatch,
err: fmt.Errorf("found multiple routing policies with name %q", desired),
}
}

policyIdx = i
}

return &policies[policyIdx], nil
}

func (o *TwoStageL3ClosClient) getDefaultRoutingPolicy(ctx context.Context) (*rawDcRoutingPolicy, error) {
policies, err := o.getAllRoutingPolicies(ctx)
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions apstra/two_stage_l3_clos_routing_policies_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,16 @@ func TestRoutingPolicies(t *testing.T) {
}
compareDcRoutingPolicyData(t, policyData, policy.Data)

log.Printf("testing GetRoutingPolicyByName() against %s %s (%s)", client.clientType, clientName, client.client.ApiVersion())
policy, err = bpClient.GetRoutingPolicyByName(ctx, policy.Data.Label)
if err != nil {
t.Fatal(err)
}
if policy.Id != policyId {
t.Fatalf("policy IDs don't match %q vs. %q", policy.Id, policyId)
}
compareDcRoutingPolicyData(t, policyData, policy.Data)

log.Printf("testing GetAllRoutingPolicies() against %s %s (%s)", client.clientType, clientName, client.client.ApiVersion())
policies, err := bpClient.GetAllRoutingPolicies(ctx)
if err != nil {
Expand Down

0 comments on commit 38b1387

Please sign in to comment.