gcp

package
v0.16.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 14 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package gcp is the CLI's Google Cloud client: REST over net/http with the tokens of gcloud (no Google client library), Google's error JSON as *Error, and the waiting of long-running and Compute Engine operations.

Index

Constants

View Source
const InvokeTimeout = 1860 * time.Second

InvokeTimeout bounds one call of Invoke: the controller's longest request (its timeoutSeconds, 1800 s: a wake of up to 1700 s runs inside it) and a margin to read its answer.

Variables

This section is empty.

Functions

func ExecRunner

func ExecRunner(ctx context.Context, name string, args ...string) ([]byte, error)

ExecRunner is the default Runner, on os/exec; a failure carries stderr.

func IsAlreadyExists

func IsAlreadyExists(err error) bool

IsAlreadyExists is a 409 whose status is ALREADY_EXISTS (a 409 may also be ABORTED: the call lost a race).

func IsConflict

func IsConflict(err error) bool

IsConflict is a 409.

func IsDenied

func IsDenied(err error) bool

IsDenied is a 403.

func IsExhausted added in v0.11.0

func IsExhausted(err error) bool

IsExhausted tells a quota or rate limit's refusal, a 429 (after Do's own tries).

func IsNotFound

func IsNotFound(err error) bool

IsNotFound is a 404.

func IsPrecondition

func IsPrecondition(err error) bool

IsPrecondition is a 412, or a 400 whose status is FAILED_PRECONDITION.

func WaitCompute

func WaitCompute(ctx context.Context, c *Client, selfLink string, timeout time.Duration) error

WaitCompute polls a Compute Engine operation's selfLink until it is DONE.

func WaitOperation

func WaitOperation(ctx context.Context, c *Client, url string, every, timeout time.Duration) error

WaitOperation polls a long-running operation until it is done.

Types

type Client

type Client struct {
	HTTP   *http.Client
	Tokens TokenSource
	// Sleep waits between the tries of a read that met a transient error (nil: a timer).
	Sleep func(ctx context.Context, d time.Duration) error
}

Client calls Google Cloud REST APIs as the account of Tokens.

func (*Client) Digest added in v0.14.0

func (c *Client) Digest(ctx context.Context, manifest string, accept ...string) (string, error)

Digest is an anonymous HEAD of an image's manifest on a public registry, asking for the media types of accept: the answer's Docker-Content-Digest. A registry that answers 401 with a Bearer challenge is asked for an anonymous token at the challenge's realm first, as Docker's clients do. A transient answer is tried again as a read is; another non-2xx answer is an *Error.

func (*Client) Do

func (c *Client) Do(ctx context.Context, method, url string, in, out any) error

Do sends in as JSON (when non-nil) and decodes the answer into out (when non-nil). A non-2xx answer is an *Error. A 401 (the token expired before gcloud's cache knew it) drops the cached token and sends the request once more with a new one from gcloud. A read answered 429, 500, 502, 503 or 504 is tried again up to 3 times, after 1 s, 2 s and 4 s; a request that may change something is sent once.

func (*Client) Head

func (c *Client) Head(ctx context.Context, url string, accept ...string) error

Head is Do's HEAD, asking for the media types of accept: nil when the resource is there.

func (*Client) Invoke

func (c *Client) Invoke(ctx context.Context, method, url, audience string, in, out any) error

Invoke is Do with an identity token for audience instead of the access token: a call of a Cloud Run service whose invoker IAM check is on.

A controller call may take long (an offboarding wakes the workload inside its request, up to 1700 s): it is bound by InvokeTimeout, not by the shorter timeout of the client Do uses.

type Error

type Error struct {
	Code       int
	Status     string
	Reason     string
	Message    string
	Permission string
	Service    string
	// Operation is set when the error is the one a long-running operation ended with (done, with an error), not the
	// failure of a call.
	Operation bool
}

Error is a Google Cloud API error. Reason, Permission and Service are from its google.rpc.ErrorInfo: Service is the API a SERVICE_DISABLED names.

func (*Error) Error

func (e *Error) Error() string

type Runner

type Runner func(ctx context.Context, name string, args ...string) ([]byte, error)

Runner runs a command and returns its standard output.

type TokenSource

type TokenSource interface {
	AccessToken(ctx context.Context) (string, error)
	IdentityToken(ctx context.Context, audience string) (string, error)
	Account(ctx context.Context) (string, error)
}

TokenSource gives the credentials of the Google account in use.

func NewGcloud

func NewGcloud(configuration string, run Runner) TokenSource

NewGcloud reads the tokens from gcloud's configuration (empty: the active one). A nil run is ExecRunner.

func OncePerAudience added in v0.15.0

func OncePerAudience(tokens TokenSource) TokenSource

OncePerAudience is tokens with each identity token asked once per audience and kept for the run: a command that calls a router's controller many times (`schedule list`) asks gcloud once, not once per call. Access tokens and the account are tokens' own.

Directories

Path Synopsis
Package access is the manifest of what Dormeto needs in Google Cloud: the APIs, the custom roles, the predefined roles' permissions, the router's grants, the manager's connection (its section manager) and, per `dormeto gcp` command, the permissions the operator needs by step.
Package access is the manifest of what Dormeto needs in Google Cloud: the APIs, the custom roles, the predefined roles' permissions, the router's grants, the manager's connection (its section manager) and, per `dormeto gcp` command, the permissions the operator needs by step.
Package gcptest is a fake Google Cloud for tests: an httptest.Server and an http.Client whose transport sends every request for a *.googleapis.com, *.pkg.dev or *.run.app host to it, so production code keeps its real URLs.
Package gcptest is a fake Google Cloud for tests: an httptest.Server and an http.Client whose transport sends every request for a *.googleapis.com, *.pkg.dev or *.run.app host to it, so production code keeps its real URLs.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL