gocloud.dev: gocloud.dev/runtimevar/gcpruntimeconfig Index | Examples | Files

package gcpruntimeconfig

import "gocloud.dev/runtimevar/gcpruntimeconfig"

Package gcpruntimeconfig provides a runtimevar implementation with variables read from GCP Cloud Runtime Configurator (https://cloud.google.com/deployment-manager/runtime-configurator). Use OpenVariable to construct a *runtimevar.Variable.

URLs

For runtimevar.OpenVariable, gcpruntimeconfig registers for the scheme "gcpruntimeconfig". The default URL opener will creating a connection using use default credentials from the environment, as described in https://cloud.google.com/docs/authentication/production. To customize the URL opener, or for more details on the URL format, see URLOpener. See https://gocloud.dev/concepts/urls/ for background information.

As

gcpruntimeconfig exposes the following types for As:

- Snapshot: *pb.Variable
- Error: *status.Status

Code:

// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
// PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/runtimevar/gcpruntimeconfig"
// PRAGMA: On gocloud.dev, hide lines until the next blank line.
ctx := context.Background()

// runtimevar.OpenVariable creates a *runtimevar.Variable from a URL.
// The URL Host+Path are used as the GCP Runtime Configurator Variable key;
// see https://cloud.google.com/deployment-manager/runtime-configurator/
// for more details.

v, err := runtimevar.OpenVariable(ctx, "gcpruntimeconfig://projects/myproject/configs/myconfigid/variables/myvar?decoder=string")
if err != nil {
    log.Fatal(err)
}
defer v.Close()

Index

Examples

Package Files

gcpruntimeconfig.go

Constants

const Scheme = "gcpruntimeconfig"

Scheme is the URL scheme gcpruntimeconfig registers its URLOpener under on runtimevar.DefaultMux.

Variables

var Set = wire.NewSet(
    Dial,
    wire.Struct(new(URLOpener), "Client"),
)

Set holds Wire providers for this package.

func Dial Uses

func Dial(ctx context.Context, ts gcp.TokenSource) (pb.RuntimeConfigManagerClient, func(), error)

Dial opens a gRPC connection to the Runtime Configurator API using credentials from ts. It is provided as an optional helper with useful defaults.

The second return value is a function that should be called to clean up the connection opened by Dial.

func OpenVariable Uses

func OpenVariable(client pb.RuntimeConfigManagerClient, variableKey string, decoder *runtimevar.Decoder, opts *Options) (*runtimevar.Variable, error)

OpenVariable constructs a *runtimevar.Variable backed by variableKey in GCP Cloud Runtime Configurator.

A variableKey will look like:

projects/[project_id]/configs/[CONFIG_ID]/variables/[VARIABLE_NAME]

You can use the full string (e.g., copied from the GCP Console), or construct one from its parts using VariableKey.

See https://cloud.google.com/deployment-manager/runtime-configurator/ for more details.

Runtime Configurator returns raw bytes; provide a decoder to decode the raw bytes into the appropriate type for runtimevar.Snapshot.Value. See the runtimevar package documentation for examples of decoders.

Code:

// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
// PRAGMA: On gocloud.dev, hide lines until the next blank line.
ctx := context.Background()

// Your GCP credentials.
// See https://cloud.google.com/docs/authentication/production
// for more info on alternatives.
creds, err := gcp.DefaultCredentials(ctx)
if err != nil {
    log.Fatal(err)
}

// Connect to the Runtime Configurator service.
client, cleanup, err := gcpruntimeconfig.Dial(ctx, creds.TokenSource)
if err != nil {
    log.Fatal(err)
}
defer cleanup()

// You can use the VariableKey helper to construct a Variable key from
// your project ID, config ID, and the variable name; alternatively,
// you can construct the full string yourself (e.g.,
// "projects/gcp-project-id/configs/config-id/variables/variable-name").
// See https://cloud.google.com/deployment-manager/runtime-configurator/
// for more details.
//
// For this example, the GCP Cloud Runtime Configurator variable being
// referenced should have a JSON string that decodes into MyConfig.
variableKey := gcpruntimeconfig.VariableKey("gcp-project-id", "config-id", "variable-name")

// Construct a *runtimevar.Variable that watches the variable.
v, err := gcpruntimeconfig.OpenVariable(client, variableKey, runtimevar.StringDecoder, nil)
if err != nil {
    log.Fatal(err)
}
defer v.Close()

func VariableKey Uses

func VariableKey(projectID gcp.ProjectID, configID, variableName string) string

VariableKey constructs a GCP Runtime Configurator variable key from component parts. See https://cloud.google.com/deployment-manager/runtime-configurator/ for more details.

type Options Uses

type Options struct {
    // WaitDuration controls the rate at which Parameter Store is polled.
    // Defaults to 30 seconds.
    WaitDuration time.Duration
}

Options sets options.

type URLOpener Uses

type URLOpener struct {
    // Client must be set to a non-nil client authenticated with
    // Cloud RuntimeConfigurator scope or equivalent.
    Client pb.RuntimeConfigManagerClient

    // Decoder specifies the decoder to use if one is not specified in the URL.
    // Defaults to runtimevar.BytesDecoder.
    Decoder *runtimevar.Decoder

    // Options specifies the options to pass to New.
    Options Options
}

URLOpener opens gcpruntimeconfig URLs like "gcpruntimeconfig://projects/[project_id]/configs/[CONFIG_ID]/variables/[VARIABLE_NAME]".

The URL Host+Path are used as the GCP Runtime Configurator Variable key; see https://cloud.google.com/deployment-manager/runtime-configurator/ for more details.

The following query parameters are supported:

- decoder: The decoder to use. Defaults to URLOpener.Decoder, or
    runtimevar.BytesDecoder if URLOpener.Decoder is nil.
    See runtimevar.DecoderByName for supported values.

func (*URLOpener) OpenVariableURL Uses

func (o *URLOpener) OpenVariableURL(ctx context.Context, u *url.URL) (*runtimevar.Variable, error)

OpenVariableURL opens a gcpruntimeconfig Variable for u.

Package gcpruntimeconfig imports 22 packages (graph) and is imported by 5 packages. Updated 2019-10-16. Refresh now. Tools for package owners.