Skip to content

Commit

Permalink
Address PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
alexott committed Jan 3, 2023
1 parent 80535d1 commit 3dca34d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 32 deletions.
67 changes: 43 additions & 24 deletions exporter/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func (ic *importContext) MatchesName(n string) bool {
return strings.Contains(strings.ToLower(n), strings.ToLower(ic.match))
}

func (ic *importContext) Find(r *resource, pick, matchType string) (string, hcl.Traversal) {
func (ic *importContext) Find(r *resource, pick string, matchType MatchType) (string, hcl.Traversal) {
for _, sr := range ic.State.Resources {
if sr.Type != r.Resource {
continue
Expand All @@ -314,8 +314,16 @@ func (ic *importContext) Find(r *resource, pick, matchType string) (string, hcl.
continue
}
strValue := v.(string)
if ((matchType == "" || matchType == "exact") && strValue != r.Value) ||
(matchType == "prefix" && !strings.HasPrefix(r.Value, strValue)) {
matched := false
switch matchType {
case MatchExact:
matched = (strValue == r.Value)
case MatchPrefix:
matched = strings.HasPrefix(r.Value, strValue)
default:
log.Printf("[WARN] Unsupported match type: %s", matchType)
}
if !matched {
continue
}
if sr.Mode == "data" {
Expand Down Expand Up @@ -490,6 +498,35 @@ func (ic *importContext) Emit(r *resource) {
ic.Add(r)
}

func (ic *importContext) getTraversalTokens(ref reference, value string) hclwrite.Tokens {
matchType := ref.MatchTypeValue()
attr := ref.MatchAttribute()
attrValue, traversal := ic.Find(&resource{
Resource: ref.Resource,
Attribute: attr,
Value: value,
}, attr, matchType)
// at least one invocation of ic.Find will assign Nil to traversal if resource with value is not found
if traversal == nil {
return nil
}
switch matchType {
case MatchExact:
return hclwrite.TokensForTraversal(traversal)
case MatchPrefix:
rest := value[len(attrValue):]
tokens := hclwrite.Tokens{&hclwrite.Token{Type: hclsyntax.TokenOQuote, Bytes: []byte{'"', '$', '{'}}}
tokens = append(tokens, hclwrite.TokensForTraversal(traversal)...)
tokens = append(tokens, &hclwrite.Token{Type: hclsyntax.TokenCQuote, Bytes: []byte{'}'}})
tokens = append(tokens, &hclwrite.Token{Type: hclsyntax.TokenQuotedLit, Bytes: []byte(rest)})
tokens = append(tokens, &hclwrite.Token{Type: hclsyntax.TokenCQuote, Bytes: []byte{'"'}})
return tokens
default:
log.Printf("[WARN] Unsupported match type: %s", ref.MatchType)
}
return nil
}

// TODO: move to IC
var dependsRe = regexp.MustCompile(`(\.[\d]+)`)

Expand All @@ -510,27 +547,9 @@ func (ic *importContext) reference(i importable, path []string, value string) hc
if d.Variable {
return ic.variable(fmt.Sprintf("%s_%s", path[0], value), "")
}
attr := "id"
if d.Match != "" {
attr = d.Match
}
attrValue, traversal := ic.Find(&resource{
Resource: d.Resource,
Attribute: attr,
Value: value,
}, attr, d.MatchType)
// at least one invocation of ic.Find will assign Nil to traversal if resource with value is not found
if traversal != nil {
if d.MatchType == "prefix" { // we're replacing the found prefix with the HCL reference using the "${reference}rest" syntax
rest := value[len(attrValue):]
tokens := hclwrite.Tokens{&hclwrite.Token{Type: hclsyntax.TokenOQuote, Bytes: []byte{'"', '$', '{'}}}
tokens = append(tokens, hclwrite.TokensForTraversal(traversal)...)
tokens = append(tokens, &hclwrite.Token{Type: hclsyntax.TokenCQuote, Bytes: []byte{'}'}})
tokens = append(tokens, &hclwrite.Token{Type: hclsyntax.TokenQuotedLit, Bytes: []byte(rest)})
tokens = append(tokens, &hclwrite.Token{Type: hclsyntax.TokenCQuote, Bytes: []byte{'"'}})
return tokens
}
return hclwrite.TokensForTraversal(traversal)

if tokens := ic.getTraversalTokens(d, value); tokens != nil {
return tokens
}
}
return hclwrite.TokensForValue(cty.StringVal(value))
Expand Down
14 changes: 7 additions & 7 deletions exporter/importables.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ var resourcesMap map[string]importable = map[string]importable{
{Path: "spark_python_task.parameters", Resource: "databricks_dbfs_file", Match: "dbfs_path"},
{Path: "spark_jar_task.jar_uri", Resource: "databricks_dbfs_file", Match: "dbfs_path"},
{Path: "notebook_task.notebook_path", Resource: "databricks_notebook"},
{Path: "notebook_task.notebook_path", Resource: "databricks_repo", Match: "path", MatchType: "prefix"},
{Path: "notebook_task.notebook_path", Resource: "databricks_repo", Match: "path", MatchType: MatchPrefix},
{Path: "pipeline_task.pipeline_id", Resource: "databricks_pipeline"},
{Path: "task.library.jar", Resource: "databricks_dbfs_file", Match: "dbfs_path"},
{Path: "task.library.whl", Resource: "databricks_dbfs_file", Match: "dbfs_path"},
Expand All @@ -297,7 +297,7 @@ var resourcesMap map[string]importable = map[string]importable{
{Path: "task.spark_python_task.parameters", Resource: "databricks_dbfs_file", Match: "dbfs_path"},
{Path: "task.spark_jar_task.jar_uri", Resource: "databricks_dbfs_file", Match: "dbfs_path"},
{Path: "task.notebook_task.notebook_path", Resource: "databricks_notebook"},
{Path: "task.notebook_task.notebook_path", Resource: "databricks_repo", Match: "path", MatchType: "prefix"},
{Path: "task.notebook_task.notebook_path", Resource: "databricks_repo", Match: "path", MatchType: MatchPrefix},
{Path: "task.pipeline_task.pipeline_id", Resource: "databricks_pipeline"},
{Path: "task.sql_task.query.query_id", Resource: "databricks_sql_query"},
{Path: "task.sql_task.dashboard.dashboard_id", Resource: "databricks_sql_dashboard"},
Expand Down Expand Up @@ -1006,8 +1006,8 @@ var resourcesMap map[string]importable = map[string]importable{
},

Depends: []reference{
{Path: "path", Resource: "databricks_user", Match: "repos", MatchType: "prefix"},
{Path: "path", Resource: "databricks_service_principal", Match: "repos", MatchType: "prefix"},
{Path: "path", Resource: "databricks_user", Match: "repos", MatchType: MatchPrefix},
{Path: "path", Resource: "databricks_service_principal", Match: "repos", MatchType: MatchPrefix},
},
},
"databricks_workspace_conf": {
Expand Down Expand Up @@ -1142,8 +1142,8 @@ var resourcesMap map[string]importable = map[string]importable{
},
Depends: []reference{
{Path: "source", File: true},
{Path: "path", Resource: "databricks_user", Match: "home", MatchType: "prefix"},
{Path: "path", Resource: "databricks_service_principal", Match: "home", MatchType: "prefix"},
{Path: "path", Resource: "databricks_user", Match: "home", MatchType: MatchPrefix},
{Path: "path", Resource: "databricks_service_principal", Match: "home", MatchType: MatchPrefix},
},
},
"databricks_sql_query": {
Expand Down Expand Up @@ -1475,7 +1475,7 @@ var resourcesMap map[string]importable = map[string]importable{
{Path: "cluster.instance_pool_id", Resource: "databricks_instance_pool"},
{Path: "cluster.driver_instance_pool_id", Resource: "databricks_instance_pool"},
{Path: "library.notebook.path", Resource: "databricks_notebook"},
{Path: "library.notebook.path", Resource: "databricks_repo", Match: "path", MatchType: "prefix"},
{Path: "library.notebook.path", Resource: "databricks_repo", Match: "path", MatchType: MatchPrefix},
{Path: "library.jar", Resource: "databricks_dbfs_file", Match: "dbfs_path"},
{Path: "library.whl", Resource: "databricks_dbfs_file", Match: "dbfs_path"},
},
Expand Down
25 changes: 24 additions & 1 deletion exporter/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,38 @@ type importable struct {
ApiVersion common.ApiVersion
}

type MatchType string

const (
// MatchExact is to specify that whole value should match
MatchExact = "exact"
// MatchPrefix is to specify that prefix of value should match
MatchPrefix = "prefix"
)

type reference struct {
Path string
Resource string
Match string
MatchType string // type of match, `prefix` - reference is embedded into string, `` (or `exact`) - full match
MatchType MatchType // type of match, `prefix` - reference is embedded into string, `` (or `exact`) - full match
Variable bool
File bool
}

func (r reference) MatchAttribute() string {
if r.Match != "" {
return r.Match
}
return "id"
}

func (r reference) MatchTypeValue() MatchType {
if r.MatchType == "" {
return MatchExact
}
return r.MatchType
}

type resource struct {
Resource string
ID string
Expand Down

0 comments on commit 3dca34d

Please sign in to comment.