Skip to content

Commit 036ce18

Browse files
authored
resourceIdentity: explcit conditional check of empty ID for identity import use (#14761)
2 parents 3086d03 + 9da4664 commit 036ce18

File tree

1 file changed

+39
-19
lines changed
  • mmv1/third_party/terraform/tpgresource

1 file changed

+39
-19
lines changed

mmv1/third_party/terraform/tpgresource/import.go

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,15 @@ import (
2121
func ParseImportId(idRegexes []string, d TerraformResourceData, config *transport_tpg.Config) error {
2222
for _, idFormat := range idRegexes {
2323
re, err := regexp.Compile(idFormat)
24-
2524
if err != nil {
2625
log.Printf("[DEBUG] Could not compile %s.", idFormat)
2726
return fmt.Errorf("Import is not supported. Invalid regex formats.")
2827
}
29-
30-
if d.Id() == "" {
31-
identity, err := d.Identity()
32-
if err != nil {
33-
return err
34-
}
35-
if err := identityImport(re, identity, idFormat, d); err != nil {
36-
return err
37-
}
38-
err = setDefaultValues(idRegexes[0], identity, d, config)
39-
if err != nil {
40-
return err
41-
}
42-
} else if fieldValues := re.FindStringSubmatch(d.Id()); fieldValues != nil {
28+
identity, err := d.Identity()
29+
if err != nil {
30+
return err
31+
}
32+
if fieldValues := re.FindStringSubmatch(d.Id()); fieldValues != nil {
4333
log.Printf("[DEBUG] matching ID %s to regex %s.", d.Id(), idFormat)
4434
// Starting at index 1, the first match is the full string.
4535
for i := 1; i < len(fieldValues); i++ {
@@ -60,13 +50,23 @@ func ParseImportId(idRegexes []string, d TerraformResourceData, config *transpor
6050
if err = d.Set(fieldName, fieldValue); err != nil {
6151
return err
6252
}
53+
if identity != nil {
54+
if err = identity.Set(fieldName, fieldValue); err != nil {
55+
return err
56+
}
57+
}
6358
} else if _, ok := val.(int); ok {
6459
if intVal, atoiErr := strconv.Atoi(fieldValue); atoiErr == nil {
6560
// If the value can be parsed as an integer, we try to set the
6661
// value as an integer.
6762
if err = d.Set(fieldName, intVal); err != nil {
6863
return err
6964
}
65+
if identity != nil {
66+
if err = identity.Set(fieldName, intVal); err != nil {
67+
return err
68+
}
69+
}
7070
} else {
7171
return fmt.Errorf("%s appears to be an integer, but %v cannot be parsed as an int", fieldName, fieldValue)
7272
}
@@ -82,22 +82,42 @@ func ParseImportId(idRegexes []string, d TerraformResourceData, config *transpor
8282
return err
8383
}
8484

85+
err = setDefaultValues(idRegexes[0], identity, d, config)
86+
if err != nil {
87+
return err
88+
}
89+
90+
return nil
91+
} else if d.Id() == "" {
92+
if err := identityImport(re, identity, idFormat, d); err != nil {
93+
return err
94+
}
95+
err = setDefaultValues(idRegexes[0], identity, d, config)
96+
if err != nil {
97+
return err
98+
}
8599
return nil
86100
}
87101
}
88102
return fmt.Errorf("Import id %q doesn't match any of the accepted formats: %v", d.Id(), idRegexes)
89103
}
90104

91105
func identityImport(re *regexp.Regexp, identity *schema.IdentityData, idFormat string, d TerraformResourceData) error {
106+
if identity == nil {
107+
return nil
108+
}
92109
log.Print("[DEBUG] Using IdentitySchema to import resource")
93110
namedGroups := re.SubexpNames()
94-
95111
for _, group := range namedGroups {
96-
if identityValue, identityExists := identity.GetOk(group); identityExists {
97-
log.Printf("[DEBUG] Importing %s = %s", group, identityValue)
112+
if val, ok := d.GetOk(group); ok && group != "" {
113+
log.Printf("[DEBUG] Group %s = %s Identity Group", group, val)
114+
identity.Set(group, val)
115+
}
116+
if identityValue, identityExists := identity.GetOk(group); identityExists && group != "" {
117+
log.Printf("[DEBUG] identity Importing %s = %s", group, identityValue)
98118
d.Set(group, identityValue)
99119
} else {
100-
return fmt.Errorf("No value was found for %s during import", group)
120+
return fmt.Errorf("[DEBUG] No value was found for %s during import", group)
101121
}
102122
}
103123

0 commit comments

Comments
 (0)