diff --git a/apstra/two_stage_l3_clos_client.go b/apstra/two_stage_l3_clos_client.go index d3ceebb4..73887cbc 100644 --- a/apstra/two_stage_l3_clos_client.go +++ b/apstra/two_stage_l3_clos_client.go @@ -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) { diff --git a/apstra/two_stage_l3_clos_routing_policies.go b/apstra/two_stage_l3_clos_routing_policies.go index bc4d4c34..8b8b1c10 100644 --- a/apstra/two_stage_l3_clos_routing_policies.go +++ b/apstra/two_stage_l3_clos_routing_policies.go @@ -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 { diff --git a/apstra/two_stage_l3_clos_routing_policies_integration_test.go b/apstra/two_stage_l3_clos_routing_policies_integration_test.go index d4098a8e..5a3a16c5 100644 --- a/apstra/two_stage_l3_clos_routing_policies_integration_test.go +++ b/apstra/two_stage_l3_clos_routing_policies_integration_test.go @@ -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 {