Skip to content

Commit

Permalink
[Exporter] Improve code generation for SQL Endpoints (#3764)
Browse files Browse the repository at this point in the history
## Changes
<!-- Summary of your changes that are easy to understand -->

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
<!-- 
How is this tested? Please see the checklist below and also describe any
other relevant 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
  • Loading branch information
alexott authored Jul 16, 2024
1 parent b571c55 commit 9e8fd30
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion exporter/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions exporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {`))
})
}

Expand Down
25 changes: 25 additions & 0 deletions exporter/importables.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"},
},
Expand Down
2 changes: 2 additions & 0 deletions exporter/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9e8fd30

Please sign in to comment.