|
| 1 | +package terraform |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + |
| 11 | + "github.com/databricks/cli/bundle" |
| 12 | + "github.com/databricks/cli/libs/cmdio" |
| 13 | + "github.com/hashicorp/terraform-exec/tfexec" |
| 14 | +) |
| 15 | + |
| 16 | +type BindOptions struct { |
| 17 | + AutoApprove bool |
| 18 | + ResourceType string |
| 19 | + ResourceKey string |
| 20 | + ResourceId string |
| 21 | +} |
| 22 | + |
| 23 | +type importResource struct { |
| 24 | + opts *BindOptions |
| 25 | +} |
| 26 | + |
| 27 | +// Apply implements bundle.Mutator. |
| 28 | +func (m *importResource) Apply(ctx context.Context, b *bundle.Bundle) error { |
| 29 | + dir, err := Dir(ctx, b) |
| 30 | + if err != nil { |
| 31 | + return err |
| 32 | + } |
| 33 | + |
| 34 | + tf := b.Terraform |
| 35 | + if tf == nil { |
| 36 | + return fmt.Errorf("terraform not initialized") |
| 37 | + } |
| 38 | + |
| 39 | + err = tf.Init(ctx, tfexec.Upgrade(true)) |
| 40 | + if err != nil { |
| 41 | + return fmt.Errorf("terraform init: %w", err) |
| 42 | + } |
| 43 | + tmpDir, err := os.MkdirTemp("", "state-*") |
| 44 | + if err != nil { |
| 45 | + return fmt.Errorf("terraform init: %w", err) |
| 46 | + } |
| 47 | + tmpState := filepath.Join(tmpDir, TerraformStateFileName) |
| 48 | + |
| 49 | + importAddress := fmt.Sprintf("%s.%s", m.opts.ResourceType, m.opts.ResourceKey) |
| 50 | + err = tf.Import(ctx, importAddress, m.opts.ResourceId, tfexec.StateOut(tmpState)) |
| 51 | + if err != nil { |
| 52 | + return fmt.Errorf("terraform import: %w", err) |
| 53 | + } |
| 54 | + |
| 55 | + buf := bytes.NewBuffer(nil) |
| 56 | + tf.SetStdout(buf) |
| 57 | + |
| 58 | + //lint:ignore SA1019 We use legacy -state flag for now to plan the import changes based on temporary state file |
| 59 | + changed, err := tf.Plan(ctx, tfexec.State(tmpState), tfexec.Target(importAddress)) |
| 60 | + if err != nil { |
| 61 | + return fmt.Errorf("terraform plan: %w", err) |
| 62 | + } |
| 63 | + |
| 64 | + defer os.RemoveAll(tmpDir) |
| 65 | + |
| 66 | + if changed && !m.opts.AutoApprove { |
| 67 | + output := buf.String() |
| 68 | + // Remove output starting from Warning until end of output |
| 69 | + output = output[:bytes.Index([]byte(output), []byte("Warning:"))] |
| 70 | + cmdio.LogString(ctx, output) |
| 71 | + ans, err := cmdio.AskYesOrNo(ctx, "Confirm import changes? Changes will be remotely applied only after running 'bundle deploy'.") |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + if !ans { |
| 76 | + return fmt.Errorf("import aborted") |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // If user confirmed changes, move the state file from temp dir to state location |
| 81 | + f, err := os.Create(filepath.Join(dir, TerraformStateFileName)) |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + defer f.Close() |
| 86 | + |
| 87 | + tmpF, err := os.Open(tmpState) |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + defer tmpF.Close() |
| 92 | + |
| 93 | + _, err = io.Copy(f, tmpF) |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + |
| 98 | + return nil |
| 99 | +} |
| 100 | + |
| 101 | +// Name implements bundle.Mutator. |
| 102 | +func (*importResource) Name() string { |
| 103 | + return "terraform.Import" |
| 104 | +} |
| 105 | + |
| 106 | +func Import(opts *BindOptions) bundle.Mutator { |
| 107 | + return &importResource{opts: opts} |
| 108 | +} |
0 commit comments