Page cover

2 1 0 Custom Terraform Provider: Setup Skeleton (Go)

Andrew defines the Provider object in Go language.

Over the course of Week 2, Andrew develops the custom provider plugin in Golang. In this episode, we are setting up the skeleton to give shape to the provider. The Provider is instantiated with three attributes which are required to interact with the TerraTowns service.

Code Notes

package main

import (
	"log"
	"fmt"
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	"github.com/hashicorp/terraform-plugin-sdk/v2/plugin"
)
 
func main() {
	plugin.Serve(&plugin.ServeOpts{
		ProviderFunc: Provider,
	})
}

func Provider() *schema.Provider {
	var p *schema.Provider
	p = &schema.Provider{
		ResourcesMap:  map[string]*schema.Resource{

		},
		DataSourcesMap:  map[string]*schema.Resource{

		},
		Schema: map[string]*schema.Schema{
			"endpoint": {
				Type: schema.TypeString,
				Required: true,
				Description: "The endpoint for hte external service",
			},
			"token": {
				Type: schema.TypeString,
				Sensitive: true, // make the token as sensitive to hide it the logs
				Required: true,
				Description: "Bearer token for authorization",
			},
			"user_uuid": {
				Type: schema.TypeString,
				Required: true,
				Description: "UUID for configuration",
				//ValidateFunc: validateUUID,
			},
		},
	}
	return p
}
  • main.go: a conventional filename used as the entry point of a Go application. The main.go file typically contains the main function, which is the starting point for the execution of the program. If you are familiar with Java, this is pretty much the Go equivalent of Java's public static void main(String[] args).

  • Lines 13-15 inside main(): are the boilerplate code provided by Terraform for creating a Terraform custom provider plugin in Go. If it doesn't make sense to you, just know that it just has to be there, otherwise the Provider won't be created :P

func Provider() *schema.Provider {
    return &schema.Provider{
        ResourcesMap: map[string]*schema.Resource{},
    }
}
  • ResourcesMap, line 3: the ResourcesMap block inside &schema.Provider{} will have to contain the blueprint of the resources the Provider can provision when instantiated. More on that later in 2.3.0.

  • Schema, lines 25-43: is the blueprint of the Provider() entity. When declaring a provider in a .tf file, you will have to provide the values for the Provider's attributes (configuration options). In our plugin, the Provider entity comes with three attributes: endpoint, token, and user_uuid. They are required when instantiating a provider in order for it to interact with TerraTowns (the cloud service).

    • endpoint: terraform should know where the service is (the website address, or API in other words). The value for this will eventually be TerraTowns' domain name.

    • Your credentials information

      • token: the token generated from ExamPro's vending machine 🎰. This token is unique to your account, so TerraTowns can authenticate your http request.

      • user_uuid: also unique to your account. In combination with the token, TerraTowns authenticates you then processes your http request.

    • schema.Schema.attribute.Type: defines the data type of the value you will provide for the attribute. They are categorised into two groups:

      Primitive types
      Aggregate types

      TypeBool

      TypeInt

      TypeFloat

      TypeString

      Date & Time data

      TypeMap

      TypeType

      TypeSet

This marks the end of the tutorial, and also the completion of the Provider skeleton. It now does have a shape - an object with three attributes. As there is no actions (functions) defined, however, it can't really do anything. If we were to compare this to web development, it's like we just finished writing an html file without any backend functions. So we can view the page, but cannot interact with it.

Resources

Terraform

Go

Last updated