terraform

package
v2.28.0 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultBackendStorageSettings = BackendStorageSettings{
	CreateResourceGroup:             true,
	RequireInfrastructureEncryption: true,
	Tags:                            map[string]string{},
	BlobContainerName:               "tfstate",
	BlobContainerKey:                "terraform.tfstate",
	AllowedIpAddresses:              []string{},
	ContainerCreateRetryCount:       10,
}
View Source
var DefaultDeploymentSettings = DeploymentSettings{
	AlwaysCleanLocalCache: true,
}

Functions

This section is empty.

Types

type BackendStorageSettings

type BackendStorageSettings struct {
	CreateResourceGroup bool
	// RequireInfrastructureEncryption adds another layer of encryption to the storage account
	RequireInfrastructureEncryption bool
	Tags                            map[string]string
	BlobContainerName               string
	BlobContainerKey                string
	// List of IPs or CIDRs to be added to network accesslist. Networking restrictions are applied when first IP or CIDR given
	// Small address ranges using "/31" or "/32" prefix sizes are not supported.
	// These ranges should be configured using individual IP address rules without prefix specified.
	AllowedIpAddresses        []string
	ContainerCreateRetryCount uint
}

type DeploymentSettings

type DeploymentSettings struct {
	AlwaysCleanLocalCache bool
}

type Terraform

type Terraform interface {
	// Init initializes the Terraform project, also creating the backend remote storage, and the Azure resource group
	// (if not overridden via BackendStorageSettings)
	Init() error

	// SetVariables is required, before any of the following methods like PlanDeploy or Deploy are called. Variables set
	// will be applied on any subsequent operation. Parameters are:
	//     terraformVariables (this is a map of terraform variables, defined as string keys, and serialized via json serializer,
	//         which means any complex or simple type is supported. Simply set the value to whatever type you want, as long as
	//         it is properly serializable. For example, make sure complex types have the required json or mapstruct annotations,
	//         and keep in mind that only the public struct members will be serialized!)
	SetVariables(terraformVariables map[string]interface{}) error

	// DeployFlow is the method which most deployments should use. It provides a single method, which can be used for all
	// IaC cases, like local deployment, or deployment in CI systems.
	// Parameters support are
	//     planOnly (will only create the plan)
	//     useExistingPlan (will reuse existing plan from the disk)
	//     autoApprove (will skip asking user questions, if required, like approving a plan before apply. Defaults to false.
	//                  Can be safely set to true on operations which don't prompt any user inputs, it will just have zero effect on the behaviour).
	// Here is a short explanation:
	//     DeployFlow(false, false, false) will show the plan, prompt the user, and apply if confirmed (setting autoApprove to true will skip confirmation)
	//     DeployFlow(true, false, false) will only show the plan, but also persist it on disk (use GetDeployPlanFileName() for details)
	//     DeployFlow(false, true, false) will reuse the plan already saved on disk, and apply it without any user confirmations (autoApprove makes no impact here)
	//     DeployFlow(true, true, false) will just show the plan persisted on the disk, without generating a new plan.
	// It is best practice to set the both planOnly and useExistingPlan from the CLI, so that CI scripts can simply override
	// the variables depending on the current CI step (usually a plan is presented, user is awaited for approval, then the existing
	// plan is applied). The autoApprove parameter is useful in local deployment scenarios, where you plan / deploy everything as one step,
	// and perhaps do not want to be prompted.
	DeployFlow(planOnly bool, useExistingPlan bool, autoApprove bool) error

	// DestroyFlow is same as DeployFlow, but only for destroy.
	DestroyFlow(planOnly bool, useExistingPlan bool, autoApprove bool) error

	// PlanDeploy executes the terraform plan for deployment, returning the changes as a string. Plan output is always
	// saved to a file as well. Common pattern is to show the changes to the user, ask for confirmation, and then to Deploy the plan.
	// For this purpose, you could use the DeployFlow method.
	PlanDeploy() (string, error)

	// PlanDestroy is same as PlanDeploy, but only for destroy. Consider using DestroyFlow method as well.
	PlanDestroy() (string, error)

	// ForceDeploy deploys the plan persisted on disk via PlanDeploy. User will not be asked for any confirmations, so it is
	// your job in code to present the plan, and prompt for confirmation! For this purpose, you can use the DeployFlow method.
	ForceDeploy() error

	// ForceDestroy is same as ForceDeploy, but only for destroy. Consider using DestroyFlow method as well.
	ForceDestroy() error

	// GetBackendStorageSettings returns the backend remote state storage settings, which can be read or modified if desired
	GetBackendStorageSettings() *BackendStorageSettings

	// GetDeploymentSettings returns the current deployment settings, which can be read or modified if desired
	GetDeploymentSettings() *DeploymentSettings

	// GetVariablesFileName returns the file name in which the terraform variables will be stored. This name is convention based
	// on the currently set project parameter while creating the terraform wrapper instance
	GetVariablesFileName() string

	// Output returns the terraform provided output if any
	// Parameters are:
	// 		parameterName will restrict output to single output parameter and output the parameter in raw mode. If not set all available output is given
	Output(parameterName *string) (string, error)
}

Terraform is a wrapper around common terraform functionality used in IaC projects with Azure. In includes remote state backend configuration and other best practices when dealing with terraform.

func New

func New(executor commands.Executor, projectName string,
	subscriptionId string, tenantId string, region string,
	resourceGroupName string, stateStorageAccountName string, terraformDirectory string,
	backendStorageSettings BackendStorageSettings, deploymentSettings DeploymentSettings) Terraform

New creates a new instance of Terraform, which is a wrapper around common Terraform functionality. In includes remote state backend configuration and other best practices when dealing with terraform. Parameters:

executor (can be provided from hq.GetExecutor() or by instantiating your own),
projectName (a pseudo-name for your terraform deployment, useful in case you have multiple terraform projects
    in one IaC. In doubt, set this to "app" if you only have a single terraform project),
subscriptionId and tenantId (required for terraform state setup)
region (Azure region, e.g. westeurope, required for terraform state setup)
resourceGroupName (name of the resource group where the terraform state will be stored. The resource group will
    also be created per default, unless overridden via backendStorageSettings. It is recommended to
    use the naming.Service to generate this name)
stateStorageAccountName (name of the storage account where the terraform state will be stored. It is recommended to
    use the naming.Service to generate this name)
terraformDirectory (directory where your terraform resources are stored. To construct the full path, simply use
    filepath.Join() method and the hq.ProjectBasePath)
backendStorageSettings (various settings which can be read or set for terraform backend setup, but it is best not
    to override these. Simply set to terraform.DefaultBackendStorageSettings)
deploymentSettings (various settings which can be read or set for terraform deployments, but it is best not
    to override these. Simply set to terraform.DefaultDeploymentSettings

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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