Skip to content

Commit

Permalink
support for auto-approve
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyas-goenka committed Jul 30, 2024
1 parent aa5f4fa commit 9752431
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 21 deletions.
5 changes: 4 additions & 1 deletion cmd/bundle/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ func newDeployCommand() *cobra.Command {
var forceLock bool
var failOnActiveRuns bool
var computeID string
var autoApprove bool
cmd.Flags().BoolVar(&force, "force", false, "Force-override Git branch validation.")
cmd.Flags().BoolVar(&forceLock, "force-lock", false, "Force acquisition of deployment lock.")
cmd.Flags().BoolVar(&failOnActiveRuns, "fail-on-active-runs", false, "Fail if there are running jobs or pipelines in the deployment.")
cmd.Flags().StringVarP(&computeID, "compute-id", "c", "", "Override compute in the deployment with the given compute ID.")
cmd.Flags().BoolVar(&autoApprove, "auto-approve", false, "Skip interactive approvals that might be required for deployment.")

cmd.RunE = func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
Expand All @@ -37,10 +39,11 @@ func newDeployCommand() *cobra.Command {
bundle.ApplyFunc(ctx, b, func(context.Context, *bundle.Bundle) diag.Diagnostics {
b.Config.Bundle.Force = force
b.Config.Bundle.Deployment.Lock.Force = forceLock
b.AutoApprove = autoApprove

if cmd.Flag("compute-id").Changed {
b.Config.Bundle.ComputeID = computeID
}

if cmd.Flag("fail-on-active-runs").Changed {
b.Config.Bundle.Deployment.FailOnActiveRuns = failOnActiveRuns
}
Expand Down
12 changes: 6 additions & 6 deletions internal/bundle/bundles/uc_schema/template/databricks.yml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ resources:
path: ./nb.sql
development: true
catalog: main
target: ${resources.schemas.bar.id}

schemas:
bar:
name: test-schema-{{.unique_id}}
catalog_name: main
comment: This schema was created from DABs
include:
- "*.yml"

targets:
development:
default: true
13 changes: 13 additions & 0 deletions internal/bundle/bundles/uc_schema/template/schema.yml.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
resources:
schemas:
bar:
name: test-schema-{{.unique_id}}
catalog_name: main
comment: This schema was created from DABs

targets:
development:
resources:
pipelines:
foo:
target: ${resources.schemas.bar.id}
73 changes: 60 additions & 13 deletions internal/bundle/deploy_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
package bundle

import (
"context"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"

"github.com/databricks/cli/internal/acc"
"github.com/databricks/databricks-sdk-go"
"github.com/databricks/databricks-sdk-go/apierr"
"github.com/databricks/databricks-sdk-go/service/catalog"
"github.com/databricks/databricks-sdk-go/service/files"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestAccBundleDeployUcSchema(t *testing.T) {
ctx, wt := acc.UcWorkspaceTest(t)
w := wt.W

uniqueId := uuid.New().String()
func setupUcSchemaBundle(t *testing.T, ctx context.Context, w *databricks.WorkspaceClient, uniqueId string) string {
bundleRoot, err := initTestTemplate(t, ctx, "uc_schema", map[string]any{
"unique_id": uniqueId,
})
Expand All @@ -29,27 +34,69 @@ func TestAccBundleDeployUcSchema(t *testing.T) {
})

// Assert the schema is created
schemaName := "main.test-schema-" + uniqueId
schema, err := w.Schemas.GetByFullName(ctx, schemaName)
catalogName := "main"
schemaName := "test-schema-" + uniqueId
schema, err := w.Schemas.GetByFullName(ctx, strings.Join([]string{catalogName, schemaName}, "."))
require.NoError(t, err)
assert.Equal(t, schema.FullName, schemaName)
assert.Equal(t, schema.Comment, "This schema was created from DABs")
require.Equal(t, strings.Join([]string{catalogName, schemaName}, "."), schema.FullName)
require.Equal(t, "This schema was created from DABs", schema.Comment)

// Assert the pipeline is created
pipelineName := "test-pipeline-" + uniqueId
pipeline, err := w.Pipelines.GetByName(ctx, pipelineName)
require.NoError(t, err)
assert.Equal(t, pipeline.Name, pipelineName)
require.Equal(t, pipelineName, pipeline.Name)
id := pipeline.PipelineId

// Assert the pipeline uses the schema
i, err := w.Pipelines.GetByPipelineId(ctx, id)
require.NoError(t, err)
assert.Equal(t, i.Spec.Catalog, "main")
assert.Equal(t, i.Spec.Target, "test-schema-"+uniqueId)
require.Equal(t, catalogName, i.Spec.Catalog)
require.Equal(t, strings.Join([]string{catalogName, schemaName}, "."), i.Spec.Target)

// Create a volume in the schema, and add a file to it. This ensure that the
// schema as some data in it and deletion will fail unless the generated
// terraform configuration has force_destroy set to true.
volumeName := "test-volume-" + uniqueId
volume, err := w.Volumes.Create(ctx, catalog.CreateVolumeRequestContent{
CatalogName: catalogName,
SchemaName: schemaName,
Name: volumeName,
VolumeType: catalog.VolumeTypeManaged,
})
require.NoError(t, err)
require.Equal(t, volume.Name, volumeName)

fileName := "test-file-" + uniqueId
err = w.Files.Upload(ctx, files.UploadRequest{
Contents: io.NopCloser(strings.NewReader("Hello, world!")),
FilePath: fmt.Sprintf("/Volumes/%s/%s/%s/%s", catalogName, schemaName, volumeName, fileName),
})
require.NoError(t, err)

return bundleRoot
}

func TestAccBundleDeployUcSchema(t *testing.T) {
ctx, wt := acc.UcWorkspaceTest(t)
w := wt.W

uniqueId := uuid.New().String()
schemaName := "test-schema-" + uniqueId
catalogName := "main"

bundleRoot := setupUcSchemaBundle(t, ctx, w, uniqueId)

// Remove the UC schema from the resource configuration.
err := os.Remove(filepath.Join(bundleRoot, "schema.yml"))
require.NoError(t, err)

// Redeploy the bundle
err = deployBundle(t, ctx, bundleRoot)
require.NoError(t, err)

// Assert the schema is deleted
_, err = w.Schemas.GetByFullName(ctx, schemaName)
_, err = w.Schemas.GetByFullName(ctx, strings.Join([]string{catalogName, schemaName}, "."))
apiErr := &apierr.APIError{}
assert.True(t, errors.As(err, &apiErr))
assert.Equal(t, "SCHEMA_DOES_NOT_EXIST", apiErr.ErrorCode)
Expand Down
2 changes: 1 addition & 1 deletion internal/bundle/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func validateBundle(t *testing.T, ctx context.Context, path string) ([]byte, err

func deployBundle(t *testing.T, ctx context.Context, path string) error {
t.Setenv("BUNDLE_ROOT", path)
c := internal.NewCobraTestRunnerWithContext(t, ctx, "bundle", "deploy", "--force-lock")
c := internal.NewCobraTestRunnerWithContext(t, ctx, "bundle", "deploy", "--force-lock", "--auto-approve")
_, _, err := c.Run()
return err
}
Expand Down

0 comments on commit 9752431

Please sign in to comment.