-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Changes Added `bundle deployment bind` and `unbind` command. This command allows to bind bundle-defined resources to existing resources in Databricks workspace so they become DABs-managed. ## Tests Manually + added E2E test
- Loading branch information
1 parent
e8b0698
commit 80670ec
Showing
25 changed files
with
643 additions
and
34 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 | ||
} | ||
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 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 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), | ||
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 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 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 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.