Skip to content

Instantly share code, notes, and snippets.

@usrbinkat
Last active October 13, 2023 19:52
Show Gist options
  • Save usrbinkat/7de564a2a7d8408469b59ec57f993465 to your computer and use it in GitHub Desktop.
Save usrbinkat/7de564a2a7d8408469b59ec57f993465 to your computer and use it in GitHub Desktop.
Pulumi Native Provider

The pulumi-go-provider is a framework for building Go-based providers for Pulumi. It is currently in active development, and breaking changes are expected. The library is designed to be flexible yet simple, and it comes in four main parts:

Provider Interface: The core abstraction for a Pulumi Provider.
Middleware Layers: Built on top of the Provider interface for features like token dispatch and schema generation.
Testing Framework: Located in the integration folder for unit and integration tests.
Infer Layer: Generates full providers from Go types and methods. It's the quickest way to start building a provider.

Cheat Sheet Main Function Example

func main() {
    p.RunProvider("greetings", "0.1.0", infer.Provider(infer.Options{
        Resources: []infer.InferredResource{
            infer.ResourceHelloWorld, HelloWorldArgs, HelloWorldState,
        },
    })
)}

Resource Structs.

HelloWorld: Main struct controlling the resource.
HelloWorldArgs: Input struct, defines accepted arguments.
HelloWorldState: State struct, describes fields on the created resource.

Tags and Fields.

Fields must be public and have a pulumi:"..." tag.
Optional fields should be pointers and marked optional.

Implementing Create Method.

func (HelloWorld) Create(ctx p.Context, name string, input HelloWorldArgs, preview bool) (string, HelloWorldState, error) {
// Implementation here
}

Quick Reference Summary.

RunProvider: Main function to run the provider.
Infer Layer: Use infer for quick provider generation.
Resource Structs: Define controlling structs for resources (HelloWorld, HelloWorldArgs, HelloWorldState).
Create Method: Must be implemented for each resource.
Middleware: Additional layers for extended functionalities.
Testing: Use the integration folder for testing.
Key Points for Developing a New Golang Native Pulumi Provider.
Start with RunProvider: This is your entry point.
Define Resource Structs: Create structs for controlling your resources and their states.
Implement Core Methods: At a minimum, implement the Create method for your resources.
Use infer for Quick Start: The infer layer can generate a lot of boilerplate for you.
Middleware for Advanced Features: Use middleware layers for advanced functionalities like token dispatch.

Lifecycle Hooks

  • create(inputs): Allocates a new instance of the resource.
  • read(id, props): Reads the current live state associated with a resource.
  • update(id, olds, news): Updates an existing resource with new values.
  • delete(id, props): Tears down a resource.
  • check(olds, news): Validates that the given property bag is valid.
  • diff(id, olds, news): Returns what differences exist between the old and new states.
  • import(id, props): Translates a raw set of resource inputs to a provider-specific ID and set of inputs.
  • getConfig: Returns the provider's configuration properties.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment