|
| 1 | +package settings_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/databricks/databricks-sdk-go/apierr" |
| 9 | + "github.com/databricks/databricks-sdk-go/service/settings" |
| 10 | + "github.com/databricks/terraform-provider-databricks/common" |
| 11 | + "github.com/databricks/terraform-provider-databricks/internal/acceptance" |
| 12 | + "github.com/hashicorp/terraform-plugin-testing/terraform" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +func TestMwsAccDisableLegacyFeaturesSetting(t *testing.T) { |
| 18 | + template := ` |
| 19 | + resource "databricks_disable_legacy_features_setting" "this" { |
| 20 | + disable_legacy_features { |
| 21 | + value = "true" |
| 22 | + } |
| 23 | + } |
| 24 | + ` |
| 25 | + acceptance.AccountLevel(t, acceptance.Step{ |
| 26 | + Template: template, |
| 27 | + Check: acceptance.ResourceCheckWithState("databricks_disable_legacy_features_setting.this", |
| 28 | + func(ctx context.Context, client *common.DatabricksClient, state *terraform.InstanceState) error { |
| 29 | + ctx = context.WithValue(ctx, common.Api, common.API_2_1) |
| 30 | + w, err := client.AccountClient() |
| 31 | + require.NoError(t, err) |
| 32 | + etag := state.Attributes["etag"] |
| 33 | + require.NotEmpty(t, etag) |
| 34 | + res, err := w.Settings.DisableLegacyFeatures().Get(ctx, settings.GetDisableLegacyFeaturesRequest{ |
| 35 | + Etag: etag, |
| 36 | + }) |
| 37 | + require.NoError(t, err) |
| 38 | + // Check that the resource has been created and that it has the correct value. |
| 39 | + assert.Equal(t, res.DisableLegacyFeatures.Value, true) |
| 40 | + return nil |
| 41 | + }), |
| 42 | + }, |
| 43 | + acceptance.Step{ |
| 44 | + Template: template, |
| 45 | + Destroy: true, |
| 46 | + Check: acceptance.ResourceCheck("databricks_disable_legacy_features_setting.this", func(ctx context.Context, client *common.DatabricksClient, id string) error { |
| 47 | + ctx = context.WithValue(ctx, common.Api, common.API_2_1) |
| 48 | + w, err := client.AccountClient() |
| 49 | + require.NoError(t, err) |
| 50 | + // Terraform Check returns the latest resource status before it is destroyed, which has an outdated eTag. |
| 51 | + // We are making an update call to get the correct eTag in the response error. |
| 52 | + _, err = w.Settings.DisableLegacyFeatures().Update(ctx, settings.UpdateDisableLegacyFeaturesRequest{ |
| 53 | + AllowMissing: true, |
| 54 | + Setting: settings.DisableLegacyFeatures{ |
| 55 | + DisableLegacyFeatures: settings.BooleanMessage{ |
| 56 | + Value: false, |
| 57 | + }, |
| 58 | + }, |
| 59 | + FieldMask: "disable_legacy_features.value", |
| 60 | + }) |
| 61 | + assert.Error(t, err) |
| 62 | + var aerr *apierr.APIError |
| 63 | + if !errors.As(err, &aerr) { |
| 64 | + assert.FailNow(t, "cannot parse error message %v", err) |
| 65 | + } |
| 66 | + etag := aerr.Details[0].Metadata["etag"] |
| 67 | + res, err := w.Settings.DisableLegacyFeatures().Get(ctx, settings.GetDisableLegacyFeaturesRequest{ |
| 68 | + Etag: etag, |
| 69 | + }) |
| 70 | + // we should not be getting any error |
| 71 | + assert.NoError(t, err) |
| 72 | + // setting should go back to default |
| 73 | + assert.Equal(t, res.DisableLegacyFeatures.Value, false) |
| 74 | + return nil |
| 75 | + }), |
| 76 | + }, |
| 77 | + ) |
| 78 | +} |
0 commit comments