provider

package module
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: Apache-2.0 Imports: 23 Imported by: 85

README

pulumi-go-provider

Go Report Card

A framework for building Go Providers for Pulumi.

Library documentation can be found at Go Reference

Note: This library is in active development, and not everything is hooked up. You should expect breaking changes as we fine tune the exposed APIs. We definitely appreciate community feedback, but you should probably wait to port any existing providers over.

The highest level of pulumi-go-provider is infer, which derives as much possible from your Go code. The "Hello, Pulumi" example below uses infer. For detailed instructions on building providers with infer, click here.

The "Hello, Pulumi" Provider

Here we provide the code to create an entire native provider consumable from any of the Pulumi languages (TypeScript, Python, Go, C#, Java and Pulumi YAML).

func main() {
	p.RunProvider("greetings", "0.1.0",
		// We tell the provider what resources it needs to support.
		// In this case, a single custom resource.
		infer.Provider(infer.Options{
			Resources: []infer.InferredResource{
				infer.Resource[HelloWorld, HelloWorldArgs, HelloWorldState](),
			},
		}))
}

// Each resource has a controlling struct.
type HelloWorld struct{}

// Each resource has in input struct, defining what arguments it accepts.
type HelloWorldArgs struct {
	// Fields projected into Pulumi must be public and hava a `pulumi:"..."` tag.
	// The pulumi tag doesn't need to match the field name, but its generally a
	// good idea.
	Name string `pulumi:"name"`
	// Fields marked `optional` are optional, so they should have a pointer
	// ahead of their type.
	Loud *bool `pulumi:"loud,optional"`
}

// Each resource has a state, describing the fields that exist on the created resource.
type HelloWorldState struct {
	// It is generally a good idea to embed args in outputs, but it isn't strictly necessary.
	HelloWorldArgs
	// Here we define a required output called message.
	Message string `pulumi:"message"`
}

// All resources must implement Create at a minumum.
func (HelloWorld) Create(
	ctx context.Context, name string, input HelloWorldArgs, preview bool,
) (string, HelloWorldState, error) {
	state := HelloWorldState{HelloWorldArgs: input}
	if preview {
		return name, state, nil
	}
	state.Message = fmt.Sprintf("Hello, %s", input.Name)
	if input.Loud != nil && *input.Loud {
		state.Message = strings.ToUpper(state.Message)
	}
	return name, state, nil
}

The framework is doing a lot of work for us here. Since we didn't implement Diff it is assumed to be structural. The diff will require a replace if any field changes, since we didn't implement Update. Check will confirm that our inputs can be serialized into HelloWorldArgs and Read will do the same. Delete is a no-op.

Library structure

The library is designed to allow as many use cases as possible while still keeping simple things simple. The library comes in 4 parts:

  1. A base abstraction for a Pulumi Provider and the facilities to drive it. This is the Provider interface and the RunProvider function respectively. The rest of the library is written against the Provider interface.
  2. Middleware layers built on top of the Provider interface. Middleware layers handle things like token dispatch, schema generation, cancel propagation, ect.
  3. A testing framework found in the integration folder. This allows unit and integration tests against Providers.
  4. A top layer called infer, which generates full providers from Go types and methods. infer is the expected entry-point into the library. It is the fastest way to get started with a provider in go.[^1]

[^1]: The "Hello, Pulumi" example shows the infer layer.

Generating SDKs and schema

Using Pulumi YAML, you can use the provider as-is. In order to use the provider in other languages, you need to generate at least one SDK. pulumi package gen-sdk ./bin/your-provider will do this, by default for all supported languages. See pulumi package gen-sdk --help for more options.

It's not necessary to export the Pulumi schema to use the provider. If you would like to do so, e.g., for debugging purposes, you can use pulumi package get-schema ./bin/your-provider.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConfigMissingKeys added in v0.5.0

func ConfigMissingKeys(missing map[string]string) error

Provide a structured error for missing provider keys.

func GetSchema added in v0.7.0

func GetSchema(ctx context.Context, name, version string, provider Provider) (schema.PackageSpec, error)

Retrieve the schema from the provider by invoking GetSchema on the provider.

func RawServer added in v0.12.0

func RawServer(
	name, version string,
	provider Provider,
) func(*pprovider.HostClient) (rpc.ResourceProviderServer, error)

RawServer converts the Provider into a factory for gRPC servers.

If you are trying to set up a standard main function, see RunProvider.

func RunProvider added in v0.4.0

func RunProvider(name, version string, provider Provider) error

Run a provider with the given name and version.

Types

type CheckFailure added in v0.4.0

type CheckFailure struct {
	Property string
	Reason   string
}

type CheckRequest added in v0.4.0

type CheckRequest struct {
	Urn        presource.URN
	Olds       presource.PropertyMap
	News       presource.PropertyMap
	RandomSeed []byte
}

type CheckResponse added in v0.4.0

type CheckResponse struct {
	Inputs   presource.PropertyMap
	Failures []CheckFailure
}

type ConfigureRequest added in v0.4.0

type ConfigureRequest struct {
	Variables map[string]string
	Args      presource.PropertyMap
}

type ConstructFunc added in v0.8.0

type ConstructRequest added in v0.8.0

type ConstructRequest struct {
	URN       presource.URN
	Preview   bool
	Construct func(context.Context, ConstructFunc) (ConstructResponse, error)
}

type ConstructResponse added in v0.8.0

type ConstructResponse struct {
	// contains filtered or unexported fields
}

type CreateRequest added in v0.4.0

type CreateRequest struct {
	Urn        presource.URN         // the Pulumi URN for this resource.
	Properties presource.PropertyMap // the provider inputs to set during creation.
	Timeout    float64               // the create request timeout represented in seconds.
	Preview    bool                  // true if this is a preview and the provider should not actually create the resource.
}

type CreateResponse added in v0.4.0

type CreateResponse struct {
	ID         string                // the ID of the created resource.
	Properties presource.PropertyMap // any properties that were computed during creation.

	// non-nil to indicate that the create failed and left the resource in a partial
	// state.
	//
	// If PartialState is non-nil, then an error will be returned, annotated with
	// [pulumirpc.ErrorResourceInitFailed].
	PartialState *InitializationFailed
}

type DeleteRequest added in v0.4.0

type DeleteRequest struct {
	ID         string                // the ID of the resource to delete.
	Urn        presource.URN         // the Pulumi URN for this resource.
	Properties presource.PropertyMap // the current properties on the resource.
	Timeout    float64               // the delete request timeout represented in seconds.
}

type DiffKind added in v0.4.0

type DiffKind string
const (
	Add           DiffKind = "add"            // this property was added
	AddReplace    DiffKind = "add&replace"    // this property was added, and this change requires a replace
	Delete        DiffKind = "delete"         // this property was removed
	DeleteReplace DiffKind = "delete&replace" // this property was removed, and this change requires a replace
	Update        DiffKind = "update"         // this property's value was changed
	UpdateReplace DiffKind = "update&replace" // this property's value was changed, and this change requires a replace
	Stable        DiffKind = "stable"         // this property's value will not change
)

type DiffRequest added in v0.4.0

type DiffRequest struct {
	ID            string
	Urn           presource.URN
	Olds          presource.PropertyMap
	News          presource.PropertyMap
	IgnoreChanges []presource.PropertyKey
}

type DiffResponse added in v0.4.0

type DiffResponse struct {
	DeleteBeforeReplace bool // if true, this resource must be deleted before replacing it.
	HasChanges          bool // if true, this diff represents an actual difference and thus requires an update.
	// detailedDiff is an optional field that contains map from each changed property to the type of the change.
	//
	// The keys of this map are property paths. These paths are essentially Javascript property access expressions
	// in which all elements are literals, and obey the following EBNF-ish grammar:
	//
	//   propertyName := [a-zA-Z_$] { [a-zA-Z0-9_$] }
	//   quotedPropertyName := '"' ( '\' '"' | [^"] ) { ( '\' '"' | [^"] ) } '"'
	//   arrayIndex := { [0-9] }
	//
	//   propertyIndex := '[' ( quotedPropertyName | arrayIndex ) ']'
	//   rootProperty := ( propertyName | propertyIndex )
	//   propertyAccessor := ( ( '.' propertyName ) |  propertyIndex )
	//   path := rootProperty { propertyAccessor }
	//
	// Examples of valid keys:
	// - root
	// - root.nested
	// - root["nested"]
	// - root.double.nest
	// - root["double"].nest
	// - root["double"]["nest"]
	// - root.array[0]
	// - root.array[100]
	// - root.array[0].nested
	// - root.array[0][1].nested
	// - root.nested.array[0].double[1]
	// - root["key with \"escaped\" quotes"]
	// - root["key with a ."]
	// - ["root key with \"escaped\" quotes"].nested
	// - ["root key with a ."][100]
	DetailedDiff map[string]PropertyDiff
}

type GetSchemaRequest added in v0.4.0

type GetSchemaRequest struct {
	Version int
}

type GetSchemaResponse added in v0.4.0

type GetSchemaResponse struct {
	Schema string
}

type InitializationFailed added in v0.16.0

type InitializationFailed struct {
	// Reasons why the resource did not fully initialize.
	Reasons []string
}

InitializationFailed indicates that a resource exists but failed to initialize, and is thus in a partial state.

type InvokeRequest added in v0.4.0

type InvokeRequest struct {
	Token tokens.Type           // the function token to invoke.
	Args  presource.PropertyMap // the arguments for the function invocation.
}

type InvokeResponse added in v0.4.0

type InvokeResponse struct {
	Return   presource.PropertyMap // the returned values, if invoke was successful.
	Failures []CheckFailure        // the failures if any arguments didn't pass verification.
}

type Logger added in v0.17.0

type Logger struct {
	// contains filtered or unexported fields
}

func GetLogger added in v0.17.0

func GetLogger(ctx context.Context) Logger

func (Logger) Debug added in v0.17.0

func (l Logger) Debug(msg string)

func (Logger) DebugStatus added in v0.17.0

func (l Logger) DebugStatus(msg string)

func (Logger) DebugStatusf added in v0.17.0

func (l Logger) DebugStatusf(msg string, a ...any)

func (Logger) Debugf added in v0.17.0

func (l Logger) Debugf(msg string, a ...any)

func (Logger) Error added in v0.17.0

func (l Logger) Error(msg string)

func (Logger) ErrorStatus added in v0.17.0

func (l Logger) ErrorStatus(msg string)

func (Logger) ErrorStatusf added in v0.17.0

func (l Logger) ErrorStatusf(msg string, a ...any)

func (Logger) Errorf added in v0.17.0

func (l Logger) Errorf(msg string, a ...any)

func (Logger) Info added in v0.17.0

func (l Logger) Info(msg string)

func (Logger) InfoStatus added in v0.17.0

func (l Logger) InfoStatus(msg string)

func (Logger) InfoStatusf added in v0.17.0

func (l Logger) InfoStatusf(msg string, a ...any)

func (Logger) Infof added in v0.17.0

func (l Logger) Infof(msg string, a ...any)

func (Logger) Warning added in v0.17.0

func (l Logger) Warning(msg string)

func (Logger) WarningStatus added in v0.17.0

func (l Logger) WarningStatus(msg string)

func (Logger) WarningStatusf added in v0.17.0

func (l Logger) WarningStatusf(msg string, a ...any)

func (Logger) Warningf added in v0.17.0

func (l Logger) Warningf(msg string, a ...any)

type PropertyDiff added in v0.4.0

type PropertyDiff struct {
	Kind      DiffKind // The kind of diff asdsociated with this property.
	InputDiff bool     // The difference is between old and new inputs, not old and new state.
}

type Provider added in v0.4.0

type Provider struct {

	// GetSchema fetches the schema for this resource provider.
	GetSchema func(context.Context, GetSchemaRequest) (GetSchemaResponse, error)
	// Cancel signals the provider to gracefully shut down and abort any ongoing resource operations.
	// Operations aborted in this way will return an error (e.g., `Update` and `Create` will either return a
	// creation error or an initialization error). Since Cancel is advisory and non-blocking, it is up
	// to the host to decide how long to wait after Cancel is called before (e.g.)
	// hard-closing any gRPC connection.
	Cancel func(context.Context) error

	// Provider Config
	CheckConfig func(context.Context, CheckRequest) (CheckResponse, error)
	DiffConfig  func(context.Context, DiffRequest) (DiffResponse, error)
	// NOTE: We opt into all options.
	Configure func(context.Context, ConfigureRequest) error

	// Invokes
	Invoke func(context.Context, InvokeRequest) (InvokeResponse, error)

	// Check validates that the given property bag is valid for a resource of the given type and returns the inputs
	// that should be passed to successive calls to Diff, Create, or Update for this resource. As a rule, the provider
	// inputs returned by a call to Check should preserve the original representation of the properties as present in
	// the program inputs. Though this rule is not required for correctness, violations thereof can negatively impact
	// the end-user experience, as the provider inputs are using for detecting and rendering diffs.
	Check  func(context.Context, CheckRequest) (CheckResponse, error)
	Diff   func(context.Context, DiffRequest) (DiffResponse, error)
	Create func(context.Context, CreateRequest) (CreateResponse, error)
	Read   func(context.Context, ReadRequest) (ReadResponse, error)
	Update func(context.Context, UpdateRequest) (UpdateResponse, error)
	Delete func(context.Context, DeleteRequest) error

	// Components Resources
	Construct func(context.Context, ConstructRequest) (ConstructResponse, error)
}

func (Provider) WithDefaults added in v0.8.0

func (d Provider) WithDefaults() Provider

Provide a default value for each function.

Most default values return a NotYetImplemented error, which the engine knows to ignore. Others are no-op functions.

You should not need to call this function manually. It will be automatically called before a provider is run.

type ReadRequest added in v0.4.0

type ReadRequest struct {
	ID         string                // the ID of the resource to read.
	Urn        presource.URN         // the Pulumi URN for this resource.
	Properties presource.PropertyMap // the current state (sufficiently complete to identify the resource).
	Inputs     presource.PropertyMap // the current inputs, if any (only populated during refresh).
}

type ReadResponse added in v0.4.0

type ReadResponse struct {
	ID         string                // the ID of the resource read back (or empty if missing).
	Properties presource.PropertyMap // the state of the resource read from the live environment.
	Inputs     presource.PropertyMap // the inputs for this resource that would be returned from Check.

	// non-nil to indicate that the read failed and left the resource in a partial
	// state.
	//
	// If PartialState is non-nil, then an error will be returned, annotated with
	// [pulumirpc.ErrorResourceInitFailed].
	PartialState *InitializationFailed
}

type RunInfo added in v0.4.0

type RunInfo struct {
	PackageName string
	Version     string
}

func GetRunInfo added in v0.17.0

func GetRunInfo(ctx context.Context) RunInfo

type UpdateRequest added in v0.4.0

type UpdateRequest struct {
	ID            string                  // the ID of the resource to update.
	Urn           presource.URN           // the Pulumi URN for this resource.
	Olds          presource.PropertyMap   // the old values of provider inputs for the resource to update.
	News          presource.PropertyMap   // the new values of provider inputs for the resource to update.
	Timeout       float64                 // the update request timeout represented in seconds.
	IgnoreChanges []presource.PropertyKey // a set of property paths that should be treated as unchanged.
	Preview       bool                    // true if the provider should not actually create the resource.
}

type UpdateResponse added in v0.4.0

type UpdateResponse struct {
	// any properties that were computed during updating.
	Properties presource.PropertyMap
	// non-nil to indicate that the update failed and left the resource in a partial
	// state.
	//
	// If PartialState is non-nil, then an error will be returned, annotated with
	// [pulumirpc.ErrorResourceInitFailed].
	PartialState *InitializationFailed
}

Directories

Path Synopsis
examples
credentials Module
dna-store Module
file Module
random-login Module
str Module
tests Module
key
key provides an internal set of keys for use with context.WithValue and context.Context.Value that can be shared across packages source.
key provides an internal set of keys for use with context.WithValue and context.Context.Value that can be shared across packages source.
middleware defines common interfaces multiple middleware components use.
middleware defines common interfaces multiple middleware components use.
cancel
The `cancel` package provides a middle-ware that ties the Cancel gRPC call from Pulumi to Go's `context.Context` cancellation system.
The `cancel` package provides a middle-ware that ties the Cancel gRPC call from Pulumi to Go's `context.Context` cancellation system.
context
Context allows systemic wrapping of provider.Context before invoking a subsidiary provider.
Context allows systemic wrapping of provider.Context before invoking a subsidiary provider.
dispatch
A provider that dispatches provider level calls such as `Create` to resource level invocations.
A provider that dispatches provider level calls such as `Create` to resource level invocations.
rpc
schema
The schema middleware provides facilities to respond to GetSchema.
The schema middleware provides facilities to respond to GetSchema.
tests module

Jump to

Keyboard shortcuts

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