tftmpl

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2022 License: MPL-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TerraformRequiredVersion is the version constraint pinned to the generated
	// root module to ensure compatibility across Sync, Terraform, and
	// modules.
	TerraformRequiredVersion = version.CompatibleTerraformVersionConstraint

	// RootFilename is the file name for the root module.
	RootFilename = "main.tf"

	// VarsFilename is the file name for the variable definitions in the root
	// module. This includes the required services variable and generated
	// provider variables based on CTS user configuration for the task.
	VarsFilename = "variables.tf"

	// ModuleVarsFilename is the file name for the variable definitions corresponding
	// to the input variables from a user that is specific to the task's module.
	ModuleVarsFilename = "variables.module.tf"

	// TFVarsFilename is the file name where the required Consul services input
	// variable is written to.
	TFVarsFilename = "terraform.tfvars"

	// VarsTFVarsFileName is the file name for a tfvars file which is generated and contains
	// variables provided as part of the task configuration. Using the *auto.tfvars naming convention
	// allows for Terraform to use this file automatically as long as the generated file in Terraform's
	// working directory
	VarsTFVarsFileName = "variables.auto.tfvars"

	// TFVarsTmplFilename is the template file for TFVarsFilename. This is used
	// by hcat for monitoring service changes from Consul.
	TFVarsTmplFilename = "terraform.tfvars.tmpl"

	// ProvidersTFVarsFilename is the file name for input variables for
	// configured Terraform providers. Generated provider input variables are
	// written in a separate file from terraform.tfvars because it may contain
	// sensitive or secret values.
	ProvidersTFVarsFilename = "providers.auto.tfvars"
)

Variables

View Source
var (
	// RootPreamble is a warning message included to the beginning of the
	// generated root module files.
	RootPreamble = []byte(
		`# This file is generated by Consul Terraform Sync.
#
# The HCL blocks, arguments, variables, and values are derived from the
# operator configuration for Sync. Any manual changes to this file
# may not be preserved and could be overwritten by a subsequent update.
#
`)

	// TaskPreamble is the base format for task information included at the
	// beginning of generated module files.
	TaskPreamble = `# Task: %s
# Description: %s
`
)
View Source
var VariableServices = []byte(`
# Service definition protocol v0
variable "services" {
  description = "Consul services monitored by Consul Terraform Sync"
  type = map(
    object({
      id        = string
      name      = string
      kind      = string
      address   = string
      port      = number
      meta      = map(string)
      tags      = list(string)
      namespace = string
      status    = string

      node                  = string
      node_id               = string
      node_address          = string
      node_datacenter       = string
      node_tagged_addresses = map(string)
      node_meta             = map(string)

      cts_user_defined_meta = map(string)
    })
  )
}
`)

VariableServices is versioned to track compatibility with the generated root module with modules.

Functions

func InitRootModule

func InitRootModule(input *RootModuleInputData) error

InitRootModule generates the root module and writes the following files to disk.

always: main.tf, variables.tf, terraform.tfvars.tmpl

conditionally: variables.module.tf, providers.tfvars

func LoadModuleVariables

func LoadModuleVariables(filePath string, reader io.Reader) (hcltmpl.Variables, error)

LoadModuleVariables loads Terraform input variables from a file.

func ParseModuleVariables

func ParseModuleVariables(content []byte, filename string) (hcltmpl.Variables, error)

ParseModuleVariables parses bytes representing Terraform input variables for a module. It encodes the content into cty.Value types. Invalid HCL syntax and unsupported Terraform variable types result in an error.

func ParseModuleVariablesFromMap added in v0.5.0

func ParseModuleVariablesFromMap(content map[string]string) (hcltmpl.Variables, error)

ParseModuleVariablesFromMap parses map[string]string content representing Terraform input variables for a module. It encodes the content into cty.Value types. Invalid HCL syntax and unsupported Terraform variable types result in an error.

Types

type CatalogServicesTemplate added in v0.5.0

type CatalogServicesTemplate struct {
	Regexp     string
	Datacenter string
	Namespace  string
	NodeMeta   map[string]string

	// RenderVar informs whether the template should render the variable or not.
	// Aligns with the task condition configuration `UseAsModuleInput“
	RenderVar bool
}

CatalogServicesTemplate handles the template for the catalog_services variable for the template function: `{{ catalogServicesRegistration }}`

func (CatalogServicesTemplate) IsServicesVar added in v0.5.0

func (t CatalogServicesTemplate) IsServicesVar() bool

IsServicesVar returns false because the template returns a catalog_services variable, not a services variable

func (CatalogServicesTemplate) RendersVar added in v0.5.0

func (t CatalogServicesTemplate) RendersVar() bool

type ConsulKVTemplate added in v0.5.0

type ConsulKVTemplate struct {
	Path       string
	Recurse    bool
	Datacenter string
	Namespace  string

	// RenderVar informs whether the template should render the variable or not.
	// Aligns with the task condition configuration `UseAsModuleInput“
	RenderVar bool
}

ConsulKVTemplate handles the template for the consul_kv variable for the template functions: `{{ key }}` and `{{ keyExistsGet }}`

func (ConsulKVTemplate) IsServicesVar added in v0.5.0

func (t ConsulKVTemplate) IsServicesVar() bool

IsServicesVar returns false because the template returns a consul_kv variable, not a services variable

func (ConsulKVTemplate) RendersVar added in v0.5.0

func (t ConsulKVTemplate) RendersVar() bool

type RootModuleInputData

type RootModuleInputData struct {
	TerraformVersion *goVersion.Version
	Backend          map[string]interface{}
	Providers        []hcltmpl.NamedBlock
	ProviderInfo     map[string]interface{}
	Task             Task
	Variables        hcltmpl.Variables
	Templates        []Template

	Path      string
	FilePerms os.FileMode
	// contains filtered or unexported fields
}

RootModuleInputData is the input data used to generate the root module

type Service

type Service struct {
	Datacenter string
	Namespace  string
	Filter     string
}

Service contains additional Consul service filtering information for services configured in ServicesTemplate

type ServicesRegexTemplate added in v0.5.0

type ServicesRegexTemplate struct {
	Regexp     string
	Datacenter string
	Namespace  string
	Filter     string

	// RenderVar informs whether the template should render the variable or not.
	// Aligns with the task condition configuration `UseAsModuleInput“
	RenderVar bool
}

ServicesRegexTemplate handles the template for the services variable for the template function: `{{ servicesRegex }}`

func (ServicesRegexTemplate) IsServicesVar added in v0.5.0

func (t ServicesRegexTemplate) IsServicesVar() bool

IsServicesVar returns true because the template is for the services variable

func (ServicesRegexTemplate) RendersVar added in v0.5.0

func (t ServicesRegexTemplate) RendersVar() bool

type ServicesTemplate added in v0.5.0

type ServicesTemplate struct {
	Names []string

	// RenderVar informs whether the template should render the variable or not.
	// Aligns with the task condition configuration `UseAsModuleInput“
	RenderVar bool

	// Introduced in 0.5 - optional overall service filtering configured through
	// the task's condition "services". These configs or Services can be
	// configured but not both.
	Datacenter string
	Namespace  string
	Filter     string

	// Deprecated in 0.5 - optional per service filtering configured through the
	// task's services list. Not all services configured in Names must have
	// filtering configured. Services or the set of {Datacenter,
	// Namespace, Filter} can be configured but not both.
	Services map[string]Service
}

ServicesTemplate handles the template for the services variable for the template function: `{{ service }}`

func (ServicesTemplate) IsServicesVar added in v0.5.0

func (t ServicesTemplate) IsServicesVar() bool

IsServicesVar returns true because the template is for the services variable

func (ServicesTemplate) RendersVar added in v0.5.0

func (t ServicesTemplate) RendersVar() bool

type Task

type Task struct {
	Description string
	Name        string
	Module      string
	Version     string
}

Task contains information for a Sync task. The Terraform driver interprets task values for determining the Terraform module.

type Template added in v0.5.0

type Template interface {
	// IsServicesVar returns whether or not the template function returns a
	// variable of type services
	IsServicesVar() bool

	// RendersVar returns whether or not the template renders the monitored
	// variable
	RendersVar() bool
	// contains filtered or unexported methods
}

Template handles templates for different template functions to monitor different types of variables

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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