mirror of https://github.com/databricks/cli.git
Added support for Databricks Apps in DABs
This commit is contained in:
parent
e5b836a6ac
commit
98140b4087
|
@ -222,6 +222,19 @@ func (m *applyPresets) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnos
|
|||
dashboard.DisplayName = prefix + dashboard.DisplayName
|
||||
}
|
||||
|
||||
// Apps: Prefix
|
||||
for key, app := range r.Apps {
|
||||
if app == nil || app.App == nil {
|
||||
diags = diags.Extend(diag.Errorf("app %s is not defined", key))
|
||||
continue
|
||||
}
|
||||
|
||||
app.Name = textutil.NormalizeString(prefix + app.Name)
|
||||
// Normalize the app name to ensure it is a valid identifier.
|
||||
// App supports only alphanumeric characters and hyphens.
|
||||
app.Name = strings.ReplaceAll(app.Name, "_", "-")
|
||||
}
|
||||
|
||||
if config.IsExplicitlyEnabled((b.Config.Presets.SourceLinkedDeployment)) {
|
||||
isDatabricksWorkspace := dbr.RunsOnRuntime(ctx) && strings.HasPrefix(b.SyncRootPath, "/Workspace/")
|
||||
if !isDatabricksWorkspace {
|
||||
|
|
|
@ -12,6 +12,7 @@ import (
|
|||
"github.com/databricks/cli/bundle/internal/bundletest"
|
||||
"github.com/databricks/cli/libs/dbr"
|
||||
"github.com/databricks/cli/libs/dyn"
|
||||
"github.com/databricks/databricks-sdk-go/service/apps"
|
||||
"github.com/databricks/databricks-sdk-go/service/catalog"
|
||||
"github.com/databricks/databricks-sdk-go/service/jobs"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
@ -482,3 +483,59 @@ func TestApplyPresetsSourceLinkedDeployment(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyPresetsPrefixForApps(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
prefix string
|
||||
app *resources.App
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "add prefix to app",
|
||||
prefix: "[prefix] ",
|
||||
app: &resources.App{
|
||||
App: &apps.App{
|
||||
Name: "app1",
|
||||
},
|
||||
},
|
||||
want: "prefix-app1",
|
||||
},
|
||||
{
|
||||
name: "add empty prefix to app",
|
||||
prefix: "",
|
||||
app: &resources.App{
|
||||
App: &apps.App{
|
||||
Name: "app1",
|
||||
},
|
||||
},
|
||||
want: "app1",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
b := &bundle.Bundle{
|
||||
Config: config.Root{
|
||||
Resources: config.Resources{
|
||||
Apps: map[string]*resources.App{
|
||||
"app1": tt.app,
|
||||
},
|
||||
},
|
||||
Presets: config.Presets{
|
||||
NamePrefix: tt.prefix,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
diag := bundle.Apply(ctx, b, mutator.ApplyPresets())
|
||||
|
||||
if diag.HasError() {
|
||||
t.Fatalf("unexpected error: %v", diag)
|
||||
}
|
||||
|
||||
require.Equal(t, tt.want, b.Config.Resources.Apps["app1"].Name)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
package mutator
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/databricks/cli/bundle"
|
||||
"github.com/databricks/cli/libs/diag"
|
||||
"github.com/databricks/cli/libs/dyn"
|
||||
"github.com/databricks/cli/libs/dyn/merge"
|
||||
)
|
||||
|
||||
type mergeApps struct{}
|
||||
|
||||
func MergeApps() bundle.Mutator {
|
||||
return &mergeApps{}
|
||||
}
|
||||
|
||||
func (m *mergeApps) Name() string {
|
||||
return "MergeApps"
|
||||
}
|
||||
|
||||
func (m *mergeApps) resourceName(v dyn.Value) string {
|
||||
switch v.Kind() {
|
||||
case dyn.KindInvalid, dyn.KindNil:
|
||||
return ""
|
||||
case dyn.KindString:
|
||||
return v.MustString()
|
||||
default:
|
||||
panic("job cluster key must be a string")
|
||||
}
|
||||
}
|
||||
|
||||
func (m *mergeApps) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
|
||||
err := b.Config.Mutate(func(v dyn.Value) (dyn.Value, error) {
|
||||
if v.Kind() == dyn.KindNil {
|
||||
return v, nil
|
||||
}
|
||||
|
||||
return dyn.Map(v, "resources.apps", dyn.Foreach(func(_ dyn.Path, app dyn.Value) (dyn.Value, error) {
|
||||
return dyn.Map(app, "resources", merge.ElementsByKey("name", m.resourceName))
|
||||
}))
|
||||
})
|
||||
|
||||
return diag.FromErr(err)
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
package mutator_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/databricks/cli/bundle"
|
||||
"github.com/databricks/cli/bundle/config"
|
||||
"github.com/databricks/cli/bundle/config/mutator"
|
||||
"github.com/databricks/cli/bundle/config/resources"
|
||||
"github.com/databricks/databricks-sdk-go/service/apps"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestMergeApps(t *testing.T) {
|
||||
b := &bundle.Bundle{
|
||||
Config: config.Root{
|
||||
Resources: config.Resources{
|
||||
Apps: map[string]*resources.App{
|
||||
"foo": {
|
||||
App: &apps.App{
|
||||
Name: "foo",
|
||||
Resources: []apps.AppResource{
|
||||
{
|
||||
Name: "job1",
|
||||
Job: &apps.AppResourceJob{
|
||||
Id: "1234",
|
||||
Permission: "CAN_MANAGE_RUN",
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "sql1",
|
||||
SqlWarehouse: &apps.AppResourceSqlWarehouse{
|
||||
Id: "5678",
|
||||
Permission: "CAN_USE",
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "job1",
|
||||
Job: &apps.AppResourceJob{
|
||||
Id: "1234",
|
||||
Permission: "CAN_MANAGE",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
diags := bundle.Apply(context.Background(), b, mutator.MergeApps())
|
||||
assert.NoError(t, diags.Error())
|
||||
|
||||
j := b.Config.Resources.Apps["foo"]
|
||||
|
||||
assert.Len(t, j.Resources, 2)
|
||||
assert.Equal(t, "job1", j.Resources[0].Name)
|
||||
assert.Equal(t, "sql1", j.Resources[1].Name)
|
||||
|
||||
assert.Equal(t, "CAN_MANAGE", string(j.Resources[0].Job.Permission))
|
||||
assert.Equal(t, "CAN_USE", string(j.Resources[1].SqlWarehouse.Permission))
|
||||
}
|
|
@ -15,6 +15,7 @@ import (
|
|||
"github.com/databricks/cli/libs/tags"
|
||||
"github.com/databricks/cli/libs/vfs"
|
||||
sdkconfig "github.com/databricks/databricks-sdk-go/config"
|
||||
"github.com/databricks/databricks-sdk-go/service/apps"
|
||||
"github.com/databricks/databricks-sdk-go/service/catalog"
|
||||
"github.com/databricks/databricks-sdk-go/service/compute"
|
||||
"github.com/databricks/databricks-sdk-go/service/dashboards"
|
||||
|
@ -144,6 +145,13 @@ func mockBundle(mode config.Mode) *bundle.Bundle {
|
|||
},
|
||||
},
|
||||
},
|
||||
Apps: map[string]*resources.App{
|
||||
"app1": {
|
||||
App: &apps.App{
|
||||
Name: "app1",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
SyncRoot: vfs.MustNew("/Users/lennart.kats@databricks.com"),
|
||||
|
|
|
@ -32,6 +32,7 @@ func allResourceTypes(t *testing.T) []string {
|
|||
// the dyn library gives us the correct list of all resources supported. Please
|
||||
// also update this check when adding a new resource
|
||||
require.Equal(t, []string{
|
||||
"apps",
|
||||
"clusters",
|
||||
"dashboards",
|
||||
"experiments",
|
||||
|
@ -143,6 +144,7 @@ func TestRunAsErrorForUnsupportedResources(t *testing.T) {
|
|||
"experiments",
|
||||
"schemas",
|
||||
"volumes",
|
||||
"apps",
|
||||
}
|
||||
|
||||
base := config.Root{
|
||||
|
|
|
@ -262,6 +262,7 @@ func (m *translatePaths) Apply(_ context.Context, b *bundle.Bundle) diag.Diagnos
|
|||
t.applyPipelineTranslations,
|
||||
t.applyArtifactTranslations,
|
||||
t.applyDashboardTranslations,
|
||||
t.applyAppsTranslations,
|
||||
} {
|
||||
v, err = fn(v)
|
||||
if err != nil {
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
package mutator
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/databricks/cli/libs/dyn"
|
||||
)
|
||||
|
||||
func (t *translateContext) applyAppsTranslations(v dyn.Value) (dyn.Value, error) {
|
||||
// Convert the `source_code_path` field to a remote absolute path.
|
||||
// We use this path for app deployment to point to the source code.
|
||||
pattern := dyn.NewPattern(
|
||||
dyn.Key("resources"),
|
||||
dyn.Key("apps"),
|
||||
dyn.AnyKey(),
|
||||
dyn.Key("source_code_path"),
|
||||
)
|
||||
|
||||
return dyn.MapByPattern(v, pattern, func(p dyn.Path, v dyn.Value) (dyn.Value, error) {
|
||||
key := p[2].Key()
|
||||
dir, err := v.Location().Directory()
|
||||
if err != nil {
|
||||
return dyn.InvalidValue, fmt.Errorf("unable to determine directory for app %s: %w", key, err)
|
||||
}
|
||||
|
||||
return t.rewriteRelativeTo(p, v, t.translateDirectoryPath, dir, "")
|
||||
})
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
package mutator_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/databricks/cli/bundle"
|
||||
"github.com/databricks/cli/bundle/config"
|
||||
"github.com/databricks/cli/bundle/config/mutator"
|
||||
"github.com/databricks/cli/bundle/config/resources"
|
||||
"github.com/databricks/cli/bundle/internal/bundletest"
|
||||
"github.com/databricks/cli/libs/dyn"
|
||||
"github.com/databricks/cli/libs/vfs"
|
||||
"github.com/databricks/databricks-sdk-go/service/apps"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestTranslatePathsApps_FilePathRelativeSubDirectory(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
touchEmptyFile(t, filepath.Join(dir, "src", "app", "app.py"))
|
||||
|
||||
b := &bundle.Bundle{
|
||||
SyncRootPath: dir,
|
||||
SyncRoot: vfs.MustNew(dir),
|
||||
Config: config.Root{
|
||||
Workspace: config.Workspace{
|
||||
FilePath: "/bundle/files",
|
||||
},
|
||||
Resources: config.Resources{
|
||||
Apps: map[string]*resources.App{
|
||||
"app": {
|
||||
App: &apps.App{
|
||||
Name: "My App",
|
||||
},
|
||||
SourceCodePath: "../src/app",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
bundletest.SetLocation(b, "resources.apps", []dyn.Location{{
|
||||
File: filepath.Join(dir, "resources/app.yml"),
|
||||
}})
|
||||
|
||||
diags := bundle.Apply(context.Background(), b, mutator.TranslatePaths())
|
||||
require.NoError(t, diags.Error())
|
||||
|
||||
// Assert that the file path for the app has been converted to its local absolute path.
|
||||
assert.Equal(
|
||||
t,
|
||||
"/bundle/files/src/app",
|
||||
b.Config.Resources.Apps["app"].SourceCodePath,
|
||||
)
|
||||
}
|
|
@ -23,6 +23,7 @@ type Resources struct {
|
|||
Volumes map[string]*resources.Volume `json:"volumes,omitempty"`
|
||||
Clusters map[string]*resources.Cluster `json:"clusters,omitempty"`
|
||||
Dashboards map[string]*resources.Dashboard `json:"dashboards,omitempty"`
|
||||
Apps map[string]*resources.App `json:"apps,omitempty"`
|
||||
}
|
||||
|
||||
type ConfigResource interface {
|
||||
|
@ -87,6 +88,7 @@ func (r *Resources) AllResources() []ResourceGroup {
|
|||
collectResourceMap(descriptions["clusters"], r.Clusters),
|
||||
collectResourceMap(descriptions["dashboards"], r.Dashboards),
|
||||
collectResourceMap(descriptions["volumes"], r.Volumes),
|
||||
collectResourceMap(descriptions["apps"], r.Apps),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -196,6 +198,11 @@ func SupportedResources() map[string]ResourceDescription {
|
|||
PluralName: "volumes",
|
||||
SingularTitle: "Volume",
|
||||
PluralTitle: "Volumes",
|
||||
"apps": {
|
||||
SingularName: "app",
|
||||
PluralName: "apps",
|
||||
SingularTitle: "App",
|
||||
PluralTitle: "Apps",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,75 @@
|
|||
package resources
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
|
||||
"github.com/databricks/cli/libs/log"
|
||||
"github.com/databricks/databricks-sdk-go"
|
||||
"github.com/databricks/databricks-sdk-go/marshal"
|
||||
"github.com/databricks/databricks-sdk-go/service/apps"
|
||||
)
|
||||
|
||||
type App struct {
|
||||
// This represents the id which is the name of the app that can be used
|
||||
// as a reference in other resources. This value is returned by terraform.
|
||||
ID string `json:"id,omitempty" bundle:"readonly"`
|
||||
|
||||
// SourceCodePath is a required field used by DABs to point databricks app source code
|
||||
// on local disk and use it to point to this source code in the app deployment
|
||||
SourceCodePath string `json:"source_code_path"`
|
||||
|
||||
// Config is an optional field which allows configuring the app following Databricks app configuration format like in app.yml.
|
||||
// When this field is set, DABs read the configuration set in this field and write
|
||||
// it to app.yml in the root of the source code folder in Databricks workspace.
|
||||
// If there’s app.yml defined already, it will be overridden.
|
||||
Config map[string]interface{} `json:"config,omitempty"`
|
||||
|
||||
Permissions []Permission `json:"permissions,omitempty"`
|
||||
ModifiedStatus ModifiedStatus `json:"modified_status,omitempty" bundle:"internal"`
|
||||
URL string `json:"url,omitempty" bundle:"internal"`
|
||||
|
||||
*apps.App
|
||||
}
|
||||
|
||||
func (a *App) UnmarshalJSON(b []byte) error {
|
||||
return marshal.Unmarshal(b, a)
|
||||
}
|
||||
|
||||
func (a App) MarshalJSON() ([]byte, error) {
|
||||
return marshal.Marshal(a)
|
||||
}
|
||||
|
||||
func (a *App) Exists(ctx context.Context, w *databricks.WorkspaceClient, name string) (bool, error) {
|
||||
_, err := w.Apps.GetByName(ctx, name)
|
||||
if err != nil {
|
||||
log.Debugf(ctx, "app %s does not exist", name)
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (a *App) TerraformResourceName() string {
|
||||
return "databricks_cluster"
|
||||
}
|
||||
|
||||
func (a *App) InitializeURL(baseURL url.URL) {
|
||||
if a.ID == "" {
|
||||
return
|
||||
}
|
||||
baseURL.Path = fmt.Sprintf("apps/%s", a.ID)
|
||||
a.URL = baseURL.String()
|
||||
}
|
||||
|
||||
func (a *App) GetName() string {
|
||||
return a.Name
|
||||
}
|
||||
|
||||
func (a *App) GetURL() string {
|
||||
return a.URL
|
||||
}
|
||||
|
||||
func (a *App) IsNil() bool {
|
||||
return a.App == nil
|
||||
}
|
|
@ -196,6 +196,16 @@ func TerraformToBundle(state *resourcesState, config *config.Root) error {
|
|||
}
|
||||
cur.ID = instance.Attributes.ID
|
||||
config.Resources.Dashboards[resource.Name] = cur
|
||||
case "databricks_app":
|
||||
if config.Resources.Apps == nil {
|
||||
config.Resources.Apps = make(map[string]*resources.App)
|
||||
}
|
||||
cur := config.Resources.Apps[resource.Name]
|
||||
if cur == nil {
|
||||
cur = &resources.App{ModifiedStatus: resources.ModifiedStatusDeleted}
|
||||
}
|
||||
cur.ID = instance.Attributes.ID
|
||||
config.Resources.Apps[resource.Name] = cur
|
||||
case "databricks_permissions":
|
||||
case "databricks_grants":
|
||||
// Ignore; no need to pull these back into the configuration.
|
||||
|
@ -260,6 +270,11 @@ func TerraformToBundle(state *resourcesState, config *config.Root) error {
|
|||
src.ModifiedStatus = resources.ModifiedStatusCreated
|
||||
}
|
||||
}
|
||||
for _, src := range config.Resources.Apps {
|
||||
if src.ModifiedStatus == "" && src.ID == "" {
|
||||
src.ModifiedStatus = resources.ModifiedStatusCreated
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@ import (
|
|||
"github.com/databricks/cli/bundle/internal/tf/schema"
|
||||
"github.com/databricks/cli/libs/dyn"
|
||||
"github.com/databricks/cli/libs/dyn/convert"
|
||||
"github.com/databricks/databricks-sdk-go/service/apps"
|
||||
"github.com/databricks/databricks-sdk-go/service/catalog"
|
||||
"github.com/databricks/databricks-sdk-go/service/compute"
|
||||
"github.com/databricks/databricks-sdk-go/service/dashboards"
|
||||
|
@ -694,6 +695,14 @@ func TestTerraformToBundleEmptyLocalResources(t *testing.T) {
|
|||
{Attributes: stateInstanceAttributes{ID: "1"}},
|
||||
},
|
||||
},
|
||||
{
|
||||
Type: "databricks_app",
|
||||
Mode: "managed",
|
||||
Name: "test_app",
|
||||
Instances: []stateResourceInstance{
|
||||
{Attributes: stateInstanceAttributes{ID: "1"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
err := TerraformToBundle(&tfState, &config)
|
||||
|
@ -732,6 +741,9 @@ func TestTerraformToBundleEmptyLocalResources(t *testing.T) {
|
|||
assert.Equal(t, "1", config.Resources.Dashboards["test_dashboard"].ID)
|
||||
assert.Equal(t, resources.ModifiedStatusDeleted, config.Resources.Dashboards["test_dashboard"].ModifiedStatus)
|
||||
|
||||
assert.Equal(t, "1", config.Resources.Apps["test_app"].ID)
|
||||
assert.Equal(t, resources.ModifiedStatusDeleted, config.Resources.Apps["test_app"].ModifiedStatus)
|
||||
|
||||
AssertFullResourceCoverage(t, &config)
|
||||
}
|
||||
|
||||
|
@ -815,6 +827,13 @@ func TestTerraformToBundleEmptyRemoteResources(t *testing.T) {
|
|||
},
|
||||
},
|
||||
},
|
||||
Apps: map[string]*resources.App{
|
||||
"test_app": {
|
||||
App: &apps.App{
|
||||
Name: "test_app",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
tfState := resourcesState{
|
||||
|
@ -856,6 +875,9 @@ func TestTerraformToBundleEmptyRemoteResources(t *testing.T) {
|
|||
assert.Equal(t, "", config.Resources.Dashboards["test_dashboard"].ID)
|
||||
assert.Equal(t, resources.ModifiedStatusCreated, config.Resources.Dashboards["test_dashboard"].ModifiedStatus)
|
||||
|
||||
assert.Equal(t, "", config.Resources.Apps["test_app"].ID)
|
||||
assert.Equal(t, resources.ModifiedStatusCreated, config.Resources.Apps["test_app"].ModifiedStatus)
|
||||
|
||||
AssertFullResourceCoverage(t, &config)
|
||||
}
|
||||
|
||||
|
@ -994,6 +1016,18 @@ func TestTerraformToBundleModifiedResources(t *testing.T) {
|
|||
},
|
||||
},
|
||||
},
|
||||
Apps: map[string]*resources.App{
|
||||
"test_app": {
|
||||
App: &apps.App{
|
||||
Name: "test_app",
|
||||
},
|
||||
},
|
||||
"test_app_new": {
|
||||
App: &apps.App{
|
||||
Name: "test_app_new",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
tfState := resourcesState{
|
||||
|
@ -1174,6 +1208,22 @@ func TestTerraformToBundleModifiedResources(t *testing.T) {
|
|||
{Attributes: stateInstanceAttributes{ID: "2"}},
|
||||
},
|
||||
},
|
||||
{
|
||||
Type: "databricks_app",
|
||||
Mode: "managed",
|
||||
Name: "test_app",
|
||||
Instances: []stateResourceInstance{
|
||||
{Attributes: stateInstanceAttributes{ID: "1"}},
|
||||
},
|
||||
},
|
||||
{
|
||||
Type: "databricks_app",
|
||||
Mode: "managed",
|
||||
Name: "test_app_old",
|
||||
Instances: []stateResourceInstance{
|
||||
{Attributes: stateInstanceAttributes{ID: "2"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
err := TerraformToBundle(&tfState, &config)
|
||||
|
@ -1256,6 +1306,13 @@ func TestTerraformToBundleModifiedResources(t *testing.T) {
|
|||
assert.Equal(t, "", config.Resources.Dashboards["test_dashboard_new"].ID)
|
||||
assert.Equal(t, resources.ModifiedStatusCreated, config.Resources.Dashboards["test_dashboard_new"].ModifiedStatus)
|
||||
|
||||
assert.Equal(t, "1", config.Resources.Apps["test_app"].ID)
|
||||
assert.Equal(t, "", config.Resources.Apps["test_app"].ModifiedStatus)
|
||||
assert.Equal(t, "2", config.Resources.Apps["test_app_old"].ID)
|
||||
assert.Equal(t, resources.ModifiedStatusDeleted, config.Resources.Apps["test_app_old"].ModifiedStatus)
|
||||
assert.Equal(t, "", config.Resources.Apps["test_app_new"].ID)
|
||||
assert.Equal(t, resources.ModifiedStatusCreated, config.Resources.Apps["test_app_new"].ModifiedStatus)
|
||||
|
||||
AssertFullResourceCoverage(t, &config)
|
||||
}
|
||||
|
||||
|
|
|
@ -63,6 +63,8 @@ func (m *interpolateMutator) Apply(ctx context.Context, b *bundle.Bundle) diag.D
|
|||
path = dyn.NewPath(dyn.Key("databricks_cluster")).Append(path[2:]...)
|
||||
case dyn.Key("dashboards"):
|
||||
path = dyn.NewPath(dyn.Key("databricks_dashboard")).Append(path[2:]...)
|
||||
case dyn.Key("apps"):
|
||||
path = dyn.NewPath(dyn.Key("databricks_app")).Append(path[2:]...)
|
||||
default:
|
||||
// Trigger "key not found" for unknown resource types.
|
||||
return dyn.GetByPath(root, path)
|
||||
|
|
|
@ -34,6 +34,7 @@ func TestInterpolate(t *testing.T) {
|
|||
"other_volume": "${resources.volumes.other_volume.id}",
|
||||
"other_cluster": "${resources.clusters.other_cluster.id}",
|
||||
"other_dashboard": "${resources.dashboards.other_dashboard.id}",
|
||||
"other_app": "${resources.apps.other_app.id}",
|
||||
},
|
||||
Tasks: []jobs.Task{
|
||||
{
|
||||
|
@ -73,6 +74,7 @@ func TestInterpolate(t *testing.T) {
|
|||
assert.Equal(t, "${databricks_volume.other_volume.id}", j.Tags["other_volume"])
|
||||
assert.Equal(t, "${databricks_cluster.other_cluster.id}", j.Tags["other_cluster"])
|
||||
assert.Equal(t, "${databricks_dashboard.other_dashboard.id}", j.Tags["other_dashboard"])
|
||||
assert.Equal(t, "${databricks_app.other_app.id}", j.Tags["other_app"])
|
||||
|
||||
m := b.Config.Resources.Models["my_model"]
|
||||
assert.Equal(t, "my_model", m.Model.Name)
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
package tfdyn
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/databricks/cli/bundle/internal/tf/schema"
|
||||
"github.com/databricks/cli/libs/dyn"
|
||||
"github.com/databricks/cli/libs/dyn/convert"
|
||||
"github.com/databricks/cli/libs/log"
|
||||
"github.com/databricks/databricks-sdk-go/service/apps"
|
||||
)
|
||||
|
||||
func convertAppResource(ctx context.Context, vin dyn.Value) (dyn.Value, error) {
|
||||
// Normalize the output value to the target schema.
|
||||
vout, diags := convert.Normalize(apps.App{}, vin)
|
||||
for _, diag := range diags {
|
||||
log.Debugf(ctx, "app normalization diagnostic: %s", diag.Summary)
|
||||
}
|
||||
|
||||
return vout, nil
|
||||
}
|
||||
|
||||
type appConverter struct{}
|
||||
|
||||
func (appConverter) Convert(ctx context.Context, key string, vin dyn.Value, out *schema.Resources) error {
|
||||
vout, err := convertAppResource(ctx, vin)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Modify top-level keys.
|
||||
vout, err = renameKeys(vout, map[string]string{
|
||||
"resources": "resource",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Add the converted resource to the output.
|
||||
out.App[key] = vout.AsAny()
|
||||
|
||||
// Configure permissions for this resource.
|
||||
if permissions := convertPermissionsResource(ctx, vin); permissions != nil {
|
||||
permissions.AppName = fmt.Sprintf("${databricks_app.%s.name}", key)
|
||||
out.Permissions["app_"+key] = permissions
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
registerConverter("apps", appConverter{})
|
||||
}
|
|
@ -0,0 +1,99 @@
|
|||
package tfdyn
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/databricks/cli/bundle/config/resources"
|
||||
"github.com/databricks/cli/bundle/internal/tf/schema"
|
||||
"github.com/databricks/cli/libs/dyn"
|
||||
"github.com/databricks/cli/libs/dyn/convert"
|
||||
"github.com/databricks/databricks-sdk-go/service/apps"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestConvertApp(t *testing.T) {
|
||||
var src = resources.App{
|
||||
SourceCodePath: "./app",
|
||||
Config: map[string]interface{}{
|
||||
"command": []string{"python", "app.py"},
|
||||
},
|
||||
App: &apps.App{
|
||||
Name: "app_id",
|
||||
Description: "app description",
|
||||
Resources: []apps.AppResource{
|
||||
{
|
||||
Name: "job1",
|
||||
Job: &apps.AppResourceJob{
|
||||
Id: "1234",
|
||||
Permission: "CAN_MANAGE_RUN",
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "sql1",
|
||||
SqlWarehouse: &apps.AppResourceSqlWarehouse{
|
||||
Id: "5678",
|
||||
Permission: "CAN_USE",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Permissions: []resources.Permission{
|
||||
{
|
||||
Level: "CAN_RUN",
|
||||
UserName: "jack@gmail.com",
|
||||
},
|
||||
{
|
||||
Level: "CAN_MANAGE",
|
||||
ServicePrincipalName: "sp",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
vin, err := convert.FromTyped(src, dyn.NilValue)
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := context.Background()
|
||||
out := schema.NewResources()
|
||||
err = appConverter{}.Convert(ctx, "my_app", vin, out)
|
||||
require.NoError(t, err)
|
||||
|
||||
app := out.App["my_app"]
|
||||
assert.Equal(t, map[string]interface{}{
|
||||
"description": "app description",
|
||||
"name": "app_id",
|
||||
"resource": []interface{}{
|
||||
map[string]interface{}{
|
||||
"name": "job1",
|
||||
"job": map[string]interface{}{
|
||||
"id": "1234",
|
||||
"permission": "CAN_MANAGE_RUN",
|
||||
},
|
||||
},
|
||||
map[string]interface{}{
|
||||
"name": "sql1",
|
||||
"sql_warehouse": map[string]interface{}{
|
||||
"id": "5678",
|
||||
"permission": "CAN_USE",
|
||||
},
|
||||
},
|
||||
},
|
||||
}, app)
|
||||
|
||||
// Assert equality on the permissions
|
||||
assert.Equal(t, &schema.ResourcePermissions{
|
||||
AppName: "${databricks_app.my_app.name}",
|
||||
AccessControl: []schema.ResourcePermissionsAccessControl{
|
||||
{
|
||||
PermissionLevel: "CAN_RUN",
|
||||
UserName: "jack@gmail.com",
|
||||
},
|
||||
{
|
||||
PermissionLevel: "CAN_MANAGE",
|
||||
ServicePrincipalName: "sp",
|
||||
},
|
||||
},
|
||||
}, out.Permissions["app_my_app"])
|
||||
|
||||
}
|
|
@ -51,6 +51,10 @@ var (
|
|||
CAN_MANAGE: "CAN_MANAGE",
|
||||
CAN_VIEW: "CAN_READ",
|
||||
},
|
||||
"apps": {
|
||||
CAN_MANAGE: "CAN_MANAGE",
|
||||
CAN_VIEW: "CAN_USE",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
|
|
|
@ -58,6 +58,10 @@ func TestApplyBundlePermissions(t *testing.T) {
|
|||
"dashboard_1": {},
|
||||
"dashboard_2": {},
|
||||
},
|
||||
Apps: map[string]*resources.App{
|
||||
"app_1": {},
|
||||
"app_2": {},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
@ -114,6 +118,10 @@ func TestApplyBundlePermissions(t *testing.T) {
|
|||
require.Len(t, b.Config.Resources.Dashboards["dashboard_1"].Permissions, 2)
|
||||
require.Contains(t, b.Config.Resources.Dashboards["dashboard_1"].Permissions, resources.Permission{Level: "CAN_MANAGE", UserName: "TestUser"})
|
||||
require.Contains(t, b.Config.Resources.Dashboards["dashboard_1"].Permissions, resources.Permission{Level: "CAN_READ", GroupName: "TestGroup"})
|
||||
|
||||
require.Len(t, b.Config.Resources.Apps["app_1"].Permissions, 2)
|
||||
require.Contains(t, b.Config.Resources.Apps["app_1"].Permissions, resources.Permission{Level: "CAN_MANAGE", UserName: "TestUser"})
|
||||
require.Contains(t, b.Config.Resources.Apps["app_1"].Permissions, resources.Permission{Level: "CAN_USE", GroupName: "TestGroup"})
|
||||
}
|
||||
|
||||
func TestWarningOnOverlapPermission(t *testing.T) {
|
||||
|
|
|
@ -37,6 +37,8 @@ func Initialize() bundle.Mutator {
|
|||
mutator.MergeJobParameters(),
|
||||
mutator.MergeJobTasks(),
|
||||
mutator.MergePipelineClusters(),
|
||||
mutator.MergeApps(),
|
||||
|
||||
mutator.InitializeWorkspaceClient(),
|
||||
mutator.PopulateCurrentUser(),
|
||||
mutator.LoadGitDetails(),
|
||||
|
|
|
@ -0,0 +1,260 @@
|
|||
package run
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/databricks/cli/bundle"
|
||||
"github.com/databricks/cli/bundle/config/resources"
|
||||
"github.com/databricks/cli/bundle/deploy"
|
||||
"github.com/databricks/cli/bundle/run/output"
|
||||
"github.com/databricks/cli/libs/cmdio"
|
||||
"github.com/databricks/cli/libs/filer"
|
||||
"github.com/databricks/databricks-sdk-go/service/apps"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func logProgress(ctx context.Context, msg string) {
|
||||
if msg == "" {
|
||||
return
|
||||
}
|
||||
cmdio.LogString(ctx, fmt.Sprintf("✓ %s", msg))
|
||||
}
|
||||
|
||||
type appRunner struct {
|
||||
key
|
||||
|
||||
bundle *bundle.Bundle
|
||||
app *resources.App
|
||||
|
||||
filerFactory deploy.FilerFactory
|
||||
}
|
||||
|
||||
func (a *appRunner) Name() string {
|
||||
if a.app == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
return a.app.Name
|
||||
}
|
||||
|
||||
func isAppStopped(app *apps.App) bool {
|
||||
return app.ComputeStatus == nil ||
|
||||
(app.ComputeStatus.State == apps.ComputeStateStopped || app.ComputeStatus.State == apps.ComputeStateError)
|
||||
}
|
||||
|
||||
func (a *appRunner) Run(ctx context.Context, opts *Options) (output.RunOutput, error) {
|
||||
app := a.app
|
||||
b := a.bundle
|
||||
if app == nil {
|
||||
return nil, fmt.Errorf("app is not defined")
|
||||
}
|
||||
|
||||
logProgress(ctx, fmt.Sprintf("Getting the status of the app %s", app.Name))
|
||||
w := b.WorkspaceClient()
|
||||
|
||||
// Check the status of the app first.
|
||||
createdApp, err := w.Apps.Get(ctx, apps.GetAppRequest{Name: app.Name})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if createdApp.AppStatus != nil {
|
||||
logProgress(ctx, fmt.Sprintf("App is in %s state", createdApp.AppStatus.State))
|
||||
}
|
||||
|
||||
if createdApp.ComputeStatus != nil {
|
||||
logProgress(ctx, fmt.Sprintf("App compute is in %s state", createdApp.ComputeStatus.State))
|
||||
}
|
||||
|
||||
// There could be 2 reasons why the app is not running:
|
||||
// 1. The app is new and was never deployed yet.
|
||||
// 2. The app was stopped (compute not running).
|
||||
// We need to start the app only if the compute is not running.
|
||||
if isAppStopped(createdApp) {
|
||||
err := a.start(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// Deploy the app.
|
||||
err = a.deploy(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// TODO: We should return the app URL here.
|
||||
cmdio.LogString(ctx, "You can access the app at <app-url>")
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (a *appRunner) start(ctx context.Context) error {
|
||||
app := a.app
|
||||
b := a.bundle
|
||||
w := b.WorkspaceClient()
|
||||
|
||||
logProgress(ctx, fmt.Sprintf("Starting the app %s", app.Name))
|
||||
wait, err := w.Apps.Start(ctx, apps.StartAppRequest{Name: app.Name})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
startedApp, err := wait.OnProgress(func(p *apps.App) {
|
||||
if p.AppStatus == nil {
|
||||
return
|
||||
}
|
||||
logProgress(ctx, "App is starting...")
|
||||
}).Get()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// If the app has a pending deployment, wait for it to complete.
|
||||
if startedApp.PendingDeployment != nil {
|
||||
_, err := w.Apps.WaitGetDeploymentAppSucceeded(ctx,
|
||||
startedApp.Name, startedApp.PendingDeployment.DeploymentId,
|
||||
20*time.Minute, nil)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// If the app has an active deployment, wait for it to complete as well
|
||||
if startedApp.ActiveDeployment != nil {
|
||||
_, err := w.Apps.WaitGetDeploymentAppSucceeded(ctx,
|
||||
startedApp.Name, startedApp.ActiveDeployment.DeploymentId,
|
||||
20*time.Minute, nil)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
logProgress(ctx, "App is started!")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *appRunner) deploy(ctx context.Context) error {
|
||||
app := a.app
|
||||
b := a.bundle
|
||||
w := b.WorkspaceClient()
|
||||
|
||||
// If the app has a config, we need to deploy it first.
|
||||
// It means we need to write app.yml file with the content of the config field
|
||||
// to the remote source code path of the app.
|
||||
if app.Config != nil {
|
||||
appPath, err := filepath.Rel(b.Config.Workspace.FilePath, app.SourceCodePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get relative path of app source code path: %w", err)
|
||||
}
|
||||
|
||||
buf, err := configToYaml(app)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// When the app is started, create a new app deployment and wait for it to complete.
|
||||
f, err := a.filerFactory(b)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = f.Write(ctx, path.Join(appPath, "app.yml"), buf, filer.OverwriteIfExists)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to write %s file: %w", path.Join(app.SourceCodePath, "app.yml"), err)
|
||||
}
|
||||
}
|
||||
|
||||
wait, err := w.Apps.Deploy(ctx, apps.CreateAppDeploymentRequest{
|
||||
AppName: app.Name,
|
||||
AppDeployment: &apps.AppDeployment{
|
||||
Mode: apps.AppDeploymentModeSnapshot,
|
||||
SourceCodePath: app.SourceCodePath,
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = wait.OnProgress(func(ad *apps.AppDeployment) {
|
||||
if ad.Status == nil {
|
||||
return
|
||||
}
|
||||
logProgress(ctx, ad.Status.Message)
|
||||
}).Get()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *appRunner) Cancel(ctx context.Context) error {
|
||||
// We should cancel the app by stopping it.
|
||||
app := a.app
|
||||
b := a.bundle
|
||||
if app == nil {
|
||||
return fmt.Errorf("app is not defined")
|
||||
}
|
||||
|
||||
w := b.WorkspaceClient()
|
||||
|
||||
logProgress(ctx, fmt.Sprintf("Stopping app %s", app.Name))
|
||||
wait, err := w.Apps.Stop(ctx, apps.StopAppRequest{Name: app.Name})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = wait.OnProgress(func(p *apps.App) {
|
||||
if p.AppStatus == nil {
|
||||
return
|
||||
}
|
||||
logProgress(ctx, p.AppStatus.Message)
|
||||
}).Get()
|
||||
|
||||
logProgress(ctx, "App is stopped!")
|
||||
return err
|
||||
}
|
||||
|
||||
func (a *appRunner) Restart(ctx context.Context, opts *Options) (output.RunOutput, error) {
|
||||
// We should restart the app by just running it again meaning a new app deployment will be done.
|
||||
return a.Run(ctx, opts)
|
||||
}
|
||||
|
||||
func (a *appRunner) ParseArgs(args []string, opts *Options) error {
|
||||
if len(args) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("received %d unexpected positional arguments", len(args))
|
||||
}
|
||||
|
||||
func (a *appRunner) CompleteArgs(args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||
return nil, cobra.ShellCompDirectiveNoFileComp
|
||||
}
|
||||
|
||||
func configToYaml(app *resources.App) (*bytes.Buffer, error) {
|
||||
buf := bytes.NewBuffer(nil)
|
||||
enc := yaml.NewEncoder(buf)
|
||||
enc.SetIndent(2)
|
||||
|
||||
err := enc.Encode(app.Config)
|
||||
defer enc.Close()
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to encode app config to yaml: %w", err)
|
||||
}
|
||||
|
||||
return buf, nil
|
||||
}
|
|
@ -0,0 +1,214 @@
|
|||
package run
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/databricks/cli/bundle"
|
||||
"github.com/databricks/cli/bundle/config"
|
||||
"github.com/databricks/cli/bundle/config/mutator"
|
||||
"github.com/databricks/cli/bundle/config/resources"
|
||||
"github.com/databricks/cli/bundle/internal/bundletest"
|
||||
mockfiler "github.com/databricks/cli/internal/mocks/libs/filer"
|
||||
"github.com/databricks/cli/libs/cmdio"
|
||||
"github.com/databricks/cli/libs/dyn"
|
||||
"github.com/databricks/cli/libs/filer"
|
||||
"github.com/databricks/cli/libs/flags"
|
||||
"github.com/databricks/cli/libs/vfs"
|
||||
"github.com/databricks/databricks-sdk-go/experimental/mocks"
|
||||
"github.com/databricks/databricks-sdk-go/service/apps"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type testAppRunner struct {
|
||||
m *mocks.MockWorkspaceClient
|
||||
b *bundle.Bundle
|
||||
mockFiler *mockfiler.MockFiler
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func (ta *testAppRunner) run(t *testing.T) {
|
||||
r := appRunner{
|
||||
key: "my_app",
|
||||
bundle: ta.b,
|
||||
app: ta.b.Config.Resources.Apps["my_app"],
|
||||
filerFactory: func(b *bundle.Bundle) (filer.Filer, error) {
|
||||
return ta.mockFiler, nil
|
||||
},
|
||||
}
|
||||
|
||||
_, err := r.Run(ta.ctx, &Options{})
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func setupBundle(t *testing.T) (context.Context, *bundle.Bundle, *mocks.MockWorkspaceClient) {
|
||||
root := t.TempDir()
|
||||
err := os.MkdirAll(filepath.Join(root, "my_app"), 0700)
|
||||
require.NoError(t, err)
|
||||
|
||||
b := &bundle.Bundle{
|
||||
BundleRootPath: root,
|
||||
SyncRoot: vfs.MustNew(root),
|
||||
Config: config.Root{
|
||||
Workspace: config.Workspace{
|
||||
RootPath: "/Workspace/Users/foo@bar.com/",
|
||||
},
|
||||
Resources: config.Resources{
|
||||
Apps: map[string]*resources.App{
|
||||
"my_app": {
|
||||
App: &apps.App{
|
||||
Name: "my_app",
|
||||
},
|
||||
SourceCodePath: "./my_app",
|
||||
Config: map[string]interface{}{
|
||||
"command": []string{"echo", "hello"},
|
||||
"env": []map[string]string{
|
||||
{"name": "MY_APP", "value": "my value"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
mwc := mocks.NewMockWorkspaceClient(t)
|
||||
b.SetWorkpaceClient(mwc.WorkspaceClient)
|
||||
bundletest.SetLocation(b, "resources.apps.my_app", []dyn.Location{{File: "./databricks.yml"}})
|
||||
|
||||
ctx := context.Background()
|
||||
ctx = cmdio.InContext(ctx, cmdio.NewIO(flags.OutputText, &bytes.Buffer{}, &bytes.Buffer{}, &bytes.Buffer{}, "", "..."))
|
||||
ctx = cmdio.NewContext(ctx, cmdio.NewLogger(flags.ModeAppend))
|
||||
|
||||
diags := bundle.Apply(ctx, b, bundle.Seq(
|
||||
mutator.DefineDefaultWorkspacePaths(),
|
||||
mutator.TranslatePaths(),
|
||||
))
|
||||
require.Empty(t, diags)
|
||||
|
||||
return ctx, b, mwc
|
||||
}
|
||||
|
||||
func setupTestApp(t *testing.T, initialAppState apps.ApplicationState, initialComputeState apps.ComputeState) *testAppRunner {
|
||||
ctx, b, mwc := setupBundle(t)
|
||||
|
||||
appApi := mwc.GetMockAppsAPI()
|
||||
appApi.EXPECT().Get(mock.Anything, apps.GetAppRequest{
|
||||
Name: "my_app",
|
||||
}).Return(&apps.App{
|
||||
Name: "my_app",
|
||||
AppStatus: &apps.ApplicationStatus{
|
||||
State: initialAppState,
|
||||
},
|
||||
ComputeStatus: &apps.ComputeStatus{
|
||||
State: initialComputeState,
|
||||
},
|
||||
}, nil)
|
||||
|
||||
wait := &apps.WaitGetDeploymentAppSucceeded[apps.AppDeployment]{
|
||||
Poll: func(_ time.Duration, _ func(*apps.AppDeployment)) (*apps.AppDeployment, error) {
|
||||
return nil, nil
|
||||
},
|
||||
}
|
||||
appApi.EXPECT().Deploy(mock.Anything, apps.CreateAppDeploymentRequest{
|
||||
AppName: "my_app",
|
||||
AppDeployment: &apps.AppDeployment{
|
||||
Mode: apps.AppDeploymentModeSnapshot,
|
||||
SourceCodePath: "/Workspace/Users/foo@bar.com/files/my_app",
|
||||
},
|
||||
}).Return(wait, nil)
|
||||
|
||||
mockFiler := mockfiler.NewMockFiler(t)
|
||||
mockFiler.EXPECT().Write(mock.Anything, "my_app/app.yml", bytes.NewBufferString(`command:
|
||||
- echo
|
||||
- hello
|
||||
env:
|
||||
- name: MY_APP
|
||||
value: my value
|
||||
`), filer.OverwriteIfExists).Return(nil)
|
||||
|
||||
return &testAppRunner{
|
||||
m: mwc,
|
||||
b: b,
|
||||
mockFiler: mockFiler,
|
||||
ctx: ctx,
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppRunStartedApp(t *testing.T) {
|
||||
r := setupTestApp(t, apps.ApplicationStateRunning, apps.ComputeStateActive)
|
||||
r.run(t)
|
||||
}
|
||||
|
||||
func TestAppRunStoppedApp(t *testing.T) {
|
||||
r := setupTestApp(t, apps.ApplicationStateCrashed, apps.ComputeStateStopped)
|
||||
|
||||
appsApi := r.m.GetMockAppsAPI()
|
||||
appsApi.EXPECT().Start(mock.Anything, apps.StartAppRequest{
|
||||
Name: "my_app",
|
||||
}).Return(&apps.WaitGetAppActive[apps.App]{
|
||||
Poll: func(_ time.Duration, _ func(*apps.App)) (*apps.App, error) {
|
||||
return &apps.App{
|
||||
Name: "my_app",
|
||||
AppStatus: &apps.ApplicationStatus{
|
||||
State: apps.ApplicationStateRunning,
|
||||
},
|
||||
ComputeStatus: &apps.ComputeStatus{
|
||||
State: apps.ComputeStateActive,
|
||||
},
|
||||
ActiveDeployment: &apps.AppDeployment{
|
||||
SourceCodePath: "/foo/bar",
|
||||
DeploymentId: "123",
|
||||
Status: &apps.AppDeploymentStatus{
|
||||
State: apps.AppDeploymentStateInProgress,
|
||||
},
|
||||
},
|
||||
PendingDeployment: &apps.AppDeployment{
|
||||
SourceCodePath: "/foo/bar",
|
||||
DeploymentId: "456",
|
||||
Status: &apps.AppDeploymentStatus{
|
||||
State: apps.AppDeploymentStateInProgress,
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
},
|
||||
}, nil)
|
||||
|
||||
appsApi.EXPECT().WaitGetDeploymentAppSucceeded(mock.Anything, "my_app", "123", mock.Anything, mock.Anything).Return(nil, nil)
|
||||
appsApi.EXPECT().WaitGetDeploymentAppSucceeded(mock.Anything, "my_app", "456", mock.Anything, mock.Anything).Return(nil, nil)
|
||||
r.run(t)
|
||||
}
|
||||
|
||||
func TestStopApp(t *testing.T) {
|
||||
ctx, b, mwc := setupBundle(t)
|
||||
appsApi := mwc.GetMockAppsAPI()
|
||||
appsApi.EXPECT().Stop(mock.Anything, apps.StopAppRequest{
|
||||
Name: "my_app",
|
||||
}).Return(&apps.WaitGetAppStopped[apps.App]{
|
||||
Poll: func(_ time.Duration, _ func(*apps.App)) (*apps.App, error) {
|
||||
return &apps.App{
|
||||
Name: "my_app",
|
||||
AppStatus: &apps.ApplicationStatus{
|
||||
State: apps.ApplicationStateUnavailable,
|
||||
},
|
||||
}, nil
|
||||
},
|
||||
}, nil)
|
||||
|
||||
r := appRunner{
|
||||
key: "my_app",
|
||||
bundle: b,
|
||||
app: b.Config.Resources.Apps["my_app"],
|
||||
filerFactory: func(b *bundle.Bundle) (filer.Filer, error) {
|
||||
return nil, nil
|
||||
},
|
||||
}
|
||||
|
||||
err := r.Cancel(ctx)
|
||||
require.NoError(t, err)
|
||||
}
|
|
@ -8,6 +8,7 @@ import (
|
|||
"github.com/databricks/cli/bundle/config/resources"
|
||||
refs "github.com/databricks/cli/bundle/resources"
|
||||
"github.com/databricks/cli/bundle/run/output"
|
||||
"github.com/databricks/cli/libs/filer"
|
||||
)
|
||||
|
||||
type key string
|
||||
|
@ -42,7 +43,7 @@ type Runner interface {
|
|||
// IsRunnable returns a filter that only allows runnable resources.
|
||||
func IsRunnable(ref refs.Reference) bool {
|
||||
switch ref.Resource.(type) {
|
||||
case *resources.Job, *resources.Pipeline:
|
||||
case *resources.Job, *resources.Pipeline, *resources.App:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
|
@ -56,6 +57,15 @@ func ToRunner(b *bundle.Bundle, ref refs.Reference) (Runner, error) {
|
|||
return &jobRunner{key: key(ref.KeyWithType), bundle: b, job: resource}, nil
|
||||
case *resources.Pipeline:
|
||||
return &pipelineRunner{key: key(ref.KeyWithType), bundle: b, pipeline: resource}, nil
|
||||
case *resources.App:
|
||||
return &appRunner{
|
||||
key: key(ref.KeyWithType),
|
||||
bundle: b,
|
||||
app: resource,
|
||||
filerFactory: func(b *bundle.Bundle) (filer.Filer, error) {
|
||||
return filer.NewWorkspaceFilesClient(b.WorkspaceClient(), b.Config.Workspace.FilePath)
|
||||
},
|
||||
}, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported resource type: %T", resource)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,71 @@
|
|||
bundle:
|
||||
name: apps
|
||||
|
||||
workspace:
|
||||
host: https://acme.cloud.databricks.com/
|
||||
|
||||
variables:
|
||||
app_config:
|
||||
type: complex
|
||||
default:
|
||||
command:
|
||||
- "python"
|
||||
- "app.py"
|
||||
env:
|
||||
- name: SOME_ENV_VARIABLE
|
||||
value: "Some value"
|
||||
|
||||
resources:
|
||||
apps:
|
||||
my_app:
|
||||
name: "my-app"
|
||||
description: "My App"
|
||||
source_code_path: ./app
|
||||
config: ${var.app_config}
|
||||
|
||||
resources:
|
||||
- name: "my-sql-warehouse"
|
||||
sql_warehouse:
|
||||
id: 1234
|
||||
permission: "CAN_USE"
|
||||
- name: "my-job"
|
||||
job:
|
||||
id: 5678
|
||||
permission: "CAN_MANAGE_RUN"
|
||||
permissions:
|
||||
- user_name: "foo@bar.com"
|
||||
level: "CAN_VIEW"
|
||||
- service_principal_name: "my_sp"
|
||||
level: "CAN_MANAGE"
|
||||
|
||||
|
||||
targets:
|
||||
default:
|
||||
|
||||
development:
|
||||
variables:
|
||||
app_config:
|
||||
command:
|
||||
- "python"
|
||||
- "dev.py"
|
||||
env:
|
||||
- name: SOME_ENV_VARIABLE_2
|
||||
value: "Some value 2"
|
||||
resources:
|
||||
apps:
|
||||
my_app:
|
||||
source_code_path: ./app-dev
|
||||
resources:
|
||||
- name: "my-sql-warehouse"
|
||||
sql_warehouse:
|
||||
id: 1234
|
||||
permission: "CAN_MANAGE"
|
||||
- name: "my-job"
|
||||
job:
|
||||
id: 5678
|
||||
permission: "CAN_MANAGE"
|
||||
- name: "my-secret"
|
||||
secret:
|
||||
key: "key"
|
||||
scope: "scope"
|
||||
permission: "CAN_USE"
|
|
@ -0,0 +1,61 @@
|
|||
package config_tests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/databricks/cli/bundle"
|
||||
"github.com/databricks/cli/bundle/config/mutator"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestApps(t *testing.T) {
|
||||
b := load(t, "./apps")
|
||||
assert.Equal(t, "apps", b.Config.Bundle.Name)
|
||||
|
||||
diags := bundle.Apply(context.Background(), b,
|
||||
bundle.Seq(
|
||||
mutator.SetVariables(),
|
||||
mutator.ResolveVariableReferences("variables"),
|
||||
))
|
||||
assert.Empty(t, diags)
|
||||
|
||||
app := b.Config.Resources.Apps["my_app"]
|
||||
assert.Equal(t, "my-app", app.Name)
|
||||
assert.Equal(t, "My App", app.Description)
|
||||
assert.Equal(t, []interface{}{"python", "app.py"}, app.Config["command"])
|
||||
assert.Equal(t, []interface{}{map[string]interface{}{"name": "SOME_ENV_VARIABLE", "value": "Some value"}}, app.Config["env"])
|
||||
|
||||
assert.Len(t, app.Resources, 2)
|
||||
assert.Equal(t, "1234", app.Resources[0].SqlWarehouse.Id)
|
||||
assert.Equal(t, "CAN_USE", string(app.Resources[0].SqlWarehouse.Permission))
|
||||
assert.Equal(t, "5678", app.Resources[1].Job.Id)
|
||||
assert.Equal(t, "CAN_MANAGE_RUN", string(app.Resources[1].Job.Permission))
|
||||
}
|
||||
|
||||
func TestAppsOverride(t *testing.T) {
|
||||
b := loadTarget(t, "./apps", "development")
|
||||
assert.Equal(t, "apps", b.Config.Bundle.Name)
|
||||
|
||||
diags := bundle.Apply(context.Background(), b,
|
||||
bundle.Seq(
|
||||
mutator.SetVariables(),
|
||||
mutator.ResolveVariableReferences("variables"),
|
||||
))
|
||||
assert.Empty(t, diags)
|
||||
app := b.Config.Resources.Apps["my_app"]
|
||||
assert.Equal(t, "my-app", app.Name)
|
||||
assert.Equal(t, "My App", app.Description)
|
||||
assert.Equal(t, []interface{}{"python", "dev.py"}, app.Config["command"])
|
||||
assert.Equal(t, []interface{}{map[string]interface{}{"name": "SOME_ENV_VARIABLE_2", "value": "Some value 2"}}, app.Config["env"])
|
||||
|
||||
assert.Len(t, app.Resources, 3)
|
||||
assert.Equal(t, "1234", app.Resources[0].SqlWarehouse.Id)
|
||||
assert.Equal(t, "CAN_MANAGE", string(app.Resources[0].SqlWarehouse.Permission))
|
||||
assert.Equal(t, "5678", app.Resources[1].Job.Id)
|
||||
assert.Equal(t, "CAN_MANAGE", string(app.Resources[1].Job.Permission))
|
||||
assert.Equal(t, "key", app.Resources[2].Secret.Key)
|
||||
assert.Equal(t, "scope", app.Resources[2].Secret.Scope)
|
||||
assert.Equal(t, "CAN_USE", string(app.Resources[2].Secret.Permission))
|
||||
|
||||
}
|
|
@ -46,6 +46,7 @@ func loadTargetWithDiags(path, env string) (*bundle.Bundle, diag.Diagnostics) {
|
|||
mutator.MergeJobParameters(),
|
||||
mutator.MergeJobTasks(),
|
||||
mutator.MergePipelineClusters(),
|
||||
mutator.MergeApps(),
|
||||
))
|
||||
return b, diags
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue