-
Notifications
You must be signed in to change notification settings - Fork 154
Added bundle deployment bind and unbind command
#1131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
ceb62c0
Added bundle bind command
andrewnester 952d660
added confirm plan and unbind command
andrewnester 8075ae1
use import block
andrewnester c65aa6c
Added e2e test
andrewnester d8fc45e
Merge branch 'main' into bind-command
andrewnester 852e7bc
fix build
andrewnester 2cfee73
push state
andrewnester b318a42
Merge branch 'main' into bind-command
andrewnester 7f795ec
fix tests
andrewnester 4e8d030
addressed feedback
andrewnester 637ceb3
added lint ignore
andrewnester b837d0b
Added test for bind abort
andrewnester 4822806
use -target flag
andrewnester 1962a68
use os.MkdirTemp
andrewnester 04071d6
always write state file
andrewnester 6cbdde2
added test for generate + bind
andrewnester 022ebc4
fix comment
andrewnester 7f8e3b4
fix pipeline test
andrewnester File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| package terraform | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/databricks/cli/bundle" | ||
| "github.com/databricks/cli/libs/cmdio" | ||
| "github.com/hashicorp/terraform-exec/tfexec" | ||
| ) | ||
|
|
||
| type BindOptions struct { | ||
| AutoApprove bool | ||
| ResourceType string | ||
| ResourceKey string | ||
| ResourceId string | ||
| } | ||
|
|
||
| type importResource struct { | ||
| opts *BindOptions | ||
| } | ||
|
|
||
| // Apply implements bundle.Mutator. | ||
| func (m *importResource) Apply(ctx context.Context, b *bundle.Bundle) error { | ||
| dir, err := Dir(ctx, b) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| tf := b.Terraform | ||
| if tf == nil { | ||
| return fmt.Errorf("terraform not initialized") | ||
| } | ||
|
|
||
| err = tf.Init(ctx, tfexec.Upgrade(true)) | ||
| if err != nil { | ||
| return fmt.Errorf("terraform init: %w", err) | ||
| } | ||
| tmpDir, err := os.MkdirTemp("", "state-*") | ||
| if err != nil { | ||
| return fmt.Errorf("terraform init: %w", err) | ||
| } | ||
| tmpState := filepath.Join(tmpDir, TerraformStateFileName) | ||
|
|
||
| importAddress := fmt.Sprintf("%s.%s", m.opts.ResourceType, m.opts.ResourceKey) | ||
| err = tf.Import(ctx, importAddress, m.opts.ResourceId, tfexec.StateOut(tmpState)) | ||
| if err != nil { | ||
| return fmt.Errorf("terraform import: %w", err) | ||
| } | ||
|
|
||
| buf := bytes.NewBuffer(nil) | ||
| tf.SetStdout(buf) | ||
|
|
||
| //lint:ignore SA1019 We use legacy -state flag for now to plan the import changes based on temporary state file | ||
| changed, err := tf.Plan(ctx, tfexec.State(tmpState), tfexec.Target(importAddress)) | ||
| if err != nil { | ||
| return fmt.Errorf("terraform plan: %w", err) | ||
| } | ||
|
|
||
| defer os.RemoveAll(tmpDir) | ||
|
|
||
| if changed && !m.opts.AutoApprove { | ||
| output := buf.String() | ||
| // Remove output starting from Warning until end of output | ||
| output = output[:bytes.Index([]byte(output), []byte("Warning:"))] | ||
| cmdio.LogString(ctx, output) | ||
| ans, err := cmdio.AskYesOrNo(ctx, "Confirm import changes? Changes will be remotely applied only after running 'bundle deploy'.") | ||
| if err != nil { | ||
| return err | ||
andrewnester marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| if !ans { | ||
| return fmt.Errorf("import aborted") | ||
| } | ||
| } | ||
|
|
||
| // If user confirmed changes, move the state file from temp dir to state location | ||
| f, err := os.Create(filepath.Join(dir, TerraformStateFileName)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer f.Close() | ||
|
|
||
| tmpF, err := os.Open(tmpState) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer tmpF.Close() | ||
|
|
||
| _, err = io.Copy(f, tmpF) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // Name implements bundle.Mutator. | ||
| func (*importResource) Name() string { | ||
| return "terraform.Import" | ||
| } | ||
|
|
||
| func Import(opts *BindOptions) bundle.Mutator { | ||
| return &importResource{opts: opts} | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package terraform | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/databricks/cli/bundle" | ||
| "github.com/hashicorp/terraform-exec/tfexec" | ||
| ) | ||
|
|
||
| type unbind struct { | ||
| resourceType string | ||
| resourceKey string | ||
| } | ||
|
|
||
| func (m *unbind) Apply(ctx context.Context, b *bundle.Bundle) error { | ||
| tf := b.Terraform | ||
| if tf == nil { | ||
| return fmt.Errorf("terraform not initialized") | ||
| } | ||
|
|
||
| err := tf.Init(ctx, tfexec.Upgrade(true)) | ||
| if err != nil { | ||
| return fmt.Errorf("terraform init: %w", err) | ||
| } | ||
|
|
||
| err = tf.StateRm(ctx, fmt.Sprintf("%s.%s", m.resourceType, m.resourceKey)) | ||
| if err != nil { | ||
| return fmt.Errorf("terraform state rm: %w", err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (*unbind) Name() string { | ||
| return "terraform.Unbind" | ||
| } | ||
|
|
||
| func Unbind(resourceType string, resourceKey string) bundle.Mutator { | ||
| return &unbind{resourceType: resourceType, resourceKey: resourceKey} | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package phases | ||
|
|
||
| import ( | ||
| "github.com/databricks/cli/bundle" | ||
| "github.com/databricks/cli/bundle/deploy/lock" | ||
| "github.com/databricks/cli/bundle/deploy/terraform" | ||
| ) | ||
|
|
||
| func Bind(opts *terraform.BindOptions) bundle.Mutator { | ||
| return newPhase( | ||
| "bind", | ||
| []bundle.Mutator{ | ||
| lock.Acquire(), | ||
| bundle.Defer( | ||
| bundle.Seq( | ||
| terraform.StatePull(), | ||
| terraform.Interpolate(), | ||
| terraform.Write(), | ||
| terraform.Import(opts), | ||
andrewnester marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| terraform.StatePush(), | ||
| ), | ||
| lock.Release(lock.GoalBind), | ||
| ), | ||
| }, | ||
| ) | ||
| } | ||
|
|
||
| func Unbind(resourceType string, resourceKey string) bundle.Mutator { | ||
| return newPhase( | ||
| "unbind", | ||
| []bundle.Mutator{ | ||
| lock.Acquire(), | ||
| bundle.Defer( | ||
| bundle.Seq( | ||
| terraform.StatePull(), | ||
| terraform.Interpolate(), | ||
| terraform.Write(), | ||
| terraform.Unbind(resourceType, resourceKey), | ||
| terraform.StatePush(), | ||
| ), | ||
| lock.Release(lock.GoalUnbind), | ||
| ), | ||
| }, | ||
| ) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For later; we shouldn't externalize the TF resource name as it doesn't correspond with the name in the configuration. In error messages we should refer to
jobs.job_keylike we do for the run args.