From 5dcead1f7bcda6ae41e1c86e55f24080eda4f479 Mon Sep 17 00:00:00 2001 From: Hasan Turken Date: Mon, 1 Nov 2021 16:26:11 +0300 Subject: [PATCH] Add guide for generating a provider Signed-off-by: Hasan Turken --- docs/generating-a-provider.md | 188 ++++++++++++++++++++++++++++++++++ 1 file changed, 188 insertions(+) diff --git a/docs/generating-a-provider.md b/docs/generating-a-provider.md index e69de29b..441b9fc9 100644 --- a/docs/generating-a-provider.md +++ b/docs/generating-a-provider.md @@ -0,0 +1,188 @@ +# Generating a Crossplane Provider from a Terraform Provider + +## Generate + +In this Guide, we will generate https://registry.terraform.io/providers/integrations/github/latest/docs + +1. Generate a GitHub repository for the Crossplane provider by hitting the + "Use this template" button in [provider-tf-template] repository. +2. Clone the repository to your local. +3. Replace `template` with your provider name. + 1. Export variables for your provider name. + + ```shell + export ProviderNameLower=github + export ProviderNameUpper=GitHub + ``` + + 2. Replace all occurrences of `template` with your provider name. + + ```shell + git grep -l 'template' -- './*' ':!build/**' ':!go.sum' | xargs sed -i.bak "s/template/${ProviderNameLower}/g" + git grep -l 'Template' -- './*' ':!build/**' ':!go.sum' | xargs sed -i.bak "s/Template/${ProviderNameUpper}/g" + # Clean up the .bak files created by sed + git clean -fd + + mv "internal/clients/template.go" "internal/clients/${ProviderNameLower}.go" + mv "cluster/images/provider-tf-template" "cluster/images/provider-tf-${ProviderNameLower}" + mv "cluster/images/provider-tf-template-controller" "cluster/images/provider-tf-${ProviderNameLower}-controller" + ``` + +4. Configure your repo with Terraform provider and schema: + 1. Update Makefile variables for Terraform Provider (`TERRAFORM_PROVIDER_*`) + + ```makefile + export TERRAFORM_PROVIDER_SOURCE := integrations/github + export TERRAFORM_PROVIDER_VERSION := 4.17.0 + export TERRAFORM_PROVIDER_DOWNLOAD_NAME := terraform-provider-github + export TERRAFORM_PROVIDER_DOWNLOAD_URL_PREFIX := https://releases.hashicorp.com/terraform-provider-github/4.17.0 + ``` + Check the Dockerfile at cluster/images/provider-tf-${ProviderNameLower}-controller/Dockerfile + to see how download URL of the Terraform provider plugin binary + constructed using these variables. + + 2. Find go repository of the Terraform provider set import path for the + package with function `func Provider() terraform.ResourceProvider` and + set as import path for `tf` alias in `config/provider.go`. + + ```go + package config + import ( + tjconfig "github.com/crossplane-contrib/terrajet/pkg/config" + tf "github.com/turkenh/terraform-provider-github/v4/github" + ) + + const resourcePrefix = "github" + ``` + + 4. If your provider uses an old version ( config/repository/config.go + package repository + + import "github.com/crossplane-contrib/terrajet/pkg/config" + + func Customize(p config.Provider) { + p.AddResourceConfigurator("github_repository", func(r *config.Resource) { + r.Group = "repository" + }) + } + EOF + ``` + + ```shell + cat < config/branch/config.go + package branch + + import "github.com/crossplane-contrib/terrajet/pkg/config" + + func Customize(p config.Provider) { + p.AddResourceConfigurator("github_branch", func(r *config.Resource) { + r.Group = "branch" + r.ExternalName = config.IdentifierFromProvider + r.References["repository"] = config.Reference{ + Type: "github.com/crossplane-contrib/provider-tf-github/apis/repository/v1alpha1.Repository", + } + }) + } + EOF + ``` + 3. Register these group customization functions in `config/provider.go` + ```go + // GetProvider returns provider configuration + func GetProvider() tjconfig.Provider { + ... + for _, configure := range []func(provider tjconfig.Provider){ + repository.Customize, + branch.Customize, + } { + configure(pc) + } + ... + } + ``` + +8. Now we can generate our Terrajet Provider: + +```shell +make generate +``` + +## Test + + +## Package + + + + +[provider-tf-template]: https://github.com/crossplane-contrib/provider-tf-template +[Terraform documentation for provider configuration]: https://registry.terraform.io/providers/integrations/github/latest/docs#argument-reference \ No newline at end of file