77 "strconv"
88 "strings"
99
10+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1011 transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
1112)
1213
@@ -20,12 +21,14 @@ import (
2021func ParseImportId (idRegexes []string , d TerraformResourceData , config * transport_tpg.Config ) error {
2122 for _ , idFormat := range idRegexes {
2223 re , err := regexp .Compile (idFormat )
23-
2424 if err != nil {
2525 log .Printf ("[DEBUG] Could not compile %s." , idFormat )
2626 return fmt .Errorf ("Import is not supported. Invalid regex formats." )
2727 }
28-
28+ identity , err := d .Identity ()
29+ if identity == nil {
30+ log .Printf ("[DEBUG] identity not set: %s" , err )
31+ }
2932 if fieldValues := re .FindStringSubmatch (d .Id ()); fieldValues != nil {
3033 log .Printf ("[DEBUG] matching ID %s to regex %s." , d .Id (), idFormat )
3134 // Starting at index 1, the first match is the full string.
@@ -47,13 +50,23 @@ func ParseImportId(idRegexes []string, d TerraformResourceData, config *transpor
4750 if err = d .Set (fieldName , fieldValue ); err != nil {
4851 return err
4952 }
53+ if identity != nil {
54+ if err = identity .Set (fieldName , fieldValue ); err != nil {
55+ return err
56+ }
57+ }
5058 } else if _ , ok := val .(int ); ok {
5159 if intVal , atoiErr := strconv .Atoi (fieldValue ); atoiErr == nil {
5260 // If the value can be parsed as an integer, we try to set the
5361 // value as an integer.
5462 if err = d .Set (fieldName , intVal ); err != nil {
5563 return err
5664 }
65+ if identity != nil {
66+ if err = identity .Set (fieldName , intVal ); err != nil {
67+ return err
68+ }
69+ }
5770 } else {
5871 return fmt .Errorf ("%s appears to be an integer, but %v cannot be parsed as an int" , fieldName , fieldValue )
5972 }
@@ -64,18 +77,56 @@ func ParseImportId(idRegexes []string, d TerraformResourceData, config *transpor
6477 }
6578
6679 // The first id format is applied first and contains all the fields.
67- err := setDefaultValues (idRegexes [0 ], d , config )
80+ err := setDefaultValues (idRegexes [0 ], nil , d , config )
81+ if err != nil {
82+ return err
83+ }
84+
85+ err = setDefaultValues (idRegexes [0 ], identity , d , config )
6886 if err != nil {
6987 return err
7088 }
7189
90+ return nil
91+ } else if d .Id () == "" {
92+ if err := identityImport (re , identity , d ); err != nil {
93+ return err
94+ }
95+ err = setDefaultValues (idRegexes [0 ], identity , d , config )
96+ if err != nil {
97+ return err
98+ }
7299 return nil
73100 }
74101 }
75102 return fmt .Errorf ("Import id %q doesn't match any of the accepted formats: %v" , d .Id (), idRegexes )
76103}
77104
78- func setDefaultValues (idRegex string , d TerraformResourceData , config * transport_tpg.Config ) error {
105+ func identityImport (re * regexp.Regexp , identity * schema.IdentityData , d TerraformResourceData ) error {
106+ if identity == nil {
107+ return nil
108+ }
109+ log .Print ("[DEBUG] Using IdentitySchema to import resource" )
110+ namedGroups := re .SubexpNames ()
111+ for _ , group := range namedGroups {
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 )
118+ d .Set (group , identityValue )
119+ } else if group == "" {
120+ continue
121+ } else {
122+ log .Printf ("[DEBUG] No value was found for %s in identity import block" , group )
123+ }
124+ }
125+
126+ return nil
127+ }
128+
129+ func setDefaultValues (idRegex string , identity * schema.IdentityData , d TerraformResourceData , config * transport_tpg.Config ) error {
79130 if _ , ok := d .GetOk ("project" ); ! ok && strings .Contains (idRegex , "?P<project>" ) {
80131 project , err := GetProject (d , config )
81132 if err != nil {
@@ -84,6 +135,11 @@ func setDefaultValues(idRegex string, d TerraformResourceData, config *transport
84135 if err := d .Set ("project" , project ); err != nil {
85136 return fmt .Errorf ("Error setting project: %s" , err )
86137 }
138+ if identity != nil {
139+ if err := identity .Set ("project" , project ); err != nil {
140+ return fmt .Errorf ("Error setting project: %s" , err )
141+ }
142+ }
87143 }
88144 if _ , ok := d .GetOk ("region" ); ! ok && strings .Contains (idRegex , "?P<region>" ) {
89145 region , err := GetRegion (d , config )
@@ -93,6 +149,11 @@ func setDefaultValues(idRegex string, d TerraformResourceData, config *transport
93149 if err := d .Set ("region" , region ); err != nil {
94150 return fmt .Errorf ("Error setting region: %s" , err )
95151 }
152+ if identity != nil {
153+ if err := identity .Set ("region" , region ); err != nil {
154+ return fmt .Errorf ("Error setting region: %s" , err )
155+ }
156+ }
96157 }
97158 if _ , ok := d .GetOk ("zone" ); ! ok && strings .Contains (idRegex , "?P<zone>" ) {
98159 zone , err := GetZone (d , config )
@@ -102,6 +163,11 @@ func setDefaultValues(idRegex string, d TerraformResourceData, config *transport
102163 if err := d .Set ("zone" , zone ); err != nil {
103164 return fmt .Errorf ("Error setting zone: %s" , err )
104165 }
166+ if identity != nil {
167+ if err := identity .Set ("zone" , zone ); err != nil {
168+ return fmt .Errorf ("Error setting zone: %s" , err )
169+ }
170+ }
105171 }
106172 return nil
107173}
0 commit comments