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:

// runtimevar.OpenVariable creates a *runtimevar.Variable from a URL.
ctx := context.Background()
v, err := runtimevar.OpenVariable(ctx, "gcpruntimeconfig://myproject/myconfigid/myvar?decoder=string")
if err != nil {
    log.Fatal(err)
}

snapshot, err := v.Latest(ctx)
_, _ = snapshot, err

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,
    URLOpener{},
)

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, name ResourceName, decoder *runtimevar.Decoder, opts *Options) (*runtimevar.Variable, error)

OpenVariable constructs a *runtimevar.Variable backed by the variable name in GCP Cloud Runtime Configurator. 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:

// Your GCP credentials.
// See https://cloud.google.com/docs/authentication/production
// for more info on alternatives.
ctx := context.Background()
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()

// Create a decoder for decoding JSON strings into MyConfig.
decoder := runtimevar.NewDecoder(MyConfig{}, runtimevar.JSONDecode)

// Fill these in with the values from the Cloud Console.
// For this example, the GCP Cloud Runtime Configurator variable being
// referenced should have a JSON string that decodes into MyConfig.
name := gcpruntimeconfig.ResourceName{
    ProjectID: "gcp-project-id",
    Config:    "cfg-name",
    Variable:  "cfg-variable-name",
}

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

// We can now read the current value of the variable from v.
snapshot, err := v.Latest(context.Background())
if err != nil {
    log.Fatal(err)
}
cfg := snapshot.Value.(MyConfig)
_ = cfg

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 ResourceName Uses

type ResourceName struct {
    ProjectID string
    Config    string
    Variable  string
}

ResourceName identifies a configuration variable.

func (ResourceName) String Uses

func (r ResourceName) String() string

String returns the full configuration variable path.

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://myproject/mycfg/myvar".

- The URL's host holds the GCP projectID.
- The first element of the URL's path holds the GCP RuntimeConfigurator ConfigID.
- The second element of the URL's path holds the GCP RuntimeConfigurator Variable Name.

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 4 packages. Updated 2019-05-16. Refresh now. Tools for package owners.