opentofu/terraform/context_import.go
James Bardin 797a1b339d DebugInfo and DebugGraph
Implement debugInfo and the DebugGraph

DebugInfo will be a global variable through which graph debug
information can we written to a compressed archive. The DebugInfo
methods are all safe for concurrent use, and noop with a nil receiver.
The API outside of the terraform package will be to call SetDebugInfo
to create the archive, and CloseDebugInfo() to properly close the file.
Each write to the archive will be flushed and sync'ed individually, so
in the event of a crash or a missing call to Close, the archive can
still be recovered.

The DebugGraph is a representation of a terraform Graph to be written to
the debug archive, currently in dot format. The DebugGraph also contains
an internal buffer with Printf and Write methods to add to this buffer.
The buffer will be written to an accompanying file in the debug archive
along with the graph.

This also adds a GraphNodeDebugger interface. Any node implementing
`NodeDebug() string` can output information to annotate the debug graph
node, and add the data to the log. This interface may change or be
removed to provide richer options for debugging graph nodes.

The new graph builders all delegate the build to the BasicGraphBuilder.
Having a Name field lets us differentiate the actual builder
implementation in the debug graphs.
2016-11-04 11:30:51 -04:00

69 lines
1.8 KiB
Go

package terraform
import (
"github.com/hashicorp/terraform/config/module"
)
// ImportOpts are used as the configuration for Import.
type ImportOpts struct {
// Targets are the targets to import
Targets []*ImportTarget
// Module is optional, and specifies a config module that is loaded
// into the graph and evaluated. The use case for this is to provide
// provider configuration.
Module *module.Tree
}
// ImportTarget is a single resource to import.
type ImportTarget struct {
// Addr is the full resource address of the resource to import.
// Example: "module.foo.aws_instance.bar"
Addr string
// ID is the ID of the resource to import. This is resource-specific.
ID string
}
// Import takes already-created external resources and brings them
// under Terraform management. Import requires the exact type, name, and ID
// of the resources to import.
//
// This operation is idempotent. If the requested resource is already
// imported, no changes are made to the state.
//
// Further, this operation also gracefully handles partial state. If during
// an import there is a failure, all previously imported resources remain
// imported.
func (c *Context) Import(opts *ImportOpts) (*State, error) {
// Hold a lock since we can modify our own state here
v := c.acquireRun("import")
defer c.releaseRun(v)
// Copy our own state
c.state = c.state.DeepCopy()
// Initialize our graph builder
builder := &ImportGraphBuilder{
ImportTargets: opts.Targets,
Module: opts.Module,
Providers: c.components.ResourceProviders(),
}
// Build the graph!
graph, err := builder.Build(RootModulePath)
if err != nil {
return c.state, err
}
// Walk it
if _, err := c.walk(graph, nil, walkImport); err != nil {
return c.state, err
}
// Clean the state
c.state.prune()
return c.state, nil
}