Skip to content

Commit

Permalink
split payload of PolicyRule into PolicyRuleData object; tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismarget-j committed Jan 12, 2024
1 parent 8e1b90b commit c1dcb59
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
2 changes: 1 addition & 1 deletion apstra/two_stage_l3_clos_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ func (o *TwoStageL3ClosClient) UpdatePolicy(ctx context.Context, id ObjectId, da
// down). Position 0 makes the new policy first on the list, 1 makes it second
// on the list, etc... Use -1 for last on the list. The returned ObjectId
// represents the new rule
func (o *TwoStageL3ClosClient) AddPolicyRule(ctx context.Context, rule *PolicyRule, position int, policyId ObjectId) (ObjectId, error) {
func (o *TwoStageL3ClosClient) AddPolicyRule(ctx context.Context, rule *PolicyRuleData, position int, policyId ObjectId) (ObjectId, error) {
return o.addPolicyRule(ctx, rule.raw(), position, policyId)
}

Expand Down
16 changes: 15 additions & 1 deletion apstra/two_stage_l3_clos_policies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,20 @@ func comparePolicyRules(aName string, a PolicyRule, bName string, b PolicyRule,
t.Fatalf("Policy Rule IDs don't match: %s has %q, %s has %q", aName, a.Id, bName, b.Id)
}

aData := a.Data != nil
bData := b.Data != nil

if (aData || bData) && !(aData && bData) { //xor
t.Fatalf("Policy Rule data presence mismatch -- a: %t vs. b: %t", aData, bData)
}

if aData && bData {
comparePolicyRuleData(aName, a.Data, bName, b.Data, t)
}

}

func comparePolicyRuleData(aName string, a *PolicyRuleData, bName string, b *PolicyRuleData, t *testing.T) {
if a.Label != b.Label {
t.Fatalf("Policy Rule Labels don't match: %s has %q, %s has %q", aName, a.Label, bName, b.Label)
}
Expand Down Expand Up @@ -417,7 +431,7 @@ func TestAddDeletePolicyRule(t *testing.T) {
t.Fatal(err)
}

newRule := &PolicyRule{
newRule := &PolicyRuleData{
Label: randString(5, "hex"),
Description: randString(5, "hex"),
Protocol: PolicyRuleProtocolTcp,
Expand Down
27 changes: 16 additions & 11 deletions apstra/two_stage_l3_clos_policy_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,11 @@ func (o PortRanges) string() string {
}

type PolicyRule struct {
Id ObjectId
Id ObjectId
Data *PolicyRuleData
}

type PolicyRuleData struct {
Label string
Description string
Protocol PolicyRuleProtocol
Expand All @@ -137,15 +141,14 @@ type PolicyRule struct {
TcpStateQualifier *TcpStateQualifier
}

func (o PolicyRule) raw() *rawPolicyRule {
func (o PolicyRuleData) raw() *rawPolicyRule {
var tcpStateQualifier *string
if o.TcpStateQualifier != nil {
s := o.TcpStateQualifier.Value
tcpStateQualifier = &s
}

return &rawPolicyRule{
Id: o.Id,
Label: o.Label,
Description: o.Description,
Protocol: o.Protocol.Value,
Expand Down Expand Up @@ -197,14 +200,16 @@ func (o rawPolicyRule) polish() (*PolicyRule, error) {
}

return &PolicyRule{
Id: o.Id,
Label: o.Label,
Description: o.Description,
Protocol: *protocol,
Action: *action,
SrcPort: srcPort,
DstPort: dstPort,
TcpStateQualifier: tcpStateQualifier,
Id: o.Id,
Data: &PolicyRuleData{
Label: o.Label,
Description: o.Description,
Protocol: *protocol,
Action: *action,
SrcPort: srcPort,
DstPort: dstPort,
TcpStateQualifier: tcpStateQualifier,
},
}, nil
}

Expand Down

0 comments on commit c1dcb59

Please sign in to comment.