Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exporter: Don't generate resources with empty names #3345

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions exporter/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,7 @@ func (ic *importContext) Find(value, attr string, ref reference, origResource *r
if sr != nil && (ref.IsValidApproximation == nil || ref.IsValidApproximation(ic, origResource, sr, origPath)) {
log.Printf("[DEBUG] Finished direct lookup for reference for resource %s, attr='%s', value='%s', ref=%v. Found: type=%s name=%s",
ref.Resource, attr, value, ref, sr.Type, sr.Name)
// TODO: we need to not generate traversals resources for which their Ignore function returns true...
return matchValue, genTraversalTokens(sr, attr), sr.Mode == "data"
}
if ref.MatchType != MatchCaseInsensitive { // for case-insensitive matching we'll try iteration
Expand Down
8 changes: 8 additions & 0 deletions exporter/importables.go
Original file line number Diff line number Diff line change
Expand Up @@ -1657,6 +1657,7 @@ var resourcesMap map[string]importable = map[string]importable{
}
return nil
},
Ignore: generateIgnoreObjectWithoutName("databricks_sql_query"),
Depends: []reference{
{Path: "data_source_id", Resource: "databricks_sql_endpoint", Match: "data_source_id"},
{Path: "parameter.query.query_id", Resource: "databricks_sql_query", Match: "id"},
Expand Down Expand Up @@ -1705,6 +1706,7 @@ var resourcesMap map[string]importable = map[string]importable{
}
return nil
},
Ignore: generateIgnoreObjectWithoutName("databricks_sql_endpoint"),
},
"databricks_sql_global_config": {
WorkspaceLevel: true,
Expand Down Expand Up @@ -1896,6 +1898,7 @@ var resourcesMap map[string]importable = map[string]importable{
}
return nil
},
Ignore: generateIgnoreObjectWithoutName("databricks_sql_alert"),
Depends: []reference{
{Path: "query_id", Resource: "databricks_sql_query", Match: "id"},
{Path: "parent", Resource: "databricks_directory", Match: "object_id",
Expand Down Expand Up @@ -2418,6 +2421,7 @@ var resourcesMap map[string]importable = map[string]importable{
}
return shouldOmitForUnityCatalog(ic, pathString, as, d)
},
Ignore: generateIgnoreObjectWithoutName("databricks_catalog"),
Depends: []reference{
{Path: "connection_name", Resource: "databricks_connection", Match: "name"},
{Path: "storage_root", Resource: "databricks_external_location", Match: "url", MatchType: MatchLongestPrefix},
Expand Down Expand Up @@ -2495,6 +2499,7 @@ var resourcesMap map[string]importable = map[string]importable{
return nil
},
ShouldOmitField: shouldOmitForUnityCatalog,
Ignore: generateIgnoreObjectWithoutName("databricks_schema"),
Depends: []reference{
{Path: "catalog_name", Resource: "databricks_catalog"},
{Path: "storage_root", Resource: "databricks_external_location", Match: "url", MatchType: MatchLongestPrefix},
Expand Down Expand Up @@ -2527,6 +2532,7 @@ var resourcesMap map[string]importable = map[string]importable{
}
return shouldOmitForUnityCatalog(ic, pathString, as, d)
},
Ignore: generateIgnoreObjectWithoutName("databricks_volume"),
Depends: []reference{
{Path: "catalog_name", Resource: "databricks_catalog"},
{Path: "schema_name", Resource: "databricks_schema", Match: "name",
Expand Down Expand Up @@ -2556,6 +2562,7 @@ var resourcesMap map[string]importable = map[string]importable{
// TODO: emit owner? See comment in catalog resource
return nil
},
Ignore: generateIgnoreObjectWithoutName("databricks_sql_table"),
ShouldOmitField: func(ic *importContext, pathString string, as *schema.Schema, d *schema.ResourceData) bool {
switch pathString {
case "storage_location":
Expand Down Expand Up @@ -2838,6 +2845,7 @@ var resourcesMap map[string]importable = map[string]importable{
}
return shouldOmitForUnityCatalog(ic, pathString, as, d)
},
Ignore: generateIgnoreObjectWithoutName("databricks_registered_model"),
Depends: []reference{
{Path: "catalog_name", Resource: "databricks_catalog"},
{Path: "schema_name", Resource: "databricks_schema", Match: "name",
Expand Down
10 changes: 10 additions & 0 deletions exporter/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -1327,3 +1327,13 @@ func isMatchingAllowListArtifact(ic *importContext, res *resource, ra *resourceA

return ok && matchType.(string) == "PREFIX_MATCH" && (artifactType == "LIBRARY_JAR" || artifactType == "INIT_SCRIPT")
}

func generateIgnoreObjectWithoutName(resourceType string) func(ic *importContext, r *resource) bool {
return func(ic *importContext, r *resource) bool {
res := (r.Data != nil && r.Data.Get("name").(string) == "")
if res {
ic.addIgnoredResource(fmt.Sprintf("%s. ID=%s", resourceType, r.ID))
}
return res
}
}
22 changes: 22 additions & 0 deletions exporter/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/databricks/databricks-sdk-go/service/iam"
tfcatalog "github.com/databricks/terraform-provider-databricks/catalog"
"github.com/databricks/terraform-provider-databricks/clusters"
"github.com/databricks/terraform-provider-databricks/common"
"github.com/databricks/terraform-provider-databricks/qa"
Expand Down Expand Up @@ -390,3 +391,24 @@ func TestParallelListing(t *testing.T) {
require.Equal(t, 4, len(objects))

}

func TestIgnoreObjectWithEmptyName(t *testing.T) {
ic := importContextForTest()
// Test importing
d := tfcatalog.ResourceVolume().ToResource().TestResourceData()
d.SetId("vtest")
r := &resource{
ID: "vtest",
Data: d,
}
//
ignoreFunc := resourcesMap["databricks_volume"].Ignore
require.NotNil(t, ignoreFunc)
assert.True(t, ignoreFunc(ic, r))
require.Equal(t, 1, len(ic.ignoredResources))
assert.Contains(t, ic.ignoredResources, "databricks_volume. ID=vtest")

d.Set("name", "test")
assert.False(t, ignoreFunc(ic, r))
assert.Equal(t, 1, len(ic.ignoredResources))
}
Loading