diff --git a/bundle/config/resources.go b/bundle/config/resources.go index 866bd7641..ab5c8f27f 100644 --- a/bundle/config/resources.go +++ b/bundle/config/resources.go @@ -118,6 +118,12 @@ func (r *Resources) FindResourceByConfigKey(key string) (ConfigResource, error) } } + for k := range r.Experiments { + if k == key { + found = append(found, r.Experiments[k]) + } + } + if len(found) == 0 { return nil, fmt.Errorf("no such resource: %s", key) } diff --git a/integration/bundle/bind_resource_test.go b/integration/bundle/bind_resource_test.go index b182150ad..a15f428db 100644 --- a/integration/bundle/bind_resource_test.go +++ b/integration/bundle/bind_resource_test.go @@ -17,8 +17,70 @@ import ( "github.com/stretchr/testify/require" "github.com/databricks/databricks-sdk-go/service/catalog" + "github.com/databricks/databricks-sdk-go/service/ml" ) +func TestBindExperimentToExistingExperiment(t *testing.T) { + ctx, wt := acc.UcWorkspaceTest(t) + + currentUser, err := wt.W.CurrentUser.Me(ctx) + require.NoError(t, err) + + // create a pre-defined experiment: + uniqueId := uuid.New().String() + experimentName := "/Workspace/Users/" + currentUser.UserName + "/test-experiment" + uniqueId + predefinedExperiment, err := wt.W.Experiments.CreateExperiment(ctx, ml.CreateExperiment{ + Name: experimentName, + ArtifactLocation: "s3://test-location", + }) + require.NoError(t, err) + t.Cleanup(func() { + err := wt.W.Experiments.DeleteExperiment(ctx, ml.DeleteExperiment{ExperimentId: predefinedExperiment.ExperimentId}) + require.NoError(t, err) + }) + + // setup the bundle: + bundleRoot := initTestTemplate(t, ctx, "ml_experiment", map[string]any{ + "unique_id": uniqueId, + "experiment_name": experimentName, + }) + ctx = env.Set(ctx, "BUNDLE_ROOT", bundleRoot) + + // run the bind command: + c := testcli.NewRunner(t, ctx, "bundle", "deployment", "bind", "experiment1", predefinedExperiment.ExperimentId, "--auto-approve") + _, _, err = c.Run() + require.NoError(t, err) + + // deploy the bundle: + deployBundle(t, ctx, bundleRoot) + + // check that the predefinedExperiment was not re-created / deleted (it is still active): + w, err := databricks.NewWorkspaceClient() + require.NoError(t, err) + + updatedExperiment, err := w.Experiments.GetExperiment(ctx, ml.GetExperimentRequest{ + ExperimentId: predefinedExperiment.ExperimentId, + }) + require.NoError(t, err) + + require.Equal(t, "active", updatedExperiment.Experiment.LifecycleStage) + + // unbind the experiment: + c = testcli.NewRunner(t, ctx, "bundle", "deployment", "unbind", "experiment1") + _, _, err = c.Run() + require.NoError(t, err) + + // destroy the bundle: + destroyBundle(t, ctx, bundleRoot) + + // Check that schema is unbound and exists after bundle is destroyed + postDestroyExperiment, err := w.Experiments.GetExperiment(ctx, ml.GetExperimentRequest{ + ExperimentId: predefinedExperiment.ExperimentId, + }) + require.NoError(t, err) + require.Equal(t, "active", postDestroyExperiment.Experiment.LifecycleStage) +} + func TestBindSchemaToExistingSchema(t *testing.T) { ctx, wt := acc.UcWorkspaceTest(t) diff --git a/integration/bundle/bundles/ml_experiment/databricks_template_schema.json b/integration/bundle/bundles/ml_experiment/databricks_template_schema.json new file mode 100644 index 000000000..05ceb2792 --- /dev/null +++ b/integration/bundle/bundles/ml_experiment/databricks_template_schema.json @@ -0,0 +1,12 @@ +{ + "properties": { + "unique_id": { + "type": "string", + "description": "Unique ID for the experiment name" + }, + "experiment_name": { + "type": "string", + "description": "Experiment name. An experiment name must be an absolute path within the Databricks workspace, e.g. '/Users//my-experiment'" + } + } +} diff --git a/integration/bundle/bundles/ml_experiment/template/databricks.yml.tmpl b/integration/bundle/bundles/ml_experiment/template/databricks.yml.tmpl new file mode 100644 index 000000000..30b62aeca --- /dev/null +++ b/integration/bundle/bundles/ml_experiment/template/databricks.yml.tmpl @@ -0,0 +1,12 @@ +bundle: + name: ml-experiment + +workspace: + root_path: "~/.bundle/{{.unique_id}}" + +resources: + experiments: + experiment1: + name: {{.experiment_name}} + artifact_location: s3://test-location +