InterfaceRoute
c.InterfaceRoute calls the API's InterfaceRoute group. The routes of an
interface.
Methods
| Method | Returns | Permission | Description |
|---|---|---|---|
List(ctx, hostname, name) | *Response[Collection[InterfaceRouteEntry]] | interface:read | List the routes of an interface. |
Create(ctx, hostname, input) | *Response[Collection[InterfaceRouteResult]] | interface:write | Add a static route to an interface. |
Update(ctx, hostname, input) | *Response[Collection[InterfaceRouteResult]] | interface:write | Change the gateway or the metric of a route. |
Delete(ctx, hostname, input) | *Response[Collection[InterfaceRouteResult]] | interface:write | Drop a route that osapi wrote. |
List
func (s *InterfaceRouteService) List(
ctx context.Context,
hostname string,
name string,
) (*Response[Collection[InterfaceRouteEntry]], error)
List the routes of an interface.
Reads the routes of the main table that go out of the interface: ip route on Linux, netstat on macOS. proto says where a route came from, such as dhcp, kernel or static. An interface with no routes has none; one the host does not have is not found.
It sends GET /api/node/{hostname}/interface/{name}/route, and needs the
permission interface:read.
The examples of the method's definition:
An interface with routes, go run interface_route.go list:
resp, err := c.InterfaceRoute.List(ctx, target, "eth0")
An interface with no routes, go run interface_route.go list-2:
resp, err := c.InterfaceRoute.List(ctx, target, "eth1")
A Mac, go run interface_route.go list-3:
resp, err := c.InterfaceRoute.List(ctx, target, "en0")
Create
func (s *InterfaceRouteService) Create(
ctx context.Context,
hostname string,
input InterfaceRouteCreateInput,
) (*Response[Collection[InterfaceRouteResult]], error)
Add a static route to an interface.
Adds the route to the interface's netplan file, such as /etc/netplan/osapi-osapi0-routes.yaml for osapi0, under a netplan definition that is the interface's own, and applies it. A route osapi already has, the same, does not change; one to the same destination by another gateway or metric is refused: update changes it. A default route is refused: the interface behavior sets the gateway. An interface the host does not have is not found. An interface that a match defines, such as cloud-init's all for en*, is refused, because the route would go on each interface the match takes. It runs on the Debian family, and elsewhere, and in a container, reports skipped.
It sends POST /api/node/{hostname}/interface/{name}/route, and needs the
permission interface:write.
The examples of the method's definition:
A route, go run interface_route.go create:
resp, err := c.InterfaceRoute.Create(ctx, target, client.InterfaceRouteCreateInput{
Destination: "198.51.100.0/24",
Name: "osapi0",
Via: "192.0.2.1",
})
A second route, with a metric, go run interface_route.go create-2:
resp, err := c.InterfaceRoute.Create(ctx, target, client.InterfaceRouteCreateInput{
Destination: "2001:db8:1::/48",
Metric: 200,
Name: "osapi0",
Via: "2001:db8::1",
})
Update
func (s *InterfaceRouteService) Update(
ctx context.Context,
hostname string,
input InterfaceRouteUpdateInput,
) (*Response[Collection[InterfaceRouteResult]], error)
Change the gateway or the metric of a route.
Writes the route over the one osapi has to the same destination, and applies it. A destination osapi has no route to is not found: create adds it.
It sends PUT /api/node/{hostname}/interface/{name}/route, and needs the
permission interface:write.
The examples of the method's definition:
A new gateway and metric, go run interface_route.go update:
resp, err := c.InterfaceRoute.Update(ctx, target, client.InterfaceRouteUpdateInput{
Destination: "198.51.100.0/24",
Metric: 10,
Name: "osapi0",
Via: "192.0.2.2",
})
Delete
func (s *InterfaceRouteService) Delete(
ctx context.Context,
hostname string,
input InterfaceRouteDeleteInput,
) (*Response[Collection[InterfaceRouteResult]], error)
Drop a route that osapi wrote.
Drops the route from the interface's netplan file and applies what is left; the last route removes the file. A route osapi does not have does not change.
It sends DELETE /api/node/{hostname}/interface/{name}/route, and needs the
permission interface:write.
The examples of the method's definition:
One of two routes, go run interface_route.go delete:
resp, err := c.InterfaceRoute.Delete(ctx, target, client.InterfaceRouteDeleteInput{
Destination: "198.51.100.0/24",
Name: "osapi0",
})
Types
The fields of each type are the properties of its schema in the API's spec.
InterfaceRouteCreateInput
| Field | Type | Description |
|---|---|---|
Name | string | The name of the path. The interface, such as eth0, or a fact such as @fact.interface.primary. |
Destination | string | Where the route goes, in CIDR notation, such as 10.0.0.0/8; not a default route. |
Metric | int | The metric, from 0 to 4294967295; none when it is 0. |
Via | string | The gateway, an address of the destination's family, such as 192.168.1.1. |
InterfaceRouteDeleteInput
| Field | Type | Description |
|---|---|---|
Name | string | The name of the path. The interface, such as eth0, or a fact such as @fact.interface.primary. |
Destination | string | The destination of the route, in CIDR notation, such as 10.0.0.0/8. |
InterfaceRouteEntry
| Field | Type | Description |
|---|---|---|
Destination | string | The destination in CIDR notation, or default. |
Error | string | Error message if the agent failed. |
Family | string | inet or inet6. |
Gateway | string | The gateway; empty for a route on the link. |
Hostname | string | Hostname of the agent that produced this result. |
Interface | string | The interface the route goes out of. |
Metric | int | The metric; 0 when the kernel gives none. |
Proto | string | Where the route came from, such as kernel, dhcp or static; empty on macOS. |
Scope | string | The scope, such as link; empty for a global route. |
Status | RowStatus | What happened on the host: ok, the operation ran and succeeded; failed, it ran and did not succeed; skipped, the host's OS family does not support it; timeout, the host did not answer, so nobody knows whether it ran. |
InterfaceRouteResult
| Field | Type | Description |
|---|---|---|
Changed | bool | Whether the host changed. |
Destination | string | The destination of the route. |
Error | string | Error message if the agent failed. |
Hostname | string | Hostname of the agent that produced this result. |
Status | RowStatus | What happened on the host: ok, the operation ran and succeeded; failed, it ran and did not succeed; skipped, the host's OS family does not support it; timeout, the host did not answer, so nobody knows whether it ran. |
InterfaceRouteUpdateInput
| Field | Type | Description |
|---|---|---|
Name | string | The name of the path. The interface, such as eth0, or a fact such as @fact.interface.primary. |
Destination | string | Where the route goes, in CIDR notation, such as 10.0.0.0/8; not a default route. |
Metric | int | The metric, from 0 to 4294967295; none when it is 0. |
Via | string | The gateway, an address of the destination's family, such as 192.168.1.1. |
RowStatus
What happened on the host: ok, the operation ran and succeeded; failed, it ran and did not succeed; skipped, the host's OS family does not support it; timeout, the host did not answer, so nobody knows whether it ran.
A string, one of ok, failed, skipped, timeout.
Example
examples/sdk/client/interface_route.go
runs each example of this page. With no argument, it lists them.