|
|
|
|
@ -62,6 +62,10 @@ func (t *ConfigTransformer) Transform(g *Graph) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := t.validateImportTargets(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Start the transformation process
|
|
|
|
|
return t.transform(g, t.Config)
|
|
|
|
|
}
|
|
|
|
|
@ -173,13 +177,6 @@ func (t *ConfigTransformer) transformSingle(g *Graph, config *configs.Config) er
|
|
|
|
|
|
|
|
|
|
// If any import targets were not claimed by resources and we are
|
|
|
|
|
// generating configuration, then let's add them into the graph now.
|
|
|
|
|
|
|
|
|
|
// TODO: use diagnostics to collect detailed errors for now, even though we
|
|
|
|
|
// can only return an error from here. This gives the user more immediate
|
|
|
|
|
// feedback, rather than waiting an unknown amount of time for the plan to
|
|
|
|
|
// fail.
|
|
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
|
|
|
|
|
|
for _, i := range importTargets {
|
|
|
|
|
if path.IsRoot() {
|
|
|
|
|
// If we have a single instance import target in the root module, we
|
|
|
|
|
@ -212,8 +209,49 @@ func (t *ConfigTransformer) transformSingle(g *Graph, config *configs.Config) er
|
|
|
|
|
g.Add(node)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// validateImportTargets ensures that the import target resources exist in the
|
|
|
|
|
// configuration. We do this here rather than statically during config loading
|
|
|
|
|
// to have any CLI imports included, and to provide feedback about possible
|
|
|
|
|
// config generation.
|
|
|
|
|
func (t *ConfigTransformer) validateImportTargets() error {
|
|
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
|
|
|
|
|
|
for _, i := range t.importTargets {
|
|
|
|
|
var toResource addrs.ConfigResource
|
|
|
|
|
switch {
|
|
|
|
|
case i.Config != nil:
|
|
|
|
|
toResource = i.Config.ToResource
|
|
|
|
|
default:
|
|
|
|
|
toResource = i.LegacyAddr.ConfigResource()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
moduleCfg := t.Config.Root.Descendent(toResource.Module)
|
|
|
|
|
if moduleCfg != nil {
|
|
|
|
|
res := moduleCfg.Module.ResourceByAddr(toResource.Resource)
|
|
|
|
|
if res != nil {
|
|
|
|
|
// the target config exists, which is all we're looking for at this point.
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if toResource.Module.IsRoot() {
|
|
|
|
|
var toDiags tfdiags.Diagnostics
|
|
|
|
|
traversal, hd := hcl.AbsTraversalForExpr(i.Config.To)
|
|
|
|
|
toDiags = toDiags.Append(hd)
|
|
|
|
|
to, td := addrs.ParseAbsResourceInstance(traversal)
|
|
|
|
|
toDiags = toDiags.Append(td)
|
|
|
|
|
canGenerate := !toDiags.HasErrors() && to.Resource.Key == addrs.NoKey
|
|
|
|
|
|
|
|
|
|
if t.generateConfigPathForImportTargets != "" {
|
|
|
|
|
if canGenerate {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if t.generateConfigPathForImportTargets != "" && !canGenerate {
|
|
|
|
|
diags = diags.Append(&hcl.Diagnostic{
|
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
|
Summary: "Cannot generate configuration",
|
|
|
|
|
|