terraform/plans/plan.go

78 lines
2.3 KiB
Go
Raw Normal View History

package plans
various: helpers for collecting necessary provider types Since schemas are required to interpret provider, resource, and provisioner attributes in configs, states, and plans, these helpers intend to make it easier to gather up the the necessary provider types in order to preload all of the needed schemas before beginning further processing. Config.ProviderTypes returns directly the list of provider types, since at this level further detail is not useful: we've not yet run the provider allocation algorithm, and so the only thing we can reliably extract here is provider types themselves. State.ProviderAddrs and Plan.ProviderAddrs each return a list of absolute provider addresses, which can then be turned into a list of provider types using the new helper providers.AddressedTypesAbs. Since we're already using configs.Config throughout core, this also updates the terraform.LoadSchemas helper to use Config.ProviderTypes to find the necessary providers, rather than implementing its own discovery logic. states.State is not yet plumbed in, so we cannot yet use State.ProviderAddrs to deal with the state but there's a TODO comment to remind us to update that in a later commit when we swap out terraform.State for states.State. A later commit will probably refactor this further so that we can easily obtain schema for the providers needed to interpret a plan too, but that is deferred here because further work is required to make core work with the new plan types first. At that point, terraform.LoadSchemas may become providers.LoadSchemas with a different interface that just accepts lists of provider and provisioner names that have been gathered by the caller using these new helpers.
2018-06-22 02:39:27 +02:00
import (
"sort"
"github.com/hashicorp/terraform/addrs"
)
// Plan is the top-level type representing a planned set of changes.
//
// A plan is a summary of the set of changes required to move from a current
// state to a goal state derived from configuration. The described changes
// are not applied directly, but contain an approximation of the final
// result that will be completed during apply by resolving any values that
// cannot be predicted.
//
// A plan must always be accompanied by the state and configuration it was
// built from, since the plan does not itself include all of the information
// required to make the changes indicated.
type Plan struct {
VariableValues map[string]DynamicValue
Changes *Changes
TargetAddrs []addrs.Targetable
ProviderSHA256s map[string][]byte
Backend Backend
}
// Backend represents the backend-related configuration and other data as it
// existed when a plan was created.
type Backend struct {
// Type is the type of backend that the plan will apply against.
Type string
// Config is the configuration of the backend, whose schema is decided by
// the backend Type.
Config DynamicValue
// Workspace is the name of the workspace that was active when the plan
// was created. It is illegal to apply a plan created for one workspace
// to the state of another workspace.
// (This constraint is already enforced by the statefile lineage mechanism,
// but storing this explicitly allows us to return a better error message
// in the situation where the user has the wrong workspace selected.)
Workspace string
}
various: helpers for collecting necessary provider types Since schemas are required to interpret provider, resource, and provisioner attributes in configs, states, and plans, these helpers intend to make it easier to gather up the the necessary provider types in order to preload all of the needed schemas before beginning further processing. Config.ProviderTypes returns directly the list of provider types, since at this level further detail is not useful: we've not yet run the provider allocation algorithm, and so the only thing we can reliably extract here is provider types themselves. State.ProviderAddrs and Plan.ProviderAddrs each return a list of absolute provider addresses, which can then be turned into a list of provider types using the new helper providers.AddressedTypesAbs. Since we're already using configs.Config throughout core, this also updates the terraform.LoadSchemas helper to use Config.ProviderTypes to find the necessary providers, rather than implementing its own discovery logic. states.State is not yet plumbed in, so we cannot yet use State.ProviderAddrs to deal with the state but there's a TODO comment to remind us to update that in a later commit when we swap out terraform.State for states.State. A later commit will probably refactor this further so that we can easily obtain schema for the providers needed to interpret a plan too, but that is deferred here because further work is required to make core work with the new plan types first. At that point, terraform.LoadSchemas may become providers.LoadSchemas with a different interface that just accepts lists of provider and provisioner names that have been gathered by the caller using these new helpers.
2018-06-22 02:39:27 +02:00
// ProviderAddrs returns a list of all of the provider configuration addresses
// referenced throughout the receiving plan.
//
// The result is de-duplicated so that each distinct address appears only once.
func (p *Plan) ProviderAddrs() []addrs.AbsProviderConfig {
if p == nil || p.Changes == nil {
return nil
}
m := map[string]addrs.AbsProviderConfig{}
for _, rc := range p.Changes.Resources {
m[rc.ProviderAddr.String()] = rc.ProviderAddr
}
if len(m) == 0 {
return nil
}
// This is mainly just so we'll get stable results for testing purposes.
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
ret := make([]addrs.AbsProviderConfig, len(keys))
for i, key := range keys {
ret[i] = m[key]
}
return ret
}