Skip to content

Commit fe32c46

Browse files
Make bundle deploy work if no resources are defined (#767)
## Changes This PR sets "resource" to nil in the terraform representation if no resources are defined in the bundle configuration. This solves two problems: 1. Makes bundle deploy work without any resources specified. 2. Previously if a `resources` block was removed after a deployment, that would fail with an error. Now the resources would get destroyed as expected. Also removes `TerraformHasNoResources` which is no longer needed. ## Tests New e2e tests.
1 parent be55310 commit fe32c46

12 files changed

Lines changed: 138 additions & 23 deletions

File tree

bundle/bundle.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ type Bundle struct {
3838
// Stores an initialized copy of this bundle's Terraform wrapper.
3939
Terraform *tfexec.Terraform
4040

41-
// Indicates that the Terraform definition based on this bundle is empty,
42-
// i.e. that it would deploy no resources.
43-
TerraformHasNoResources bool
44-
4541
// Stores the locker responsible for acquiring/releasing a deployment lock.
4642
Locker *locker.Locker
4743

bundle/deploy/terraform/apply.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ func (w *apply) Name() string {
1616
}
1717

1818
func (w *apply) Apply(ctx context.Context, b *bundle.Bundle) error {
19-
if b.TerraformHasNoResources {
20-
cmdio.LogString(ctx, "Note: there are no resources to deploy for this bundle")
21-
return nil
22-
}
2319
tf := b.Terraform
2420
if tf == nil {
2521
return fmt.Errorf("terraform not initialized")

bundle/deploy/terraform/convert.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func convPermission(ac resources.Permission) schema.ResourcePermissionsAccessCon
4949
//
5050
// NOTE: THIS IS CURRENTLY A HACK. WE NEED A BETTER WAY TO
5151
// CONVERT TO/FROM TERRAFORM COMPATIBLE FORMAT.
52-
func BundleToTerraform(config *config.Root) (*schema.Root, bool) {
52+
func BundleToTerraform(config *config.Root) *schema.Root {
5353
tfroot := schema.NewRoot()
5454
tfroot.Provider = schema.NewProviders()
5555
tfroot.Resource = schema.NewResources()
@@ -174,7 +174,13 @@ func BundleToTerraform(config *config.Root) (*schema.Root, bool) {
174174
}
175175
}
176176

177-
return tfroot, noResources
177+
// We explicitly set "resource" to nil to omit it from a JSON encoding.
178+
// This is required because the terraform CLI requires >= 1 resources defined
179+
// if the "resource" property is used in a .tf.json file.
180+
if noResources {
181+
tfroot.Resource = nil
182+
}
183+
return tfroot
178184
}
179185

180186
func TerraformToBundle(state *tfjson.State, config *config.Root) error {

bundle/deploy/terraform/convert_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func TestConvertJob(t *testing.T) {
5151
},
5252
}
5353

54-
out, _ := BundleToTerraform(&config)
54+
out := BundleToTerraform(&config)
5555
assert.Equal(t, "my job", out.Resource.Job["my_job"].Name)
5656
assert.Len(t, out.Resource.Job["my_job"].JobCluster, 1)
5757
assert.Equal(t, "https://github.com/foo/bar", out.Resource.Job["my_job"].GitSource.Url)
@@ -79,7 +79,7 @@ func TestConvertJobPermissions(t *testing.T) {
7979
},
8080
}
8181

82-
out, _ := BundleToTerraform(&config)
82+
out := BundleToTerraform(&config)
8383
assert.NotEmpty(t, out.Resource.Permissions["job_my_job"].JobId)
8484
assert.Len(t, out.Resource.Permissions["job_my_job"].AccessControl, 1)
8585

@@ -115,7 +115,7 @@ func TestConvertJobTaskLibraries(t *testing.T) {
115115
},
116116
}
117117

118-
out, _ := BundleToTerraform(&config)
118+
out := BundleToTerraform(&config)
119119
assert.Equal(t, "my job", out.Resource.Job["my_job"].Name)
120120
require.Len(t, out.Resource.Job["my_job"].Task, 1)
121121
require.Len(t, out.Resource.Job["my_job"].Task[0].Library, 1)
@@ -149,7 +149,7 @@ func TestConvertPipeline(t *testing.T) {
149149
},
150150
}
151151

152-
out, _ := BundleToTerraform(&config)
152+
out := BundleToTerraform(&config)
153153
assert.Equal(t, "my pipeline", out.Resource.Pipeline["my_pipeline"].Name)
154154
assert.Len(t, out.Resource.Pipeline["my_pipeline"].Library, 2)
155155
assert.Nil(t, out.Data)
@@ -173,7 +173,7 @@ func TestConvertPipelinePermissions(t *testing.T) {
173173
},
174174
}
175175

176-
out, _ := BundleToTerraform(&config)
176+
out := BundleToTerraform(&config)
177177
assert.NotEmpty(t, out.Resource.Permissions["pipeline_my_pipeline"].PipelineId)
178178
assert.Len(t, out.Resource.Permissions["pipeline_my_pipeline"].AccessControl, 1)
179179

@@ -208,7 +208,7 @@ func TestConvertModel(t *testing.T) {
208208
},
209209
}
210210

211-
out, _ := BundleToTerraform(&config)
211+
out := BundleToTerraform(&config)
212212
assert.Equal(t, "name", out.Resource.MlflowModel["my_model"].Name)
213213
assert.Equal(t, "description", out.Resource.MlflowModel["my_model"].Description)
214214
assert.Len(t, out.Resource.MlflowModel["my_model"].Tags, 2)
@@ -237,7 +237,7 @@ func TestConvertModelPermissions(t *testing.T) {
237237
},
238238
}
239239

240-
out, _ := BundleToTerraform(&config)
240+
out := BundleToTerraform(&config)
241241
assert.NotEmpty(t, out.Resource.Permissions["mlflow_model_my_model"].RegisteredModelId)
242242
assert.Len(t, out.Resource.Permissions["mlflow_model_my_model"].AccessControl, 1)
243243

@@ -261,7 +261,7 @@ func TestConvertExperiment(t *testing.T) {
261261
},
262262
}
263263

264-
out, _ := BundleToTerraform(&config)
264+
out := BundleToTerraform(&config)
265265
assert.Equal(t, "name", out.Resource.MlflowExperiment["my_experiment"].Name)
266266
assert.Nil(t, out.Data)
267267
}
@@ -284,7 +284,7 @@ func TestConvertExperimentPermissions(t *testing.T) {
284284
},
285285
}
286286

287-
out, _ := BundleToTerraform(&config)
287+
out := BundleToTerraform(&config)
288288
assert.NotEmpty(t, out.Resource.Permissions["mlflow_experiment_my_experiment"].ExperimentId)
289289
assert.Len(t, out.Resource.Permissions["mlflow_experiment_my_experiment"].AccessControl, 1)
290290

@@ -327,7 +327,7 @@ func TestConvertModelServing(t *testing.T) {
327327
},
328328
}
329329

330-
out, _ := BundleToTerraform(&config)
330+
out := BundleToTerraform(&config)
331331
resource := out.Resource.ModelServing["my_model_serving_endpoint"]
332332
assert.Equal(t, "name", resource.Name)
333333
assert.Equal(t, "model_name", resource.Config.ServedModels[0].ModelName)
@@ -357,7 +357,7 @@ func TestConvertModelServingPermissions(t *testing.T) {
357357
},
358358
}
359359

360-
out, _ := BundleToTerraform(&config)
360+
out := BundleToTerraform(&config)
361361
assert.NotEmpty(t, out.Resource.Permissions["model_serving_my_model_serving_endpoint"].ServingEndpointId)
362362
assert.Len(t, out.Resource.Permissions["model_serving_my_model_serving_endpoint"].AccessControl, 1)
363363

bundle/deploy/terraform/write.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ func (w *write) Apply(ctx context.Context, b *bundle.Bundle) error {
2121
return err
2222
}
2323

24-
root, noResources := BundleToTerraform(&b.Config)
25-
b.TerraformHasNoResources = noResources
24+
root := BundleToTerraform(&b.Config)
2625
f, err := os.Create(filepath.Join(dir, "bundle.tf.json"))
2726
if err != nil {
2827
return err
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"properties": {
3+
"unique_id": {
4+
"type": "string",
5+
"description": "Unique ID for job name"
6+
}
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
bundle:
2+
name: deploy-then-remove
3+
4+
workspace:
5+
root_path: "~/.bundle/{{.unique_id}}"
6+
7+
include:
8+
- "./*.yml"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print("hello")
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
resources:
2+
pipelines:
3+
bar:
4+
name: test-bundle-pipeline-{{.unique_id}}
5+
libraries:
6+
- notebook:
7+
path: "./foo.py"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bundle:
2+
name: abc

0 commit comments

Comments
 (0)