Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions acceptance/bundle/destroy/all-resources/databricks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
resources:
pipelines:
my_pipeline:
name: test-pipeline
libraries:
- file:
path: "./foo.py"

schemas:
my_schema:
name: test-schema
catalog_name: main
comment: COMMENT1
Empty file.
16 changes: 16 additions & 0 deletions acceptance/bundle/destroy/all-resources/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

>>> [CLI] bundle deploy
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files...
Deploying resources...
Updating deployment state...
Deployment complete!

>>> [CLI] bundle destroy --auto-approve
The following resources will be deleted:
delete pipeline my_pipeline
delete schema my_schema

All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle/default

Deleting files...
Destroy complete!
2 changes: 2 additions & 0 deletions acceptance/bundle/destroy/all-resources/script
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
trace $CLI bundle deploy
trace $CLI bundle destroy --auto-approve
4 changes: 2 additions & 2 deletions bundle/terranova/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (d *Deployer) Create(ctx context.Context, resource tnresources.IResource, c
}

func (d *Deployer) Recreate(ctx context.Context, oldResource tnresources.IResource, oldID string, config any) error {
err := oldResource.DoDelete(ctx, oldID)
err := tnresources.DeleteResource(ctx, d.client, d.group, oldID)
if err != nil {
return fmt.Errorf("deleting old id=%s: %w", oldID, err)
}
Expand Down Expand Up @@ -267,7 +267,7 @@ func (d *Deployer) Update(ctx context.Context, resource tnresources.IResource, o

func (d *Deployer) Delete(ctx context.Context, resource tnresources.IResource, oldID string) error {
// TODO: recognize 404 and 403 as "deleted" and proceed to removing state
err := resource.DoDelete(ctx, oldID)
err := tnresources.DeleteResource(ctx, d.client, d.group, oldID)
if err != nil {
return fmt.Errorf("deleting id=%s: %w", oldID, err)
}
Expand Down
2 changes: 1 addition & 1 deletion bundle/terranova/tnresources/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (r *ResourceApp) DoUpdate(ctx context.Context, id string) (string, error) {
return response.Name, nil
}

func (r *ResourceApp) DoDelete(ctx context.Context, id string) error {
func DeleteApp(ctx context.Context, client *databricks.WorkspaceClient, id string) error {
// TODO: implement app deletion
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions bundle/terranova/tnresources/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ func (r *ResourceJob) DoUpdate(ctx context.Context, id string) (string, error) {
return id, nil
}

func (r *ResourceJob) DoDelete(ctx context.Context, id string) error {
func DeleteJob(ctx context.Context, client *databricks.WorkspaceClient, id string) error {
idInt, err := strconv.ParseInt(id, 10, 64)
if err != nil {
return err
}
err = r.client.Jobs.DeleteByJobId(ctx, idInt)
err = client.Jobs.DeleteByJobId(ctx, idInt)
if err != nil {
return SDKError{Method: "Jobs.DeleteByJobId", Err: err}
}
Expand Down
4 changes: 2 additions & 2 deletions bundle/terranova/tnresources/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func (r *ResourcePipeline) DoUpdate(ctx context.Context, id string) (string, err
return id, nil
}

func (r *ResourcePipeline) DoDelete(ctx context.Context, id string) error {
err := r.client.Pipelines.DeleteByPipelineId(ctx, id)
func DeletePipeline(ctx context.Context, client *databricks.WorkspaceClient, id string) error {
err := client.Pipelines.DeleteByPipelineId(ctx, id)
if err != nil {
return SDKError{Method: "Pipelines.DeleteByPipelineId", Err: err}
}
Expand Down
48 changes: 34 additions & 14 deletions bundle/terranova/tnresources/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,35 @@ import (
"github.com/databricks/databricks-sdk-go"
)

const (
_jobs = "jobs"
_pipelines = "pipelines"
_schemas = "schemas"
_apps = "apps"
)

var supportedResources = map[string]reflect.Value{
"jobs": reflect.ValueOf(NewResourceJob),
"pipelines": reflect.ValueOf(NewResourcePipeline),
"schemas": reflect.ValueOf(NewResourceSchema),
"apps": reflect.ValueOf(NewResourceApp),
_jobs: reflect.ValueOf(NewResourceJob),
_pipelines: reflect.ValueOf(NewResourcePipeline),
_schemas: reflect.ValueOf(NewResourceSchema),
_apps: reflect.ValueOf(NewResourceApp),
}

// This types matches what Config() returns and should match 'config' field in the resource struct
var supportedResourcesTypes = map[string]reflect.Type{
"jobs": reflect.TypeOf(ResourceJob{}.config),
"pipelines": reflect.TypeOf(ResourcePipeline{}.config),
"schemas": reflect.TypeOf(ResourceSchema{}.config),
"apps": reflect.TypeOf(ResourceApp{}.config),
_jobs: reflect.TypeOf(ResourceJob{}.config),
_pipelines: reflect.TypeOf(ResourcePipeline{}.config),
_schemas: reflect.TypeOf(ResourceSchema{}.config),
_apps: reflect.TypeOf(ResourceApp{}.config),
}

type DeleteResourceFN = func(ctx context.Context, client *databricks.WorkspaceClient, oldID string) error

var deletableResources = map[string]DeleteResourceFN{
_jobs: DeleteJob,
_pipelines: DeletePipeline,
_schemas: DeleteSchema,
_apps: DeleteApp,
}

type IResource interface {
Expand All @@ -34,9 +50,7 @@ type IResource interface {

// Update the resource. Returns id of the resource.
// Usually returns the same id as oldId but can also return a different one (e.g. schemas and volumes when certain fields are changed)
DoUpdate(ctx context.Context, oldId string) (string, error)

DoDelete(ctx context.Context, oldId string) error
DoUpdate(ctx context.Context, oldID string) (string, error)

WaitAfterCreate(ctx context.Context) error
WaitAfterUpdate(ctx context.Context) error
Expand All @@ -59,9 +73,7 @@ func invokeConstructor(ctor reflect.Value, client *databricks.WorkspaceClient, c
// Prepare the config value matching the expected type.
var cfgVal reflect.Value
if cfg == nil {
// Treat nil as a request for the zero value of the expected config type. This
// is useful for actions (like deletion) where the config is irrelevant.
cfgVal = reflect.Zero(expectedCfgType)
return nil, errors.New("internal error, config must not be nil")
} else {
suppliedVal := reflect.ValueOf(cfg)
if suppliedVal.Type() != expectedCfgType {
Expand Down Expand Up @@ -116,3 +128,11 @@ func New(client *databricks.WorkspaceClient, group, name string, config any) (IR

return result, cfgType, nil
}

func DeleteResource(ctx context.Context, client *databricks.WorkspaceClient, group, id string) error {
fn, ok := deletableResources[group]
if !ok {
return fmt.Errorf("cannot delete %s", group)
}
return fn(ctx, client, id)
}
2 changes: 1 addition & 1 deletion bundle/terranova/tnresources/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (r *ResourceSchema) DoUpdate(ctx context.Context, id string) (string, error
return response.FullName, nil
}

func (r *ResourceSchema) DoDelete(ctx context.Context, id string) error {
func DeleteSchema(ctx context.Context, client *databricks.WorkspaceClient, id string) error {
// TODO: implement schema deletion
return nil
}
Expand Down
Loading