|
| 1 | +// Package backend provides interfaces that the CLI uses to interact with |
| 2 | +// Terraform. A backend provides the abstraction that allows the same CLI |
| 3 | +// to simultaneously support both local and remote operations for seamlessly |
| 4 | +// using Terraform in a team environment. |
| 5 | +package backend |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform/config/module" |
| 11 | + "github.com/hashicorp/terraform/state" |
| 12 | + "github.com/hashicorp/terraform/terraform" |
| 13 | +) |
| 14 | + |
| 15 | +// Backend is the minimal interface that must be implemented to enable Terraform. |
| 16 | +type Backend interface { |
| 17 | + // Ask for input and configure the backend. Similar to |
| 18 | + // terraform.ResourceProvider. |
| 19 | + Input(terraform.UIInput, *terraform.ResourceConfig) (*terraform.ResourceConfig, error) |
| 20 | + Validate(*terraform.ResourceConfig) ([]string, []error) |
| 21 | + Configure(*terraform.ResourceConfig) error |
| 22 | + |
| 23 | + // State returns the current state for this environment. This state may |
| 24 | + // not be loaded locally: the proper APIs should be called on state.State |
| 25 | + // to load the state. |
| 26 | + State() (state.State, error) |
| 27 | +} |
| 28 | + |
| 29 | +// Enhanced implements additional behavior on top of a normal backend. |
| 30 | +// |
| 31 | +// Enhanced backends allow customizing the behavior of Terraform operations. |
| 32 | +// This allows Terraform to potentially run operations remotely, load |
| 33 | +// configurations from external sources, etc. |
| 34 | +type Enhanced interface { |
| 35 | + Backend |
| 36 | + |
| 37 | + // Operation performs a Terraform operation such as refresh, plan, apply. |
| 38 | + // It is up to the implementation to determine what "performing" means. |
| 39 | + // This DOES NOT BLOCK. The context returned as part of RunningOperation |
| 40 | + // should be used to block for completion. |
| 41 | + Operation(context.Context, *Operation) (*RunningOperation, error) |
| 42 | +} |
| 43 | + |
| 44 | +// Local implements additional behavior on a Backend that allows local |
| 45 | +// operations in addition to remote operations. |
| 46 | +// |
| 47 | +// This enables more behaviors of Terraform that require more data such |
| 48 | +// as `console`, `import`, `graph`. These require direct access to |
| 49 | +// configurations, variables, and more. Not all backends may support this |
| 50 | +// so we separate it out into its own optional interface. |
| 51 | +type Local interface { |
| 52 | + // Context returns a runnable terraform Context. The operation parameter |
| 53 | + // doesn't need a Type set but it needs other options set such as Module. |
| 54 | + Context(*Operation) (*terraform.Context, state.State, error) |
| 55 | +} |
| 56 | + |
| 57 | +// An operation represents an operation for Terraform to execute. |
| 58 | +// |
| 59 | +// Note that not all fields are supported by all backends and can result |
| 60 | +// in an error if set. All backend implementations should show user-friendly |
| 61 | +// errors explaining any incorrectly set values. For example, the local |
| 62 | +// backend doesn't support a PlanId being set. |
| 63 | +// |
| 64 | +// The operation options are purposely designed to have maximal compatibility |
| 65 | +// between Terraform and Terraform Servers (a commercial product offered by |
| 66 | +// HashiCorp). Therefore, it isn't expected that other implementation support |
| 67 | +// every possible option. The struct here is generalized in order to allow |
| 68 | +// even partial implementations to exist in the open, without walling off |
| 69 | +// remote functionality 100% behind a commercial wall. Anyone can implement |
| 70 | +// against this interface and have Terraform interact with it just as it |
| 71 | +// would with HashiCorp-provided Terraform Servers. |
| 72 | +type Operation struct { |
| 73 | + // Type is the operation to perform. |
| 74 | + Type OperationType |
| 75 | + |
| 76 | + // PlanId is an opaque value that backends can use to execute a specific |
| 77 | + // plan for an apply operation. |
| 78 | + // |
| 79 | + // PlanOutBackend is the backend to store with the plan. This is the |
| 80 | + // backend that will be used when applying the plan. |
| 81 | + PlanId string |
| 82 | + PlanRefresh bool // PlanRefresh will do a refresh before a plan |
| 83 | + PlanOutPath string // PlanOutPath is the path to save the plan |
| 84 | + PlanOutBackend *terraform.BackendState |
| 85 | + |
| 86 | + // Module settings specify the root module to use for operations. |
| 87 | + Module *module.Tree |
| 88 | + |
| 89 | + // Plan is a plan that was passed as an argument. This is valid for |
| 90 | + // plan and apply arguments but may not work for all backends. |
| 91 | + Plan *terraform.Plan |
| 92 | + |
| 93 | + // The options below are more self-explanatory and affect the runtime |
| 94 | + // behavior of the operation. |
| 95 | + Destroy bool |
| 96 | + Targets []string |
| 97 | + Variables map[string]interface{} |
| 98 | + |
| 99 | + // Input/output/control options. |
| 100 | + UIIn terraform.UIInput |
| 101 | + UIOut terraform.UIOutput |
| 102 | +} |
| 103 | + |
| 104 | +// RunningOperation is the result of starting an operation. |
| 105 | +type RunningOperation struct { |
| 106 | + // Context should be used to track Done and Err for errors. |
| 107 | + // |
| 108 | + // For implementers of a backend, this context should not wrap the |
| 109 | + // passed in context. Otherwise, canceling the parent context will |
| 110 | + // immediately mark this context as "done" but those aren't the semantics |
| 111 | + // we want: we want this context to be done only when the operation itself |
| 112 | + // is fully done. |
| 113 | + context.Context |
| 114 | + |
| 115 | + // Err is the error of the operation. This is populated after |
| 116 | + // the operation has completed. |
| 117 | + Err error |
| 118 | + |
| 119 | + // PlanEmpty is populated after a Plan operation completes without error |
| 120 | + // to note whether a plan is empty or has changes. |
| 121 | + PlanEmpty bool |
| 122 | + |
| 123 | + // State is the final state after the operation completed. Persisting |
| 124 | + // this state is managed by the backend. This should only be read |
| 125 | + // after the operation completes to avoid read/write races. |
| 126 | + State *terraform.State |
| 127 | +} |
0 commit comments