This commit is contained in:
Andrew Nester 2024-12-18 11:49:22 +01:00
parent ab85a7f992
commit f6de4c1e9e
No known key found for this signature in database
GPG Key ID: 12BC628A44B7DA57
4 changed files with 33 additions and 37 deletions

View File

@ -19,37 +19,27 @@ func (i *interpolateVariables) Apply(ctx context.Context, b *bundle.Bundle) diag
dyn.Key("config"), dyn.Key("config"),
) )
tfToConfigMap := map[string]string{
"databricks_pipeline": "pipelines",
"databricks_job": "jobs",
"databricks_mlflow_model": "models",
"databricks_mlflow_experiment": "experiments",
"databricks_model_serving": "model_serving_endpoints",
"databricks_registered_model": "registered_models",
"databricks_quality_monitor": "quality_monitors",
"databricks_schema": "schemas",
"databricks_volume": "volumes",
"databricks_cluster": "clusters",
"databricks_dashboard": "dashboards",
"databricks_app": "apps",
}
err := b.Config.Mutate(func(root dyn.Value) (dyn.Value, error) { err := b.Config.Mutate(func(root dyn.Value) (dyn.Value, error) {
return dyn.MapByPattern(root, pattern, func(p dyn.Path, v dyn.Value) (dyn.Value, error) { return dyn.MapByPattern(root, pattern, func(p dyn.Path, v dyn.Value) (dyn.Value, error) {
return dynvar.Resolve(v, func(path dyn.Path) (dyn.Value, error) { return dynvar.Resolve(v, func(path dyn.Path) (dyn.Value, error) {
switch path[0] { key, ok := tfToConfigMap[path[0].Key()]
case dyn.Key("databricks_pipeline"): if ok {
path = dyn.NewPath(dyn.Key("resources"), dyn.Key("pipelines")).Append(path[1:]...) path = dyn.NewPath(dyn.Key("resources"), dyn.Key(key)).Append(path[1:]...)
case dyn.Key("databricks_job"):
path = dyn.NewPath(dyn.Key("resources"), dyn.Key("jobs")).Append(path[1:]...)
case dyn.Key("databricks_mlflow_model"):
path = dyn.NewPath(dyn.Key("resources"), dyn.Key("models")).Append(path[1:]...)
case dyn.Key("databricks_mlflow_experiment"):
path = dyn.NewPath(dyn.Key("resources"), dyn.Key("experiments")).Append(path[1:]...)
case dyn.Key("databricks_model_serving"):
path = dyn.NewPath(dyn.Key("resources"), dyn.Key("model_serving_endpoints")).Append(path[1:]...)
case dyn.Key("databricks_registered_model"):
path = dyn.NewPath(dyn.Key("resources"), dyn.Key("registered_models")).Append(path[1:]...)
case dyn.Key("databricks_quality_monitor"):
path = dyn.NewPath(dyn.Key("resources"), dyn.Key("quality_monitors")).Append(path[1:]...)
case dyn.Key("databricks_schema"):
path = dyn.NewPath(dyn.Key("resources"), dyn.Key("schemas")).Append(path[1:]...)
case dyn.Key("databricks_volume"):
path = dyn.NewPath(dyn.Key("resources"), dyn.Key("volumes")).Append(path[1:]...)
case dyn.Key("databricks_cluster"):
path = dyn.NewPath(dyn.Key("resources"), dyn.Key("clusters")).Append(path[1:]...)
case dyn.Key("databricks_dashboard"):
path = dyn.NewPath(dyn.Key("resources"), dyn.Key("dashboards")).Append(path[1:]...)
case dyn.Key("databricks_app"):
path = dyn.NewPath(dyn.Key("resources"), dyn.Key("apps")).Append(path[1:]...)
default:
// Trigger "key not found" for unknown resource types.
return dyn.GetByPath(root, path)
} }
return dyn.GetByPath(root, path) return dyn.GetByPath(root, path)

View File

@ -25,6 +25,8 @@ func (u *uploadConfig) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnos
var diags diag.Diagnostics var diags diag.Diagnostics
errGroup, ctx := errgroup.WithContext(ctx) errGroup, ctx := errgroup.WithContext(ctx)
diagsPerApp := make(map[string]diag.Diagnostic)
for key, app := range b.Config.Resources.Apps { for key, app := range b.Config.Resources.Apps {
// If the app has a config, we need to deploy it first. // 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 // It means we need to write app.yml file with the content of the config field
@ -57,12 +59,12 @@ func (u *uploadConfig) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnos
errGroup.Go(func() error { errGroup.Go(func() error {
err = f.Write(ctx, path.Join(appPath, "app.yml"), buf, filer.OverwriteIfExists) err = f.Write(ctx, path.Join(appPath, "app.yml"), buf, filer.OverwriteIfExists)
if err != nil { if err != nil {
diags = append(diags, diag.Diagnostic{ diagsPerApp[key] = diag.Diagnostic{
Severity: diag.Error, Severity: diag.Error,
Summary: "Failed to save config", Summary: "Failed to save config",
Detail: fmt.Sprintf("Failed to write %s file: %s", path.Join(app.SourceCodePath, "app.yml"), err), Detail: fmt.Sprintf("Failed to write %s file: %s", path.Join(app.SourceCodePath, "app.yml"), err),
Locations: b.Config.GetLocations(fmt.Sprintf("resources.apps.%s", key)), Locations: b.Config.GetLocations(fmt.Sprintf("resources.apps.%s", key)),
}) }
} }
return nil return nil
}) })
@ -70,7 +72,11 @@ func (u *uploadConfig) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnos
} }
if err := errGroup.Wait(); err != nil { if err := errGroup.Wait(); err != nil {
return diag.FromErr(err) return diags.Extend(diag.FromErr(err))
}
for _, diag := range diagsPerApp {
diags = append(diags, diag)
} }
return diags return diags

View File

@ -70,8 +70,7 @@ env:
value: "%d"`, job.JobId)) value: "%d"`, job.JobId))
// Try to run the app // Try to run the app
_, out, err := runResourceWithStderr(t, ctx, root, "test_app") _, out := runResourceWithStderr(t, ctx, root, "test_app")
require.NoError(t, err)
require.Contains(t, out, app.Url) require.Contains(t, out, app.Url)
// App should be in the running state // App should be in the running state
@ -89,8 +88,7 @@ env:
require.Equal(t, apps.ApplicationStateUnavailable, app.AppStatus.State) require.Equal(t, apps.ApplicationStateUnavailable, app.AppStatus.State)
// Try to run the app again // Try to run the app again
_, out, err = runResourceWithStderr(t, ctx, root, "test_app") _, out = runResourceWithStderr(t, ctx, root, "test_app")
require.NoError(t, err)
require.Contains(t, out, app.Url) require.Contains(t, out, app.Url)
// App should be in the running state // App should be in the running state

View File

@ -119,13 +119,15 @@ func runResource(t testutil.TestingT, ctx context.Context, path, key string) (st
return stdout.String(), err return stdout.String(), err
} }
func runResourceWithStderr(t testutil.TestingT, ctx context.Context, path, key string) (string, string, error) { func runResourceWithStderr(t testutil.TestingT, ctx context.Context, path, key string) (string, string) {
ctx = env.Set(ctx, "BUNDLE_ROOT", path) ctx = env.Set(ctx, "BUNDLE_ROOT", path)
ctx = cmdio.NewContext(ctx, cmdio.Default()) ctx = cmdio.NewContext(ctx, cmdio.Default())
c := testcli.NewRunner(t, ctx, "bundle", "run", key) c := testcli.NewRunner(t, ctx, "bundle", "run", key)
stdout, stderr, err := c.Run() stdout, stderr, err := c.Run()
return stdout.String(), stderr.String(), err require.NoError(t, err)
return stdout.String(), stderr.String()
} }
func runResourceWithParams(t testutil.TestingT, ctx context.Context, path, key string, params ...string) (string, error) { func runResourceWithParams(t testutil.TestingT, ctx context.Context, path, key string, params ...string) (string, error) {