|
| 1 | +package repositories |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/hashicorp/go-azure-helpers/lang/pointer" |
| 7 | + "github.com/hashicorp/pandora/tools/sdk/dataapimodels" |
| 8 | +) |
| 9 | + |
| 10 | +func mapObjectDefinition(input *dataapimodels.ObjectDefinition) (*ObjectDefinition, error) { |
| 11 | + if input == nil { |
| 12 | + return nil, nil |
| 13 | + } |
| 14 | + |
| 15 | + objectDefinitionType, err := mapObjectDefinitionType(input.Type) |
| 16 | + if err != nil { |
| 17 | + return nil, err |
| 18 | + } |
| 19 | + |
| 20 | + output := ObjectDefinition{ |
| 21 | + ReferenceName: input.ReferenceName, |
| 22 | + Type: pointer.From(objectDefinitionType), |
| 23 | + } |
| 24 | + |
| 25 | + if input.NestedItem != nil { |
| 26 | + nestedItem, err := mapObjectDefinition(input.NestedItem) |
| 27 | + if err != nil { |
| 28 | + return nil, fmt.Errorf("mapping Nested Item for Object Definition: %+v", err) |
| 29 | + } |
| 30 | + output.NestedItem = nestedItem |
| 31 | + } |
| 32 | + |
| 33 | + return &output, nil |
| 34 | +} |
| 35 | + |
| 36 | +func mapOptionObjectDefinition(input *dataapimodels.OptionObjectDefinition, constants map[string]ConstantDetails, apiModels map[string]ModelDetails) (*OptionObjectDefinition, error) { |
| 37 | + optionObjectType, err := mapOptionObjectDefinitionType(input.Type) |
| 38 | + if err != nil { |
| 39 | + return nil, err |
| 40 | + } |
| 41 | + |
| 42 | + output := OptionObjectDefinition{ |
| 43 | + ReferenceName: input.ReferenceName, |
| 44 | + Type: pointer.From(optionObjectType), |
| 45 | + } |
| 46 | + |
| 47 | + if input.NestedItem != nil { |
| 48 | + nestedItem, err := mapOptionObjectDefinition(input.NestedItem, constants, apiModels) |
| 49 | + if err != nil { |
| 50 | + return nil, fmt.Errorf("mapping Nested Item for Option Object Definition: %+v", err) |
| 51 | + } |
| 52 | + output.NestedItem = nestedItem |
| 53 | + } |
| 54 | + |
| 55 | + if err := validateOptionObjectDefinition(output, constants, apiModels); err != nil { |
| 56 | + return nil, fmt.Errorf("validating mapped Option Object Definition: %+v", err) |
| 57 | + } |
| 58 | + |
| 59 | + return &output, nil |
| 60 | +} |
| 61 | + |
| 62 | +func mapDateFormatType(input dataapimodels.DateFormat) (*DateFormat, error) { |
| 63 | + mappings := map[dataapimodels.DateFormat]DateFormat{ |
| 64 | + dataapimodels.RFC3339DateFormat: RFC3339DateFormat, |
| 65 | + } |
| 66 | + if v, ok := mappings[input]; ok { |
| 67 | + return &v, nil |
| 68 | + } |
| 69 | + |
| 70 | + return nil, fmt.Errorf("unmapped Date Format Type %q", string(input)) |
| 71 | +} |
| 72 | + |
| 73 | +func mapObjectDefinitionType(input dataapimodels.ObjectDefinitionType) (*ObjectDefinitionType, error) { |
| 74 | + mappings := map[dataapimodels.ObjectDefinitionType]ObjectDefinitionType{ |
| 75 | + dataapimodels.BooleanObjectDefinitionType: BooleanObjectDefinitionType, |
| 76 | + dataapimodels.DateTimeObjectDefinitionType: DateTimeObjectDefinitionType, |
| 77 | + dataapimodels.IntegerObjectDefinitionType: IntegerObjectDefinitionType, |
| 78 | + dataapimodels.FloatObjectDefinitionType: FloatObjectDefinitionType, |
| 79 | + dataapimodels.RawFileObjectDefinitionType: RawFileObjectDefinitionType, |
| 80 | + dataapimodels.RawObjectObjectDefinitionType: RawObjectObjectDefinitionType, |
| 81 | + dataapimodels.ReferenceObjectDefinitionType: ReferenceObjectDefinitionType, |
| 82 | + dataapimodels.StringObjectDefinitionType: StringObjectDefinitionType, |
| 83 | + dataapimodels.CsvObjectDefinitionType: CsvObjectDefinitionType, |
| 84 | + dataapimodels.DictionaryObjectDefinitionType: DictionaryObjectDefinitionType, |
| 85 | + dataapimodels.ListObjectDefinitionType: ListObjectDefinitionType, |
| 86 | + |
| 87 | + dataapimodels.EdgeZoneObjectDefinitionType: EdgeZoneObjectDefinitionType, |
| 88 | + dataapimodels.LocationObjectDefinitionType: LocationObjectDefinitionType, |
| 89 | + dataapimodels.TagsObjectDefinitionType: TagsObjectDefinitionType, |
| 90 | + dataapimodels.SystemAssignedIdentityObjectDefinitionType: SystemAssignedIdentityObjectDefinitionType, |
| 91 | + dataapimodels.SystemAndUserAssignedIdentityListObjectDefinitionType: SystemAndUserAssignedIdentityListObjectDefinitionType, |
| 92 | + dataapimodels.SystemAndUserAssignedIdentityMapObjectDefinitionType: SystemAndUserAssignedIdentityMapObjectDefinitionType, |
| 93 | + dataapimodels.LegacySystemAndUserAssignedIdentityListObjectDefinitionType: LegacySystemAndUserAssignedIdentityListObjectDefinitionType, |
| 94 | + dataapimodels.LegacySystemAndUserAssignedIdentityMapObjectDefinitionType: LegacySystemAndUserAssignedIdentityMapObjectDefinitionType, |
| 95 | + dataapimodels.SystemOrUserAssignedIdentityListObjectDefinitionType: SystemOrUserAssignedIdentityListObjectDefinitionType, |
| 96 | + dataapimodels.SystemOrUserAssignedIdentityMapObjectDefinitionType: SystemOrUserAssignedIdentityMapObjectDefinitionType, |
| 97 | + dataapimodels.UserAssignedIdentityListObjectDefinitionType: UserAssignedIdentityListObjectDefinitionType, |
| 98 | + dataapimodels.UserAssignedIdentityMapObjectDefinitionType: UserAssignedIdentityMapObjectDefinitionType, |
| 99 | + dataapimodels.SystemDataObjectDefinitionType: SystemDataObjectDefinitionType, |
| 100 | + dataapimodels.ZoneObjectDefinitionType: ZoneObjectDefinitionType, |
| 101 | + dataapimodels.ZonesObjectDefinitionType: ZonesObjectDefinitionType, |
| 102 | + } |
| 103 | + if v, ok := mappings[input]; ok { |
| 104 | + return &v, nil |
| 105 | + } |
| 106 | + |
| 107 | + return nil, fmt.Errorf("unmapped Object Definition Type %q", string(input)) |
| 108 | +} |
| 109 | + |
| 110 | +func mapOptionObjectDefinitionType(input dataapimodels.OptionObjectDefinitionType) (*OptionObjectDefinitionType, error) { |
| 111 | + mappings := map[dataapimodels.OptionObjectDefinitionType]OptionObjectDefinitionType{ |
| 112 | + dataapimodels.BooleanOptionObjectDefinitionType: BooleanOptionObjectDefinition, |
| 113 | + dataapimodels.IntegerOptionObjectDefinitionType: IntegerOptionObjectDefinition, |
| 114 | + dataapimodels.FloatOptionObjectDefinitionType: FloatOptionObjectDefinitionType, |
| 115 | + dataapimodels.StringOptionObjectDefinitionType: StringOptionObjectDefinitionType, |
| 116 | + dataapimodels.CsvOptionObjectDefinitionType: CsvOptionObjectDefinitionType, |
| 117 | + dataapimodels.ListOptionObjectDefinitionType: ListOptionObjectDefinitionType, |
| 118 | + dataapimodels.ReferenceOptionObjectDefinitionType: ReferenceOptionObjectDefinitionType, |
| 119 | + } |
| 120 | + if v, ok := mappings[input]; ok { |
| 121 | + return &v, nil |
| 122 | + } |
| 123 | + |
| 124 | + return nil, fmt.Errorf("unmapped Options Object Definition Type %q", string(input)) |
| 125 | +} |
| 126 | + |
| 127 | +func mapConstantFieldType(input dataapimodels.ConstantType) (*ConstantType, error) { |
| 128 | + mappings := map[dataapimodels.ConstantType]ConstantType{ |
| 129 | + dataapimodels.FloatConstant: FloatConstant, |
| 130 | + dataapimodels.IntegerConstant: IntegerConstant, |
| 131 | + dataapimodels.StringConstant: StringConstant, |
| 132 | + } |
| 133 | + if v, ok := mappings[input]; ok { |
| 134 | + return &v, nil |
| 135 | + } |
| 136 | + |
| 137 | + return nil, fmt.Errorf("unmapped Constant Type %q", string(input)) |
| 138 | +} |
| 139 | + |
| 140 | +func mapResourceIdSegmentType(input dataapimodels.ResourceIdSegmentType) (*ResourceIdSegmentType, error) { |
| 141 | + mappings := map[dataapimodels.ResourceIdSegmentType]ResourceIdSegmentType{ |
| 142 | + dataapimodels.ConstantResourceIdSegmentType: ConstantResourceIdSegmentType, |
| 143 | + dataapimodels.ResourceGroupResourceIdSegmentType: ResourceGroupResourceIdSegmentType, |
| 144 | + dataapimodels.ResourceProviderResourceIdSegmentType: ResourceProviderResourceIdSegmentType, |
| 145 | + dataapimodels.ScopeResourceIdSegmentType: ScopeResourceIdSegmentType, |
| 146 | + dataapimodels.StaticResourceIdSegmentType: StaticResourceIdSegmentType, |
| 147 | + dataapimodels.SubscriptionIdResourceIdSegmentType: SubscriptionIdResourceIdSegmentType, |
| 148 | + dataapimodels.UserSpecifiedResourceIdSegmentType: UserSpecifiedResourceIdSegmentType, |
| 149 | + } |
| 150 | + if v, ok := mappings[input]; ok { |
| 151 | + return &v, nil |
| 152 | + } |
| 153 | + |
| 154 | + return nil, fmt.Errorf("unmapped Resource Id Segment Type %q", string(input)) |
| 155 | +} |
0 commit comments