Skip to main content

Client Library

The osapi package provides a typed Go client for the OSAPI REST API. Create a client with New() and use domain-specific services to interact with the API.

Quick Start​

import "github.com/osapi-io/osapi/pkg/sdk/client"

c := client.New("https://localhost:8080", "your-jwt-token")

resp, err := c.Hostname.Get(ctx, "_any")

How the services are made​

internal/sdk/gen writes the services and their types from the spec of the API, in just sdk-generate. Each group of the API is a service, and each operation is a method:

  • The service has the words of the group's path, such as NetworkDNS for /api/node/{hostname}/network/dns.
  • The method has the name of the method in the group's definition, such as Get. The CLI command has the same name.
  • A type of an answer has the name of its schema, such as NetworkDnsEntry. A {job_id, results} answer is a Collection of its item.
  • A method with a body takes the target, then one input struct, such as NetworkDNSUpdateInput: the keys of the path and the fields of the body.
  • A method with no body takes the parameters of the path, then a struct of the parameters of the query that a request can leave out.
  • Each answer outside 2xx is one of the error types of the SDK.

The methods that upload a file, File.Upload and File.Update, and File.GetChanged are written by hand.

The same run writes a program for each service, examples/sdk/client/<service>.go. It calls each method with the input of each example of the method's definition, and prints the answer. just examples-check compiles each program, so an example cannot go stale.

Services​

Each service has a page, which internal/sdk/gen writes from the same model: its methods with their signatures, doc comments and permissions, the code of each example of the method's definition, and its types. The services of a node act on the hosts of a target. The services of the controller act on the controller itself.

The pages are in a folder for each area, such as Networking or Fleet. The UI's Behaviors page has the same areas, in the same order. Each behavior and each endpoint group declares its area in Go, and internal/area gives the label and the order of each area. The spec carries them, and the generator reads them from the spec.

Services​

ServiceDescription
ServiceThe systemd services of the host.

Software​

ServiceDescription
PackageThe packages installed on the host.
PackageUpdatesThe updates the package index has for the host.

Config​

ServiceDescription
HostnameGet or set the hostname of target hosts.
NTPThe NTP servers of chrony, and its sync status.
NodeFileFiles of the object store, deployed to the host.
ScheduleThe cron entries that osapi deploys.
SysctlKernel parameters, written to sysctl.d files and applied.
TimezoneThe timezone of the host.

Node​

ServiceDescription
LoadGet the load averages of target hosts.
OSGet the operating system of target hosts.
PowerReboot or shut down the host.
ProcessThe processes of the host.
StatusThe status summary of a node.
UptimeGet the uptime of target hosts.

Logs​

ServiceDescription
LogThe entries of the systemd journal.
LogSourceThe sources that write to the systemd journal.

Networking​

ServiceDescription
ConnectivityWhether the host reaches an address, by ICMP ping.
DNSThe DNS resolvers of each interface of the host.
InterfaceThe network interfaces of the host.
InterfaceRouteThe routes of an interface.

Security​

ServiceDescription
CertificateThe certificate authorities that the host trusts.
GroupThe groups of the host.
UserThe users of the host.
UserPasswordThe password of a user.
UserSSHKeyThe authorized SSH keys of a user.

Containers​

ServiceDescription
DockerThe Docker containers of the host.
ImageThe Docker images of the host.

Object Store​

ServiceDescription
FileObject Store file management operations.

Secrets​

ServiceDescription
SecretThe vaults of the controller and the metadata of their keys. No operation gives a value.

Command​

ServiceDescription
CommandRun a command on target hosts.

Hardware​

ServiceDescription
DiskGet the disk usage of target hosts.
MemoryGet the memory usage of target hosts.

Audit​

ServiceDescription
AuditAudit log endpoints for viewing API activity.

Fleet​

ServiceDescription
AdmissionAdmit, reject and revoke the nodes that ask to join.
AgentOperations related to agent discovery and details.
CatalogThe behaviors, the permissions and the roles that the controller serves, for the UI.
HealthHealth check endpoints for liveness, readiness, and detailed status.
JobOperations related to the job queue.
TelemetryTelemetry through the controller, such as the spans of a trace.

Client Options​

OptionDescription
WithLogger(logger)Set custom slog.Logger
WithHTTPTransport(transport)Set custom http.RoundTripper

WithLogger defaults to slog.Default(). WithHTTPTransport sets the base transport for HTTP requests.

Targeting​

Most operations accept a target parameter:

TargetBehavior
_anySend to any available agent (load balanced)
_allBroadcast to every agent
hostnameSend to a specific host
key:valueSend to agents matching a label