@@ -252,6 +252,11 @@ type Resource struct {
252252 // used for maintaining state stability with resources first provisioned on older api versions.
253253 SchemaVersion int `yaml:"schema_version,omitempty"`
254254
255+ // The version of the identity schema for the resource.
256+ IdentitySchemaVersion int `yaml:"identity_schema_version,omitempty"`
257+
258+ IdentityUpgraders bool `yaml:"identity_upgraders,omitempty"`
259+
255260 // From this schema version on, state_upgrader code is generated for the resource.
256261 // When unset, state_upgrade_base_schema_version defauts to 0.
257262 // Normally, it is not needed to be set.
@@ -634,6 +639,26 @@ func (r Resource) AllNestedProperties(props []*Type) []*Type {
634639 return nested
635640}
636641
642+ func (r Resource ) IdentityProperties () []* Type {
643+ props := make ([]* Type , 0 )
644+ importFormat := r .ExtractIdentifiers (ImportIdFormats (r .ImportFormat , r .Identity , r .BaseUrl )[0 ])
645+ optionalValues := map [string ]bool {"project" : false , "zone" : false , "region" : false }
646+ for _ , p := range r .AllProperties () {
647+ if slices .Contains (importFormat , google .Underscore (p .Name )) {
648+ props = append (props , p )
649+ optionalValues [p .Name ] = true
650+ }
651+ }
652+
653+ for _ , field := range []string {"project" , "zone" , "region" } { // prevents duplicates
654+ if slices .Contains (importFormat , field ) && ! optionalValues [field ] {
655+ props = append (props , & Type {Name : field , Type : "string" })
656+ }
657+ }
658+
659+ return props
660+ }
661+
637662func (r Resource ) SensitiveProps () []* Type {
638663 props := r .AllNestedProperties (r .RootProperties ())
639664 return google .Select (props , func (p * Type ) bool {
@@ -1009,6 +1034,10 @@ func (r Resource) StateMigrationFile() string {
10091034 return fmt .Sprintf ("templates/terraform/state_migrations/%s_%s.go.tmpl" , google .Underscore (r .ProductMetadata .Name ), google .Underscore (r .Name ))
10101035}
10111036
1037+ func (r Resource ) IdentityUpgraderFile () string {
1038+ return fmt .Sprintf ("templates/terraform/identity_upgraders/%s_%s.go.tmpl" , google .Underscore (r .ProductMetadata .Name ), google .Underscore (r .Name ))
1039+ }
1040+
10121041// ====================
10131042// Version-related methods
10141043// ====================
@@ -1268,7 +1297,6 @@ func ImportIdFormats(importFormat, identity []string, baseUrl string) []string {
12681297 // short id: {{project}}/{{zone}}/{{name}}
12691298 fieldMarkers := regexp .MustCompile (`{{[[:word:]]+}}` ).FindAllString (idFormats [0 ], - 1 )
12701299 shortIdFormat := strings .Join (fieldMarkers , "/" )
1271-
12721300 // short ids without fields with provider-level defaults:
12731301
12741302 // without project
@@ -1314,6 +1342,7 @@ func ImportIdFormats(importFormat, identity []string, baseUrl string) []string {
13141342 uniq = google .Reject (slices .Compact (uniq ), func (i string ) bool {
13151343 return i == ""
13161344 })
1345+
13171346 return uniq
13181347}
13191348
@@ -2050,6 +2079,21 @@ func (r Resource) StateUpgradersCount() []int {
20502079 return nums
20512080}
20522081
2082+ func (r Resource ) IdentityUpgradersCount () []int {
2083+ var nums []int
2084+ for i := 1 ; i < r .IdentitySchemaVersion ; i ++ {
2085+ nums = append (nums , i )
2086+ }
2087+ return nums
2088+ }
2089+
2090+ func (r Resource ) GetIdentitySchemaVersion () int {
2091+ if r .IdentitySchemaVersion == 0 { // default to 1 if not set; a resource with no identity support has a version of 0
2092+ return 1
2093+ }
2094+ return r .IdentitySchemaVersion
2095+ }
2096+
20532097func (r Resource ) CaiProductBaseUrl () string {
20542098 return r .ProductMetadata .ServiceBaseUrl ()
20552099}
0 commit comments