Skip to main content

Route

Static network route management via Netplan.

Methods

MethodDescription
List(ctx, target)List all routes
Get(ctx, target, interfaceName)Get routes for an interface
Create(ctx, target, interfaceName, opts)Create routes for an interface
Update(ctx, target, interfaceName, opts)Update routes for an interface
Delete(ctx, target, interfaceName)Delete routes for an interface

Request Types

TypeFields
RouteConfigOptsRoutes ([]RouteItem)
RouteItemTo, Via, Metric

Result Types

RouteListResult (List)

FieldTypeDescription
HostnamestringAgent hostname
StatusstringResult status (ok, skipped)
Routes[]RouteInfoList of routes on the host
ErrorstringError message (if any)

RouteInfo

FieldTypeDescription
DestinationstringDestination in CIDR notation
GatewaystringGateway IP address
InterfacestringNetwork interface name
MetricintRoute metric (priority)
ScopestringRoute scope

RouteGetResult (Get)

FieldTypeDescription
HostnamestringAgent hostname
StatusstringResult status (ok, skipped)
Routes[]RouteInfoRoutes for the interface
ErrorstringError message (if any)

RouteMutationResult (Create, Update, Delete)

FieldTypeDescription
HostnamestringAgent hostname
StatusstringResult status (ok, skipped)
InterfacestringInterface name
ChangedboolWhether a change was made
ErrorstringError message (if any)

Usage

import "github.com/retr0h/osapi/pkg/sdk/client"

c := client.New("http://localhost:8080", token)

// List all routes on the host
resp, err := c.Route.List(ctx, "web-01")
for _, r := range resp.Data.Results {
for _, rt := range r.Routes {
fmt.Printf("%s via %s dev %s metric %d\n",
rt.Destination, rt.Gateway,
rt.Interface, rt.Metric)
}
}

// Get managed routes for a specific interface
resp, err := c.Route.Get(ctx, "web-01", "eth0")
for _, rt := range resp.Data.First().Routes {
fmt.Printf("%s via %s\n", rt.Destination, rt.Gateway)
}

// Create static routes for an interface
metric := 100
resp, err := c.Route.Create(ctx, "web-01", "eth0",
client.RouteConfigOpts{
Routes: []client.RouteItem{
{To: "10.0.0.0/8", Via: "192.168.1.1"},
{To: "172.16.0.0/12", Via: "192.168.1.1",
Metric: &metric},
},
})
fmt.Printf("changed=%v\n", resp.Data.First().Changed)

// Update routes (replace all routes for the interface)
resp, err := c.Route.Update(ctx, "web-01", "eth0",
client.RouteConfigOpts{
Routes: []client.RouteItem{
{To: "10.0.0.0/8", Via: "192.168.1.254"},
},
})

// Delete routes for an interface
resp, err := c.Route.Delete(ctx, "web-01", "eth0")
fmt.Printf("changed=%v\n", resp.Data.First().Changed)

// Broadcast route create to all hosts
resp, err := c.Route.Create(ctx, "_all", "eth0",
client.RouteConfigOpts{
Routes: []client.RouteItem{
{To: "10.0.0.0/8", Via: "192.168.1.1"},
},
})
for _, r := range resp.Data.Results {
fmt.Printf("%s changed=%v\n", r.Hostname, r.Changed)
}

Example

See examples/sdk/client/route.go for a complete working example.

Permissions

OperationPermission
List, Getnetwork:read
Create, Update, Deletenetwork:write

Route management is supported on the Debian OS family (Ubuntu, Debian, Raspbian). On unsupported platforms (Darwin, generic Linux), operations return status: skipped.