SDK Development Guidelines
How the OSAPI Go SDK (pkg/sdk/client) is made, and how to change it.
The SDK document gives the design. This page shows it in use.
The services are generated
internal/sdk/gen writes the typed services and their types from the spec of
the API, internal/controller/api/gen/api.yaml. just sdk-generate runs it,
after go generate writes the spec in internal/behaviors/all. Thus
just generate makes the spec and the SDK in one run.
Do not edit a file that starts with // Code generated by internal/sdk/gen. To
change a method, change the definition of its behavior or its endpoint group,
then run just generate. go-generate-check fails when a generated file is not
what the generator writes.
| File | What it has |
|---|---|
<service>.gen.go | The service, its methods, and the struct of its query |
<service>_types.gen.go | The types that only that service sends and receives |
types.gen.go | The types that more than one service has |
services.gen.go | The struct that holds each service, which Client embeds |
The examples and the pages are generated
The same just sdk-generate run writes a program for each service,
examples/sdk/client/<service>.go. The program calls each method with the input
of each example of the method's definition. The endpoint layer puts these inputs
in the spec, in the x-osapi-examples extension of each operation: it reads the
flags of each CLI example into the parameters and the body. Give the name of an
example to run it, such as go run network_dns.go update. examples-check
compiles each program.
just sdk-docs-generate writes the reference pages under
docs/docs/sidebar/sdk, one for each service in the folder of its area, and the
table of services in library/client.md. A page has the methods of the service
from the SDK's source, with their signatures and doc comments, the permission of
each method from the spec, the code of each example, and the types.
just generate runs it after just sdk-generate.
sdk-gen-check fails when an example or a page is not what the generator
writes. To change one, change the definition, then run just generate.
file_upload.go in examples/sdk/client is the only example that a person
writes: it calls File.Upload, File.Update and File.GetChanged, which the
generator does not write.
Names
The generator takes each name from the spec. It does not keep a list of names.
- A service has the words of the path of its group, such as
NetworkDNSfor/api/node/{hostname}/network/dns. A group of a node that has the name of a group of the controller getsNodein front:NodeFile. - A method has the name of the method in the definition of the group, from
the operation's
x-osapi-methodextension, such asGetorDrain. The CLI command has the same name. - A type of an answer has the name of its schema, such as
NetworkDnsEntry. An input has the name of its service and method, such asNetworkDNSUpdateInput. - A field has the Go name of its property, such as
SearchDomainsforsearch_domains. Initialisms are in capitals:ID,DNS,MTU.
To change a name, change it in the definition. The CLI, the API and the SDK then have the same name.
Signatures
A method with a JSON body takes ctx, the target hostname when the path has
one, and one input struct, <Service><Method>Input. The input has a field for
each other key of the path, with the json tag -, then a field for each
property of the body. The method puts the keys in the path and sends the rest as
the body.
func (s *NetworkDNSService) Update(
ctx context.Context,
hostname string,
input NetworkDNSUpdateInput,
) (*Response[Collection[NetworkDnsResult]], error)
c.NetworkDNS.Update(ctx, "web-01", client.NetworkDNSUpdateInput{
InterfaceName: "eth0",
Servers: []string{"9.9.9.9"},
})
The input is the same set of fields as the behavior's input, and as the flags of its CLI command. Thus a call does not depend on the order of its arguments.
A method with no body takes ctx, each parameter of the path, each parameter of
the query that the request must have, then params: a struct of the parameters
of the query that a request can leave out, such as JobListParams.
func (s *NetworkDNSService) Get(
ctx context.Context,
hostname string,
interfaceName string,
) (*Response[Collection[NetworkDnsEntry]], error)
A method returns *Response[T]. T is the type of the answer of a success. A
{job_id, results} answer is Collection[T] of its item. A method whose answer
has no body, such as a 204, returns only error.
Types
A type has a field for each property of its schema, with a json tag that has
the name of the property. A property that the schema does not require has
omitempty, or omitzero for a struct or a time. Thus a request leaves out a
field that has its zero value.
A field is a value, not a pointer. The API reads a property that is not in the
request as its zero value, so a pointer adds nothing. A uuid is a string,
and a date-time is a time.Time.
A type that a person writes in the package wins. The generator does not write a
type with the name of a type in a file of the package that it did not make. For
example, Step in job_steps.go has a *bool Changed, because a step whose
method has no changed field leaves it out.
What is written by hand
| File | Why it is written by hand |
|---|---|
osapi.go, transport.go | The constructor, the options and the authentication |
request.go | Send and Request, which each method sends through |
response.go | Response[T] and the trace ID |
collection.go | Collection[T] and First |
errors.go, check_error.go | The error types, and how a status becomes one |
file_upload.go | The multipart upload of a file, and GetChanged |
job_steps.go, status.go, operations.go, file_deploy.go, permissions.go | Types and constants that osapi's own packages use |
The generator writes a method only for a JSON body. File.Upload and
File.Update send a multipart form. Thus a person writes them.
Errors
Each method sends through Client.Send. An answer outside 2xx becomes a typed
error, with the message and the reason of the answer's body:
| Status | Error |
|---|---|
| 400 | *ValidationError |
| 401, 403 | *AuthError |
| 404 | *NotFoundError |
| 409 | *ConflictError |
| 500 | *ServerError |
| other | *UnexpectedStatusError |
A body that does not decode into the type of the method is an error that names the method and the path.
Testing
- The hand-written files have 100% coverage, with
httptest.Server. .coverignoreleaves the generated*.gen.gofiles out of the total.send_public_test.gosends a request through generated methods of each shape: a parameter of the path, a body, a query, and an answer with no body.internal/sdk/gen/sdkmodelhas its own tests: the names, the signatures and the types that it makes from a spec.
Consumer guidance
SDK consumers, for example osapi-orchestrator, must do these things:
- Use the SDK's types. Do not define them again.
- Use
Collection.First(), notResults[0]. - Never panic on SDK responses. Always propagate errors.