runtimeconfigurator

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2019 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

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

As

runtimeconfigurator exposes the following types for As:

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

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dial

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 NewVariable added in v0.9.0

func NewVariable(client pb.RuntimeConfigManagerClient, name ResourceName, decoder *runtimevar.Decoder, opts *Options) (*runtimevar.Variable, error)

NewVariable 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.

Example
package main

import (
	"context"
	"fmt"
	"log"

	"gocloud.dev/runtimevar"
	"gocloud.dev/runtimevar/runtimeconfigurator"
	"golang.org/x/oauth2/google"
)

// MyConfig is a sample configuration struct.
type MyConfig struct {
	Server string
	Port   int
}

// jsonCreds is a fake GCP JSON credentials file.
const jsonCreds = `
{
  "type": "service_account",
  "project_id": "my-project-id"
}
`

func main() {
	ctx := context.Background()

	// Get GCP credentials and dial the server.
	// Here we use a fake JSON credentials file, but you could also use
	// gcp.DefaultCredentials(ctx) to use the default GCP credentials from
	// the environment.
	// See https://cloud.google.com/docs/authentication/production
	// for more info on alternatives.
	creds, err := google.CredentialsFromJSON(ctx, []byte(jsonCreds))
	if err != nil {
		log.Fatal(err)
	}
	client, cleanup, err := runtimeconfigurator.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 := runtimeconfigurator.ResourceName{
		ProjectID: "projectID",
		Config:    "configName",
		Variable:  "appConfig",
	}

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

	// You can now read the current value of the variable from v.
	snapshot, err := v.Watch(ctx)
	if err != nil {
		// This is expected due to the fake credentials we used above.
		fmt.Println("Watch failed due to invalid credentials")
		return
	}
	// We'll never get here when running this sample, but the resulting
	// runtimevar.Snapshot.Value would be of type MyConfig.
	log.Printf("Snapshot.Value: %#v", snapshot.Value.(MyConfig))

}
Output:

Watch failed due to invalid credentials

Types

type Options

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

type ResourceName struct {
	ProjectID string
	Config    string
	Variable  string
}

ResourceName identifies a configuration variable.

func (ResourceName) String

func (r ResourceName) String() string

String returns the full configuration variable path.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳