Documentation
¶
Index ¶
- Constants
- type ExecutionOutput
- type ModuleDefinition
- type ModuleInstance
- type ParameterMapping
- type TerraformExecutor
- type TerraformWorkspace
- func (workspace *TerraformWorkspace) Apply(ctx context.Context) error
- func (workspace *TerraformWorkspace) Destroy(ctx context.Context) error
- func (workspace *TerraformWorkspace) Import(ctx context.Context, resources map[string]string) error
- func (workspace *TerraformWorkspace) Outputs(instance string) (map[string]interface{}, error)
- func (workspace *TerraformWorkspace) Plan(ctx context.Context) error
- func (workspace *TerraformWorkspace) Serialize() (string, error)
- func (workspace *TerraformWorkspace) Show(ctx context.Context) (string, error)
- func (workspace *TerraformWorkspace) String() string
- func (workspace *TerraformWorkspace) Validate(ctx context.Context) error
- type TfTransformer
- type Tfstate
Examples ¶
Constants ¶
const (
DefaultInstanceName = "instance"
)
DefaultInstanceName is the default name of an instance of a particular module.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ExecutionOutput ¶
ExecutionOutput captures output from tf cli execution
func DefaultExecutor ¶
DefaultExecutor is the default executor that shells out to Terraform and logs results to stdout.
type ModuleDefinition ¶
ModuleDefinition represents a module in a Terraform workspace.
func (*ModuleDefinition) Inputs ¶
func (module *ModuleDefinition) Inputs() ([]string, error)
Inputs gets the input parameter names for the module.
func (*ModuleDefinition) Outputs ¶
func (module *ModuleDefinition) Outputs() ([]string, error)
Outputs gets the output parameter names for the module.
func (*ModuleDefinition) Validate ¶
func (module *ModuleDefinition) Validate() (errs *validation.FieldError)
Validate checks the validity of the ModuleDefinition struct.
type ModuleInstance ¶
type ModuleInstance struct {
ModuleName string `json:"module_name"`
InstanceName string `json:"instance_name"`
Configuration map[string]interface{} `json:"configuration"`
}
ModuleInstance represents the configuration of a single instance of a module.
func (*ModuleInstance) MarshalDefinition ¶
func (instance *ModuleInstance) MarshalDefinition(outputs []string) (json.RawMessage, error)
MarshalDefinition converts the module instance definition into a JSON definition that can be fed to Terraform to be created/destroyed.
Example ¶
instance := ModuleInstance{
ModuleName: "foo-module",
InstanceName: "instance",
Configuration: map[string]interface{}{"foo": "bar"},
}
outputs := []string{"output1", "output2"}
defnJson, err := instance.MarshalDefinition(outputs)
fmt.Println(err)
fmt.Printf("%s\n", string(defnJson))
Output: <nil> {"module":{"instance":{"foo":"bar","source":"./foo-module"}},"output":{"output1":{"value":"${module.instance.output1}"},"output2":{"value":"${module.instance.output2}"}}}
Example (EmptyOutputs) ¶
instance := ModuleInstance{
ModuleName: "foo-module",
InstanceName: "instance",
Configuration: map[string]interface{}{"foo": "bar"},
}
defnJson, err := instance.MarshalDefinition([]string{})
fmt.Println(err)
fmt.Printf("%s\n", string(defnJson))
Output: <nil> {"module":{"instance":{"foo":"bar","source":"./foo-module"}}}
type ParameterMapping ¶
type ParameterMapping struct {
TfVariable string `yaml:"tf_variable"`
ParameterName string `yaml:"parameter_name"`
}
ParameterMapping mapping for tf variable to service parameter
type TerraformExecutor ¶
TerraformExecutor is the function that shells out to Terraform. It can intercept, modify or retry the given command.
func CustomEnvironmentExecutor ¶
func CustomEnvironmentExecutor(environment map[string]string, wrapped TerraformExecutor) TerraformExecutor
CustomEnvironmentExecutor sets custom environment variables on the Terraform execution.
func CustomTerraformExecutor ¶
func CustomTerraformExecutor(tfBinaryPath, tfPluginDir string, tfVersion *version.Version, wrapped TerraformExecutor) TerraformExecutor
CustomTerraformExecutor executes a custom Terraform binary that uses plugins from a given plugin directory rather than the Terraform that's on the PATH which will download provider binaries from the web.
type TerraformWorkspace ¶
type TerraformWorkspace struct {
Modules []ModuleDefinition `json:"modules"`
Instances []ModuleInstance `json:"instances"`
State []byte `json:"tfstate"`
// Executor is a function that gets invoked to shell out to Terraform.
// If left nil, the default executor is used.
Executor TerraformExecutor `json:"-"`
Transformer TfTransformer `json:"transform"`
// contains filtered or unexported fields
}
TerraformWorkspace represents the directory layout of a Terraform execution. The structure is strict, consisting of several Terraform modules and instances of those modules. The strictness is artificial, but maintains a clear separation between data and code.
It manages the directory structure needed for the commands, serializing and deserializing Terraform state, and all the flags necessary to call Terraform.
All public functions that shell out to Terraform maintain the following invariants: - The function blocks if another terraform shell is running. - The function updates the tfstate once finished. - The function creates and destroys its own dir.
func DeserializeWorkspace ¶
func DeserializeWorkspace(definition string) (*TerraformWorkspace, error)
DeserializeWorkspace creates a new TerraformWorkspace from a given JSON serialization of one.
func NewWorkspace ¶
func NewWorkspace(templateVars map[string]interface{}, terraformTemplate string, terraformTemplates map[string]string, importParameterMappings []ParameterMapping, parametersToRemove []string, parametersToAdd []ParameterMapping) (*TerraformWorkspace, error)
NewWorkspace creates a new TerraformWorkspace from a given template and variables to populate an instance of it. The created instance will have the name specified by the DefaultInstanceName constant.
func (*TerraformWorkspace) Apply ¶
func (workspace *TerraformWorkspace) Apply(ctx context.Context) error
Apply runs `terraform apply` on this workspace. This function blocks if another Terraform command is running on this workspace.
func (*TerraformWorkspace) Destroy ¶
func (workspace *TerraformWorkspace) Destroy(ctx context.Context) error
Destroy runs `terraform destroy` on this workspace. This function blocks if another Terraform command is running on this workspace.
func (*TerraformWorkspace) Import ¶
Import runs `terraform import` on this workspace. This function blocks if another Terraform command is running on this workspace.
func (*TerraformWorkspace) Outputs ¶
func (workspace *TerraformWorkspace) Outputs(instance string) (map[string]interface{}, error)
Outputs gets the Terraform outputs from the state for the instance with the given name. This function DOES NOT invoke Terraform and instead uses the stored state. If no instance exists with the given name, it could be that Terraform pruned it due to having no contents so a blank map is returned.
func (*TerraformWorkspace) Plan ¶
func (workspace *TerraformWorkspace) Plan(ctx context.Context) error
func (*TerraformWorkspace) Serialize ¶
func (workspace *TerraformWorkspace) Serialize() (string, error)
Serialize converts the TerraformWorkspace into a JSON string.
func (*TerraformWorkspace) Show ¶
func (workspace *TerraformWorkspace) Show(ctx context.Context) (string, error)
Show runs `terraform show` on this workspace. This function blocks if another Terraform command is running on this workspace.
func (*TerraformWorkspace) String ¶
func (workspace *TerraformWorkspace) String() string
String returns a human-friendly representation of the workspace suitable for printing to the console.
type TfTransformer ¶
type TfTransformer struct {
ParameterMappings []ParameterMapping `json:"parameter_mappings"`
ParametersToRemove []string `json:"parameters_to_remove"`
ParametersToAdd []ParameterMapping `json:"parameters_to_add"`
}
TfTransformer terraform transformation
func (*TfTransformer) AddParametersInTf ¶
func (ttf *TfTransformer) AddParametersInTf(tf string) string
func (*TfTransformer) CleanTf ¶
func (ttf *TfTransformer) CleanTf(tf string) string
CleanTf removes ttf.ParametersToRemove from tf string
func (*TfTransformer) ReplaceParametersInTf ¶
ReplaceParametersInTf replaces ttf.ParameterMappings in tf
type Tfstate ¶
type Tfstate struct {
Version int `json:"version"`
Outputs map[string]struct {
Type string `json:"type"`
Value interface{} `json:"value"`
} `json:"outputs"`
}
Tfstate is a struct that can help us deserialize the tfstate JSON file.
func NewTfstate ¶
NewTfstate deserializes a tfstate file.
Example (BadVersion) ¶
state := `{
"version": 5,
"terraform_version": "0.12.20",
"serial": 2,
"outputs": {
"hostname": {
"value": "brokertemplate.instance.hostname",
"type": "string"
}
},
"resources": [
{
"module": "module.instance",
"mode": "managed",
"type": "google_sql_database",
"name": "database",
"provider": "provider.google",
"instances": []
}
]
}`
_, err := NewTfstate([]byte(state))
fmt.Printf("%v", err)
Output: unsupported tfstate version: 5
Example (Good) ¶
state := `{
"version": 4,
"terraform_version": "0.12.20",
"serial": 2,
"outputs": {
"hostname": {
"value": "brokertemplate.instance.hostname",
"type": "string"
}
},
"resources": [
{
"module": "module.instance",
"mode": "managed",
"type": "google_sql_database",
"name": "database",
"provider": "provider.google",
"instances": []
},
{
"module": "module.instance",
"mode": "managed",
"type": "google_sql_database_instance",
"name": "instance",
"provider": "provider.google",
"instances": []
}
]
}`
_, err := NewTfstate([]byte(state))
fmt.Printf("%v", err)
Output: <nil>
func (*Tfstate) GetOutputs ¶
GetOutputs gets the key/value outputs defined for a module.
Example ¶
state := `{
"version": 4,
"terraform_version": "0.12.20",
"serial": 2,
"outputs": {
"hostname": {
"value": "somehost",
"type": "string"
}
},
"resources": [
{
"module": "module.instance",
"mode": "managed",
"type": "google_sql_database",
"name": "database",
"provider": "provider.google",
"instances": []
}
]
}`
tfstate, _ := NewTfstate([]byte(state))
fmt.Printf("%v\n", tfstate.GetOutputs())
Output: map[hostname:somehost]