@@ -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
@@ -64,13 +69,15 @@ func getLocationStructsContext(names *NameProvider, spec *properties.Normalizati
6469 paramTag = "`tfsdk:\" name\" `"
6570 }
6671 fields = append (fields , locationStructFieldCtx {
67- Name : name ,
68- Type : "types.String" ,
69- Tags : []string {paramTag },
72+ Name : name ,
73+ Type : "types.String" ,
74+ AsIdentity : true ,
75+ Tags : []string {paramTag },
7076 })
7177 }
7278
7379 location := locationStructCtx {
80+ XpathName : data .Name ,
7481 StructName : structName ,
7582 Fields : fields ,
7683 }
@@ -390,3 +397,138 @@ func RenderLocationAttributeTypes(names *NameProvider, spec *properties.Normaliz
390397 }
391398 return processTemplate ("location/attribute_types.tmpl" , "render-location-structs" , data , commonFuncMap )
392399}
400+
401+ // RenderIdentityModel generates identity model struct.
402+ func RenderIdentityModel (resourceTyp properties.ResourceType , names * NameProvider , spec * properties.Normalization ) (string , error ) {
403+ switch resourceTyp {
404+ case properties .ResourceUuid , properties .ResourceUuidPlural , properties .ResourceEntryPlural :
405+ return "" , nil
406+ case properties .ResourceConfig , properties .ResourceCustom :
407+ return "" , nil
408+ case properties .ResourceEntry :
409+ }
410+
411+ if spec .TerraformProviderConfig .Ephemeral {
412+ return "" , nil
413+ }
414+
415+ if spec .TerraformProviderConfig .SkipResource {
416+ return "" , nil
417+ }
418+
419+ type fieldCtx struct {
420+ Name string
421+ Type string
422+ Tag string
423+ }
424+
425+ type context struct {
426+ StructName string
427+ Fields []fieldCtx
428+ }
429+
430+ var fields []fieldCtx
431+ if spec .HasEntryName () {
432+ fields = []fieldCtx {
433+ {Name : "Name" , Type : "types.String" , Tag : "name" },
434+ {Name : "Location" , Type : "types.String" , Tag : "location" },
435+ }
436+ } else {
437+ fields = []fieldCtx {
438+ {Name : "Config" , Type : "types.String" , Tag : "config" },
439+ {Name : "Location" , Type : "types.String" , Tag : "location" },
440+ }
441+ }
442+
443+ data := context {
444+ StructName : names .IdentityModelStructName (),
445+ Fields : fields ,
446+ }
447+
448+ return processTemplate ("common/identity_model.tmpl" , "render-identity-model" , data , commonFuncMap )
449+ }
450+
451+ // RenderIdentitySchema generates identity schema method.
452+ func RenderIdentitySchema (resourceTyp properties.ResourceType , names * NameProvider , spec * properties.Normalization ) (string , error ) {
453+ switch resourceTyp {
454+ case properties .ResourceUuid , properties .ResourceUuidPlural , properties .ResourceEntryPlural :
455+ return "" , nil
456+ case properties .ResourceConfig , properties .ResourceCustom :
457+ return "" , nil
458+ case properties .ResourceEntry :
459+ }
460+
461+ if spec .TerraformProviderConfig .Ephemeral {
462+ return "" , nil
463+ }
464+
465+ if spec .TerraformProviderConfig .SkipResource {
466+ return "" , nil
467+ }
468+
469+ type fieldCtx struct {
470+ Name string
471+ Type string
472+ RequiredForImport bool
473+ OptionalForImport bool
474+ }
475+
476+ type context struct {
477+ StructName string
478+ Fields []fieldCtx
479+ }
480+
481+ var fields []fieldCtx
482+ if spec .HasEntryName () {
483+ fields = []fieldCtx {
484+ {Name : "name" , Type : "identityschema.StringAttribute" , RequiredForImport : true },
485+ {Name : "location" , Type : "identityschema.StringAttribute" , RequiredForImport : true },
486+ }
487+ } else {
488+ fields = []fieldCtx {
489+ {Name : "config" , Type : "identityschema.StringAttribute" , RequiredForImport : true },
490+ {Name : "location" , Type : "identityschema.StringAttribute" , RequiredForImport : true },
491+ }
492+ }
493+
494+ data := context {
495+ StructName : fmt .Sprintf ("%sResource" , names .StructName ),
496+ Fields : fields ,
497+ }
498+
499+ return processTemplate ("common/identity_schema.tmpl" , "render-identity-schema" , data , commonFuncMap )
500+
501+ }
502+
503+ // RenderLocationAsIdentityGetter generates location-to-identity conversion code.
504+ func RenderLocationAsIdentityGetter (resourceTyp properties.ResourceType , names * NameProvider , spec * properties.Normalization ) (string , error ) {
505+ switch resourceTyp {
506+ case properties .ResourceUuid , properties .ResourceUuidPlural , properties .ResourceEntryPlural :
507+ return "" , nil
508+ case properties .ResourceConfig , properties .ResourceCustom :
509+ return "" , nil
510+ case properties .ResourceEntry :
511+ }
512+
513+ if spec .TerraformProviderConfig .Ephemeral {
514+ return "" , nil
515+ }
516+
517+ if spec .TerraformProviderConfig .SkipResource {
518+ return "" , nil
519+ }
520+
521+ type context struct {
522+ StructName string
523+ Specs []locationStructCtx
524+ }
525+
526+ locations := getLocationStructsContext (names , spec )
527+
528+ data := context {
529+ StructName : names .StructName ,
530+ Specs : locations ,
531+ }
532+
533+ return processTemplate ("location/identity.tmpl" , "render-location-as-identity" , data , commonFuncMap )
534+ }
0 commit comments