Skip to content

Commit fcf6910

Browse files
authored
Exporter: fix issue with exporting of resources with simple name (#1891)
* Exporter: fix issue with exporting of resources with simple name The `Add` function is called only from the `Emit` so it's safe to remove the check if resource was already imported or not. this fixes #1838 * Make unique names for secret scopes & secrets It's possible to use mixed characters in names that will generate non-unique names of secret scopes & secrets. Like, for unique scopes `Test-scope` and `test_scope` will generate the same name, so terraform will complain about it. The solution - add MD5 checksum over the scope & secret names * Restore Has check in the Add, but do it differently
1 parent 883cca6 commit fcf6910

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

exporter/context.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,14 @@ func (ic *importContext) Find(r *resource, pick string, matchType MatchType) (st
345345
return "", nil
346346
}
347347

348+
// This function checks if resource exist in any state (already added or in process of addition)
348349
func (ic *importContext) Has(r *resource) bool {
349-
if _, visiting := ic.importing[r.String()]; visiting {
350+
return ic.HasInState(r, false)
351+
}
352+
353+
// This function checks if resource exist. onlyAdded flag enforces that true is returned only if it was added with Add()
354+
func (ic *importContext) HasInState(r *resource, onlyAdded bool) bool {
355+
if v, visiting := ic.importing[r.String()]; visiting && (v || !onlyAdded) {
350356
return true
351357
}
352358
k, v := r.MatchPair()
@@ -364,9 +370,10 @@ func (ic *importContext) Has(r *resource) bool {
364370
}
365371

366372
func (ic *importContext) Add(r *resource) {
367-
if ic.Has(r) {
373+
if ic.HasInState(r, true) { // resource must exist and already marked as added
368374
return
369375
}
376+
ic.importing[r.String()] = true // mark resource as added
370377
state := r.Data.State()
371378
if state == nil {
372379
log.Printf("[ERROR] state is nil for %s", r)
@@ -437,7 +444,7 @@ func (ic *importContext) Emit(r *resource) {
437444
ic.testEmits[r.String()] = true
438445
return
439446
}
440-
ic.importing[r.String()] = true
447+
ic.importing[r.String()] = false // we're starting to add a new resource
441448
pr, ok := ic.Resources[r.Resource]
442449
if !ok {
443450
log.Printf("[ERROR] %s is not available in provider", r)

exporter/importables.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,8 @@ var resourcesMap map[string]importable = map[string]importable{
774774
"databricks_secret_scope": {
775775
Service: "secrets",
776776
Name: func(ic *importContext, d *schema.ResourceData) string {
777-
return d.Get("name").(string)
777+
name := d.Get("name").(string)
778+
return name + "_" + generateUniqueID(name)
778779
},
779780
List: func(ic *importContext) error {
780781
ssAPI := secrets.NewSecretScopesAPI(ic.Context, ic.Client)
@@ -788,7 +789,6 @@ var resourcesMap map[string]importable = map[string]importable{
788789
ic.Emit(&resource{
789790
Resource: "databricks_secret_scope",
790791
ID: scope.Name,
791-
Name: scope.Name,
792792
})
793793
log.Printf("[INFO] Imported %d of %d secret scopes", i+1, len(scopes))
794794
}
@@ -829,7 +829,8 @@ var resourcesMap map[string]importable = map[string]importable{
829829
{Path: "string_value", Resource: "aws_secretsmanager_secret_version", Match: "secret_string"},
830830
},
831831
Name: func(ic *importContext, d *schema.ResourceData) string {
832-
return fmt.Sprintf("%s_%s", d.Get("scope"), d.Get("key"))
832+
name := fmt.Sprintf("%s_%s", d.Get("scope"), d.Get("key"))
833+
return name + "_" + generateUniqueID(name)
833834
},
834835
},
835836
"databricks_secret_acl": {

exporter/importables_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func TestSecretScope(t *testing.T) {
194194
d.Set("name", "abc")
195195
ic := importContextForTest()
196196
name := ic.Importables["databricks_secret_scope"].Name(ic, d)
197-
assert.Equal(t, "abc", name)
197+
assert.Equal(t, "abc_a9993e3647", name)
198198
}
199199

200200
func TestInstancePoolNameFromID(t *testing.T) {
@@ -769,8 +769,8 @@ func TestSecretGen(t *testing.T) {
769769

770770
ic.generateHclForResources(nil)
771771
assert.Equal(t, commands.TrimLeadingWhitespace(`
772-
resource "databricks_secret" "a_b" {
773-
string_value = var.string_value_a_b
772+
resource "databricks_secret" "a_b_eb2980a5a2" {
773+
string_value = var.string_value_a_b_eb2980a5a2
774774
scope = "a"
775775
key = "b"
776776
}`), string(ic.Files["secrets"].Bytes()))

exporter/util.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package exporter
22

33
import (
4+
"crypto/sha1"
45
"encoding/json"
56
"fmt"
67
"log"
@@ -506,3 +507,7 @@ func resourceOrDataBlockBody(ic *importContext, body *hclwrite.Body, r *resource
506507
return ic.dataToHcl(ic.Importables[r.Resource],
507508
[]string{}, ic.Resources[r.Resource], r.Data, resourceBlock.Body())
508509
}
510+
511+
func generateUniqueID(v string) string {
512+
return fmt.Sprintf("%x", sha1.Sum([]byte(v)))[:10]
513+
}

0 commit comments

Comments
 (0)