Skip to main content

Interface

c.Interface calls the API's Interface group. The network interfaces of the host.

Methods​

MethodReturnsPermissionDescription
List(ctx, hostname)*Response[Collection[InterfaceEntry]]interface:readList the network interfaces of the host.
Get(ctx, hostname, name)*Response[Collection[InterfaceEntry]]interface:readGet a network interface of the host.
Create(ctx, hostname, input)*Response[Collection[InterfaceResult]]interface:writeConfigure a network interface.
Update(ctx, hostname, input)*Response[Collection[InterfaceResult]]interface:writeChange the config osapi has of a network interface.
Delete(ctx, hostname, name)*Response[Collection[InterfaceResult]]interface:writeDrop the config osapi has of a network interface.

List​

func (s *InterfaceService) List(
ctx context.Context,
hostname string,
) (*Response[Collection[InterfaceEntry]], error)

List the network interfaces of the host.

Reads each interface but the loopback, in the order of its index: /sys/class/net and ip on Linux, ifconfig and netstat on macOS. dhcp4 is true when a route of the interface has proto dhcp, and primary when it has the default route.

It sends GET /api/node/{hostname}/interface, and needs the permission interface:read.

The examples of the method's definition:

A Linux host, go run interface.go list:

resp, err := c.Interface.List(ctx, target)

Get​

func (s *InterfaceService) Get(
ctx context.Context,
hostname string,
name string,
) (*Response[Collection[InterfaceEntry]], error)

Get a network interface of the host.

Reads the interface as list does.

It sends GET /api/node/{hostname}/interface/{name}, and needs the permission interface:read.

The examples of the method's definition:

An interface with a DHCP address, go run interface.go get:

resp, err := c.Interface.Get(ctx, target, "eth0")

A Mac, go run interface.go get-2:

resp, err := c.Interface.Get(ctx, target, "en0")

Create​

func (s *InterfaceService) Create(
ctx context.Context,
hostname string,
input InterfaceCreateInput,
) (*Response[Collection[InterfaceResult]], error)

Configure a network interface.

Writes a netplan file of the interface's own, such as /etc/netplan/osapi-osapi0.yaml for osapi0, under a netplan definition that is the interface's own, and applies it. type is ethernet, the default, or dummy. An ethernet is an interface the host has, in the netplan section that netplan status gives for it: one the host does not have is not found. A dummy goes under dummy-devices, and netplan apply makes it, so the host need not have it. dummy-devices needs netplan 0.107 or later. dhcp4 and dhcp6 are false unless given, so the interface has only the addresses given. An interface that a match defines, such as cloud-init's all for en*, is refused, because the config would change each interface the match takes, now and later. An interface osapi already configures does not change: update replaces its config. It runs on the Debian family, and elsewhere, and in a container, reports skipped.

It sends POST /api/node/{hostname}/interface/{name}, and needs the permission interface:write.

The examples of the method's definition:

A dummy interface with a static address, go run interface.go create:

resp, err := c.Interface.Create(ctx, target, client.InterfaceCreateInput{
Addresses: []string{"10.0.1.5/24"},
Name: "osapi0",
Type: types.InterfaceType("dummy"),
})

Each setting, go run interface.go create-2:

resp, err := c.Interface.Create(ctx, target, client.InterfaceCreateInput{
Addresses: []string{"2001:db8::5/64"},
DHCP6: true,
MACAddress: "52:54:00:aa:bb:cc",
MTU: 9000,
Name: "osapi0",
Type: types.InterfaceType("dummy"),
})

An interface the host has, go run interface.go create-3:

resp, err := c.Interface.Create(ctx, target, client.InterfaceCreateInput{
DHCP4: true,
Name: "eth1",
Wakeonlan: true,
})

Update​

func (s *InterfaceService) Update(
ctx context.Context,
hostname string,
input InterfaceUpdateInput,
) (*Response[Collection[InterfaceResult]], error)

Change the config osapi has of a network interface.

Writes the interface's netplan file over the one osapi has, as create writes it, and applies it. Without type dummy, the config goes in the section that netplan status gives, which is dummy-devices for a dummy, so a dummy stays a dummy. An interface osapi does not configure is not found: create configures it.

It sends PUT /api/node/{hostname}/interface/{name}, and needs the permission interface:write.

The examples of the method's definition:

Move an interface to DHCP, go run interface.go update:

resp, err := c.Interface.Update(ctx, target, client.InterfaceUpdateInput{
DHCP4: true,
Name: "osapi0",
Type: types.InterfaceType("dummy"),
})

Delete​

func (s *InterfaceService) Delete(
ctx context.Context,
hostname string,
name string,
) (*Response[Collection[InterfaceResult]], error)

Drop the config osapi has of a network interface.

Removes the interface's netplan file and applies what is left. netplan apply leaves the link of a dummy, so delete then deletes the link with ip link delete. An interface osapi does not configure does not change.

It sends DELETE /api/node/{hostname}/interface/{name}, and needs the permission interface:write.

The examples of the method's definition:

A dummy interface osapi configures, go run interface.go delete:

resp, err := c.Interface.Delete(ctx, target, "osapi0")

Types​

The fields of each type are the properties of its schema in the API's spec.

InterfaceCreateInput​

FieldTypeDescription
NamestringThe name of the path. The interface, such as eth0, or a fact such as @fact.interface.primary.
Addresses[]stringThe static addresses, in CIDR notation, such as 10.0.0.5/24.
DHCP4boolTake an IPv4 address from DHCP.
DHCP6boolTake an IPv6 address from DHCP.
Gateway4stringThe IPv4 default gateway.
Gateway6stringThe IPv6 default gateway.
MACAddressstringThe MAC address to set.
MTUintThe MTU, from 68 to 9000.
TypeInterfaceTypeInterfaceType is the type of a network interface that osapi manages.
WakeonlanboolTurn on wake-on-LAN.

InterfaceEntry​

FieldTypeDescription
Addresses[]stringEach of its addresses, in CIDR notation.
DHCP4boolWhether DHCP gave it an IPv4 address: a route of it has proto dhcp.
ErrorstringError message if the agent failed.
HostnamestringHostname of the agent that produced this result.
IPv4stringIts first IPv4 address that is not link-local.
IPv6stringIts first IPv6 address that is not link-local.
MACstringIts MAC address; empty for one without, such as lo.
MTUintIts MTU.
NamestringThe interface.
PrimaryboolWhether it has the default route.
Statestringup or down.
StatusRowStatusWhat 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.

InterfaceResult​

FieldTypeDescription
ChangedboolWhether the host changed.
ErrorstringError message if the agent failed.
HostnamestringHostname of the agent that produced this result.
NamestringThe interface.
StatusRowStatusWhat 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.

InterfaceType​

InterfaceType is the type of a network interface that osapi manages.

A string, one of ethernet, dummy.

InterfaceUpdateInput​

FieldTypeDescription
NamestringThe name of the path. The interface, such as eth0, or a fact such as @fact.interface.primary.
Addresses[]stringThe static addresses, in CIDR notation, such as 10.0.0.5/24.
DHCP4boolTake an IPv4 address from DHCP.
DHCP6boolTake an IPv6 address from DHCP.
Gateway4stringThe IPv4 default gateway.
Gateway6stringThe IPv6 default gateway.
MACAddressstringThe MAC address to set.
MTUintThe MTU, from 68 to 9000.
TypeInterfaceTypeInterfaceType is the type of a network interface that osapi manages.
WakeonlanboolTurn on wake-on-LAN.

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.go runs each example of this page. With no argument, it lists them.