Skip to content

Commit

Permalink
iotas -> enums; raw/polish -> marshal/unmarshal
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismarget-j committed Oct 24, 2024
1 parent 7ce7b45 commit f0f7ce3
Show file tree
Hide file tree
Showing 12 changed files with 531 additions and 645 deletions.
3 changes: 2 additions & 1 deletion apstra/client_rendered_diff_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"sync"
"testing"

"github.com/Juniper/apstra-go-sdk/apstra/enum"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -97,7 +98,7 @@ func TestGetNodeRenderedDiff(t *testing.T) {
Label: randString(6, "hex"),
SecurityZoneId: szId,
VnBindings: vnBindings,
VnType: VnTypeVxlan,
VnType: enum.VnTypeVxlan,
})
require.NoError(t, err)
t.Logf(vnId.String())
Expand Down
32 changes: 32 additions & 0 deletions apstra/enum/enums.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,35 @@ var (
RenderedConfigTypeStaging = RenderedConfigType{Value: "staging"}
RenderedConfigTypeDeployed = RenderedConfigType{Value: "deployed"}
)

type SviIpv4Mode oenum.Member[string]

var (
SviIpv4ModeDisabled = SviIpv4Mode{Value: "disabled"}
SviIpv4ModeEnabled = SviIpv4Mode{Value: "enabled"}
SviIpv4ModeForced = SviIpv4Mode{Value: "forced"}
)

type SviIpv6Mode oenum.Member[string]

var (
SviIpv6ModeDisabled = SviIpv6Mode{Value: "disabled"}
SviIpv6ModeEnabled = SviIpv6Mode{Value: "enabled"}
SviIpv6ModeForced = SviIpv6Mode{Value: "forced"}
SviIpv6ModeLinkLocal = SviIpv6Mode{Value: "link_local"}
)

type DhcpServiceMode oenum.Member[string]

var (
DhcpServiceModeDisabled = DhcpServiceMode{Value: "dhcpServiceDisabled"}
DhcpServiceModeEnabled = DhcpServiceMode{Value: "dhcpServiceEnabled"}
)

type VnType oenum.Member[string]

var (
VnTypeExternal = VnType{Value: "disabled"}
VnTypeVlan = VnType{Value: "vlan"}
VnTypeVxlan = VnType{Value: "vxlan"}
)
84 changes: 84 additions & 0 deletions apstra/enum/generated_enums.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ func (o *DeviceProfileType) FromString(s string) error {
return nil
}

var _ enum = (*DhcpServiceMode)(nil)

func (o DhcpServiceMode) String() string {
return o.Value
}

func (o *DhcpServiceMode) FromString(s string) error {
if DhcpServiceModes.Parse(s) == nil {
return newEnumParseError(o, s)
}
o.Value = s
return nil
}

var _ enum = (*FFResourceType)(nil)

func (o FFResourceType) String() string {
Expand Down Expand Up @@ -260,6 +274,34 @@ func (o *StorageSchemaPath) FromString(s string) error {
return nil
}

var _ enum = (*SviIpv4Mode)(nil)

func (o SviIpv4Mode) String() string {
return o.Value
}

func (o *SviIpv4Mode) FromString(s string) error {
if SviIpv4Modes.Parse(s) == nil {
return newEnumParseError(o, s)
}
o.Value = s
return nil
}

var _ enum = (*SviIpv6Mode)(nil)

func (o SviIpv6Mode) String() string {
return o.Value
}

func (o *SviIpv6Mode) FromString(s string) error {
if SviIpv6Modes.Parse(s) == nil {
return newEnumParseError(o, s)
}
o.Value = s
return nil
}

var _ enum = (*TcpStateQualifier)(nil)

func (o TcpStateQualifier) String() string {
Expand All @@ -274,6 +316,20 @@ func (o *TcpStateQualifier) FromString(s string) error {
return nil
}

var _ enum = (*VnType)(nil)

func (o VnType) String() string {
return o.Value
}

func (o *VnType) FromString(s string) error {
if VnTypes.Parse(s) == nil {
return newEnumParseError(o, s)
}
o.Value = s
return nil
}

var (
_ enum = new(ApiFeature)
ApiFeatures = oenum.New(
Expand All @@ -300,6 +356,12 @@ var (
DeviceProfileTypeMonolithic,
)

_ enum = new(DhcpServiceMode)
DhcpServiceModes = oenum.New(
DhcpServiceModeDisabled,
DhcpServiceModeEnabled,
)

_ enum = new(FFResourceType)
FFResourceTypes = oenum.New(
FFResourceTypeAsn,
Expand Down Expand Up @@ -434,8 +496,30 @@ var (
StorageSchemaPathXcvr,
)

_ enum = new(SviIpv4Mode)
SviIpv4Modes = oenum.New(
SviIpv4ModeDisabled,
SviIpv4ModeEnabled,
SviIpv4ModeForced,
)

_ enum = new(SviIpv6Mode)
SviIpv6Modes = oenum.New(
SviIpv6ModeDisabled,
SviIpv6ModeEnabled,
SviIpv6ModeForced,
SviIpv6ModeLinkLocal,
)

_ enum = new(TcpStateQualifier)
TcpStateQualifiers = oenum.New(
TcpStateQualifierEstablished,
)

_ enum = new(VnType)
VnTypes = oenum.New(
VnTypeExternal,
VnTypeVlan,
VnTypeVxlan,
)
)
48 changes: 48 additions & 0 deletions apstra/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,51 @@ func isv6(ip net.IP) bool {
}
return 16 == len(ip.To16())
}

// ipFromString is an improvemen ton calling net.ParseIP() directly because it
// handles empty strings gracefully (returns nil net.IP) and because it returns
// errors in case of un-parseable input strings.
func ipFromString(s string) (net.IP, error) {
if s == "" {
return nil, nil
}

ip := net.ParseIP(s)
if ip == nil {
return nil, fmt.Errorf("cannot parse IP %q", s)
}

return ip, nil
}

// ipNetFromString is an improvement on calling net.ParseCIDR() directly because
// it handles empty strings gracefully (returns nil pointer) and because it
// returns a net.IPNet with the actual IP address, rather than the base address.
func ipNetFromString(s string) (*net.IPNet, error) {
if s == "" {
return nil, nil
}

ip, ipNet, err := net.ParseCIDR(s)
if err != nil {
return nil, fmt.Errorf("while parsing CIDR string %q - %w", s, err)
}
ipNet.IP = ip

return ipNet, nil
}

// macFromString is an improvement on calling net.ParseMAC() directly because it
// handles empty strings gracefully (returns nil net.HardwareAddr)
func macFromString(s string) (net.HardwareAddr, error) {
if s == "" {
return nil, nil
}

mac, err := net.ParseMAC(s)
if err != nil {
return nil, fmt.Errorf("cannot parse hardware address %q", s)
}

return mac, nil
}
2 changes: 1 addition & 1 deletion apstra/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ func testVirtualNetwork(t testing.TB, ctx context.Context, bp *TwoStageL3ClosCli
SecurityZoneId: szId,
VirtualGatewayIpv4Enabled: true,
VnBindings: vnBindings,
VnType: VnTypeVxlan,
VnType: enum.VnTypeVxlan,
})
require.NoError(t, err)

Expand Down
32 changes: 5 additions & 27 deletions apstra/two_stage_l3_clos_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ func (o *TwoStageL3ClosClient) DeletePolicyRuleById(ctx context.Context, policyI

// CreateVirtualNetwork creates a new virtual network according to the supplied VirtualNetworkData
func (o *TwoStageL3ClosClient) CreateVirtualNetwork(ctx context.Context, in *VirtualNetworkData) (ObjectId, error) {
return o.createVirtualNetwork(ctx, in.raw())
return o.createVirtualNetwork(ctx, in)
}

// ListAllVirtualNetworkIds returns []ObjectId representing virtual networks configured in the blueprint
Expand All @@ -354,48 +354,26 @@ func (o *TwoStageL3ClosClient) ListAllVirtualNetworkIds(ctx context.Context) ([]

// GetVirtualNetwork returns *VirtualNetwork representing the given vnId
func (o *TwoStageL3ClosClient) GetVirtualNetwork(ctx context.Context, vnId ObjectId) (*VirtualNetwork, error) {
raw, err := o.getVirtualNetwork(ctx, vnId)
if err != nil {
return nil, err
}
return raw.polish()
return o.getVirtualNetwork(ctx, vnId)
}

// 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()
return o.getVirtualNetworkByName(ctx, name)
}

// 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
// Data.SviIps struct element.
func (o *TwoStageL3ClosClient) GetAllVirtualNetworks(ctx context.Context) (map[ObjectId]VirtualNetwork, error) {
rawMap, err := o.getAllVirtualNetworks(ctx)
if err != nil {
return nil, err
}

result := make(map[ObjectId]VirtualNetwork, len(rawMap))
for id, raw := range rawMap {
polished, err := raw.polish()
if err != nil {
return nil, err
}
result[id] = *polished
}

return result, nil
return o.getAllVirtualNetworks(ctx)
}

// UpdateVirtualNetwork updates the virtual network specified by ID using the
// VirtualNetworkData and HTTP method PUT.
func (o *TwoStageL3ClosClient) UpdateVirtualNetwork(ctx context.Context, id ObjectId, in *VirtualNetworkData) error {
return o.updateVirtualNetwork(ctx, id, in.raw())
return o.updateVirtualNetwork(ctx, id, in)
}

// DeleteVirtualNetwork deletes the virtual network specified by id from the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"context"
"log"
"testing"

"github.com/Juniper/apstra-go-sdk/apstra/enum"
)

func compareConnectivityTemplateAssignments(a, b map[ObjectId]bool, applicationPointId ObjectId, t *testing.T) {
Expand Down Expand Up @@ -100,7 +102,7 @@ func TestAssignClearCtToInterface(t *testing.T) {
Label: randString(6, "hex"),
SecurityZoneId: szId,
VnBindings: bindings,
VnType: VnTypeVxlan,
VnType: enum.VnTypeVxlan,
})
if err != nil {
t.Fatal(err)
Expand Down
4 changes: 2 additions & 2 deletions apstra/two_stage_l3_clos_policies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func TestCreateDatacenterPolicy(t *testing.T) {
Label: "vn_" + strconv.Itoa(i),
SecurityZoneId: szId,
VnBindings: bindings,
VnType: VnTypeVxlan,
VnType: enum.VnTypeVxlan,
})
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -399,7 +399,7 @@ func TestAddDeletePolicyRule(t *testing.T) {
Label: "vn_" + strconv.Itoa(i),
SecurityZoneId: szId,
VnBindings: bindings,
VnType: VnTypeVxlan,
VnType: enum.VnTypeVxlan,
})
if err != nil {
t.Fatal(err)
Expand Down
Loading

0 comments on commit f0f7ce3

Please sign in to comment.