Skip to content

linode/linodego

Folders and files

NameName
Last commit message
Last commit date

Latest commit

1cfb144 · Jan 10, 2025
Jan 9, 2025
Jul 29, 2024
Aug 21, 2024
Jan 10, 2025
Jun 17, 2024
Jan 10, 2025
Jul 10, 2024
Jul 29, 2024
Nov 12, 2024
Jul 6, 2023
Oct 30, 2017
Jun 3, 2024
Jul 18, 2024
Nov 12, 2024
Nov 5, 2024
Jun 21, 2024
Jun 21, 2024
Jun 26, 2024
Nov 12, 2024
Nov 12, 2024
Jun 21, 2024
Nov 5, 2024
Jun 20, 2024
Dec 9, 2024
Nov 6, 2024
Nov 6, 2024
Nov 6, 2024
Dec 9, 2024
Jun 20, 2024
Jun 20, 2024
Dec 2, 2024
Dec 2, 2024
Sep 6, 2024
Jun 25, 2024
Dec 20, 2024
Sep 3, 2024
Dec 20, 2024
Nov 7, 2024
Oct 6, 2023
Jan 7, 2025
Nov 7, 2024
Nov 22, 2024
Sep 9, 2018
Jul 29, 2024
Jul 29, 2024
Jul 6, 2022
Jul 6, 2022
Jun 25, 2024
Jul 8, 2024
Jul 8, 2024
Jan 9, 2025
Jan 9, 2025
Jan 10, 2025
Jan 10, 2025
Nov 27, 2024
Jul 8, 2024
Jul 8, 2024
Nov 13, 2024
Jul 8, 2024
Sep 25, 2024
Nov 13, 2024
Jul 5, 2024
Jul 5, 2024
Jul 5, 2024
Jan 10, 2025
Jul 19, 2024
Jul 10, 2024
Nov 28, 2024
Nov 19, 2024
Oct 31, 2024
Sep 6, 2024
Aug 2, 2024
Jul 10, 2024
Jul 10, 2024
Jan 7, 2025
Dec 13, 2024
Jul 8, 2024
Jul 8, 2024
Sep 17, 2024
Sep 6, 2024
Jul 8, 2024
Jul 8, 2024
Jul 8, 2024
Jul 11, 2024
Jul 11, 2024
Sep 6, 2024
Jul 11, 2024
Jul 11, 2024
Dec 9, 2024
Jul 11, 2024
Jul 15, 2024
Jul 15, 2024
Jul 19, 2024
Aug 21, 2024
Feb 26, 2024
Dec 6, 2024
Jul 5, 2024
Jul 5, 2024
Jan 7, 2025
Nov 12, 2024
Nov 18, 2024
Nov 18, 2024
Jul 15, 2024
Jul 15, 2024
Jul 19, 2024
Nov 18, 2024
Jul 19, 2024
Jul 19, 2024
Jul 19, 2024
Jul 19, 2024
Jan 10, 2025
Jul 19, 2024
Jul 5, 2024
Jun 28, 2024
May 6, 2024
Aug 16, 2024
Aug 16, 2024
Sep 10, 2021
Jul 19, 2024
Nov 12, 2024
Jul 19, 2024
Jan 10, 2025
May 12, 2021
Jul 19, 2024
Dec 4, 2024
Sep 6, 2024
Jul 15, 2024
Apr 10, 2024
Jul 15, 2024
Jan 7, 2025

Repository files navigation

linodego

Tests Release GoDoc Go Report Card

Go client for Linode REST v4 API

Installation

go get -u github.com/linode/linodego

Documentation

See godoc for a complete reference.

The API generally follows the naming patterns prescribed in the OpenAPIv3 document for Linode APIv4.

Deviations in naming have been made to avoid using "Linode" and "Instance" redundantly or inconsistently.

A brief summary of the features offered in this API client are shown here.

Examples

General Usage

package main

import (
	"context"
	"fmt"
	"log"
	"net/http"
	"os"

	"github.com/linode/linodego"
	"golang.org/x/oauth2"
)

func main() {
	apiKey, ok := os.LookupEnv("LINODE_TOKEN")
	if !ok {
		log.Fatal("Could not find LINODE_TOKEN, please assert it is set.")
	}
	tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: apiKey})

	oauth2Client := &http.Client{
		Transport: &oauth2.Transport{
			Source: tokenSource,
		},
	}

	linodeClient := linodego.NewClient(oauth2Client)
	linodeClient.SetDebug(true)

	res, err := linodeClient.GetInstance(context.Background(), 4090913)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%v", res)
}

Pagination

Auto-Pagination Requests

kernels, err := linodego.ListKernels(context.Background(), nil)
// len(kernels) == 218

Or, use a page value of "0":

opts := linodego.NewListOptions(0,"")
kernels, err := linodego.ListKernels(context.Background(), opts)
// len(kernels) == 218

Single Page

opts := linodego.NewListOptions(2,"")
// or opts := linodego.ListOptions{PageOptions: &linodego.PageOptions{Page: 2}, PageSize: 500}
kernels, err := linodego.ListKernels(context.Background(), opts)
// len(kernels) == 100

ListOptions are supplied as a pointer because the Pages and Results values are set in the supplied ListOptions.

// opts.Results == 218

NOTES:

  • The ListOptions will be mutated by list endpoint functions.
  • Instances of ListOptions should NOT be shared across multiple list endpoint functions.
  • The resulting number of results and pages can be accessed through the user-supplied ListOptions instance.

Filtering

f := linodego.Filter{}
f.AddField(linodego.Eq, "mine", true)
fStr, err := f.MarshalJSON()
if err != nil {
    log.Fatal(err)
}
opts := linodego.NewListOptions(0, string(fStr))
stackscripts, err := linodego.ListStackscripts(context.Background(), opts)

Error Handling

Getting Single Entities

linode, err := linodego.GetInstance(context.Background(), 555) // any Linode ID that does not exist or is not yours
// linode == nil: true
// err.Error() == "[404] Not Found"
// err.Code == "404"
// err.Message == "Not Found"

Lists

For lists, the list is still returned as [], but err works the same way as on the Get request.

linodes, err := linodego.ListInstances(context.Background(), linodego.NewListOptions(0, "{\"foo\":bar}"))
// linodes == []
// err.Error() == "[400] [X-Filter] Cannot filter on foo"

Otherwise sane requests beyond the last page do not trigger an error, just an empty result:

linodes, err := linodego.ListInstances(context.Background(), linodego.NewListOptions(9999, ""))
// linodes == []
// err = nil

Response Caching

By default, certain endpoints with static responses will be cached into memory. Endpoints with cached responses are identified in their accompanying documentation.

The default cache entry expiry time is 15 minutes. Certain endpoints may override this value to allow for more frequent refreshes (e.g. client.GetRegion(...)). The global cache expiry time can be customized using the client.SetGlobalCacheExpiration(...) method.

Response caching can be globally disabled or enabled for a client using the client.UseCache(...) method.

The global cache can be cleared and refreshed using the client.InvalidateCache() method.

Writes

When performing a POST or PUT request, multiple field related errors will be returned as a single error, currently like:

// err.Error() == "[400] [field1] foo problem; [field2] bar problem; [field3] baz problem"

Tests

Run make testunit to run the unit tests.

Run make testint to run the integration tests. The integration tests use fixtures.

To update the test fixtures, run make fixtures. This will record the API responses into the fixtures/ directory. Be careful about committing any sensitive account details. An attempt has been made to sanitize IP addresses and dates, but no automated sanitization will be performed against fixtures/*Account*.yaml, for example.

To prevent disrupting unaffected fixtures, target fixture generation like so: make ARGS="-run TestListVolumes" fixtures.

Discussion / Help

Join us at #linodego on the gophers slack

License

MIT License