Documentation ¶
Index ¶
- Constants
- Variables
- func DefaultExecutor(c *exec.Cmd) error
- type ModuleDefinition
- type ModuleInstance
- type TerraformExecutor
- type TerraformWorkspace
- func (workspace *TerraformWorkspace) Apply() error
- func (workspace *TerraformWorkspace) Destroy() error
- func (workspace *TerraformWorkspace) Outputs(instance string) (map[string]interface{}, error)
- func (workspace *TerraformWorkspace) Serialize() (string, error)
- func (workspace *TerraformWorkspace) String() string
- func (workspace *TerraformWorkspace) Validate() error
- type Tfstate
- type TfstateModule
Examples ¶
Constants ¶
const (
DefaultInstanceName = "instance"
)
DefaultInstanceName is the default name of an instance of a particular module.
Variables ¶
var (
FsInitializationErr = errors.New("Filesystem must first be initialized.")
)
Functions ¶
func DefaultExecutor ¶
DefaultExecutor is the default executor that shells out to Terraform and logs results to stdout.
Types ¶
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.
Example ¶
module := ModuleDefinition{ Name: "cloud_storage", Definition: ` variable name {type = "string"} variable storage_class {type = "string"} resource "google_storage_bucket" "bucket" { name = "${var.name}" storage_class = "${var.storage_class}" } `, } inputs, err := module.Inputs() if err != nil { panic(err) } fmt.Printf("%v\n", inputs)
Output: [name storage_class]
func (*ModuleDefinition) Outputs ¶
func (module *ModuleDefinition) Outputs() ([]string, error)
Outputs gets the output parameter names for the module.
Example ¶
module := ModuleDefinition{ Name: "cloud_storage", Definition: ` resource "google_storage_bucket" "bucket" { name = "my-bucket" storage_class = "STANDARD" } output id {value = "${google_storage_bucket.bucket.id}"} output bucket_name {value = "my-bucket"} `, } outputs, err := module.Outputs() if err != nil { panic(err) } fmt.Printf("%v\n", outputs)
Output: [bucket_name id]
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() (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"}, } defnJson, err := instance.MarshalDefinition() fmt.Println(err) fmt.Printf("%s\n", string(defnJson))
Output: <nil> {"module":{"instance":{"foo":"bar","source":"foo-module"}}}
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, 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:"-"` // contains filtered or unexported fields }
TerraformWorkspace represents the directory layout of a Terraform execution. The structure is strict, consiting 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) (*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() error
Apply runs `terraform apply` on this workspace. This funciton blocks if another Terraform command is running on this workspace.
func (*TerraformWorkspace) Destroy ¶
func (workspace *TerraformWorkspace) Destroy() error
Destroy runs `terraform destroy` on this workspace. This funciton 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) Serialize ¶
func (workspace *TerraformWorkspace) Serialize() (string, error)
Serialize converts the TerraformWorkspace into a JSON string.
func (*TerraformWorkspace) String ¶
func (workspace *TerraformWorkspace) String() string
String returns a human-friendly representation of the workspace suitable for printing to the console.
func (*TerraformWorkspace) Validate ¶
func (workspace *TerraformWorkspace) Validate() error
Validate runs `terraform Validate` on this workspace. This funciton blocks if another Terraform command is running on this workspace.
type Tfstate ¶
type Tfstate struct { Version int `json:"version"` Modules []TfstateModule `json:"modules"` }
Tfstate is a struct that can help us deserialize the tfstate JSON file.
func NewTfstate ¶
NewTfstate deserializes a tfstate file.
func (*Tfstate) GetModule ¶
func (state *Tfstate) GetModule(path ...string) *TfstateModule
GetModule gets a module at a given path or nil if none exists for that path.
Example ¶
state := `{ "version": 3, "terraform_version": "0.11.10", "serial": 2, "modules": [ { "path": ["root", "instance"], "outputs": { "Name": { "sensitive": false, "type": "string", "value": "pcf-binding-ex351277" } }, "resources": {}, "depends_on": [] } ] }` tfstate, _ := NewTfstate([]byte(state)) fmt.Printf("%v\n", tfstate.GetModule("does-not-exist")) fmt.Printf("%v\n", tfstate.GetModule("root", "instance"))
Output: <nil> [module: root/instance with 1 outputs]
type TfstateModule ¶
type TfstateModule struct { Path []string `json:"path"` Outputs map[string]struct { Type string `json:"type"` Value interface{} `json:"value"` } `json:"outputs"` }
func (*TfstateModule) GetOutputs ¶
func (module *TfstateModule) GetOutputs() map[string]interface{}
GetOutputs gets the key/value outputs defined for a module.
func (*TfstateModule) String ¶
func (module *TfstateModule) String() string