@@ -249,6 +249,11 @@ type Resource struct {
249249 // used for maintaining state stability with resources first provisioned on older api versions.
250250 SchemaVersion int `yaml:"schema_version,omitempty"`
251251
252+ // The version of the identity schema for the resource.
253+ IdentitySchemaVersion int `yaml:"identity_schema_version,omitempty"`
254+
255+ IdentityUpgraders bool `yaml:"identity_upgraders,omitempty"`
256+
252257 // From this schema version on, state_upgrader code is generated for the resource.
253258 // When unset, state_upgrade_base_schema_version defauts to 0.
254259 // Normally, it is not needed to be set.
@@ -598,6 +603,26 @@ func (r Resource) AllNestedProperties(props []*Type) []*Type {
598603 return nested
599604}
600605
606+ func (r Resource ) IdentityProperties () []* Type {
607+ props := make ([]* Type , 0 )
608+ importFormat := r .ExtractIdentifiers (ImportIdFormats (r .ImportFormat , r .Identity , r .BaseUrl )[0 ])
609+ optionalValues := map [string ]bool {"project" : false , "zone" : false , "region" : false }
610+ for _ , p := range r .AllProperties () {
611+ if slices .Contains (importFormat , google .Underscore (p .Name )) {
612+ props = append (props , p )
613+ optionalValues [p .Name ] = true
614+ }
615+ }
616+
617+ for _ , field := range []string {"project" , "zone" , "region" } { // prevents duplicates
618+ if slices .Contains (importFormat , field ) && ! optionalValues [field ] {
619+ props = append (props , & Type {Name : field , Type : "string" })
620+ }
621+ }
622+
623+ return props
624+ }
625+
601626func (r Resource ) SensitiveProps () []* Type {
602627 props := r .AllNestedProperties (r .RootProperties ())
603628 return google .Select (props , func (p * Type ) bool {
@@ -886,6 +911,10 @@ func (r Resource) StateMigrationFile() string {
886911 return fmt .Sprintf ("templates/terraform/state_migrations/%s_%s.go.tmpl" , google .Underscore (r .ProductMetadata .Name ), google .Underscore (r .Name ))
887912}
888913
914+ func (r Resource ) IdentityUpgraderFile () string {
915+ return fmt .Sprintf ("templates/terraform/identity_upgraders/%s_%s.go.tmpl" , google .Underscore (r .ProductMetadata .Name ), google .Underscore (r .Name ))
916+ }
917+
889918// ====================
890919// Version-related methods
891920// ====================
@@ -1145,7 +1174,6 @@ func ImportIdFormats(importFormat, identity []string, baseUrl string) []string {
11451174 // short id: {{project}}/{{zone}}/{{name}}
11461175 fieldMarkers := regexp .MustCompile (`{{[[:word:]]+}}` ).FindAllString (idFormats [0 ], - 1 )
11471176 shortIdFormat := strings .Join (fieldMarkers , "/" )
1148-
11491177 // short ids without fields with provider-level defaults:
11501178
11511179 // without project
@@ -1191,6 +1219,7 @@ func ImportIdFormats(importFormat, identity []string, baseUrl string) []string {
11911219 uniq = google .Reject (slices .Compact (uniq ), func (i string ) bool {
11921220 return i == ""
11931221 })
1222+
11941223 return uniq
11951224}
11961225
@@ -1835,6 +1864,21 @@ func (r Resource) StateUpgradersCount() []int {
18351864 return nums
18361865}
18371866
1867+ func (r Resource ) IdentityUpgradersCount () []int {
1868+ var nums []int
1869+ for i := 1 ; i < r .IdentitySchemaVersion ; i ++ {
1870+ nums = append (nums , i )
1871+ }
1872+ return nums
1873+ }
1874+
1875+ func (r Resource ) GetIdentitySchemaVersion () int {
1876+ if r .IdentitySchemaVersion == 0 { // default to 1 if not set; a resource with no identity support has a version of 0
1877+ return 1
1878+ }
1879+ return r .IdentitySchemaVersion
1880+ }
1881+
18381882func (r Resource ) CaiProductBaseUrl () string {
18391883 version := r .ProductMetadata .VersionObjOrClosest (r .TargetVersionName )
18401884 baseUrl := version .CaiBaseUrl
0 commit comments