infra

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2019 License: Apache-2.0 Imports: 10 Imported by: 26

README

API Reference

Infra

Package infra provides cloud infrastructure management for Go programs. The package includes facilities for configuring, provisioning, and migrating cloud infrastructure that is used by a Go program. You can think of infra as a simple, embedded version of Terraform, combined with a self-contained dependency injection framework.

Infrastructure managed by this package is exposed through a configuration. Configurations specify which providers should be used for which infrastructure component; configurations also store provider configuration and migration state. Users instantiate typed values directly from the configuration: the details of configuration and of managing dependencies between infrastructure components is handled by the config object itself. Configurations are marshaled and must be stored by the user.

Infrastructure migration is handled by maintaining a set of versions for each provider; migrations perform side-effects and can modify the configuration accordingly (e.g., to store identifiers used by the cloud infrastructure provider).

Documentation

Overview

Package infra provides cloud infrastructure management for Go programs. The package includes facilities for configuring, provisioning, and migrating cloud infrastructure that is used by a Go program. You can think of infra as a simple, embedded version of Terraform, combined with a self-contained dependency injection framework.

Infrastructure managed by this package is exposed through a configuration. Configurations specify which providers should be used for which infrastructure component; configurations also store provider configuration and migration state. Users instantiate typed values directly from the configuration: the details of configuration and of managing dependencies between infrastructure components is handled by the config object itself. Configurations are marshaled and must be stored by the user.

Infrastructure migration is handled by maintaining a set of versions for each provider; migrations perform side-effects and can modify the configuration accordingly (e.g., to store identifiers used by the cloud infrastructure provider).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(name string, iface interface{})

Register registers a provider for the given key. Key name may only contain characters a-z, 0-9, _ or -. Providers must be a "defined" type -- i.e., they must be named (they cannot be struct{}, int, string, etc.).

Register uses only the type of the provided object (which should be zero-valued): every config creates a new instance for exclusive use by that configuration. If iface is a pointer, then each config allocates a fresh instance. The registered value is the value managed by this provider. Management functions are provided by methods implemented directly on the value; they are one or more of the following:

	// Init initializes the value using the provided requirements, which are
	// themselves provisioned by the current configuration.
	Init(req1 type1, req2 type2, ...) error

	// Flags registers provider (instance) parameters in the provided
	// FlagSet. Flag values are set before the value is used, initialized,
	// or setup.
	Flags(flags *flag.FlagSet)

	// Setup performs infrastructure setup using the given requirements
	// which are themselves provisioned by the config that manages the
	// value.
	Setup(req1 type1, req2 type2, ...) error

	// Version returns the provider's version. Managed infrastructure
	// is considered out of date if the currently configured version
	// is less than the returned version. (Configured versions start
	// at 0.) If Version is missing, then the version is taken to be
	// 0.
	Version() int

	// Config returns the provider's configuration. The configuration
	// is marshaled from and unmarshaled into the returned value.
	// Configurations are restored before calls to Init or Setup.
	Config() interface{}

	// InstanceConfig returns the provider's instance configuration.
	// This is used to marshal and unmarshal the specific instance (as
	// initialized by Init) so that it may be restored later.
	InstanceConfig() interface{}

 // Help returns the help text for the provider.
 Help() string

Types

type Config

type Config struct {
	Keys
	// contains filtered or unexported fields
}

A Config manages a concrete configuration of infrastructure providers. Configs are instantiated from a Schema, which also performs validation. Configurations are responsible for mapping and configuring concrete instances into the types specified by the schema.

func (Config) Help

func (c Config) Help() map[string][]Usage

Help returns Usages, organized by schema keys.

func (Config) Instance

func (c Config) Instance(ptr interface{}) error

Instance stores the configuration-managed instance into the provided pointer. Instance panics if ptr is not pointer-typed. Instance returns an error if no providers are configured for the requested type, or if the provider's initialization failed.

func (Config) Marshal

func (c Config) Marshal(instances bool) ([]byte, error)

Marshal marshals the configuration's using YAML and returns the marshaled content. The configuration can thus be persisted and restored with Schema.Unmarshal. If instances is true, then the instance configuration is marshaled as well, so that they may be restored.

func (Config) Must

func (c Config) Must(ptr interface{})

Must stores the configuration-managed instance into the provider pointer, as in Instance. Must fails fatally if any errors occur.

func (Config) Setup

func (c Config) Setup() error

Setup performs any required provider setup actions implied by this configuration. The configuration may be marshaled in the process and the caller should (re-)marshal the configuration after setup completes.

type Flag

type Flag struct {
	// Name of the flag
	Name string
	// Default Value if non empty.
	DefaultValue string
	// Help is the help text for the flag.
	Help string
}

Flag is a provider flag.

type Keys

type Keys map[string]interface{}

Keys holds the toplevel configuration keys as managed by a Keys. Each config instance defines a provider for this type to be used by other providers that may need to access the raw configuration (e.g., common config values).

func (Keys) Clone

func (k Keys) Clone() Keys

Clone returns a deeply-copied version of keys.

func (Keys) Int

func (k Keys) Int(key string) (int, bool, error)

Int returns the integer value of the provided key.

func (Keys) Keys

func (k Keys) Keys(key string) (Keys, bool, error)

Keys returns the Keys value of the provided key.

func (Keys) String

func (k Keys) String(key string) (string, bool, error)

String returns the string value of the provided key.

func (Keys) Value

func (k Keys) Value(key string) interface{}

Value returns the value associated with the provided key.

type Schema

type Schema map[string]interface{}

A Schema defines a mapping between configuration keys and the types of values provided by those configuration keys. For example, the key "cluster" may provide values of the type "cluster.Interface". Schemas themselves are represented by strings to zero values of the mapped type. Interface types should use a pointer to a zero value. The following schema defines a mapping between to two interface types and a value type.

type Cluster interface { ... }
type BlobStore interface { ... }
type User string

var schema = infra.Schema{
	"cluster": new(Cluster),
	"repository": new(BlobStore),
	"user": User(""),
}

Schemas must be bijective: multiple keys cannot map to the same type.

func (Schema) Make

func (s Schema) Make(keys Keys) (Config, error)

Make builds a new configuration based on the Schema s with the provided configuration keys. Make ensures that the configuration is well-formed: that there are no dependency cycles and that all needed dependencies are satisfied. Make panics if the schema is not a bijection.

Make performs all necessary type checking, ensuring that the schema is valid and that the configured providers are type-compatible with the keys laid out in the schema. Besides exact matches, where the schema type matches the provider type exactly, the following type conversions are allowable:

- the provider type is assignable to the schema type (e.g., the schema type is an interface that is implemented by the provider); or - the provider type is a struct (or pointer to struct) that contains an embedded field that is assignable to the schema type.

func (Schema) Unmarshal

func (s Schema) Unmarshal(p []byte) (Config, error)

Unmarshal unmarshals the configuration keys in the YAML-formatted byte buffer p. The configuration is then initialized with Make.

type Usage

type Usage struct {
	// Name of the provider.
	Name string
	// Usage information of the provider.
	Usage string
	// Args to the provider.
	Args []Flag
}

Usage contains the usage information of the provider

Directories

Path Synopsis
Package AWS defines infrastructure providers for AWS configurations.
Package AWS defines infrastructure providers for AWS configurations.
Package tls implements TLS infrastructure providers.
Package tls implements TLS infrastructure providers.

Jump to

Keyboard shortcuts

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