@@ -12,12 +12,15 @@ type locationStructFieldCtx struct {
1212 Name * properties.NameVariant
1313 TerraformType string
1414 Type string
15+ AsIdentity bool
1516 Tags []string
1617}
1718
1819// locationStructCtx describes a location struct.
1920type locationStructCtx struct {
2021 StructName string
22+ XpathName * properties.NameVariant
23+ TopLevel bool
2124 Fields []locationStructFieldCtx
2225}
2326
@@ -32,6 +35,7 @@ func getLocationStructsContext(names *NameProvider, spec *properties.Normalizati
3235 // Create the top location structure that references other locations
3336 topLocation := locationStructCtx {
3437 StructName : fmt .Sprintf ("%sLocation" , names .StructName ),
38+ TopLevel : true ,
3539 }
3640
3741 for _ , data := range spec .OrderedLocations () {
@@ -43,6 +47,7 @@ func getLocationStructsContext(names *NameProvider, spec *properties.Normalizati
4347 Name : data .Name ,
4448 TerraformType : structName ,
4549 Type : structType ,
50+ AsIdentity : true ,
4651 Tags : []string {tfTag },
4752 })
4853
@@ -72,13 +77,15 @@ func getLocationStructsContext(names *NameProvider, spec *properties.Normalizati
7277 paramTag = "`tfsdk:\" name\" `"
7378 }
7479 fields = append (fields , locationStructFieldCtx {
75- Name : name ,
76- Type : "types.String" ,
77- Tags : []string {paramTag },
80+ Name : name ,
81+ Type : "types.String" ,
82+ AsIdentity : true ,
83+ Tags : []string {paramTag },
7884 })
7985 }
8086
8187 location := locationStructCtx {
88+ XpathName : data .Name ,
8289 StructName : structName ,
8390 Fields : fields ,
8491 }
@@ -416,3 +423,138 @@ func RenderLocationAttributeTypes(names *NameProvider, spec *properties.Normaliz
416423 }
417424 return processTemplate ("location/attribute_types.tmpl" , "render-location-structs" , data , commonFuncMap )
418425}
426+
427+ // RenderIdentityModel generates identity model struct.
428+ func RenderIdentityModel (resourceTyp properties.ResourceType , names * NameProvider , spec * properties.Normalization ) (string , error ) {
429+ switch resourceTyp {
430+ case properties .ResourceUuid , properties .ResourceUuidPlural , properties .ResourceEntryPlural :
431+ return "" , nil
432+ case properties .ResourceConfig , properties .ResourceCustom :
433+ return "" , nil
434+ case properties .ResourceEntry :
435+ }
436+
437+ if spec .TerraformProviderConfig .Ephemeral {
438+ return "" , nil
439+ }
440+
441+ if spec .TerraformProviderConfig .SkipResource {
442+ return "" , nil
443+ }
444+
445+ type fieldCtx struct {
446+ Name string
447+ Type string
448+ Tag string
449+ }
450+
451+ type context struct {
452+ StructName string
453+ Fields []fieldCtx
454+ }
455+
456+ var fields []fieldCtx
457+ if spec .HasEntryName () {
458+ fields = []fieldCtx {
459+ {Name : "Name" , Type : "types.String" , Tag : "name" },
460+ {Name : "Location" , Type : "types.String" , Tag : "location" },
461+ }
462+ } else {
463+ fields = []fieldCtx {
464+ {Name : "Config" , Type : "types.String" , Tag : "config" },
465+ {Name : "Location" , Type : "types.String" , Tag : "location" },
466+ }
467+ }
468+
469+ data := context {
470+ StructName : names .IdentityModelStructName (),
471+ Fields : fields ,
472+ }
473+
474+ return processTemplate ("common/identity_model.tmpl" , "render-identity-model" , data , commonFuncMap )
475+ }
476+
477+ // RenderIdentitySchema generates identity schema method.
478+ func RenderIdentitySchema (resourceTyp properties.ResourceType , names * NameProvider , spec * properties.Normalization ) (string , error ) {
479+ switch resourceTyp {
480+ case properties .ResourceUuid , properties .ResourceUuidPlural , properties .ResourceEntryPlural :
481+ return "" , nil
482+ case properties .ResourceConfig , properties .ResourceCustom :
483+ return "" , nil
484+ case properties .ResourceEntry :
485+ }
486+
487+ if spec .TerraformProviderConfig .Ephemeral {
488+ return "" , nil
489+ }
490+
491+ if spec .TerraformProviderConfig .SkipResource {
492+ return "" , nil
493+ }
494+
495+ type fieldCtx struct {
496+ Name string
497+ Type string
498+ RequiredForImport bool
499+ OptionalForImport bool
500+ }
501+
502+ type context struct {
503+ StructName string
504+ Fields []fieldCtx
505+ }
506+
507+ var fields []fieldCtx
508+ if spec .HasEntryName () {
509+ fields = []fieldCtx {
510+ {Name : "name" , Type : "identityschema.StringAttribute" , RequiredForImport : true },
511+ {Name : "location" , Type : "identityschema.StringAttribute" , RequiredForImport : true },
512+ }
513+ } else {
514+ fields = []fieldCtx {
515+ {Name : "config" , Type : "identityschema.StringAttribute" , RequiredForImport : true },
516+ {Name : "location" , Type : "identityschema.StringAttribute" , RequiredForImport : true },
517+ }
518+ }
519+
520+ data := context {
521+ StructName : fmt .Sprintf ("%sResource" , names .StructName ),
522+ Fields : fields ,
523+ }
524+
525+ return processTemplate ("common/identity_schema.tmpl" , "render-identity-schema" , data , commonFuncMap )
526+
527+ }
528+
529+ // RenderLocationAsIdentityGetter generates location-to-identity conversion code.
530+ func RenderLocationAsIdentityGetter (resourceTyp properties.ResourceType , names * NameProvider , spec * properties.Normalization ) (string , error ) {
531+ switch resourceTyp {
532+ case properties .ResourceUuid , properties .ResourceUuidPlural , properties .ResourceEntryPlural :
533+ return "" , nil
534+ case properties .ResourceConfig , properties .ResourceCustom :
535+ return "" , nil
536+ case properties .ResourceEntry :
537+ }
538+
539+ if spec .TerraformProviderConfig .Ephemeral {
540+ return "" , nil
541+ }
542+
543+ if spec .TerraformProviderConfig .SkipResource {
544+ return "" , nil
545+ }
546+
547+ type context struct {
548+ StructName string
549+ Specs []locationStructCtx
550+ }
551+
552+ locations := getLocationStructsContext (names , spec )
553+
554+ data := context {
555+ StructName : names .StructName ,
556+ Specs : locations ,
557+ }
558+
559+ return processTemplate ("location/identity.tmpl" , "render-location-as-identity" , data , commonFuncMap )
560+ }
0 commit comments