Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fold obsolete getLoopbackInterfaceNode() into getLoopback0Addresses() #938

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 47 additions & 75 deletions apstra/blueprint/device_allocation_system_attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,71 +201,85 @@ func (o *DeviceAllocationSystemAttributes) getAsn(ctx context.Context, bp *apstr
o.Asn = types.Int64Value(int64(asn))
}

// getLoopback0Addresses loads the IPv4 and IPv6 values of Loopback0 from the API. It does not overwrite known values.
func (o *DeviceAllocationSystemAttributes) getLoopback0Addresses(ctx context.Context, bp *apstra.TwoStageL3ClosClient, nodeId apstra.ObjectId, diags *diag.Diagnostics) {
if utils.HasValue(o.LoopbackIpv4) && utils.HasValue(o.LoopbackIpv6) {
return
}

idx := 0
// todo: Fold getLoopbackInterfaceNode into this function, or at least clean up the
// passing of json.RawMessage because we're only using it for a single purpose now.
// Maybe use this: https://github.com/Juniper/apstra-go-sdk/issues/421
rawJson := getLoopbackInterfaceNode(ctx, bp, nodeId, idx, diags)
if diags.HasError() {
return
}
idx := 0 // loopback 0

if len(rawJson) == 0 {
o.LoopbackIpv4 = cidrtypes.NewIPv4PrefixNull()
o.LoopbackIpv6 = cidrtypes.NewIPv6PrefixNull()
return // no loopback idx interface node found
}
query := new(apstra.PathQuery).
SetBlueprintId(bp.Id()).
SetClient(bp.Client()).
Node([]apstra.QEEAttribute{
apstra.NodeTypeSystem.QEEAttribute(),
{Key: "id", Value: apstra.QEStringVal(nodeId)},
}).
Out([]apstra.QEEAttribute{apstra.RelationshipTypeHostedInterfaces.QEEAttribute()}).
Node([]apstra.QEEAttribute{
apstra.NodeTypeInterface.QEEAttribute(),
{Key: "if_type", Value: apstra.QEStringVal("loopback")},
{Key: "loopback_id", Value: apstra.QEIntVal(idx)},
{Key: "name", Value: apstra.QEStringVal("n_interface")},
})

var loopbackNode struct {
IPv4Addr *string `json:"ipv4_addr"`
IPv6Addr *string `json:"ipv6_addr"`
var queryResult struct {
Items []struct {
Node struct {
IPv4Addr *string `json:"ipv4_addr"`
IPv6Addr *string `json:"ipv6_addr"`
} `json:"n_interface"`
} `json:"items"`
}

err := json.Unmarshal(rawJson, &loopbackNode)
err := query.Do(ctx, &queryResult)
if err != nil {
diags.AddError(
fmt.Sprintf("failed while unpacking system %q loopback %d interface node", nodeId, idx),
err.Error(),
)
diags.AddError(fmt.Sprintf("failed while querying for system %q loopback %d node", nodeId, idx), err.Error())
return
}

if loopbackNode.IPv4Addr == nil && loopbackNode.IPv6Addr == nil {
switch len(queryResult.Items) {
case 0:
// no loopback node found
o.LoopbackIpv4 = cidrtypes.NewIPv4PrefixNull()
o.LoopbackIpv6 = cidrtypes.NewIPv6PrefixNull()
return // no loopback IP addresses found in domain node
return
case 1:
// this is the normal case - handled below
default:
diags.AddError(
fmt.Sprintf("unexpected rewult while querying for system %q loopback node %d", nodeId, idx),
fmt.Sprintf("node has graph relationships with %d loopback nodes with id %d", len(queryResult.Items), idx),
)
return
}

if !utils.HasValue(o.LoopbackIpv4) {
if !utils.HasValue(o.LoopbackIpv4) { // don't overwrite known values (apparently?)
o.LoopbackIpv4 = cidrtypes.NewIPv4PrefixNull()
if loopbackNode.IPv4Addr != nil && len(*loopbackNode.IPv4Addr) != 0 {
_, _, err := net.ParseCIDR(*loopbackNode.IPv4Addr)
if queryResult.Items[0].Node.IPv4Addr != nil && *queryResult.Items[0].Node.IPv4Addr != "" {
_, _, err := net.ParseCIDR(*queryResult.Items[0].Node.IPv4Addr)
if err != nil {
diags.AddError(
fmt.Sprintf("failed to parse `ipv4_addr` from API response %q", string(rawJson)),
fmt.Sprintf("failed to parse API response value for `ipv4_addr`: %q", *queryResult.Items[0].Node.IPv4Addr),
err.Error())
return
}
o.LoopbackIpv4 = cidrtypes.NewIPv4PrefixValue(*loopbackNode.IPv4Addr)
o.LoopbackIpv4 = cidrtypes.NewIPv4PrefixValue(*queryResult.Items[0].Node.IPv4Addr)
}
}

if !utils.HasValue(o.LoopbackIpv6) {
if !utils.HasValue(o.LoopbackIpv6) { // don't overwrite known values (apparently?)
o.LoopbackIpv6 = cidrtypes.NewIPv6PrefixNull()
if loopbackNode.IPv6Addr != nil && len(*loopbackNode.IPv6Addr) != 0 {
_, _, err := net.ParseCIDR(*loopbackNode.IPv6Addr)
if queryResult.Items[0].Node.IPv6Addr != nil && *queryResult.Items[0].Node.IPv6Addr != "" {
_, _, err := net.ParseCIDR(*queryResult.Items[0].Node.IPv6Addr)
if err != nil {
diags.AddError(
fmt.Sprintf("failed to parse `ipv6_addr` from API response %q", string(rawJson)),
fmt.Sprintf("failed to parse API response value for `ipv6_addr`: %q", *queryResult.Items[0].Node.IPv6Addr),
err.Error())
return
}
o.LoopbackIpv6 = cidrtypes.NewIPv6PrefixValue(*loopbackNode.IPv6Addr)
o.LoopbackIpv6 = cidrtypes.NewIPv6PrefixValue(*queryResult.Items[0].Node.IPv6Addr)
}
}
}
Expand Down Expand Up @@ -600,45 +614,3 @@ func getDomainNode(ctx context.Context, bp *apstra.TwoStageL3ClosClient, nodeId
return nil
}
}

func getLoopbackInterfaceNode(ctx context.Context, bp *apstra.TwoStageL3ClosClient, nodeId apstra.ObjectId, idx int, diags *diag.Diagnostics) json.RawMessage {
query := new(apstra.PathQuery).
SetBlueprintId(bp.Id()).
SetClient(bp.Client()).
Node([]apstra.QEEAttribute{
apstra.NodeTypeSystem.QEEAttribute(),
{Key: "id", Value: apstra.QEStringVal(nodeId)},
}).
Out([]apstra.QEEAttribute{apstra.RelationshipTypeHostedInterfaces.QEEAttribute()}).
Node([]apstra.QEEAttribute{
apstra.NodeTypeInterface.QEEAttribute(),
{Key: "if_type", Value: apstra.QEStringVal("loopback")},
{Key: "loopback_id", Value: apstra.QEIntVal(idx)},
{Key: "name", Value: apstra.QEStringVal("n_interface")},
})

var queryResult struct {
Items []struct {
Node json.RawMessage `json:"n_interface"`
} `json:"items"`
}

err := query.Do(ctx, &queryResult)
if err != nil {
diags.AddError(fmt.Sprintf("failed while querying for system %q loopback %d node", nodeId, idx), err.Error())
return nil
}

switch len(queryResult.Items) {
case 0:
return nil
case 1:
return queryResult.Items[0].Node
default:
diags.AddError(
fmt.Sprintf("unexpected rewult while querying for system %q loopback node %d", nodeId, idx),
fmt.Sprintf("node has graph relationships with %d loopback nodes with id %d", len(queryResult.Items), idx),
)
return nil
}
}
Loading