resource

package
v0.0.0-...-1eb86f4 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2019 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package resource contains resource types.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CtyType

func CtyType(t reflect.Type) cty.Type

CtyType converts a reflect type to the cty type system.

The function is essentially the same as gocty.ImpliedType, except nested structs do not require a cty struct tag. Instead, Fields() is used to get the fields of the nested struct.

Panics if the type cannot be converted. In practice this only applies to more complex types, such as functions and slices.

func FieldName

func FieldName(f reflect.StructField) string

FieldName returns the user-facing name of a field.

If the field has a `name:"<fieldname>"` struct tag set, it is returned. Otherwise, the field name is derived from the struct field name by converting it to lower snake case.

Example (Camel)
package main

import (
	"fmt"
	"reflect"

	"github.com/func/func/resource"
)

func main() {
	field := reflect.StructField{
		Name: "DeadLetterConfig",
	}
	got := resource.FieldName(field)
	fmt.Println(got)
}
Output:

dead_letter_config
Example (Camel2)
package main

import (
	"fmt"
	"reflect"

	"github.com/func/func/resource"
)

func main() {
	field := reflect.StructField{
		Name: "KMSKeyArn",
	}
	got := resource.FieldName(field)
	fmt.Println(got)
}
Output:

kms_key_arn
Example (WithCustom)
package main

import (
	"fmt"
	"reflect"

	"github.com/func/func/resource"
)

func main() {
	field := reflect.StructField{
		Name: "RestAPIID",
		Tag:  reflect.StructTag(`name:"rest_api_id"`),
	}
	got := resource.FieldName(field)
	fmt.Println(got)
}
Output:

rest_api_id
Example (WithoutCustom)
package main

import (
	"fmt"
	"reflect"

	"github.com/func/func/resource"
)

func main() {
	field := reflect.StructField{
		Name: "RestAPIID", // Will not split before ID
	}
	got := resource.FieldName(field)
	fmt.Println(got)
}
Output:

rest_apiid

Types

type AuthProvider

type AuthProvider interface {
	AWS() (aws.CredentialsProvider, error)
}

An AuthProvider provides authentication information for provisioning a resource.

type CreateRequest

type CreateRequest struct {
	Auth   AuthProvider
	Source []SourceCode
}

A CreateRequest is passed to a resource's Create method when a new resource is being created.

type Definition

type Definition interface {
	Create(ctx context.Context, req *CreateRequest) error
	Update(ctx context.Context, req *UpdateRequest) error
	Delete(ctx context.Context, req *DeleteRequest) error
}

A Definition describes a resource.

All resources must implement this interface.

type DeleteRequest

type DeleteRequest struct {
	Auth AuthProvider
}

A DeleteRequest is passed to a resource when it is being deleted.

type Dependency

type Dependency struct {
	// Child is the name of the child resource with the dependency.
	Child string

	// Field is the path to the field within the child resource. The Field is
	// relative to the resource's Data.
	Field cty.Path

	// Expression is the expression value to resolve. The expression may refer
	// to multiple parent resources.
	Expression Expression
}

A Dependency is a dependency for a single field between two resources.

func (Dependency) Equals

func (d Dependency) Equals(other Dependency) bool

Equals returns true if two expression are equal.

func (Dependency) Parents

func (d Dependency) Parents() []string

Parents returns the names of the parent resources in the dependency's expression.

type Deployed

type Deployed struct {
	// Desired state that resulted in the deployed resource.
	*Desired

	// ID is a unique id that is assigned to the resource when it has been
	// deployed. The ID uniquely identifies the resource.
	ID string

	// Output contains the outputs from the resource. The value is set after
	// the resource has been provisioned.
	Output cty.Value

	// Deps contains the names of the resources that are dependencies of this
	// resources, that is, one or more field refers to an input or an output in
	// it.
	//
	// Deps are used for traversing the graph backwards when deleting resources.
	Deps []string
}

Deployed is a deployed resource.

type Desired

type Desired struct {
	// Name used in resource config.
	//
	// The Name uniquely identifies the resource within the user's desired
	// resource graph.
	Name string

	// Type used in resource config.
	//
	// The Type determines how to process the resource.
	Type string

	// Input is the user specified static configuration for the resource. The
	// shape of this field will depend on the Type. When creating resources,
	// the creator is responsible for only setting data that is valid for the
	// given resource type.
	Input cty.Value

	// Sources contain the source code hashes that were provided to the
	// resource. The value is only set for resources that have been created.
	Sources []string
}

Desired represents the desired state of a resource.

type EvalContext

type EvalContext struct {
	// Variable values for resolving reference values in the expression.
	Variables map[string]cty.Value
}

An EvalContext provides context for evaluating an expression. See Value() for more details.

type ExprLiteral

type ExprLiteral struct {
	Value cty.Value
}

ExprLiteral is a literal value in an expression.

type ExprReference

type ExprReference struct {
	Path cty.Path
}

ExprReference is a part in an expression that has a reference to another field.

type Expression

type Expression []exprPart

An Expression describes a value for a field.

The Expression may consist of any combination of literals and references. The exprPart interface is closed, only ExprLiteral and ExprReference are allowed.

Example (ForTest)
package main

import (
	"github.com/func/func/resource"
	"github.com/zclconf/go-cty/cty"
)

// Dummy variable to hold the value from Example
var expr resource.Expression

func main() {
	// Construct a new expression for test
	expr = resource.Expression{
		resource.ExprLiteral{Value: cty.StringVal("hello")},
		resource.ExprReference{Path: cty.GetAttrPath("other").GetAttr("output")},
		resource.ExprLiteral{Value: cty.NumberIntVal(123)},
	}

	// expr is equivalent to "hello${other.output}123"
}
Output:

func (Expression) Equals

func (expr Expression) Equals(other Expression) bool

Equals returns true if the expression is equivalent to the other expression.

func (Expression) MergeLiterals

func (expr Expression) MergeLiterals() Expression

MergeLiterals merges consecutive literal values into a single literal. Parts of the expression that are not literals are returned in place as-is.

Example
package main

import (
	"fmt"

	"github.com/func/func/resource"
	"github.com/zclconf/go-cty/cty"
)

func main() {
	input := resource.Expression{
		resource.ExprLiteral{Value: cty.StringVal("foo")},
		resource.ExprLiteral{Value: cty.StringVal("bar")},
		resource.ExprReference{Path: cty.GetAttrPath("abc")},
		resource.ExprLiteral{Value: cty.StringVal("baz")},
		resource.ExprLiteral{Value: cty.StringVal("qux")},
	}

	merged := input.MergeLiterals()

	output := resource.Expression{
		resource.ExprLiteral{Value: cty.StringVal("foobar")},
		resource.ExprReference{Path: cty.GetAttrPath("abc")},
		resource.ExprLiteral{Value: cty.StringVal("bazqux")},
	}

	fmt.Println(output.Equals(merged))
}
Output:

true

func (Expression) References

func (expr Expression) References() []cty.Path

References returns all referenced paths that are found in the expression.

If the returned slice is empty, the expression contains no dynamic references. Such an expression can be evaluated with expr.Value(nil).

func (Expression) Value

func (expr Expression) Value(ctx *EvalContext) (cty.Value, error)

Value evaluates the expression value with the given variables.

The following rules apply:

  • If the expression contains a single literal value, it is returned.
  • If the expression contains a single reference values, the referenced value is extracted from vars and returned.
  • If the expression contains a combination of values, they are concatenated to a string value. Every value in vars must be convertible to string.
  • I an unknown value is encountered, an unknown value is returned. If it was the only part in the expression, the type will match this part. Otherwise, the returned value will be an unknown string.

If the expression contains a reference to a variable that was not set in the ctx, an error is returned.

A nil ctx is equivalent to an EvalContext with no variables, meaning only expressions with static literals can be evaluated.

type Field

type Field struct {
	Index int               // The field's index, relative to the parent struct.
	Type  reflect.Type      // The field's type.
	Tags  map[string]string // Struct tags set on the field, excluding func and name tags.
	// contains filtered or unexported fields
}

A Field represents an extracted field from a struct.

type FieldSet

type FieldSet map[string]Field

A FieldSet contains extracted schema fields.

func Fields

func Fields(target reflect.Type) FieldSet

Fields extracts fields from target. Unexported fields are ignored.

All fields are extracted, regardless if they are marked as an input, output or neither. The returned FieldSet may be further filtered to get the desired fields. The func struct tag is excluded from the Tags in the returned fields.

The name of the field is derived from the struct field name. For example, ExampleField becomes example_field. This can be overridden by setting a `name:"<override>"` tag.

Panics if target is not a struct or a pointer to a struct.

func (FieldSet) CtyType

func (ff FieldSet) CtyType() cty.Type

CtyType converts the FieldSet to a cty object type.

The type is processed deeply, nested structs or pointers to structs are included.

Fields that have interface types are not included as they cannot be represented in the cty type system.

Panics if a field cannot be converted. See ImpliedType() for details.

func (FieldSet) Inputs

func (ff FieldSet) Inputs() FieldSet

Inputs filters the FieldSet and returns all fields that are marked as an input, based on the func:"input" struct tag.

func (FieldSet) Outputs

func (ff FieldSet) Outputs() FieldSet

Outputs filters the FieldSet and returns all fields that are marked as an output, based on the func:"output" struct tag.

type Graph

type Graph struct {
	Resources    []*Desired
	Dependencies []*Dependency
}

A Graph contains a resources and relationships between resources.

func (*Graph) AddDependency

func (g *Graph) AddDependency(dep *Dependency) error

AddDependency adds a dependency to a resource.

The dependency is checked for invalid references to resources (that do not exist). Failing this precondition will return an error. Beyond that, no validation is done on the dependency (such as ensuring the field exists).

func (*Graph) AddResource

func (g *Graph) AddResource(res *Desired) error

AddResource adds a new resource to the graph.

Returns an error if another resource with an identical name already exists.

func (*Graph) DependenciesOf

func (g *Graph) DependenciesOf(child string) []*Dependency

DependenciesOf returns the dependencies for a given child.

func (*Graph) LeafResources

func (g *Graph) LeafResources() []*Desired

LeafResources returns all resources that have no children.

func (*Graph) ParentResources

func (g *Graph) ParentResources(child string) []*Desired

ParentResources returns the parent resources that are are a dependency to the given child resource. In case multiple references exist to the parent resource, it is included only once.

func (*Graph) Resource

func (g *Graph) Resource(name string) *Desired

Resource returns a resource with a given name from the graph. Returns nil if the resource does not exist.

type Registry

type Registry struct {
	// Types contains the types registered in the registry.
	// Outside of tests, the Types map should not be directly accessed.
	Types map[string]reflect.Type
}

A Registry maintains a list of registered resources.

func RegistryFromDefinitions

func RegistryFromDefinitions(defs map[string]Definition) *Registry

RegistryFromDefinitions creates a new registry from a predefined list of resources. It should primarily used in tests to set up a registry.

func (*Registry) Register

func (r *Registry) Register(typename string, def Definition)

Register adds a new resource type.

The graph.Resource interface must be implemented on a pointer receiver on a struct. Panics otherwise. If another resource with the same type is already registered, it is overwritten.

Not safe for concurrent access.

func (*Registry) Type

func (r *Registry) Type(typename string) reflect.Type

Type returns the registered type with a certain name. Returns nil if the type has not been registered.

func (*Registry) Typenames

func (r *Registry) Typenames() []string

Typenames returns the type names that have been registered. The results are lexicographically sorted.

type SourceCode

type SourceCode interface {
	// Digest returns a hash digest.
	Key() string

	// Reader returns a reader to the source tarball.
	Reader(ctx context.Context) (targz io.ReadCloser, err error)
}

SourceCode contains one set of source code, matching a single source entry for the resource.

type UpdateRequest

type UpdateRequest struct {
	Auth     AuthProvider
	Source   []SourceCode
	Previous Definition

	SourceChanged bool
	ConfigChanged bool
}

An UpdateRequest is passed to a resource's Update method when a new resource is being updated.

Previous contains the previous version of the resource. The type for Previous will match the resource type.

func (*UpdateRequest) CreateRequest

func (r *UpdateRequest) CreateRequest() *CreateRequest

CreateRequest converts the update to a Create Request.

func (*UpdateRequest) DeleteRequest

func (r *UpdateRequest) DeleteRequest() *DeleteRequest

DeleteRequest converts the update to a Delete Request.

Directories

Path Synopsis
Package hcldecoder provides a decoder for hcl2 resources that define a graph.
Package hcldecoder provides a decoder for hcl2 resources that define a graph.
Package reconciler reconciles resources in a graph.
Package reconciler reconciles resources in a graph.
internal/task
Package task provides mechanics for de-duplicating task execution.
Package task provides mechanics for de-duplicating task execution.
Package validation provides input validation for resources.
Package validation provides input validation for resources.

Jump to

Keyboard shortcuts

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