@@ -16,6 +16,7 @@ import (
1616 "github.com/databricks/terraform-provider-databricks/common"
1717 "github.com/databricks/terraform-provider-databricks/provider"
1818 "github.com/databricks/terraform-provider-databricks/scim"
19+ "github.com/databricks/terraform-provider-databricks/workspace"
1920
2021 "github.com/hashicorp/hcl/v2"
2122 "github.com/hashicorp/hcl/v2/hclsyntax"
@@ -66,6 +67,7 @@ type importContext struct {
6667 testEmits map [string ]bool
6768 sqlDatasources map [string ]string
6869 workspaceConfKeys map [string ]any
70+ allDirectories []workspace.ObjectStatus
6971
7072 includeUserDomains bool
7173 importAllUsers bool
@@ -132,9 +134,9 @@ func newImportContext(c *common.DatabricksClient) *importContext {
132134 nameFixes : nameFixes ,
133135 hclFixes : []regexFix { // Be careful with that! it may break working code
134136 },
135- allUsers : []scim.User {},
136- variables : map [string ]string {},
137-
137+ allUsers : []scim.User {},
138+ variables : map [string ]string {},
139+ allDirectories : []workspace. ObjectStatus {},
138140 workspaceConfKeys : workspaceConfKeys ,
139141 }
140142}
@@ -300,11 +302,24 @@ func (ic *importContext) MatchesName(n string) bool {
300302 return strings .Contains (strings .ToLower (n ), strings .ToLower (ic .match ))
301303}
302304
303- func (ic * importContext ) Find (r * resource , pick string , matchType MatchType ) (string , hcl.Traversal ) {
305+ func (ic * importContext ) Find (r * resource , pick string , ref reference ) (string , hcl.Traversal ) {
304306 for _ , sr := range ic .State .Resources {
305307 if sr .Type != r .Resource {
306308 continue
307309 }
310+ // optimize performance by avoiding doing regexp matching multiple times
311+ matchValue := ""
312+ if ref .MatchType == MatchRegexp {
313+ if ref .Regexp == nil {
314+ log .Printf ("[WARN] you must provide regular expression for 'regexp' match type" )
315+ continue
316+ }
317+ res := ref .Regexp .FindStringSubmatch (r .Value )
318+ if len (res ) < 2 {
319+ log .Printf ("[WARN] no match for regexp: %v in string %s" , ref .Regexp , r .Value )
320+ }
321+ matchValue = res [1 ]
322+ }
308323 for _ , i := range sr .Instances {
309324 v := i .Attributes [r .Attribute ]
310325 if v == nil {
@@ -314,13 +329,15 @@ func (ic *importContext) Find(r *resource, pick string, matchType MatchType) (st
314329 }
315330 strValue := v .(string )
316331 matched := false
317- switch matchType {
318- case MatchExact :
332+ switch ref . MatchType {
333+ case MatchExact , MatchDefault :
319334 matched = (strValue == r .Value )
320335 case MatchPrefix :
321336 matched = strings .HasPrefix (r .Value , strValue )
337+ case MatchRegexp :
338+ matched = (matchValue == strValue )
322339 default :
323- log .Printf ("[WARN] Unsupported match type: %s" , matchType )
340+ log .Printf ("[WARN] Unsupported match type: %s" , ref . MatchType )
324341 }
325342 if ! matched {
326343 continue
@@ -511,13 +528,13 @@ func (ic *importContext) getTraversalTokens(ref reference, value string) hclwrit
511528 Resource : ref .Resource ,
512529 Attribute : attr ,
513530 Value : value ,
514- }, attr , matchType )
531+ }, attr , ref )
515532 // at least one invocation of ic.Find will assign Nil to traversal if resource with value is not found
516533 if traversal == nil {
517534 return nil
518535 }
519536 switch matchType {
520- case MatchExact :
537+ case MatchExact , MatchDefault :
521538 return hclwrite .TokensForTraversal (traversal )
522539 case MatchPrefix :
523540 rest := value [len (attrValue ):]
@@ -527,6 +544,19 @@ func (ic *importContext) getTraversalTokens(ref reference, value string) hclwrit
527544 tokens = append (tokens , & hclwrite.Token {Type : hclsyntax .TokenQuotedLit , Bytes : []byte (rest )})
528545 tokens = append (tokens , & hclwrite.Token {Type : hclsyntax .TokenCQuote , Bytes : []byte {'"' }})
529546 return tokens
547+ case MatchRegexp :
548+ indices := ref .Regexp .FindStringSubmatchIndex (value )
549+ if len (indices ) == 4 {
550+ tokens := hclwrite.Tokens {& hclwrite.Token {Type : hclsyntax .TokenOQuote , Bytes : []byte {'"' }}}
551+ tokens = append (tokens , & hclwrite.Token {Type : hclsyntax .TokenQuotedLit , Bytes : []byte (value [0 :indices [2 ]])})
552+ tokens = append (tokens , & hclwrite.Token {Type : hclsyntax .TokenOQuote , Bytes : []byte {'$' , '{' }})
553+ tokens = append (tokens , hclwrite .TokensForTraversal (traversal )... )
554+ tokens = append (tokens , & hclwrite.Token {Type : hclsyntax .TokenCQuote , Bytes : []byte {'}' }})
555+ tokens = append (tokens , & hclwrite.Token {Type : hclsyntax .TokenQuotedLit , Bytes : []byte (value [indices [3 ]:])})
556+ tokens = append (tokens , & hclwrite.Token {Type : hclsyntax .TokenCQuote , Bytes : []byte {'"' }})
557+ return tokens
558+ }
559+ log .Printf ("[WARN] Can't match found data in '%s'. Indices: %v" , value , indices )
530560 default :
531561 log .Printf ("[WARN] Unsupported match type: %s" , ref .MatchType )
532562 }
0 commit comments