From 9e8fd30f9b613314a3ec64f82fe78eb54c54b510 Mon Sep 17 00:00:00 2001 From: Alex Ott Date: Tue, 16 Jul 2024 10:21:07 +0200 Subject: [PATCH] [Exporter] Improve code generation for SQL Endpoints (#3764) ## Changes Changes include: - Always generate `enable_serverless_compute` attribute for `databricks_sql_endpoint` and `databricks_sql_global_config` - this is required when there is a mix of serverless & non-serverless SQL endpoints - Don't generate `channel` when it's empty or is current - Don't generate `tags` block when there are no tags - Importable can define `ShouldGenerateField` function to specify if the code for a field should be generated even if it's `omitempty` and having the zero or default value. This was required for generation of `enable_serverless_compute = false` that is marked as `omitempty` Fixes #3758 ## Tests - [x] `make test` run locally - [ ] relevant change in `docs/` folder - [ ] covered with integration tests in `internal/acceptance` - [ ] relevant acceptance tests are passing - [ ] using Go SDK --- exporter/context.go | 2 +- exporter/exporter_test.go | 7 +++++++ exporter/importables.go | 25 +++++++++++++++++++++++++ exporter/model.go | 2 ++ 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/exporter/context.go b/exporter/context.go index ee3f2a753b..441ed12078 100644 --- a/exporter/context.go +++ b/exporter/context.go @@ -1651,7 +1651,7 @@ func (ic *importContext) dataToHcl(i importable, path []string, // In case when have zero value, but there is non-zero default, we also need to produce it shouldSkip = false } - if shouldSkip { + if shouldSkip && (i.ShouldGenerateField == nil || !i.ShouldGenerateField(ic, pathString, as, d)) { continue } switch as.Type { diff --git a/exporter/exporter_test.go b/exporter/exporter_test.go index 30c92591d6..2739286e66 100644 --- a/exporter/exporter_test.go +++ b/exporter/exporter_test.go @@ -1917,6 +1917,13 @@ func TestImportingSqlObjects(t *testing.T) { err := ic.Run() assert.NoError(t, err) + + content, err := os.ReadFile(tmpDir + "/sql-endpoints.tf") + assert.NoError(t, err) + contentStr := string(content) + assert.True(t, strings.Contains(contentStr, `enable_serverless_compute = false`)) + assert.True(t, strings.Contains(contentStr, `resource "databricks_sql_endpoint" "test" {`)) + assert.False(t, strings.Contains(contentStr, `tags {`)) }) } diff --git a/exporter/importables.go b/exporter/importables.go index 72e8f71d4b..e607951f04 100644 --- a/exporter/importables.go +++ b/exporter/importables.go @@ -1713,6 +1713,25 @@ var resourcesMap map[string]importable = map[string]importable{ return nil }, Ignore: generateIgnoreObjectWithoutName("databricks_sql_endpoint"), + ShouldOmitField: func(ic *importContext, pathString string, as *schema.Schema, d *schema.ResourceData) bool { + switch pathString { + case "enable_serverless_compute": + return false + case "tags": + return d.Get("tags.0.custom_tags.#").(int) == 0 + case "channel.0.name": + channelName := d.Get(pathString).(string) + return channelName == "" || channelName == "CHANNEL_NAME_CURRENT" + case "channel": + channelName := d.Get(pathString + ".0.name").(string) + return channelName == "" || channelName == "CHANNEL_NAME_CURRENT" + } + return defaultShouldOmitFieldFunc(ic, pathString, as, d) + }, + ShouldGenerateField: func(ic *importContext, pathString string, as *schema.Schema, d *schema.ResourceData) bool { + // We need to generate it even if it's false... + return pathString == "enable_serverless_compute" + }, }, "databricks_sql_global_config": { WorkspaceLevel: true, @@ -1739,6 +1758,12 @@ var resourcesMap map[string]importable = map[string]importable{ } return nil }, + ShouldOmitField: func(ic *importContext, pathString string, as *schema.Schema, d *schema.ResourceData) bool { + if pathString == "enable_serverless_compute" { + return false + } + return defaultShouldOmitFieldFunc(ic, pathString, as, d) + }, Depends: []reference{ {Path: "instance_profile_arn", Resource: "databricks_instance_profile"}, }, diff --git a/exporter/model.go b/exporter/model.go index c03709207d..63b56a7fe0 100644 --- a/exporter/model.go +++ b/exporter/model.go @@ -153,6 +153,8 @@ type importable struct { Ignore func(ic *importContext, r *resource) bool // Function to check if the field in the given resource should be omitted or not ShouldOmitField func(ic *importContext, pathString string, as *schema.Schema, d *schema.ResourceData) bool + // Function to check if the field in the given resource should be generated or not independently of the value + ShouldGenerateField func(ic *importContext, pathString string, as *schema.Schema, d *schema.ResourceData) bool // Defines which API version should be used for this specific resource ApiVersion common.ApiVersion // Defines if specific service is account level resource