Skip to content

Latest commit

 

History

History
314 lines (218 loc) · 14 KB

SETUP.md

File metadata and controls

314 lines (218 loc) · 14 KB

Terraform Bridge Provider Boilerplate

This repository is the template for authoring a Pulumi package from an existing Terraform provider as part of the guide for authoring and publishing Pulumi packages.

This repository is initially set up as a fictitious provider named "xyz" to demonstrate a resource, a data source and configuration derived from the github.com/pulumi/terraform-provider-xyz provider.

Prerequisites

Ensure the following tools are installed and present in your $PATH:

Creating the Repository

Pulumi offers this repository as a GitHub template repository for convenience. From this repository:

  1. Click "Use this template".
  2. Follow the new repository options:
    • Owner: select your GitHub organization or username
    • Repository name: A name prefixed with pulumi- as is the standard convention for Pulumi providers
    • Description: E.g. "Pulumi provider for ..."
    • Repository type: This must be Public to be published to the Pulumi registry
  3. Clone your new repository ready for development.

Initializing the Provider

From the templated repository run the init program and follow the prompts to replace the name "xyz" with the name of your provider:

./setup.sh

Tip

This program can also be run non-interactively, passing the options via arguments. Run ./setup.sh -h for the usage instructions.

Pulumi provider repositories have the following general structure:

  • examples/ sample code which may optionally be included as integration tests to be run as part of a CI/CD pipeline
  • provider/ Go code used to create the provider as well as generate the SDKs in the various languages that Pulumi supports
    • provider/cmd/pulumi-tfgen-xyz executable program to generate the Pulumi resource schema
    • provider/cmd/pulumi-resource-xyz provider plugin executable
    • provider/resources.go Terraform-to-Pulumi mappings and configuration
  • sdk/ generated SDKs for each language in a separate subfolder

Review & Customise Your Provider

  1. Review the updated README.md to include the following:

    • Short introductory paragraph describing the type of resources the provider manages.
    • In the "Installing" section, correct package names for the various SDK libraries in the languages Pulumi supports.
    • In the "Configuration" section, any configurable options for the provider. These may include, but are not limited to, environment variables or options that can be set via pulumi config set.
    • In the "Reference" section, provide a link to the to-be-published documentation.
  2. Update the DisplayName, Publisher, and Homepage values in provider/resources.go.

  3. Verify the upstream Terraform provider module import in the provider/go.mod and provider/resources.go are correct.

    • If the name of the desired Pulumi provider differs from the name of the Terraform provider, you will need to carefully distinguish between the references - see https://github.com/pulumi/pulumi-azure for an example.
  4. Verify and download dependencies in the provider/go.mod:

    (cd provider && go mod tidy)
  5. Generate the provider's schema:

    make tfgen

Tip

Take note of warnings & errors in the output when generating the schema.

Build the Provider and SDKs

Note

Most providers can use automatic token mapping which generates the bridge-metadata.json file. If you have warnings about unmapped resources in the step above, refer to the section on Manual Mappings. If you need to customise the behaviour of the provider configuration, refer to the section Customise Provider Configuration.

  1. Build the provider binary and ensure there are no warnings about unmapped resources and no warnings about unmapped data sources:

    make provider

    You may see warnings about documentation and examples, including "unexpected code snippets". These can be safely ignored for now. Pulumi will add additional documentation on mapping docs in a future revision of this guide.

  2. Build the SDKs in the various languages Pulumi supports:

    make build_sdks
  3. Ensure the Golang SDK is a proper go module:

    cd sdk && go mod tidy && cd -

    This will pull in the correct dependencies in sdk/go.mod as well as setting the dependency tree in sdk/go.sum.

  4. Finally, ensure the provider code conforms to Go standards:

    make lint_provider

    Some issues found by the linter can be fixed automatically by running make lint_provider.fix.

Note

If you make revisions to code in resources.go, you must re-run the make tfgen target to regenerate the schema.

Sample Program

In this section, we will create a Pulumi program in TypeScript that utilizes the provider we created to ensure everything is working properly.

  1. Create an account with the provider's service and generate any necessary credentials, e.g. API keys.

    • Email: [email protected]
    • Password: (Create a random password in 1Password with the maximum length and complexity allowed by the provider.)
    • Ensure all secrets (passwords, generated API keys) are stored in Pulumi's 1Password vault.
  2. Copy the pulumi-resource-xyz binary generated by make provider and place it in your $PATH ($GOPATH/bin is a convenient choice), e.g.:

    cp bin/pulumi-resource-xyz $GOPATH/bin
  3. Tell Yarn to use your local copy of the SDK:

    make install_nodejs_sdk
  4. Create a new Pulumi program in the examples/ directory, e.g.:

    mkdir examples/my-example/ts # Change "my-example" to something more meaningful.
    cd examples/my-example/ts
    pulumi new typescript
    # (Go through the prompts with the default values)
    npm install
    yarn link @pulumi/xyz
  5. Create a minimal program for the provider, i.e. one that creates the smallest-footprint resource. Place this code in index.ts.

  6. Configure any necessary environment variables for authentication, e.g $XYZ_USERNAME, $XYZ_TOKEN, in your local environment.

  7. Ensure the program runs successfully via pulumi up.

  8. Once the program completes successfully, verify the resource was created in the provider's UI.

  9. Destroy any resources created by the program via pulumi destroy.

End-to-end Testing

Integration tests are Go tests residing in examples/*_test.go. Each test executes a program from a subdirectory within examples/. All tests are initially marked to be skipped as the example xyz provider is not fully implemented and will fail with "not implemented".

  1. Update an example to update to use a resource generated from your provider SDK (e.g. update the code in index.ts of examples/basic-ts)

  2. Locate the Go test for the example (e.g. examples/basic-ts is tested by TestBasicTs in examples/examples_nodejs_test.go)

  3. Remove the "skip" directive on the first line: t.Skip(...)

  4. Run the tests:

    make test

    You can also run each test file separately via test tags:

    (cd examples && go test -v -tags=nodejs)
  5. Repeat the process for each language and add new examples and tests as needed.

Configure GitHub Actions CI

This template utilizes Pulumi's ci-mgmt templates to automatically generate CI workflows and other build tooling.

This is configured via the .ci-mgmt.yaml file and can be re-generated by running make ci-mgmt which also updates to the latest version of the tool.

The default configuration is designed for third-party use and will need to be adjusted for Pulumi internal providers.

Final Steps

  1. Ensure all required configurations (API keys, etc.) are documented in README-PROVIDER.md.

  2. Delete the boilerplate's SETUP.md and setup.sh files.

  3. Register your provider in the Pulumi Registry. See Publishing a Community Package on the Registry.

Now you are ready to use the provider, cut releases, and have some well-deserved 🍨!

Manual Mappings

In this section we will add the mappings that allow the interoperation between the Pulumi provider and the Terraform provider. Terraform resources map to an identically named concept in Pulumi. Terraform data sources map to plain old functions in your supported programming language of choice. Pulumi also allows provider functions and resources to be grouped into namespaces to improve the cohesion of a provider's code, thereby making it easier for developers to use. If your provider has a large number of resources, consider using namespaces to improve usability.

The following instructions all pertain to provider/resources.go, in the section of the code where we construct a tfbridge.ProviderInfo object:

  1. Add resource mappings: For each resource in the provider, add an entry in the Resources property of the tfbridge.ProviderInfo, e.g.:

    // Most providers will have all resources (and data sources) in the main module.
    // Note the mapping from snake_case HCL naming conventions to UpperCamelCase Pulumi SDK naming conventions.
    // The name of the provider is omitted from the mapped name due to the presence of namespaces in all supported Pulumi languages.
    "xyz_something":      {Tok: tfbridge.MakeResource(mainPkg, mainMod, "Something")},
    "xyz_something_else": {Tok: tfbridge.MakeResource(mainPkg, mainMod, "SomethingElse")},
  2. Add CSharpName (if necessary): Dotnet does not allow for fields named the same as the enclosing type, which sometimes results in errors during the dotnet SDK build. If you see something like

    error CS0542: 'ApiKey': member names cannot be the same as their enclosing type [/Users/guin/go/src/github.com/pulumi/pulumi-artifactory/sdk/dotnet/Pulumi.Artifactory.csproj]
    

    you'll want to give your Resource a CSharpName, which can have any value that makes sense:

    "xyz_something": {
        Tok: tfbridge.MakeResource(mainPkg, mainMod, "Something"),
        Fields: map[string]*tfbridge.SchemaInfo{
            "something": {
                CSharpName: "SomethingField",
            },
        },
    },

    See the underlying terraform-bridge code here.

  3. Add data source mappings: For each data source in the provider, add an entry in the DataSources property of the tfbridge.ProviderInfo, e.g.:

    // Note the 'get' prefix for data sources
    "xyz_something":      {Tok: tfbridge.MakeDataSource(mainPkg, mainMod, "getSomething")},
    "xyz_something_else": {Tok: tfbridge.MakeDataSource(mainPkg, mainMod, "getSomethingElse")},
  4. Add documentation mapping (sometimes needed): If the upstream provider's repo is not a part of the terraform-providers GitHub organization, specify the GitHubOrg property of tfbridge.ProviderInfo to ensure that documentation is picked up by the codegen process, and that attribution for the upstream provider is correct, e.g.:

    GitHubOrg: "my-gh-org",

Customise Provider Configuration

Pulumi's Terraform bridge automatically detects configuration options for the upstream provider. However, in rare cases these settings may need to be overridden, e.g. if we want to change an environment variable default from API_KEY to XYZ_API_KEY. Examples of common uses cases:

"additional_required_parameter": {},
"additional_optional_string_parameter": {
    Default: &tfbridge.DefaultInfo{
        Value: "default_value",
    },
"additional_optional_boolean_parameter": {
    Default: &tfbridge.DefaultInfo{
        Value: true,
    },
// Renamed environment variables can be accounted for like so:
"apikey": {
    Default: &tfbridge.DefaultInfo{
        EnvVars: []string{"XYZ_API_KEY"},
    },

The Shim Pattern

If you receive the following error: use of internal package github.com/example/terraform-provider-example/internal/provider not allowed, you need to use the shim model below, and replace the example item:

mkdir -p provider/shim
cat <<-EOF> provider/shim/go.mod
module github.com/example/terraform-provider-example/shim

go 1.18

require github.com/hashicorp/terraform-plugin-sdk/v2 v2.22.0
require github.com/example/terraform-provider-example v1.0.0

EOF

cat <<-EOF> provider/shim/shim.go
package shim

import (
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	"github.com/example/terraform-provider-example/internal/provider"
)

// fix provider.Provider here to match whats in internal/provider
func Provider() *schema.Provider {
	return provider.Provider()
}

EOF

cd provider/shim/ && go mod tidy && cd ../../

cat <<EOF>> provider/go.mod
replace github.com/example/terraform-provider-example/shim => ./shim
require github.com/example/terraform-provider-example/shim v0.0.0
EOF

cd provider && go mod tidy