Debian Codegraph @

/

debian golang-github-linode-linodego 1.55.0-1debian golang-github-linode-linodego-dev gomod github.com/linode/linodego shell shell-project 0.0.0 debiangogoinijsonmakemakefilemarkdownpythonshellyaml 943 files · 4377 symbols
View dependency graph

Depends on

README (full)

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 test-unit to run the unit tests.

Run make test-int 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 TEST_ARGS="-run TestListVolumes" fixtures.

Discussion / Help

Join us at #linodego on the gophers slack

License

MIT License

dir .github/
dir debian/
dir e2e_scripts/
dir internal/
dir k8s/
dir scripts/
dir test/
file .gitignore
file .gitmodules
file .golangci.yml
file CODEOWNERS
file LICENSE
file Makefile
file README.md
file account.go
file account_agreements.go
file account_availability.go
file account_betas.go
file account_child.go
file account_events.go
file account_invoices.go
file account_logins.go
file account_maintenance.go
file account_notifications.go
file account_oauth_client.go
file account_payment_methods.go
file account_payments.go
file account_promo_credits.go
file account_service_transfer.go
file account_settings.go
file account_transfer.go
file account_user_grants.go
file account_users.go
file base_types.go
file betas.go
file client.go
file client_http.go
file client_monitor.go
file client_test.go
file config.go
file config_test.go
file databases.go
file domain_records.go
file domains.go
file env.sample
file errors.go
file errors_test.go
file filter.go
file filter_test.go
file firewall_devices.go
file firewall_rules.go
file firewalls.go
file go.mod
file go.sum
file go.work
file images.go
file instance_config_interfaces.go
file instance_configs.go
file instance_disks.go
file instance_firewalls.go
file instance_ips.go
file instance_nodebalancers.go
file instance_snapshots.go
file instance_stats.go
file instance_volumes.go
file instances.go
file kernels.go
file lke_cluster_pools.go
file lke_clusters.go
file lke_clusters_control_plane.go
file lke_node_pools.go
file lke_types.go
file logger.go
file longview.go
file longview_subscriptions.go
file maintenance_policy.go
file monitor_api_services.go
file monitor_dashboards.go
file monitor_metrics_definitions.go
file monitor_services.go
file monitor_services_create_token.go
file mysql.go
file network_ips.go
file network_pools.go
file network_ranges.go
file network_reserved_ips.go
file network_transfer_prices.go
file nodebalancer.go
file nodebalancer_config_nodes.go
file nodebalancer_config_vpc.go
file nodebalancer_configs.go
file nodebalancer_firewalls.go
file nodebalancer_stats.go
file nodebalancer_types.go
file object_storage.go
file object_storage_bucket_certs.go
file object_storage_buckets.go
file object_storage_clusters.go
file object_storage_endpoints.go
file object_storage_keys.go
file object_storage_object.go
file object_storage_quota.go
file paged_response_structs.go
file pagination.go
file pagination_test.go
file placement_groups.go
file pointer_helpers.go
file pointer_helpers_test.go
file postgres.go
file profile.go
file profile_apps.go
file profile_devices.go
file profile_grants_list.go
file profile_logins.go
file profile_phone_number.go
file profile_preferences.go
file profile_security_questions.go
file profile_sshkeys.go
file profile_tfa.go
file profile_tokens.go
file regions.go
file regions_availability.go
file request_helpers.go
file request_helpers_test.go
file retries.go
file retries_http.go
file retries_http_test.go
file retries_test.go
file stackscripts.go
file support.go
file tags.go
file types.go
file version.go
file vlans.go
file volumes.go
file volumes_types.go
file vpc.go
file vpc_ips.go
file vpc_subnet.go
file waitfor.go