diff --git a/.nextchanges/bundles/app-source-code-path.md b/.nextchanges/bundles/app-source-code-path.md new file mode 100644 index 00000000000..92d30119638 --- /dev/null +++ b/.nextchanges/bundles/app-source-code-path.md @@ -0,0 +1 @@ +* Send `source_code_path` on the app Create request and `git_source` on the app Create and Update requests, so the app records its configured source before its first deployment. ([#6399](https://github.com/databricks/cli/pull/6399)) diff --git a/acceptance/bundle/empty_string_dropped/out.requests.json b/acceptance/bundle/empty_string_dropped/out.requests.json index a4798a110a2..88b90d86a8d 100644 --- a/acceptance/bundle/empty_string_dropped/out.requests.json +++ b/acceptance/bundle/empty_string_dropped/out.requests.json @@ -6,7 +6,8 @@ }, "body": { "description": "", - "name": "test-app-direct" + "name": "test-app-direct", + "source_code_path": "/Workspace/Users/[USERNAME]/.bundle/test-bundle/direct/files/app" } } { diff --git a/acceptance/bundle/resource_deps/remote_app_url/output.txt b/acceptance/bundle/resource_deps/remote_app_url/output.txt index 23808c5a896..7aad3166273 100644 --- a/acceptance/bundle/resource_deps/remote_app_url/output.txt +++ b/acceptance/bundle/resource_deps/remote_app_url/output.txt @@ -65,7 +65,8 @@ Resources: 2 created, 0 changed, 0 deleted, 0 unchanged }, "body": { "description": "best app", - "name": "myapp" + "name": "myapp", + "source_code_path": "/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files/app" } } { diff --git a/acceptance/bundle/resources/apps/config-no-deployment/output.txt b/acceptance/bundle/resources/apps/config-no-deployment/output.txt index edfde35bf49..f8072b5de08 100644 --- a/acceptance/bundle/resources/apps/config-no-deployment/output.txt +++ b/acceptance/bundle/resources/apps/config-no-deployment/output.txt @@ -46,8 +46,9 @@ Plan: 0 to add, 0 to change, 0 to delete, 1 unchanged }, "source_code_path": { "action": "skip", - "reason": "no active deployment", + "reason": "spec:input_only", "old": "/Workspace/Users/[USERNAME]/.bundle/app-config-no-deployment/default/files/app", - "new": "/Workspace/Users/[USERNAME]/.bundle/app-config-no-deployment/default/files/app" + "new": "/Workspace/Users/[USERNAME]/.bundle/app-config-no-deployment/default/files/app", + "remote": "" } } diff --git a/acceptance/bundle/resources/apps/default_description/output.txt b/acceptance/bundle/resources/apps/default_description/output.txt index a8260fca553..f669b91c679 100644 --- a/acceptance/bundle/resources/apps/default_description/output.txt +++ b/acceptance/bundle/resources/apps/default_description/output.txt @@ -14,6 +14,7 @@ Resources: 1 created, 0 changed, 0 deleted, 0 unchanged }, "body": { "description": "", - "name": "myappname" + "name": "myappname", + "source_code_path": "/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files" } } diff --git a/acceptance/bundle/resources/apps/git-source-no-deployment/output.txt b/acceptance/bundle/resources/apps/git-source-no-deployment/output.txt index 84def7b23cb..0fd4487ea03 100644 --- a/acceptance/bundle/resources/apps/git-source-no-deployment/output.txt +++ b/acceptance/bundle/resources/apps/git-source-no-deployment/output.txt @@ -18,7 +18,7 @@ Plan: 0 to add, 0 to change, 0 to delete, 1 unchanged { "git_source": { "action": "skip", - "reason": "no active deployment", + "reason": "spec:input_only", "old": { "branch": "main" }, diff --git a/acceptance/bundle/resources/apps/immutable/output.txt b/acceptance/bundle/resources/apps/immutable/output.txt index ef18eb0b92f..b9d91399c08 100644 --- a/acceptance/bundle/resources/apps/immutable/output.txt +++ b/acceptance/bundle/resources/apps/immutable/output.txt @@ -24,7 +24,8 @@ You can access the app at my-immutable-app-123.cloud.databricksapps.com }, "body": { "description": "", - "name": "my-immutable-app" + "name": "my-immutable-app", + "source_code_path": "/Workspace/Users/[UUID][0]/.snapshots/[SNAPSHOT_HASH]/[SNAPSHOT_HASH]-[UUID][1]/snapshot/files/app" } } { diff --git a/acceptance/bundle/resources/apps/lifecycle-started-omitted/output.txt b/acceptance/bundle/resources/apps/lifecycle-started-omitted/output.txt index 3202e7480a8..d2538fa2525 100644 --- a/acceptance/bundle/resources/apps/lifecycle-started-omitted/output.txt +++ b/acceptance/bundle/resources/apps/lifecycle-started-omitted/output.txt @@ -16,7 +16,8 @@ Resources: 1 created, 0 changed, 0 deleted, 0 unchanged }, "body": { "description": "my_app_description", - "name": "[UNIQUE_NAME]" + "name": "[UNIQUE_NAME]", + "source_code_path": "/Workspace/Users/[USERNAME]/.bundle/lifecycle-started-omitted-[UNIQUE_NAME]/default/files/app" } } diff --git a/acceptance/bundle/resources/apps/lifecycle-started-toggle/output.txt b/acceptance/bundle/resources/apps/lifecycle-started-toggle/output.txt index b9ceb3f7265..ba4aab368e8 100644 --- a/acceptance/bundle/resources/apps/lifecycle-started-toggle/output.txt +++ b/acceptance/bundle/resources/apps/lifecycle-started-toggle/output.txt @@ -16,7 +16,8 @@ Resources: 1 created, 0 changed, 0 deleted, 0 unchanged }, "body": { "description": "my_app_description", - "name": "[UNIQUE_NAME]" + "name": "[UNIQUE_NAME]", + "source_code_path": "/Workspace/Users/[USERNAME]/.bundle/lifecycle-started-toggle-[UNIQUE_NAME]/default/files/app" } } diff --git a/acceptance/bundle/resources/apps/lifecycle-started/output.txt b/acceptance/bundle/resources/apps/lifecycle-started/output.txt index 60680023e55..971d83b085f 100644 --- a/acceptance/bundle/resources/apps/lifecycle-started/output.txt +++ b/acceptance/bundle/resources/apps/lifecycle-started/output.txt @@ -13,7 +13,8 @@ Resources: 1 created, 0 changed, 0 deleted, 0 unchanged "path": "/api/2.0/apps", "body": { "description": "my_app_description", - "name": "[UNIQUE_NAME]" + "name": "[UNIQUE_NAME]", + "source_code_path": "/Workspace/Users/[USERNAME]/.bundle/lifecycle-started-[UNIQUE_NAME]/default/files/app" } } { @@ -95,7 +96,7 @@ Resources: 0 created, 1 changed, 0 deleted, 0 unchanged "description": "MY_APP_DESCRIPTION_2", "name": "[UNIQUE_NAME]" }, - "update_mask": "description,budget_policy_id,usage_policy_id,resources,user_api_scopes,compute_min_instances,compute_max_instances,git_repository,telemetry_export_destinations" + "update_mask": "description,budget_policy_id,usage_policy_id,resources,user_api_scopes,compute_min_instances,compute_max_instances,git_repository,git_source,telemetry_export_destinations" } } @@ -123,7 +124,7 @@ Resources: 0 created, 1 changed, 0 deleted, 0 unchanged "description": "MY_APP_DESCRIPTION_3", "name": "[UNIQUE_NAME]" }, - "update_mask": "description,budget_policy_id,usage_policy_id,resources,user_api_scopes,compute_min_instances,compute_max_instances,git_repository,telemetry_export_destinations" + "update_mask": "description,budget_policy_id,usage_policy_id,resources,user_api_scopes,compute_min_instances,compute_max_instances,git_repository,git_source,telemetry_export_destinations" } } { diff --git a/acceptance/bundle/resources/apps/missing-service-principal/output.txt b/acceptance/bundle/resources/apps/missing-service-principal/output.txt index fa5969f6e1a..e9171104845 100644 --- a/acceptance/bundle/resources/apps/missing-service-principal/output.txt +++ b/acceptance/bundle/resources/apps/missing-service-principal/output.txt @@ -33,7 +33,8 @@ Exit code: 1 }, "body": { "description": "my_app_description", - "name": "myappname" + "name": "myappname", + "source_code_path": "/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files/app" } } { @@ -44,7 +45,7 @@ Exit code: 1 "description": "MY_APP_DESCRIPTION", "name": "myappname" }, - "update_mask": "description,budget_policy_id,usage_policy_id,resources,user_api_scopes,compute_min_instances,compute_max_instances,git_repository,telemetry_export_destinations" + "update_mask": "description,budget_policy_id,usage_policy_id,resources,user_api_scopes,compute_min_instances,compute_max_instances,git_repository,git_source,telemetry_export_destinations" } } diff --git a/acceptance/bundle/resources/apps/resource-refs/output.txt b/acceptance/bundle/resources/apps/resource-refs/output.txt index 1690bee41eb..57da4c9bcb7 100644 --- a/acceptance/bundle/resources/apps/resource-refs/output.txt +++ b/acceptance/bundle/resources/apps/resource-refs/output.txt @@ -26,7 +26,8 @@ You can access the app at data-app-123.cloud.databricksapps.com }, "body": { "description": "A Streamlit app that uses a SQL warehouse", - "name": "data-app" + "name": "data-app", + "source_code_path": "/Workspace/Users/[USERNAME]/.bundle/resource-refs/default/files/app" } } { diff --git a/acceptance/bundle/resources/apps/source-code-path-update/app/app.py b/acceptance/bundle/resources/apps/source-code-path-update/app/app.py new file mode 100644 index 00000000000..c471b2f5ecf --- /dev/null +++ b/acceptance/bundle/resources/apps/source-code-path-update/app/app.py @@ -0,0 +1 @@ +print("v1") diff --git a/acceptance/bundle/resources/apps/source-code-path-update/app2/app.py b/acceptance/bundle/resources/apps/source-code-path-update/app2/app.py new file mode 100644 index 00000000000..3d6edeb28df --- /dev/null +++ b/acceptance/bundle/resources/apps/source-code-path-update/app2/app.py @@ -0,0 +1 @@ +print("v2") diff --git a/acceptance/bundle/resources/apps/source-code-path-update/databricks.yml b/acceptance/bundle/resources/apps/source-code-path-update/databricks.yml new file mode 100644 index 00000000000..7c707baa0a4 --- /dev/null +++ b/acceptance/bundle/resources/apps/source-code-path-update/databricks.yml @@ -0,0 +1,9 @@ +bundle: + name: app-source-code-path-update + +resources: + apps: + myapp: + name: test-app-source-code-path-update + description: my_app_description + source_code_path: ./app diff --git a/acceptance/bundle/resources/apps/source-code-path-update/out.test.toml b/acceptance/bundle/resources/apps/source-code-path-update/out.test.toml new file mode 100644 index 00000000000..a927a5fbc06 --- /dev/null +++ b/acceptance/bundle/resources/apps/source-code-path-update/out.test.toml @@ -0,0 +1,2 @@ +Cloud = false +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/resources/apps/source-code-path-update/output.txt b/acceptance/bundle/resources/apps/source-code-path-update/output.txt new file mode 100644 index 00000000000..cc99dfa7e9f --- /dev/null +++ b/acceptance/bundle/resources/apps/source-code-path-update/output.txt @@ -0,0 +1,64 @@ + +=== Deploy: source_code_path is sent on Create + +>>> [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/app-source-code-path-update/default/files... +Created apps.myapp +Files: 3 uploaded, 0 deleted +Resources: 1 created, 0 changed, 0 deleted, 0 unchanged + +>>> print_requests.py //apps +{ + "method": "POST", + "path": "/api/2.0/apps", + "q": { + "no_compute": "true" + }, + "body": { + "description": "my_app_description", + "name": "test-app-source-code-path-update", + "source_code_path": "/Workspace/Users/[USERNAME]/.bundle/app-source-code-path-update/default/files/app" + } +} + +=== Change source_code_path: plan detects an update + +>>> update_file.py databricks.yml source_code_path: ./app source_code_path: ./app2 + +>>> [CLI] bundle plan +update apps.myapp + +Plan: 0 to add, 1 to change, 0 to delete, 0 unchanged + +=== Redeploy: UpdateApp rejects source_code_path, so no app request is sent; the next Deploy applies it + +>>> [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/app-source-code-path-update/default/files... +Updated apps.myapp +Files: 1 uploaded, 0 deleted +Resources: 0 created, 1 changed, 0 deleted, 0 unchanged + +>>> print_requests.py //apps + +=== Change description: App Update omits source_code_path from the body and the update mask + +>>> update_file.py databricks.yml my_app_description MY_APP_DESCRIPTION + +>>> [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/app-source-code-path-update/default/files... +Updated apps.myapp +Files: 1 uploaded, 0 deleted +Resources: 0 created, 1 changed, 0 deleted, 0 unchanged + +>>> print_requests.py //apps +{ + "method": "POST", + "path": "/api/2.0/apps/test-app-source-code-path-update/update", + "body": { + "app": { + "description": "MY_APP_DESCRIPTION", + "name": "test-app-source-code-path-update" + }, + "update_mask": "description,budget_policy_id,usage_policy_id,resources,user_api_scopes,compute_min_instances,compute_max_instances,git_repository,git_source,telemetry_export_destinations" + } +} diff --git a/acceptance/bundle/resources/apps/source-code-path-update/script b/acceptance/bundle/resources/apps/source-code-path-update/script new file mode 100644 index 00000000000..58260eb246d --- /dev/null +++ b/acceptance/bundle/resources/apps/source-code-path-update/script @@ -0,0 +1,16 @@ +title "Deploy: source_code_path is sent on Create" +trace $CLI bundle deploy +trace print_requests.py //apps + +title "Change source_code_path: plan detects an update" +trace update_file.py databricks.yml "source_code_path: ./app" "source_code_path: ./app2" +trace $CLI bundle plan + +title "Redeploy: UpdateApp rejects source_code_path, so no app request is sent; the next Deploy applies it" +trace $CLI bundle deploy +trace print_requests.py //apps + +title "Change description: App Update omits source_code_path from the body and the update mask" +trace update_file.py databricks.yml my_app_description MY_APP_DESCRIPTION +trace $CLI bundle deploy +trace print_requests.py //apps diff --git a/acceptance/bundle/resources/apps/source-code-path-update/test.toml b/acceptance/bundle/resources/apps/source-code-path-update/test.toml new file mode 100644 index 00000000000..731086bc182 --- /dev/null +++ b/acceptance/bundle/resources/apps/source-code-path-update/test.toml @@ -0,0 +1,3 @@ +Cloud = false + +Ignore = [".databricks"] diff --git a/acceptance/bundle/resources/apps/update-compute-size/out.requests.json b/acceptance/bundle/resources/apps/update-compute-size/out.requests.json index c39f33c9920..86487cb3ebb 100644 --- a/acceptance/bundle/resources/apps/update-compute-size/out.requests.json +++ b/acceptance/bundle/resources/apps/update-compute-size/out.requests.json @@ -7,7 +7,8 @@ "body": { "compute_size": "LARGE", "description": "my_app_description", - "name": "myappname" + "name": "myappname", + "source_code_path": "/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files/app" } } { @@ -19,7 +20,7 @@ "description": "MY_APP_DESCRIPTION", "name": "myappname" }, - "update_mask": "description,budget_policy_id,usage_policy_id,resources,user_api_scopes,compute_size,compute_min_instances,compute_max_instances,git_repository,telemetry_export_destinations" + "update_mask": "description,budget_policy_id,usage_policy_id,resources,user_api_scopes,compute_size,compute_min_instances,compute_max_instances,git_repository,git_source,telemetry_export_destinations" } } { @@ -30,6 +31,6 @@ "description": "MY_APP_DESCRIPTION_2", "name": "myappname" }, - "update_mask": "description,budget_policy_id,usage_policy_id,resources,user_api_scopes,compute_min_instances,compute_max_instances,git_repository,telemetry_export_destinations" + "update_mask": "description,budget_policy_id,usage_policy_id,resources,user_api_scopes,compute_min_instances,compute_max_instances,git_repository,git_source,telemetry_export_destinations" } } diff --git a/acceptance/bundle/resources/apps/update-source-code-path-cloud/app/app.py b/acceptance/bundle/resources/apps/update-source-code-path-cloud/app/app.py new file mode 100644 index 00000000000..c471b2f5ecf --- /dev/null +++ b/acceptance/bundle/resources/apps/update-source-code-path-cloud/app/app.py @@ -0,0 +1 @@ +print("v1") diff --git a/acceptance/bundle/resources/apps/update-source-code-path-cloud/app2/app.py b/acceptance/bundle/resources/apps/update-source-code-path-cloud/app2/app.py new file mode 100644 index 00000000000..3d6edeb28df --- /dev/null +++ b/acceptance/bundle/resources/apps/update-source-code-path-cloud/app2/app.py @@ -0,0 +1 @@ +print("v2") diff --git a/acceptance/bundle/resources/apps/update-source-code-path-cloud/databricks.yml.tmpl b/acceptance/bundle/resources/apps/update-source-code-path-cloud/databricks.yml.tmpl new file mode 100644 index 00000000000..2c88975cf7f --- /dev/null +++ b/acceptance/bundle/resources/apps/update-source-code-path-cloud/databricks.yml.tmpl @@ -0,0 +1,9 @@ +bundle: + name: update-source-code-path-cloud-$UNIQUE_NAME + +resources: + apps: + myapp: + name: $UNIQUE_NAME + description: my_app_description + source_code_path: ./app diff --git a/acceptance/bundle/resources/apps/update-source-code-path-cloud/out.test.toml b/acceptance/bundle/resources/apps/update-source-code-path-cloud/out.test.toml new file mode 100644 index 00000000000..0f167effef1 --- /dev/null +++ b/acceptance/bundle/resources/apps/update-source-code-path-cloud/out.test.toml @@ -0,0 +1,2 @@ +Cloud = true +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/resources/apps/update-source-code-path-cloud/output.txt b/acceptance/bundle/resources/apps/update-source-code-path-cloud/output.txt new file mode 100644 index 00000000000..70e37f0f185 --- /dev/null +++ b/acceptance/bundle/resources/apps/update-source-code-path-cloud/output.txt @@ -0,0 +1,42 @@ + +=== Deploy: create the app with source_code_path + +>>> errcode [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/update-source-code-path-cloud-[UNIQUE_NAME]/default/files... +Created apps.myapp +Files: 4 uploaded, 0 deleted +Resources: 1 created, 0 changed, 0 deleted, 0 unchanged + +>>> print_requests.py //apps +{ + "method": "POST", + "path": "/api/2.0/apps", + "q": { + "no_compute": "true" + }, + "body": { + "description": "my_app_description", + "name": "[UNIQUE_NAME]", + "source_code_path": "/Workspace/Users/[USERNAME]/.bundle/update-source-code-path-cloud-[UNIQUE_NAME]/default/files/app" + } +} + +=== Change source_code_path on an existing app: no UpdateApp request is sent + +>>> update_file.py databricks.yml source_code_path: ./app source_code_path: ./app2 + +>>> errcode [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/update-source-code-path-cloud-[UNIQUE_NAME]/default/files... +Updated apps.myapp +Files: 1 uploaded, 0 deleted +Resources: 0 created, 1 changed, 0 deleted, 0 unchanged + +>>> print_requests.py //apps + +>>> [CLI] bundle destroy --auto-approve +The following resources will be deleted: + delete resources.apps.myapp + +All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/update-source-code-path-cloud-[UNIQUE_NAME]/default + +Destroy: 1 deleted diff --git a/acceptance/bundle/resources/apps/update-source-code-path-cloud/script b/acceptance/bundle/resources/apps/update-source-code-path-cloud/script new file mode 100755 index 00000000000..6179a002e1d --- /dev/null +++ b/acceptance/bundle/resources/apps/update-source-code-path-cloud/script @@ -0,0 +1,17 @@ +envsubst < databricks.yml.tmpl > databricks.yml + +cleanup() { + trace $CLI bundle destroy --auto-approve + rm -f out.requests.txt +} +trap cleanup EXIT + +title "Deploy: create the app with source_code_path" +trace errcode $CLI bundle deploy +trace print_requests.py //apps +rm -f out.requests.txt + +title "Change source_code_path on an existing app: no UpdateApp request is sent" +trace update_file.py databricks.yml "source_code_path: ./app" "source_code_path: ./app2" +trace errcode $CLI bundle deploy +trace print_requests.py //apps diff --git a/acceptance/bundle/resources/apps/update-source-code-path-cloud/test.toml b/acceptance/bundle/resources/apps/update-source-code-path-cloud/test.toml new file mode 100644 index 00000000000..34b9beb94c8 --- /dev/null +++ b/acceptance/bundle/resources/apps/update-source-code-path-cloud/test.toml @@ -0,0 +1,13 @@ +# Runs against a real workspace: production rejects source_code_path in the UpdateApp mask +# (400 INVALID_PARAMETER_VALUE), so changing it on an existing app must not call UpdateApp. +# Locally the fake server enforces the same rule; see libs/testserver/apps.go. +Cloud = true +RecordRequests = true + +Ignore = [".databricks", "databricks.yml"] + +# App deployment progress messages are non-deterministic on cloud (different status messages +# per poll), so filter them out entirely. +[[Repls]] +Old = '(?m)^✓ [^\n]*\n' +New = "" diff --git a/acceptance/bundle/resources/apps/update/out.requests.json b/acceptance/bundle/resources/apps/update/out.requests.json index 0d5a3bb4f1e..0d5eeecc7c2 100644 --- a/acceptance/bundle/resources/apps/update/out.requests.json +++ b/acceptance/bundle/resources/apps/update/out.requests.json @@ -6,7 +6,8 @@ }, "body": { "description": "my_app_description", - "name": "myappname" + "name": "myappname", + "source_code_path": "/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files/app" } } { @@ -17,7 +18,7 @@ "description": "MY_APP_DESCRIPTION", "name": "myappname" }, - "update_mask": "description,budget_policy_id,usage_policy_id,resources,user_api_scopes,compute_min_instances,compute_max_instances,git_repository,telemetry_export_destinations" + "update_mask": "description,budget_policy_id,usage_policy_id,resources,user_api_scopes,compute_min_instances,compute_max_instances,git_repository,git_source,telemetry_export_destinations" } } { @@ -32,7 +33,8 @@ }, "body": { "description": "MY_APP_DESCRIPTION", - "name": "mynewappname" + "name": "mynewappname", + "source_code_path": "/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files/app" } } { diff --git a/bundle/config/resources/apps.go b/bundle/config/resources/apps.go index 76acbb29a96..fcf2b9f3851 100644 --- a/bundle/config/resources/apps.go +++ b/bundle/config/resources/apps.go @@ -44,10 +44,13 @@ type App struct { // Lifecycle shadows BaseResource.Lifecycle to add support for lifecycle.started. Lifecycle *LifecycleWithStarted `json:"lifecycle,omitempty"` - // SourceCodePath and GitSource come from the embedded apps.App. DABs treats them as - // deploy-only: source_code_path points at app source on local disk and is translated to - // its workspace path, and both are passed to the Deploy API rather than sent as resource - // attributes. Declaring them here too would duplicate their json tags and break diffing. + // SourceCodePath and GitSource come from the embedded apps.App and are sent in the Create + // App request so they are recorded on the app; git_source is also sent on Update, while + // UpdateApp rejects source_code_path. source_code_path points at app source on local disk + // and is translated to its workspace path. The API accepts both on write but does not echo + // them on read (input_only), so drift is suppressed via ignore_remote_changes. They are + // also passed to the Deploy API, which requires a deployment source. Declaring them here + // too would duplicate their json tags and break diffing. // Config represents inline app.yaml configuration for the app. // When specified, this configuration is written to an app.yaml file in the source code path during deployment. diff --git a/bundle/direct/dresources/app.go b/bundle/direct/dresources/app.go index f76681feb98..ea705285376 100644 --- a/bundle/direct/dresources/app.go +++ b/bundle/direct/dresources/app.go @@ -97,29 +97,37 @@ func (r *ResourceApp) DoRead(ctx context.Context, id string) (*AppRemote, error) Lifecycle: &StateLifecycle{Started: &started}, } if app.ActiveDeployment != nil { - // The source code path in active deployment is snapshotted version of the source code path in the app. - // We need to use the default source code path to get the correct source code path for drift detection. - remote.SourceCodePath = app.DefaultSourceCodePath - remote.GitSource = app.ActiveDeployment.GitSource + // config (command/env) is not part of apps.App, so it is read back from the + // active deployment for drift detection. source_code_path and git_source are + // classified input_only, so their remote value is ignored (see + // apps.generated.yml) and does not need populating here. remote.Config = deploymentToAppConfig(app.ActiveDeployment) } return remote, nil } -// appRequestBody returns config.App with the deploy-only fields cleared. source_code_path -// and git_source became part of apps.App in SDK v0.175, but DABs applies them through the -// Deploy API (see manageLifecycle), so they must not ride along in create/update bodies. -// ForceSendFields is cloned and stripped of SourceCodePath because the SDK's JSON -// unmarshal (used when loading the plan) adds every present basic-type field to -// ForceSendFields, which would otherwise force "source_code_path": "" into the body. -func appRequestBody(config *AppState) apps.App { +// appRequestBody returns app for the Create/Update request body. An empty source_code_path +// is never sent: the SDK's JSON unmarshal (used when loading the plan) adds every present +// basic-type field to ForceSendFields, which would otherwise force "source_code_path": "" +// into the body, and the API rejects that (see #6401, fixed in #6404). ForceSendFields is +// cloned so the plan entry is not modified. +func appRequestBody(app apps.App) apps.App { + if app.SourceCodePath == "" { + app.ForceSendFields = slices.DeleteFunc(slices.Clone(app.ForceSendFields), func(s string) bool { + return s == "SourceCodePath" + }) + } + return app +} + +// appUpdateBody returns config.App for the Update request. UpdateApp rejects source_code_path +// (400 INVALID_PARAMETER_VALUE: invalid update mask), so it is sent on Create only and is not +// in UpdateMaskFields; a changed source_code_path reaches the app through the Deploy API +// (see manageLifecycle). git_source is accepted on Update and stays in the body. +func appUpdateBody(config *AppState) apps.App { app := config.App app.SourceCodePath = "" - app.GitSource = nil - app.ForceSendFields = slices.DeleteFunc(slices.Clone(app.ForceSendFields), func(s string) bool { - return s == "SourceCodePath" - }) - return app + return appRequestBody(app) } func (r *ResourceApp) DoCreate(ctx context.Context, config *AppState) (string, *AppRemote, error) { @@ -127,7 +135,7 @@ func (r *ResourceApp) DoCreate(ctx context.Context, config *AppState) (string, * // For nil (omitted) or false, use no_compute=true (do not start compute). noCompute := config.Lifecycle == nil || config.Lifecycle.Started == nil || !*config.Lifecycle.Started request := apps.CreateAppRequest{ - App: appRequestBody(config), + App: appRequestBody(config.App), NoCompute: noCompute, ForceSendFields: nil, } @@ -163,6 +171,8 @@ func (r *ResourceApp) DoCreate(ctx context.Context, config *AppState) (string, * return app.Name, nil, nil } +// 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{ "description", "budget_policy_id", @@ -173,6 +183,7 @@ var UpdateMaskFields = []string{ "compute_min_instances", "compute_max_instances", "git_repository", + "git_source", "telemetry_export_destinations", } @@ -195,10 +206,11 @@ func (r *ResourceApp) DoUpdate(ctx context.Context, id string, config *AppState, } func (r *ResourceApp) doUpdate(ctx context.Context, id string, config *AppState, entry *PlanEntry) error { - // Deploy-only fields (source_code_path, config, git_source, lifecycle) are excluded - // from the request body; see appRequestBody. + // git_source is sent as part of the App body (in the update mask). source_code_path and + // config (command/env) are deploy-only on update and applied via manageLifecycle; see + // appUpdateBody. if hasAppChanges(entry) { - app := appRequestBody(config) + app := appUpdateBody(config) request := apps.AsyncUpdateAppRequest{ App: &app, AppName: id, @@ -259,6 +271,9 @@ func (r *ResourceApp) manageLifecycle(ctx context.Context, id string, config *Ap return err } } + // source_code_path is recorded on the app at Create and git_source at Create/Update, + // but the Deploy API requires a deployment source, so the deployment carries them + // too. This is also the only way a changed source_code_path reaches the app. deployment := appdeploy.BuildDeployment(config.SourceCodePath, config.Config, config.GitSource) if err := appdeploy.Deploy(ctx, r.client, id, deployment); err != nil { return err @@ -279,20 +294,20 @@ func (r *ResourceApp) manageLifecycle(ctx context.Context, id string, config *Ap return nil } -// hasAppChanges reports whether the plan entry contains any Update changes -// to fields that belong to the App Update API (i.e., not deploy-only fields). +// hasAppChanges reports whether the plan entry contains any Update changes to fields that +// 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", "git_source", "lifecycle", "lifecycle.started") + return entry.Changes.HasChangeExcept("source_code_path", "config", "lifecycle", "lifecycle.started") } -// OverrideChangeDesc skips drift on the deploy-only fields (source_code_path, config, -// git_source) while the app has no active deployment. DoRead reads them only from the -// active deployment, so before the first deploy (or once a stop clears it) the remote -// side is empty and the diff is spurious; it applies on the next start (manageLifecycle). +// OverrideChangeDesc skips drift on config (command/env) while the app has no active +// deployment. config is read only from the active deployment (DoRead), so before the +// first deploy (or once a stop clears it) the remote side is empty and the diff is +// spurious; it applies on the next start (manageLifecycle). func (*ResourceApp) OverrideChangeDesc(_ context.Context, path *structpath.PathNode, change *ChangeDesc, remote *AppRemote) error { // Prefix(1) so a nested diff (e.g. config.command) matches its top-level field. - switch path.Prefix(1).String() { - case "source_code_path", "config", "git_source": + if path.Prefix(1).String() == "config" { if remote.ActiveDeployment == nil { change.Action = deployplan.Skip change.Reason = "no active deployment" @@ -414,8 +429,6 @@ func (r *ResourceApp) waitForApp(ctx context.Context, w *databricks.WorkspaceCli Lifecycle: &StateLifecycle{Started: &started}, } if app.ActiveDeployment != nil { - remote.SourceCodePath = app.DefaultSourceCodePath - remote.GitSource = app.ActiveDeployment.GitSource remote.Config = deploymentToAppConfig(app.ActiveDeployment) } return remote, nil diff --git a/bundle/direct/dresources/app_test.go b/bundle/direct/dresources/app_test.go index 01b619bae21..ebc8a7ee7dc 100644 --- a/bundle/direct/dresources/app_test.go +++ b/bundle/direct/dresources/app_test.go @@ -195,14 +195,13 @@ func TestAppUpdateMask_ComputeSizeFollowsBody(t *testing.T) { } } -// TestAppRequestBody_StripsSourceCodePathFromForceSendFields verifies that -// appRequestBody removes SourceCodePath from ForceSendFields even when the plan -// JSON round-trip has forced it in — reproduces the v1.14.0 regression where -// "source_code_path": "" was sent in the UpdateApp body, causing a 400. -func TestAppRequestBody_StripsSourceCodePathFromForceSendFields(t *testing.T) { - config := &AppState{ - Name: "my-app", - SourceCodePath: "/Workspace/Users/me/app", +// TestAppRequestBody_StripsEmptySourceCodePathFromForceSendFields verifies that an empty +// SourceCodePath is removed from ForceSendFields even when the plan JSON round-trip has +// forced it in — reproduces the v1.14.0 regression (#6401) where "source_code_path": "" +// was sent in the UpdateApp body, causing a 400. +func TestAppRequestBody_StripsEmptySourceCodePathFromForceSendFields(t *testing.T) { + app := apps.App{ + Name: "my-app", // Simulate ForceSendFields as populated by marshal.Unmarshal when the // plan entry is deserialized from JSON (ToStructVar calls json.Unmarshal, // which calls apps.App.UnmarshalJSON -> marshal.Unmarshal, which adds @@ -210,22 +209,68 @@ func TestAppRequestBody_StripsSourceCodePathFromForceSendFields(t *testing.T) { ForceSendFields: []string{"Name", "SourceCodePath"}, } - body := appRequestBody(config) + body := appRequestBody(app) - // SourceCodePath must be cleared and absent from ForceSendFields so that - // the SDK's marshal.Marshal omits it from the request body. - assert.Empty(t, body.SourceCodePath) + // Absent from ForceSendFields so that the SDK's marshal.Marshal omits it from the request body. assert.NotContains(t, body.ForceSendFields, "SourceCodePath") // Verify the field is absent from the marshaled JSON (the root cause of the 400). - data, err := json.Marshal(body) - require.NoError(t, err) - var m map[string]any - require.NoError(t, json.Unmarshal(data, &m)) - assert.NotContains(t, m, "source_code_path", "source_code_path must not appear in the UpdateApp request body") + assert.NotContains(t, marshalFields(t, body), "source_code_path", "source_code_path must not appear in the request body when empty") // Other ForceSendFields entries must be preserved. assert.Contains(t, body.ForceSendFields, "Name") + // The plan entry itself must not be modified. + assert.Contains(t, app.ForceSendFields, "SourceCodePath") +} + +func TestAppRequestBody_SendsNonEmptySourceCodePath(t *testing.T) { + app := apps.App{ + Name: "my-app", + SourceCodePath: "/Workspace/Users/me/app", + ForceSendFields: []string{"SourceCodePath"}, + } + + body := appRequestBody(app) + + assert.Equal(t, "/Workspace/Users/me/app", body.SourceCodePath) + assert.Contains(t, body.ForceSendFields, "SourceCodePath") +} + +// TestAppUpdateBody_OmitsSourceCodePath verifies that the UpdateApp body never carries +// source_code_path, even when the plan entry has a value and ForceSendFields lists it, +// while git_source is kept. Production rejects source_code_path on UpdateApp. +func TestAppUpdateBody_OmitsSourceCodePath(t *testing.T) { + config := &AppState{} + config.App = apps.App{ + Name: "my-app", + SourceCodePath: "/Workspace/Users/me/app", + GitSource: &apps.GitSource{Branch: "main"}, + ForceSendFields: []string{"Name", "SourceCodePath"}, + } + + body := appUpdateBody(config) + + fields := marshalFields(t, body) + assert.NotContains(t, fields, "source_code_path") + assert.Contains(t, fields, "git_source") + assert.NotContains(t, body.ForceSendFields, "SourceCodePath") + // The plan entry itself must not be modified. + assert.Equal(t, "/Workspace/Users/me/app", config.SourceCodePath) + assert.Contains(t, config.ForceSendFields, "SourceCodePath") +} + +func TestAppUpdateMaskOmitsSourceCodePath(t *testing.T) { + assert.NotContains(t, UpdateMaskFields, "source_code_path") + assert.Contains(t, UpdateMaskFields, "git_source") +} + +// marshalFields returns the JSON field names the SDK marshaler emits for app. +func marshalFields(t *testing.T, app apps.App) map[string]any { + data, err := json.Marshal(app) + require.NoError(t, err) + var m map[string]any + require.NoError(t, json.Unmarshal(data, &m)) + return m } func TestWithMissingServicePrincipalHint(t *testing.T) { diff --git a/libs/testserver/apps.go b/libs/testserver/apps.go index 29bc70adc2a..208c9a33e5a 100644 --- a/libs/testserver/apps.go +++ b/libs/testserver/apps.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "net/http" + "slices" "strconv" "strings" @@ -28,6 +29,16 @@ func setUcSecurableKinds(app *apps.App) { } } +// maskInputOnly clears the fields the Apps API accepts on write but never echoes +// on read (source_code_path, git_source are input_only per the API spec; the read +// side exposes default_source_code_path / default_git_source instead). The stored +// app keeps the real values so deployments can resolve the configured source. +func maskInputOnly(app apps.App) apps.App { + app.SourceCodePath = "" + app.GitSource = nil + return app +} + func (s *FakeWorkspace) AppsCreateUpdate(req Request, name string) Response { var updateReq apps.AsyncUpdateAppRequest if err := json.Unmarshal(req.Body, &updateReq); err != nil { @@ -37,8 +48,20 @@ func (s *FakeWorkspace) AppsCreateUpdate(req Request, name string) Response { } } - // The real API rejects source_code_path in the UpdateApp body at any value. + // The real API rejects source_code_path on UpdateApp both in the update mask + // (production returns 400 INVALID_PARAMETER_VALUE "Invalid update mask", see + // https://github.com/databricks/cli/pull/6399) and in the body at any value (#6401: + // the CLI sent "source_code_path": "" and got a 400). if updateReq.App != nil { + if slices.Contains(strings.Split(updateReq.UpdateMask, ","), "source_code_path") { + return Response{ + StatusCode: http.StatusBadRequest, + Body: map[string]string{ + "error_code": "INVALID_PARAMETER_VALUE", + "message": "Invalid update mask: source_code_path cannot be updated.", + }, + } + } var rawBody struct { App json.RawMessage `json:"app"` } @@ -156,6 +179,7 @@ func (s *FakeWorkspace) AppsCreateDeployment(req Request, name string) Response app.ActiveDeployment = &deployment app.DefaultSourceCodePath = deployment.SourceCodePath + app.DefaultGitSource = deployment.GitSource s.Apps[name] = app return Response{Body: deployment} @@ -195,7 +219,7 @@ func (s *FakeWorkspace) AppsStart(_ Request, name string) Response { } s.Apps[name] = app - return Response{Body: app} + return Response{Body: maskInputOnly(app)} } func (s *FakeWorkspace) AppsStop(_ Request, name string) Response { @@ -221,7 +245,7 @@ func (s *FakeWorkspace) AppsStop(_ Request, name string) Response { app.PendingDeployment = nil s.Apps[name] = app - return Response{Body: app} + return Response{Body: maskInputOnly(app)} } // AppsGet returns the app, keeping DELETING resources visible so callers can @@ -237,7 +261,7 @@ func (s *FakeWorkspace) AppsGet(name string) Response { } } - return Response{Body: app} + return Response{Body: maskInputOnly(app)} } // AppsDelete simulates the real Apps DELETE lifecycle: the first DELETE flips @@ -336,9 +360,17 @@ func (s *FakeWorkspace) AppsUpsert(req Request, name string) Response { Message: "App compute is active.", } - // Simulate the apps platform side effect: when an app is created, it is deployed with the default source code path. + // Simulate the apps platform side effect: when an app with running compute + // is created, it is deployed from its configured source. source_code_path and + // git_source now arrive on the Create request; fall back to the default path + // for apps that configure neither. + sourceCodePath := app.SourceCodePath + if sourceCodePath == "" && app.GitSource == nil { + sourceCodePath = "/Workspace/Users/tester@databricks.com/" + name + } deployment := apps.AppDeployment{ - SourceCodePath: "/Workspace/Users/tester@databricks.com/" + name, + SourceCodePath: sourceCodePath, + GitSource: app.GitSource, } deployment.DeploymentId = fmt.Sprintf("deploy-%d", nextID()) @@ -349,6 +381,7 @@ func (s *FakeWorkspace) AppsUpsert(req Request, name string) Response { app.ActiveDeployment = &deployment app.DefaultSourceCodePath = deployment.SourceCodePath + app.DefaultGitSource = app.GitSource } app.Url = name + "-123.cloud.databricksapps.com" @@ -389,6 +422,6 @@ func (s *FakeWorkspace) AppsUpsert(req Request, name string) Response { s.Apps[name] = app return Response{ - Body: app, + Body: maskInputOnly(app), } }