diff --git a/.nextchanges/bundles/app-thumbnail.md b/.nextchanges/bundles/app-thumbnail.md new file mode 100644 index 00000000000..a58fdca22a1 --- /dev/null +++ b/.nextchanges/bundles/app-thumbnail.md @@ -0,0 +1 @@ +* Add `thumbnail_path` to the `apps` resource, so `bundle deploy` sets, updates and deletes the thumbnail shown for the app in the Apps UI. ([#7728](https://github.com/databricks/cli/pull/7728)) diff --git a/acceptance/bundle/refschema/output/apps.txt b/acceptance/bundle/refschema/output/apps.txt index 9ab8ebb19aa..cf74fb702eb 100644 --- a/acceptance/bundle/refschema/output/apps.txt +++ b/acceptance/bundle/refschema/output/apps.txt @@ -230,6 +230,9 @@ resources.apps.*.telemetry_export_destinations[*].unity_catalog *apps.UnityCatal resources.apps.*.telemetry_export_destinations[*].unity_catalog.logs_table string ALL resources.apps.*.telemetry_export_destinations[*].unity_catalog.metrics_table string ALL resources.apps.*.telemetry_export_destinations[*].unity_catalog.traces_table string ALL +resources.apps.*.thumbnail string STATE +resources.apps.*.thumbnail_content string INPUT +resources.apps.*.thumbnail_path string INPUT resources.apps.*.thumbnail_url string ALL resources.apps.*.update_time string ALL resources.apps.*.updater string ALL diff --git a/acceptance/bundle/resources/apps/thumbnail-invalid/databricks.yml.tmpl b/acceptance/bundle/resources/apps/thumbnail-invalid/databricks.yml.tmpl new file mode 100644 index 00000000000..6094f957d5d --- /dev/null +++ b/acceptance/bundle/resources/apps/thumbnail-invalid/databricks.yml.tmpl @@ -0,0 +1,9 @@ +bundle: + name: test-bundle + +resources: + apps: + myapp: + name: myappname + source_code_path: . + thumbnail_path: $THUMBNAIL_PATH diff --git a/acceptance/bundle/resources/apps/thumbnail-invalid/out.test.toml b/acceptance/bundle/resources/apps/thumbnail-invalid/out.test.toml new file mode 100644 index 00000000000..0f167effef1 --- /dev/null +++ b/acceptance/bundle/resources/apps/thumbnail-invalid/out.test.toml @@ -0,0 +1,2 @@ +Cloud = true +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/resources/apps/thumbnail-invalid/output.txt b/acceptance/bundle/resources/apps/thumbnail-invalid/output.txt new file mode 100644 index 00000000000..bee13d5ac7c --- /dev/null +++ b/acceptance/bundle/resources/apps/thumbnail-invalid/output.txt @@ -0,0 +1,42 @@ + +=== thumbnail_path: ./thumbnail.gif + +>>> musterr [CLI] bundle validate +Error: app thumbnail thumbnail.gif must be a PNG or JPEG image, got image/gif + in databricks.yml:9:23 + +Name: test-bundle +Target: default +Workspace: + User: [USERNAME] + Path: /Workspace/Users/[USERNAME]/.bundle/test-bundle/default + +Found 1 error + +=== thumbnail_path: ./large.png + +>>> musterr [CLI] bundle validate +Error: app thumbnail large.png is 250001 bytes, larger than the limit of 250000 bytes + in databricks.yml:9:23 + +Name: test-bundle +Target: default +Workspace: + User: [USERNAME] + Path: /Workspace/Users/[USERNAME]/.bundle/test-bundle/default + +Found 1 error + +=== thumbnail_path: ./missing.png + +>>> musterr [CLI] bundle validate +Error: failed to read app thumbnail missing.png: open missing.png: no such file or directory + in databricks.yml:9:23 + +Name: test-bundle +Target: default +Workspace: + User: [USERNAME] + Path: /Workspace/Users/[USERNAME]/.bundle/test-bundle/default + +Found 1 error diff --git a/acceptance/bundle/resources/apps/thumbnail-invalid/script b/acceptance/bundle/resources/apps/thumbnail-invalid/script new file mode 100644 index 00000000000..980a4a49fd5 --- /dev/null +++ b/acceptance/bundle/resources/apps/thumbnail-invalid/script @@ -0,0 +1,9 @@ +# One byte over the 250,000-byte limit, with a PNG signature. +python3 -c "import sys; sys.stdout.buffer.write(b'\x89PNG\r\n\x1a\n' + bytes(250_001 - 8))" > large.png + +for THUMBNAIL_PATH in ./thumbnail.gif ./large.png ./missing.png; do + title "thumbnail_path: $THUMBNAIL_PATH" + export THUMBNAIL_PATH + envsubst < databricks.yml.tmpl > databricks.yml + trace musterr $CLI bundle validate +done diff --git a/acceptance/bundle/resources/apps/thumbnail-invalid/test.toml b/acceptance/bundle/resources/apps/thumbnail-invalid/test.toml new file mode 100644 index 00000000000..01ca44e6cb1 --- /dev/null +++ b/acceptance/bundle/resources/apps/thumbnail-invalid/test.toml @@ -0,0 +1,7 @@ +RecordRequests = false +Cloud = true +Ignore = ["large.png"] + +[[Repls]] +Old = 'The system cannot find the file specified.' +New = 'no such file or directory' diff --git a/acceptance/bundle/resources/apps/thumbnail-invalid/thumbnail.gif b/acceptance/bundle/resources/apps/thumbnail-invalid/thumbnail.gif new file mode 100644 index 00000000000..c98b9a76c6f Binary files /dev/null and b/acceptance/bundle/resources/apps/thumbnail-invalid/thumbnail.gif differ diff --git a/acceptance/bundle/resources/apps/thumbnail-upload-fails/databricks.yml b/acceptance/bundle/resources/apps/thumbnail-upload-fails/databricks.yml new file mode 100644 index 00000000000..946d3f18574 --- /dev/null +++ b/acceptance/bundle/resources/apps/thumbnail-upload-fails/databricks.yml @@ -0,0 +1,9 @@ +bundle: + name: test-bundle + +resources: + apps: + mykey: + name: myappname + source_code_path: . + thumbnail_path: ./thumbnail.png diff --git a/acceptance/bundle/resources/apps/thumbnail-upload-fails/out.test.toml b/acceptance/bundle/resources/apps/thumbnail-upload-fails/out.test.toml new file mode 100644 index 00000000000..a927a5fbc06 --- /dev/null +++ b/acceptance/bundle/resources/apps/thumbnail-upload-fails/out.test.toml @@ -0,0 +1,2 @@ +Cloud = false +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/resources/apps/thumbnail-upload-fails/output.txt b/acceptance/bundle/resources/apps/thumbnail-upload-fails/output.txt new file mode 100644 index 00000000000..0278091c6ab --- /dev/null +++ b/acceptance/bundle/resources/apps/thumbnail-upload-fails/output.txt @@ -0,0 +1,38 @@ + +=== A failed upload after create keeps the app in state and leaves the thumbnail out of it + +>>> [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files... +Warning: failed to upload the thumbnail of app myappname, the next deploy retries it: Unsupported image +Created apps.mykey +Files: 2 uploaded, 0 deleted +Resources: 1 created, 0 changed, 0 deleted, 0 unchanged + +>>> jq .state["resources.apps.mykey"].state.thumbnail .databricks/bundle/default/resources.json +null + +=== The next deploy retries the upload + +>>> [CLI] bundle plan +update apps.mykey + +Plan: 0 to add, 1 to change, 0 to delete, 0 unchanged + +>>> musterr [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files... +Error: cannot update resources.apps.mykey: updating id=myappname: Unsupported image (400 INVALID_PARAMETER_VALUE) + +Endpoint: PATCH [DATABRICKS_URL]/api/2.0/apps/myappname/thumbnail +HTTP Status: 400 Bad Request +API error_code: INVALID_PARAMETER_VALUE +API message: Unsupported image + +Files: 0 uploaded, 0 deleted + +>>> [CLI] bundle destroy --auto-approve +The following resources will be deleted: + delete resources.apps.mykey + +All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle/default + +Destroy: 1 deleted diff --git a/acceptance/bundle/resources/apps/thumbnail-upload-fails/script b/acceptance/bundle/resources/apps/thumbnail-upload-fails/script new file mode 100644 index 00000000000..1c7508569e2 --- /dev/null +++ b/acceptance/bundle/resources/apps/thumbnail-upload-fails/script @@ -0,0 +1,12 @@ +cleanup() { + trace $CLI bundle destroy --auto-approve +} +trap cleanup EXIT + +title "A failed upload after create keeps the app in state and leaves the thumbnail out of it" +trace $CLI bundle deploy +trace jq '.state["resources.apps.mykey"].state.thumbnail' .databricks/bundle/default/resources.json + +title "The next deploy retries the upload" +trace $CLI bundle plan +trace musterr $CLI bundle deploy diff --git a/acceptance/bundle/resources/apps/thumbnail-upload-fails/test.toml b/acceptance/bundle/resources/apps/thumbnail-upload-fails/test.toml new file mode 100644 index 00000000000..07e5fff4dcc --- /dev/null +++ b/acceptance/bundle/resources/apps/thumbnail-upload-fails/test.toml @@ -0,0 +1,7 @@ +RecordRequests = false +Ignore = [".databricks"] + +[[Server]] +Pattern = "PATCH /api/2.0/apps/{name}/thumbnail" +Response.StatusCode = 400 +Response.Body = '''{"error_code": "INVALID_PARAMETER_VALUE", "message": "Unsupported image"}''' diff --git a/acceptance/bundle/resources/apps/thumbnail-upload-fails/thumbnail.png b/acceptance/bundle/resources/apps/thumbnail-upload-fails/thumbnail.png new file mode 100644 index 00000000000..221d0325199 Binary files /dev/null and b/acceptance/bundle/resources/apps/thumbnail-upload-fails/thumbnail.png differ diff --git a/acceptance/bundle/resources/apps/thumbnail/app/app.py b/acceptance/bundle/resources/apps/thumbnail/app/app.py new file mode 100644 index 00000000000..f1a18139c84 --- /dev/null +++ b/acceptance/bundle/resources/apps/thumbnail/app/app.py @@ -0,0 +1 @@ +print("Hello world!") diff --git a/acceptance/bundle/resources/apps/thumbnail/blue.png b/acceptance/bundle/resources/apps/thumbnail/blue.png new file mode 100644 index 00000000000..221d0325199 Binary files /dev/null and b/acceptance/bundle/resources/apps/thumbnail/blue.png differ diff --git a/acceptance/bundle/resources/apps/thumbnail/databricks.yml b/acceptance/bundle/resources/apps/thumbnail/databricks.yml new file mode 100644 index 00000000000..6e4dccc4aaf --- /dev/null +++ b/acceptance/bundle/resources/apps/thumbnail/databricks.yml @@ -0,0 +1,9 @@ +bundle: + name: test-bundle + +resources: + apps: + mykey: + name: myappname + source_code_path: ./app + thumbnail_path: ./thumbnail.png diff --git a/acceptance/bundle/resources/apps/thumbnail/out.requests.json b/acceptance/bundle/resources/apps/thumbnail/out.requests.json new file mode 100644 index 00000000000..8b5e958b16f --- /dev/null +++ b/acceptance/bundle/resources/apps/thumbnail/out.requests.json @@ -0,0 +1,38 @@ +{ + "method": "POST", + "path": "/api/2.0/apps", + "q": { + "no_compute": "true" + }, + "body": { + "description": "", + "name": "myappname", + "source_code_path": "/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files/app" + } +} +{ + "method": "PATCH", + "path": "/api/2.0/apps/myappname/thumbnail", + "body": { + "app_thumbnail": { + "thumbnail": "iVBORw0KGgoAAAANSUhEUgAAACAAAAASCAIAAAC1qksFAAAAHklEQVR42mNgYPhPYzRqwagFoxaMWjBqwagFdLAAAM9QPd+sTeHpAAAAAElFTkSuQmCC" + } + } +} +{ + "method": "PATCH", + "path": "/api/2.0/apps/myappname/thumbnail", + "body": { + "app_thumbnail": { + "thumbnail": "iVBORw0KGgoAAAANSUhEUgAAACAAAAASCAIAAAC1qksFAAAAH0lEQVR42mP4z8BAU8QwasGoBaMWjFowasGoBfSwAABLGz3fTe+jHwAAAABJRU5ErkJggg==" + } + } +} +{ + "method": "DELETE", + "path": "/api/2.0/apps/myappname/thumbnail" +} +{ + "method": "DELETE", + "path": "/api/2.0/apps/myappname" +} diff --git a/acceptance/bundle/resources/apps/thumbnail/out.test.toml b/acceptance/bundle/resources/apps/thumbnail/out.test.toml new file mode 100644 index 00000000000..a927a5fbc06 --- /dev/null +++ b/acceptance/bundle/resources/apps/thumbnail/out.test.toml @@ -0,0 +1,2 @@ +Cloud = false +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/resources/apps/thumbnail/output.txt b/acceptance/bundle/resources/apps/thumbnail/output.txt new file mode 100644 index 00000000000..f919b65231a --- /dev/null +++ b/acceptance/bundle/resources/apps/thumbnail/output.txt @@ -0,0 +1,84 @@ + +=== Deploy with a thumbnail: the image is uploaded after the app is created + +>>> [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files... +Created apps.mykey +Files: 5 uploaded, 0 deleted +Resources: 1 created, 0 changed, 0 deleted, 0 unchanged + +>>> print_requests + +>>> [CLI] apps get myappname +"/ajax-api/2.0/thumbnails/raw/apps/1000/SMALL_DEFAULT.png" + +=== State keeps only a hash of the image + +>>> jq .state["resources.apps.mykey"].state.thumbnail .databricks/bundle/default/resources.json +"sha256:[HASH]" + +=== Redeploy with an unchanged image: no thumbnail request + +>>> [CLI] bundle plan +Plan: 0 to add, 0 to change, 0 to delete, 1 unchanged + +>>> [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files... +Files: 1 uploaded, 0 deleted +Resources: 0 created, 0 changed, 0 deleted, 1 unchanged + +>>> print_requests + +=== New content at the same path: only the thumbnail is uploaded + +>>> [CLI] bundle plan +update apps.mykey + +Plan: 0 to add, 1 to change, 0 to delete, 0 unchanged + +>>> [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files... +Updated apps.mykey +Files: 1 uploaded, 0 deleted +Resources: 0 created, 1 changed, 0 deleted, 0 unchanged + +>>> print_requests + +=== Deploy from a saved plan: the plan carries the image, not its hash + +>>> [CLI] bundle deploy --plan tmp.plan.json +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files... +Updated apps.mykey +Files: 3 uploaded, 0 deleted +Resources: 0 created, 1 changed, 0 deleted, 0 unchanged + +>>> print_requests.py //apps ^//api/2.0/bundle +iVBORw0KGgoA + +=== Remove thumbnail_path: the thumbnail is deleted + +>>> [CLI] bundle plan +update apps.mykey + +Plan: 0 to add, 1 to change, 0 to delete, 0 unchanged + +>>> [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files... +Updated apps.mykey +Files: 1 uploaded, 0 deleted +Resources: 0 created, 1 changed, 0 deleted, 0 unchanged + +>>> print_requests + +>>> [CLI] apps get myappname +null + +>>> [CLI] bundle destroy --auto-approve +The following resources will be deleted: + delete resources.apps.mykey + +All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle/default + +Destroy: 1 deleted + +>>> print_requests diff --git a/acceptance/bundle/resources/apps/thumbnail/red.png b/acceptance/bundle/resources/apps/thumbnail/red.png new file mode 100644 index 00000000000..d7a119ce4bf Binary files /dev/null and b/acceptance/bundle/resources/apps/thumbnail/red.png differ diff --git a/acceptance/bundle/resources/apps/thumbnail/script b/acceptance/bundle/resources/apps/thumbnail/script new file mode 100644 index 00000000000..f83addb80cc --- /dev/null +++ b/acceptance/bundle/resources/apps/thumbnail/script @@ -0,0 +1,43 @@ +print_requests() { + print_requests.py //apps '^//api/2.0/bundle' +} + +cleanup() { + trace $CLI bundle destroy --auto-approve + trace print_requests >> out.requests.json +} +trap cleanup EXIT + +cp blue.png thumbnail.png + +title "Deploy with a thumbnail: the image is uploaded after the app is created" +trace $CLI bundle deploy +trace print_requests > out.requests.json +trace $CLI apps get myappname | jq .thumbnail_url + +title "State keeps only a hash of the image" +trace jq '.state["resources.apps.mykey"].state.thumbnail' .databricks/bundle/default/resources.json + +title "Redeploy with an unchanged image: no thumbnail request" +trace $CLI bundle plan +trace $CLI bundle deploy +trace print_requests >> out.requests.json + +title "New content at the same path: only the thumbnail is uploaded" +cp red.png thumbnail.png +trace $CLI bundle plan +trace $CLI bundle deploy +trace print_requests >> out.requests.json + +title "Deploy from a saved plan: the plan carries the image, not its hash" +cp blue.png thumbnail.png +$CLI bundle plan -o json > tmp.plan.json +trace $CLI bundle deploy --plan tmp.plan.json +trace print_requests.py //apps '^//api/2.0/bundle' | jq -r '.body.app_thumbnail.thumbnail | .[0:12]' + +title "Remove thumbnail_path: the thumbnail is deleted" +update_file.py databricks.yml $' thumbnail_path: ./thumbnail.png\n' '' +trace $CLI bundle plan +trace $CLI bundle deploy +trace print_requests >> out.requests.json +trace $CLI apps get myappname | jq .thumbnail_url diff --git a/acceptance/bundle/resources/apps/thumbnail/test.toml b/acceptance/bundle/resources/apps/thumbnail/test.toml new file mode 100644 index 00000000000..81f10da5319 --- /dev/null +++ b/acceptance/bundle/resources/apps/thumbnail/test.toml @@ -0,0 +1 @@ +Ignore = [".databricks", "thumbnail.png", "tmp.plan.json"] diff --git a/bundle/config/mutator/paths/app_paths_visitor.go b/bundle/config/mutator/paths/app_paths_visitor.go index 21a7e467ae2..d604af0e721 100644 --- a/bundle/config/mutator/paths/app_paths_visitor.go +++ b/bundle/config/mutator/paths/app_paths_visitor.go @@ -5,14 +5,28 @@ import ( ) func VisitAppPaths(value dyn.Value, fn VisitFunc) (dyn.Value, error) { - pattern := dyn.NewPattern( + sourceCodePattern := dyn.NewPattern( dyn.Key("resources"), dyn.Key("apps"), dyn.AnyKey(), dyn.Key("source_code_path"), ) - return dyn.MapByPattern(value, pattern, func(path dyn.Path, value dyn.Value) (dyn.Value, error) { + value, err := dyn.MapByPattern(value, sourceCodePattern, func(path dyn.Path, value dyn.Value) (dyn.Value, error) { return fn(path, TranslateModeDirectory, value) }) + if err != nil { + return dyn.InvalidValue, err + } + + thumbnailPattern := dyn.NewPattern( + dyn.Key("resources"), + dyn.Key("apps"), + dyn.AnyKey(), + dyn.Key("thumbnail_path"), + ) + + return dyn.MapByPattern(value, thumbnailPattern, func(path dyn.Path, value dyn.Value) (dyn.Value, error) { + return fn(path, TranslateModeLocalRelative, value) + }) } diff --git a/bundle/config/mutator/paths/app_paths_visitor_test.go b/bundle/config/mutator/paths/app_paths_visitor_test.go index 9ec01aeaf83..a66180fb59a 100644 --- a/bundle/config/mutator/paths/app_paths_visitor_test.go +++ b/bundle/config/mutator/paths/app_paths_visitor_test.go @@ -18,6 +18,7 @@ func TestAppPathsVisitor(t *testing.T) { App: apps.App{ SourceCodePath: "foo", }, + ThumbnailPath: "thumbnail.png", }, }, }, @@ -26,6 +27,7 @@ func TestAppPathsVisitor(t *testing.T) { actual := collectVisitedPaths(t, root, VisitAppPaths) expected := []dyn.Path{ dyn.MustPathFromString("resources.apps.app0.source_code_path"), + dyn.MustPathFromString("resources.apps.app0.thumbnail_path"), } assert.ElementsMatch(t, expected, actual) diff --git a/bundle/config/mutator/resourcemutator/configure_app_thumbnail.go b/bundle/config/mutator/resourcemutator/configure_app_thumbnail.go new file mode 100644 index 00000000000..9a2040c860c --- /dev/null +++ b/bundle/config/mutator/resourcemutator/configure_app_thumbnail.go @@ -0,0 +1,82 @@ +package resourcemutator + +import ( + "context" + "encoding/base64" + "fmt" + "net/http" + + "github.com/databricks/cli/bundle" + "github.com/databricks/cli/libs/diag" + "github.com/databricks/cli/libs/dyn" +) + +// maxAppThumbnailBytes matches the limit of the Apps UI and of the databricks_app +// Terraform resource, so all clients accept the same images. +const maxAppThumbnailBytes = 250_000 + +type configureAppThumbnail struct{} + +// ConfigureAppThumbnail reads the image at resources.apps.*.thumbnail_path, checks that it +// is a PNG or JPEG within the size limit, and stores it base64-encoded in thumbnail_content. +func ConfigureAppThumbnail() bundle.Mutator { + return &configureAppThumbnail{} +} + +func (c configureAppThumbnail) Name() string { + return "ConfigureAppThumbnail" +} + +func (c configureAppThumbnail) Apply(_ context.Context, b *bundle.Bundle) diag.Diagnostics { + var diags diag.Diagnostics + + pattern := dyn.NewPattern( + dyn.Key("resources"), + dyn.Key("apps"), + dyn.AnyKey(), + ) + + err := b.Config.Mutate(func(v dyn.Value) (dyn.Value, error) { + return dyn.MapByPattern(v, pattern, func(p dyn.Path, v dyn.Value) (dyn.Value, error) { + pathValue := v.Get("thumbnail_path") + thumbnailPath, ok := pathValue.AsString() + if !ok || thumbnailPath == "" { + return v, nil + } + + contents, err := b.SyncRoot.ReadFile(thumbnailPath) + if err != nil { + diags = diags.Append(diag.Diagnostic{ + Severity: diag.Error, + Summary: fmt.Sprintf("failed to read app thumbnail %s: %s", thumbnailPath, err), + Locations: pathValue.Locations(), + }) + return v, nil + } + + // The Apps API accepts only PNG and JPEG, detected by their file signatures. + contentType := http.DetectContentType(contents) + if contentType != "image/png" && contentType != "image/jpeg" { + diags = diags.Append(diag.Diagnostic{ + Severity: diag.Error, + Summary: fmt.Sprintf("app thumbnail %s must be a PNG or JPEG image, got %s", thumbnailPath, contentType), + Locations: pathValue.Locations(), + }) + return v, nil + } + + if len(contents) > maxAppThumbnailBytes { + diags = diags.Append(diag.Diagnostic{ + Severity: diag.Error, + Summary: fmt.Sprintf("app thumbnail %s is %d bytes, larger than the limit of %d bytes", thumbnailPath, len(contents), maxAppThumbnailBytes), + Locations: pathValue.Locations(), + }) + return v, nil + } + + return dyn.Set(v, "thumbnail_content", dyn.V(base64.StdEncoding.EncodeToString(contents))) + }) + }) + + return diags.Extend(diag.FromErr(err)) +} diff --git a/bundle/config/mutator/resourcemutator/configure_app_thumbnail_test.go b/bundle/config/mutator/resourcemutator/configure_app_thumbnail_test.go new file mode 100644 index 00000000000..c6963acdec9 --- /dev/null +++ b/bundle/config/mutator/resourcemutator/configure_app_thumbnail_test.go @@ -0,0 +1,98 @@ +package resourcemutator_test + +import ( + "bytes" + "encoding/base64" + "os" + "path/filepath" + "testing" + + "github.com/databricks/cli/bundle" + "github.com/databricks/cli/bundle/config" + "github.com/databricks/cli/bundle/config/mutator/resourcemutator" + "github.com/databricks/cli/bundle/config/resources" + "github.com/databricks/cli/libs/vfs" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +var ( + pngHeader = []byte("\x89PNG\r\n\x1a\n") + jpegHeader = []byte("\xff\xd8\xff\xe0") +) + +func padTo(header []byte, size int) []byte { + return append(bytes.Clone(header), make([]byte, size-len(header))...) +} + +func TestConfigureAppThumbnail(t *testing.T) { + tests := []struct { + name string + // contents is written to thumbnail.png when non-nil. + contents []byte + thumbnailPath string + wantErr string + }{ + {name: "png", contents: padTo(pngHeader, 100), thumbnailPath: "thumbnail.png"}, + {name: "jpeg", contents: padTo(jpegHeader, 100), thumbnailPath: "thumbnail.png"}, + {name: "png at the size limit", contents: padTo(pngHeader, 250_000), thumbnailPath: "thumbnail.png"}, + {name: "no thumbnail_path", thumbnailPath: ""}, + { + name: "png over the size limit", + contents: padTo(pngHeader, 250_001), + thumbnailPath: "thumbnail.png", + wantErr: "app thumbnail thumbnail.png is 250001 bytes, larger than the limit of 250000 bytes", + }, + { + name: "gif", + contents: padTo([]byte("GIF89a"), 100), + thumbnailPath: "thumbnail.png", + wantErr: "app thumbnail thumbnail.png must be a PNG or JPEG image, got image/gif", + }, + { + name: "empty file", + contents: []byte{}, + thumbnailPath: "thumbnail.png", + wantErr: "must be a PNG or JPEG image", + }, + { + name: "missing file", + thumbnailPath: "missing.png", + wantErr: "failed to read app thumbnail missing.png", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + dir := t.TempDir() + if tt.contents != nil { + require.NoError(t, os.WriteFile(filepath.Join(dir, "thumbnail.png"), tt.contents, 0o600)) + } + + app := &resources.App{ThumbnailPath: tt.thumbnailPath} + b := &bundle.Bundle{ + SyncRootPath: dir, + BundleRootPath: dir, + SyncRoot: vfs.MustNew(dir), + Config: config.Root{ + Resources: config.Resources{ + Apps: map[string]*resources.App{"my_app": app}, + }, + }, + } + + diags := bundle.ApplySeq(t.Context(), b, resourcemutator.ConfigureAppThumbnail()) + + if tt.wantErr != "" { + assert.ErrorContains(t, diags.Error(), tt.wantErr) + return + } + require.NoError(t, diags.Error()) + want := "" + if tt.contents != nil { + want = base64.StdEncoding.EncodeToString(tt.contents) + } + assert.Equal(t, want, b.Config.Resources.Apps["my_app"].ThumbnailContent) + }) + } +} diff --git a/bundle/config/mutator/resourcemutator/resource_mutator.go b/bundle/config/mutator/resourcemutator/resource_mutator.go index e00444cc773..4269a6aef7a 100644 --- a/bundle/config/mutator/resourcemutator/resource_mutator.go +++ b/bundle/config/mutator/resourcemutator/resource_mutator.go @@ -188,6 +188,10 @@ func applyNormalizeMutators(ctx context.Context, b *bundle.Bundle) { // Drops (dynamic): resources.dashboards.*.file_path ConfigureDashboardSerializedDashboard(), + // Reads (dynamic): resources.apps.*.thumbnail_path + // Updates (dynamic): resources.apps.*.thumbnail_content + ConfigureAppThumbnail(), + // Reads (dynamic): resources.genie_spaces.*.file_path // Updates (dynamic): resources.genie_spaces.*.serialized_space ConfigureGenieSpaceSerializedSpace(), diff --git a/bundle/config/resources/apps.go b/bundle/config/resources/apps.go index fcf2b9f3851..bfc8b4bb3b1 100644 --- a/bundle/config/resources/apps.go +++ b/bundle/config/resources/apps.go @@ -57,6 +57,12 @@ type App struct { // This allows users to define app configuration directly in the bundle YAML instead of maintaining a separate app.yaml file. Config *AppConfig `json:"config,omitempty"` + // ThumbnailPath is the local path of a PNG or JPEG image shown for the app in the Apps UI. + ThumbnailPath string `json:"thumbnail_path,omitempty"` + + // ThumbnailContent is the base64-encoded image read from ThumbnailPath (see ConfigureAppThumbnail). + ThumbnailContent string `json:"thumbnail_content,omitempty" bundle:"internal"` + Permissions []AppPermission `json:"permissions,omitempty"` } diff --git a/bundle/direct/dresources/app.go b/bundle/direct/dresources/app.go index a24b6678aaa..78a7420ef2e 100644 --- a/bundle/direct/dresources/app.go +++ b/bundle/direct/dresources/app.go @@ -12,6 +12,7 @@ import ( "github.com/databricks/cli/bundle/appdeploy" "github.com/databricks/cli/bundle/config/resources" "github.com/databricks/cli/bundle/deployplan" + "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/log" "github.com/databricks/cli/libs/retry" "github.com/databricks/cli/libs/structs/structpath" @@ -23,11 +24,13 @@ import ( ) // AppState is the state type for App resources. source_code_path and git_source come from -// apps.App; config and lifecycle are DABs-only additions persisted in state. +// apps.App; config, lifecycle and thumbnail are DABs-only additions persisted in state. +// thumbnail is the base64-encoded image; state keeps only its hash (hashed_fields in apps.yml). type AppState struct { apps.App Config *resources.AppConfig `json:"config,omitempty"` Lifecycle *StateLifecycle `json:"lifecycle,omitempty"` + Thumbnail string `json:"thumbnail,omitempty"` } // AppRemote extends apps.App with the same DABs-only fields as AppState so they @@ -69,6 +72,7 @@ func (*ResourceApp) PrepareState(input *resources.App) *AppState { App: input.App, Config: input.Config, Lifecycle: nil, + Thumbnail: input.ThumbnailContent, } if input.Lifecycle != nil && input.Lifecycle.Started != nil { s.Lifecycle = &StateLifecycle{Started: input.Lifecycle.Started} @@ -85,6 +89,7 @@ func (*ResourceApp) RemapState(remote *AppRemote) *AppState { App: remote.App, Config: remote.Config, Lifecycle: remote.Lifecycle, + Thumbnail: "", } } @@ -171,9 +176,28 @@ func (r *ResourceApp) DoCreate(ctx context.Context, config *AppState) (string, * return "", nil, err } + // The thumbnail is uploaded before the state is saved, so a failed upload is not recorded + // and the next deploy retries it. Failing here would leave the new app out of the state. + if config.Thumbnail != "" { + if err := r.updateThumbnail(ctx, app.Name, config.Thumbnail); err != nil { + cmdio.LogString(ctx, fmt.Sprintf("Warning: failed to upload the thumbnail of app %s, the next deploy retries it: %s", app.Name, err)) + config.Thumbnail = "" + } + } + return app.Name, nil, nil } +func (r *ResourceApp) updateThumbnail(ctx context.Context, name, thumbnail string) error { + _, err := r.client.Apps.UpdateAppThumbnail(ctx, apps.UpdateAppThumbnailRequest{ + Name: name, + AppThumbnail: &apps.AppThumbnail{Thumbnail: thumbnail, ForceSendFields: nil}, + }) + return err +} + +var appThumbnailPath = structpath.MustParsePath("thumbnail") + // UpdateMaskFields lists the App fields UpdateApp may change. source_code_path is absent // because the API rejects it in the update mask; see appUpdateBody. var UpdateMaskFields = []string{ @@ -244,6 +268,18 @@ func (r *ResourceApp) doUpdate(ctx context.Context, id string, config *AppState, } } + if entry.Changes.HasChange(appThumbnailPath) { + var err error + if config.Thumbnail == "" { + err = r.client.Apps.DeleteAppThumbnail(ctx, apps.DeleteAppThumbnailRequest{Name: id}) + } else { + err = r.updateThumbnail(ctx, id, config.Thumbnail) + } + if err != nil { + return err + } + } + return r.manageLifecycle(ctx, id, config, alreadyStarted) } @@ -311,7 +347,7 @@ func (r *ResourceApp) manageLifecycle(ctx context.Context, id string, config *Ap // belong to the App Update API. source_code_path and config (command/env) are deploy-only // on update and applied via manageLifecycle; lifecycle drives compute start/stop. func hasAppChanges(entry *PlanEntry) bool { - return entry.Changes.HasChangeExcept("source_code_path", "config", "lifecycle", "lifecycle.started") + return entry.Changes.HasChangeExcept("source_code_path", "config", "lifecycle", "lifecycle.started", "thumbnail") } // OverrideChangeDesc skips drift on config (command/env) while the app has no active diff --git a/bundle/direct/dresources/configs/apps.yml b/bundle/direct/dresources/configs/apps.yml index 808a552cd15..14f19d30c70 100644 --- a/bundle/direct/dresources/configs/apps.yml +++ b/bundle/direct/dresources/configs/apps.yml @@ -25,3 +25,6 @@ ignore_remote_changes: reason: input_only - field: config.env[*].value_from reason: input_only +# "thumbnail" holds the base64-encoded image, so state keeps only its hash. +hashed_fields: + - thumbnail diff --git a/bundle/direct/dresources/type_test.go b/bundle/direct/dresources/type_test.go index d7515ae55a8..e867690bf5f 100644 --- a/bundle/direct/dresources/type_test.go +++ b/bundle/direct/dresources/type_test.go @@ -16,6 +16,10 @@ import ( // These are known issues that should be fixed. If a field listed here is found in RemoteType, // the test fails to ensure the entry is removed from this map. var knownMissingInRemoteType = map[string][]string{ + "apps": { + // The Apps API can't return the thumbnail image, only its thumbnail_url. + "thumbnail", + }, "external_locations": { "skip_validation", }, @@ -83,6 +87,8 @@ var knownMissingInStateType = map[string][]string{ "apps": { "lifecycle.prevent_destroy", "lifecycle.depends_on", + // The image read from thumbnail_path is kept in state as thumbnail. + "thumbnail_path", }, "clusters": { "lifecycle.prevent_destroy", diff --git a/bundle/internal/schema/annotations.yml b/bundle/internal/schema/annotations.yml index 0c980344562..9da6c1df002 100644 --- a/bundle/internal/schema/annotations.yml +++ b/bundle/internal/schema/annotations.yml @@ -354,6 +354,9 @@ resources: "user_name": "description": |- The name of the user granted the permission level. + "thumbnail_path": + "description": |- + The local path of a PNG or JPEG image of at most 250 KB to show as the app's thumbnail in the Apps UI, relative to the file that defines the app. Removing it deletes the thumbnail. "catalogs": "description": |- PLACEHOLDER diff --git a/bundle/schema/jsonschema.json b/bundle/schema/jsonschema.json index b0fd7b7d3bd..e8b5f410ed9 100644 --- a/bundle/schema/jsonschema.json +++ b/bundle/schema/jsonschema.json @@ -256,6 +256,10 @@ "$ref": "#/$defs/slice/github.com/databricks/databricks-sdk-go/service/apps.TelemetryExportDestination", "x-databricks-launch-stage": "GA" }, + "thumbnail_path": { + "description": "The local path of a PNG or JPEG image of at most 250 KB to show as the app's thumbnail in the Apps UI, relative to the file that defines the app. Removing it deletes the thumbnail.", + "$ref": "#/$defs/string" + }, "usage_policy_id": { "description": "[Public Preview] ID of the usage policy that the app's compute usage is attributed to. Alternative to\n`budget_policy_id`.", "$ref": "#/$defs/string", diff --git a/libs/testserver/apps.go b/libs/testserver/apps.go index 208c9a33e5a..338ed0bd570 100644 --- a/libs/testserver/apps.go +++ b/libs/testserver/apps.go @@ -248,6 +248,34 @@ func (s *FakeWorkspace) AppsStop(_ Request, name string) Response { return Response{Body: maskInputOnly(app)} } +// AppsUpdateThumbnail sets thumbnail_url, the only trace of the image the real API returns. +func (s *FakeWorkspace) AppsUpdateThumbnail(name string) Response { + defer s.LockUnlock()() + + app, ok := s.Apps[name] + if !ok { + return Response{StatusCode: 404} + } + app.ThumbnailUrl = "/ajax-api/2.0/thumbnails/raw/apps/" + app.Id + "/SMALL_DEFAULT.png" + s.Apps[name] = app + + return Response{Body: apps.AppThumbnail{}} +} + +// AppsDeleteThumbnail clears thumbnail_url. +func (s *FakeWorkspace) AppsDeleteThumbnail(name string) Response { + defer s.LockUnlock()() + + app, ok := s.Apps[name] + if !ok { + return Response{StatusCode: 404} + } + app.ThumbnailUrl = "" + s.Apps[name] = app + + return Response{} +} + // AppsGet returns the app, keeping DELETING resources visible so callers can // observe transient state (matches the cloud DELETE lifecycle). func (s *FakeWorkspace) AppsGet(name string) Response { diff --git a/libs/testserver/handlers.go b/libs/testserver/handlers.go index 87219299844..6777aa84833 100644 --- a/libs/testserver/handlers.go +++ b/libs/testserver/handlers.go @@ -818,6 +818,14 @@ func AddDefaultHandlers(server *Server) { return req.Workspace.AppsGetUpdate(req, req.Vars["name"]) }) + server.Handle("PATCH", "/api/2.0/apps/{name}/thumbnail", func(req Request) any { + return req.Workspace.AppsUpdateThumbnail(req.Vars["name"]) + }) + + server.Handle("DELETE", "/api/2.0/apps/{name}/thumbnail", func(req Request) any { + return req.Workspace.AppsDeleteThumbnail(req.Vars["name"]) + }) + server.Handle("GET", "/api/2.0/apps/{name}", func(req Request) any { return req.Workspace.AppsGet(req.Vars["name"]) }) diff --git a/python/databricks/bundles/apps/_models/app.py b/python/databricks/bundles/apps/_models/app.py index baeb535cf2d..b1a78d42d6e 100644 --- a/python/databricks/bundles/apps/_models/app.py +++ b/python/databricks/bundles/apps/_models/app.py @@ -139,6 +139,11 @@ class App(Resource): effect when the app is next deployed. """ + thumbnail_path: VariableOrOptional[str] = None + """ + The local path of a PNG or JPEG image of at most 250 KB to show as the app's thumbnail in the Apps UI, relative to the file that defines the app. Removing it deletes the thumbnail. + """ + usage_policy_id: VariableOrOptional[str] = None """ [Public Preview] ID of the usage policy that the app's compute usage is attributed to. Alternative to @@ -258,6 +263,11 @@ class AppDict(TypedDict, total=False): effect when the app is next deployed. """ + thumbnail_path: VariableOrOptional[str] + """ + The local path of a PNG or JPEG image of at most 250 KB to show as the app's thumbnail in the Apps UI, relative to the file that defines the app. Removing it deletes the thumbnail. + """ + usage_policy_id: VariableOrOptional[str] """ [Public Preview] ID of the usage policy that the app's compute usage is attributed to. Alternative to