Skip to content

Commit

Permalink
update CreatePodBasedTemplateRequest to use map of pods, rather than …
Browse files Browse the repository at this point in the history
…two slices
  • Loading branch information
chrismarget-j committed Apr 2, 2024
1 parent 611c866 commit ea4e826
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 43 deletions.
68 changes: 43 additions & 25 deletions apstra/api_design_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -1386,26 +1386,21 @@ func (o *CreateRackBasedTemplateRequest) raw(ctx context.Context, client *Client
if ri.RackTypeData != nil {
return nil, fmt.Errorf("the RackTypeData field must be nil when creating a rack-based template")
}

// grab the rack type from the API using the caller's map key (ObjectId) and stash it in rackTypes
rt, err := client.getRackType(ctx, k)
if err != nil {
return nil, err
}
rackTypes[i] = *rt

// prep the rackTypeCount object using the caller's map key (ObjectId) as
// the link between the racktype data copy and the racktypecount
// prep the RackTypeCount object using the caller's map key (ObjectId) as
// the link between the rawRackType data copy and the RackTypeCount
rackTypeCounts[i].RackTypeId = k
rackTypeCounts[i].Count = ri.Count
i++
}

var err error
var dhcpServiceIntent DhcpServiceIntent
if o.DhcpServiceIntent != nil {
dhcpServiceIntent = *o.DhcpServiceIntent
}

switch {
case o.Spine == nil:
return nil, errors.New("spine cannot be <nil> when creating a rack-based template")
Expand All @@ -1417,6 +1412,12 @@ func (o *CreateRackBasedTemplateRequest) raw(ctx context.Context, client *Client
return nil, errors.New("virtual network policy cannot be <nil> when creating a rack-based template")
}

var err error
var dhcpServiceIntent DhcpServiceIntent
if o.DhcpServiceIntent != nil {
dhcpServiceIntent = *o.DhcpServiceIntent
}

spine, err := o.Spine.raw(ctx, client)
if err != nil {
return nil, err
Expand Down Expand Up @@ -1494,33 +1495,50 @@ func (o *Client) updateRackBasedTemplate(ctx context.Context, id ObjectId, in *C
}

type CreatePodBasedTemplateRequest struct {
DisplayName string
Superspine *TemplateElementSuperspineRequest
RackBasedTemplateIds []ObjectId
RackBasedTemplateCounts []RackBasedTemplateCount
AntiAffinityPolicy *AntiAffinityPolicy
FabricAddressingPolicy *TemplateFabricAddressingPolicy410Only // Apstra 4.1.0 only
DisplayName string
Superspine *TemplateElementSuperspineRequest
PodInfos map[ObjectId]TemplatePodBasedInfo
AntiAffinityPolicy *AntiAffinityPolicy
FabricAddressingPolicy *TemplateFabricAddressingPolicy410Only // Apstra 4.1.0 only
}

func (o *CreatePodBasedTemplateRequest) raw(ctx context.Context, client *Client) (*rawCreatePodBasedTemplateRequest, error) {
var err error
templatesRackBased := make([]rawTemplateRackBased, len(o.PodInfos))
rackBasedTemplatesCounts := make([]RackBasedTemplateCount, len(o.PodInfos))
var i int
for k, pi := range o.PodInfos {
if pi.TemplateRackBasedData != nil {
return nil, fmt.Errorf("the TemplateRackBasedData (pod info) field must be nil when creating a pod-based template")
}

var superspine *rawSuperspine
if o.Superspine != nil {
superspine, err = o.Superspine.raw(ctx, client)
// grab the rack-based template (pod) from the API using the caller's map key (ObjectId) and stash it in templatesRackBased
rbt, err := client.getRackBasedTemplate(ctx, k)
if err != nil {
return nil, err
}
templatesRackBased[i] = *rbt

// prep the RackBasedTemplateCount object using the caller's map key (ObjectId) as
// the link between the rawTemplateRackBased data copy and the RackBasedTemplateCount
rackBasedTemplatesCounts[i].RackBasedTemplateId = k
rackBasedTemplatesCounts[i].Count = pi.Count
i++
}

rawRackBasedTemplates := make([]rawTemplateRackBased, len(o.RackBasedTemplateIds))
for i, id := range o.RackBasedTemplateIds {
rbt, err := client.getRackBasedTemplate(ctx, id)
rbt.Type = templateTypeRackBased
switch {
case o.Superspine == nil:
return nil, errors.New("super spine cannot be <nil> when creating a pod-based template")
case o.AntiAffinityPolicy == nil && leApstra420.Check(client.apiVersion):
return nil, fmt.Errorf("anti-affinity policy cannot be <nil> when creating a pod-based template with Apstra %s", leApstra420)
}

var err error
var superspine *rawSuperspine
if o.Superspine != nil {
superspine, err = o.Superspine.raw(ctx, client)
if err != nil {
return nil, err
}
rawRackBasedTemplates[i] = *rbt
}

var antiAffinityPolicy *rawAntiAffinityPolicy
Expand All @@ -1537,8 +1555,8 @@ func (o *CreatePodBasedTemplateRequest) raw(ctx context.Context, client *Client)
Type: templateTypePodBased,
DisplayName: o.DisplayName,
Superspine: *superspine,
RackBasedTemplates: rawRackBasedTemplates,
RackBasedTemplateCounts: o.RackBasedTemplateCounts,
RackBasedTemplates: templatesRackBased,
RackBasedTemplateCounts: rackBasedTemplatesCounts,
AntiAffinityPolicy: antiAffinityPolicy,
FabricAddressingPolicy: fabricAddressingPolicy,
}, nil
Expand Down
18 changes: 7 additions & 11 deletions apstra/api_design_templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/hashicorp/go-version"
"log"
"math/rand"
"strings"
"testing"

"github.com/hashicorp/go-version"
)

func TestUnmarshalTemplate(t *testing.T) {
Expand Down Expand Up @@ -299,7 +300,6 @@ func TestUnmarshalTemplate(t *testing.T) {
}

log.Println(tType)

}

func TestTemplateStrings(t *testing.T) {
Expand Down Expand Up @@ -611,11 +611,11 @@ func TestCreateGetDeletePodBasedTemplate(t *testing.T) {
SuperspinePerPlane: 4,
LogicalDeviceId: "AOS-4x40_8x10-1",
},
RackBasedTemplateIds: []ObjectId{rbtid},
RackBasedTemplateCounts: []RackBasedTemplateCount{{
RackBasedTemplateId: rbtid,
Count: 1,
}},
PodInfos: map[ObjectId]TemplatePodBasedInfo{
rbtid: {
Count: 1,
},
},
AntiAffinityPolicy: &AntiAffinityPolicy{
Algorithm: AlgorithmHeuristic,
MaxLinksPerPort: 1,
Expand Down Expand Up @@ -677,8 +677,6 @@ func TestCreateGetDeleteL3CollapsedTemplate(t *testing.T) {
RackTypeId: "L3_collapsed_acs",
Count: 1,
}},
//DhcpServiceIntent: DhcpServiceIntent{},
//AntiAffinityPolicy: AntiAffinityPolicy{},
VirtualNetworkPolicy: VirtualNetworkPolicy{OverlayControlProtocol: OverlayControlProtocolEvpn},
}

Expand Down Expand Up @@ -1199,9 +1197,7 @@ func TestRackBasedTemplateMethods(t *testing.T) {
for clientName, client := range clients {
apiVersionString = client.client.apiVersion.String()
for i, tc := range testCases {

t.Run(fmt.Sprintf("Test-%d", i), func(t *testing.T) {

if !tc.versionConstraints.Check(client.client.apiVersion) {
t.Skipf("skipping testcase %d because of versionConstraint %s, version %s", i, tc.versionConstraints, client.client.apiVersion)
}
Expand Down
15 changes: 8 additions & 7 deletions apstra/compatibility.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ import (
)

const (
apstra410 = "4.1.0"
apstra411 = "4.1.1"
apstra412 = "4.1.2"
apstra420 = "4.2.0"
apstra421 = "4.2.1"
apstra410 = "4.1.0"
apstra411 = "4.1.1"
apstra412 = "4.1.2"
apstra420 = "4.2.0"
apstra421 = "4.2.1"
apstra4211 = "4.2.1.1"

apstraSupportedApiVersions = "4.1.0, 4.1.1, 4.1.2, 4.2.0, 4.2.1"
apstraSupportedVersionSep = ","

podBasedTemplateFabricAddressingPolicyForbiddenVersions = "4.1.1, 4.1.2, 4.2.0, 4.2.1"
podBasedTemplateFabricAddressingPolicyForbiddenVersions = "4.1.1, 4.1.2, 4.2.0, 4.2.1, 4.2.1.1"

rackBasedTemplateFabricAddressingPolicyForbiddenVersions = "4.1.1, 4.1.2, 4.2.0, 4.2.1"
rackBasedTemplateFabricAddressingPolicyForbiddenVersions = "4.1.1, 4.1.2, 4.2.0, 4.2.1, 4.2.1.1"

fabricL3MtuForbiddenError = "fabric_l3_mtu permitted only with Apstra 4.2.0 and later"

Expand Down

0 comments on commit ea4e826

Please sign in to comment.