diff --git a/nsxt/metadata/metadata.go b/nsxt/metadata/metadata.go index e38d3027b..aa393c64b 100644 --- a/nsxt/metadata/metadata.go +++ b/nsxt/metadata/metadata.go @@ -17,8 +17,8 @@ import ( var logger = log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile) const ( - PolymorphicTypeFlat = "flat" - PolymorphicTypeWrapped = "wrapped" + PolymorphicTypeFlatten = "flatten" + PolymorphicTypeNested = "nested" ) type Metadata struct { @@ -35,10 +35,10 @@ type Metadata struct { // SDK vapi binding type for converting polymorphic structs BindingType vapiBindings_.BindingType // SDK resource type to match and filter for a schema key - // Only applicable to PolymorphicTypeFlat schema + // Only applicable to PolymorphicTypeFlatten schema ResourceType string // Map from schema key of polymorphic attr to this SDK resource type - // Only applicable to PolymorphicTypeWrapped schema + // Only applicable to PolymorphicTypeNested schema ResourceTypeMap map[string]string // Type identifier name for both SDK and JSON (StructValue field name) TypeIdentifier TypeIdentifier @@ -168,10 +168,10 @@ func StructToSchema(elem reflect.Value, d *schema.ResourceData, metadata map[str childElem := elem.FieldByName(item.Metadata.SdkFieldName) var nestedVal interface{} switch item.Metadata.PolymorphicType { - case PolymorphicTypeWrapped: - nestedVal, err = polyStructToWrappedSchema(ctx, childElem, item) - case PolymorphicTypeFlat: - nestedVal, err = polyStructToFlatSchema(ctx, childElem, key, item) + case PolymorphicTypeNested: + nestedVal, err = polyStructToNestedSchema(ctx, childElem, item) + case PolymorphicTypeFlatten: + nestedVal, err = polyStructToFlattenSchema(ctx, childElem, key, item) default: err = fmt.Errorf("%s unknown polymorphic type %s", ctx, item.Metadata.PolymorphicType) @@ -287,10 +287,10 @@ func SchemaToStruct(elem reflect.Value, d *schema.ResourceData, metadata map[str if len(item.Metadata.PolymorphicType) > 0 { itemList := getItemListForSchemaToStruct(d, item.Metadata.SchemaType, key, parent, parentMap) switch item.Metadata.PolymorphicType { - case PolymorphicTypeWrapped: - err = polyWrappedSchemaToStruct(ctx, elem, itemList, item) - case PolymorphicTypeFlat: - err = polyFlatSchemaToStruct(ctx, elem, key, itemList, item) + case PolymorphicTypeNested: + err = polyNestedSchemaToStruct(ctx, elem, itemList, item) + case PolymorphicTypeFlatten: + err = polyFlattenSchemaToStruct(ctx, elem, key, itemList, item) default: err = fmt.Errorf("%s unknown polymorphic type %s", ctx, item.Metadata.PolymorphicType) @@ -437,9 +437,9 @@ func getResourceTypeFromStructValue(ctx string, value data.StructValue, identifi return "", err } -func polyStructToWrappedSchema(ctx string, elem reflect.Value, item *ExtendedSchema) (ret []map[string]interface{}, err error) { - if item.Metadata.PolymorphicType != PolymorphicTypeWrapped { - err = fmt.Errorf("%s polyStructToWrappedSchema called on non-wrapped polymorphic attr", ctx) +func polyStructToNestedSchema(ctx string, elem reflect.Value, item *ExtendedSchema) (ret []map[string]interface{}, err error) { + if item.Metadata.PolymorphicType != PolymorphicTypeNested { + err = fmt.Errorf("%s polyStructToNestedSchema called on non-wrapped polymorphic attr", ctx) logger.Printf("[ERROR] %v", err) return } @@ -508,9 +508,9 @@ func polyStructToWrappedSchema(ctx string, elem reflect.Value, item *ExtendedSch return } -func polyWrappedSchemaToStruct(ctx string, elem reflect.Value, dataList []interface{}, item *ExtendedSchema) (err error) { - if item.Metadata.PolymorphicType != PolymorphicTypeWrapped { - err = fmt.Errorf("%s polyWrappedSchemaToStruct called on non-wrapped polymorphic attr", ctx) +func polyNestedSchemaToStruct(ctx string, elem reflect.Value, dataList []interface{}, item *ExtendedSchema) (err error) { + if item.Metadata.PolymorphicType != PolymorphicTypeNested { + err = fmt.Errorf("%s polyNestedSchemaToStruct called on non-wrapped polymorphic attr", ctx) logger.Printf("[ERROR] %v", err) return } @@ -584,9 +584,9 @@ func polyWrappedSchemaToStruct(ctx string, elem reflect.Value, dataList []interf return } -func polyStructToFlatSchema(ctx string, elem reflect.Value, key string, item *ExtendedSchema) (ret []map[string]interface{}, err error) { - if item.Metadata.PolymorphicType != PolymorphicTypeFlat { - err = fmt.Errorf("%s polyStructToFlatSchema called on non-flat polymorphic attr", ctx) +func polyStructToFlattenSchema(ctx string, elem reflect.Value, key string, item *ExtendedSchema) (ret []map[string]interface{}, err error) { + if item.Metadata.PolymorphicType != PolymorphicTypeFlatten { + err = fmt.Errorf("%s polyStructToFlattenSchema called on non-flat polymorphic attr", ctx) logger.Printf("[ERROR] %v", err) return } @@ -629,9 +629,9 @@ func polyStructToFlatSchema(ctx string, elem reflect.Value, key string, item *Ex return } -func polyFlatSchemaToStruct(ctx string, elem reflect.Value, key string, dataList []interface{}, item *ExtendedSchema) (err error) { - if item.Metadata.PolymorphicType != PolymorphicTypeFlat { - err = fmt.Errorf("%s polyFlatSchemaToStruct called on non-flat polymorphic attr", ctx) +func polyFlattenSchemaToStruct(ctx string, elem reflect.Value, key string, dataList []interface{}, item *ExtendedSchema) (err error) { + if item.Metadata.PolymorphicType != PolymorphicTypeFlatten { + err = fmt.Errorf("%s polyFlattenSchemaToStruct called on non-flat polymorphic attr", ctx) logger.Printf("[ERROR] %v", err) return } diff --git a/nsxt/metadata/metadata_poly_test.go b/nsxt/metadata/metadata_poly_test.go index 76205ada4..57989125a 100644 --- a/nsxt/metadata/metadata_poly_test.go +++ b/nsxt/metadata/metadata_poly_test.go @@ -92,7 +92,7 @@ type testPolyListStruct struct { PolyList []*data.StructValue } -func testPolyStructWrappedSchema(t string) map[string]*schema.Schema { +func testPolyStructNestedSchema(t string) map[string]*schema.Schema { schemaType := schema.TypeList maxItems := 0 if t == "set" { @@ -143,7 +143,7 @@ func testPolyStructWrappedSchema(t string) map[string]*schema.Schema { } } -func testPolyStructWrappedExtSchema(t, sdkName string) map[string]*ExtendedSchema { +func testPolyStructNestedExtSchema(t, sdkName string) map[string]*ExtendedSchema { typeIdentier := TypeIdentifier{ SdkName: "Type", APIFieldName: "type", @@ -207,7 +207,7 @@ func testPolyStructWrappedExtSchema(t, sdkName string) map[string]*ExtendedSchem Metadata: Metadata{ SchemaType: t, SdkFieldName: sdkName, - PolymorphicType: PolymorphicTypeWrapped, + PolymorphicType: PolymorphicTypeNested, ResourceTypeMap: map[string]string{ "cat": "FakeCat", "coffee": "FakeCoffee", @@ -218,7 +218,7 @@ func testPolyStructWrappedExtSchema(t, sdkName string) map[string]*ExtendedSchem } } -func TestPolyStructToWrappedSchema(t *testing.T) { +func TestPolyStructToNestedSchema(t *testing.T) { t.Run("cat struct", func(t *testing.T) { name := "matcha" rType := "FakeCat" @@ -234,10 +234,10 @@ func TestPolyStructToWrappedSchema(t *testing.T) { assert.Nil(t, errors, "unexpected error calling ConvertToGolang") obj.PolyStruct = dv.(*data.StructValue) d := schema.TestResourceDataRaw( - t, testPolyStructWrappedSchema("struct"), map[string]interface{}{}) + t, testPolyStructNestedSchema("struct"), map[string]interface{}{}) elem := reflect.ValueOf(&obj).Elem() - err := StructToSchema(elem, d, testPolyStructWrappedExtSchema("struct", "PolyStruct"), "", nil) + err := StructToSchema(elem, d, testPolyStructNestedExtSchema("struct", "PolyStruct"), "", nil) assert.NoError(t, err, "unexpected error calling StructToSchema") assert.Len(t, d.Get("poly_struct"), 1) polyData := d.Get("poly_struct").([]interface{})[0].(map[string]interface{}) @@ -264,10 +264,10 @@ func TestPolyStructToWrappedSchema(t *testing.T) { assert.Nil(t, errors, "unexpected error calling ConvertToGolang") obj.PolyStruct = dv.(*data.StructValue) d := schema.TestResourceDataRaw( - t, testPolyStructWrappedSchema("struct"), map[string]interface{}{}) + t, testPolyStructNestedSchema("struct"), map[string]interface{}{}) elem := reflect.ValueOf(&obj).Elem() - err := StructToSchema(elem, d, testPolyStructWrappedExtSchema("struct", "PolyStruct"), "", nil) + err := StructToSchema(elem, d, testPolyStructNestedExtSchema("struct", "PolyStruct"), "", nil) assert.NoError(t, err, "unexpected error calling StructToSchema") assert.Len(t, d.Get("poly_struct"), 1) polyData := d.Get("poly_struct").([]interface{})[0].(map[string]interface{}) @@ -307,10 +307,10 @@ func TestPolyStructToWrappedSchema(t *testing.T) { assert.Nil(t, errors, "unexpected error calling ConvertToGolang") obj.PolyList[1] = dv.(*data.StructValue) d := schema.TestResourceDataRaw( - t, testPolyStructWrappedSchema("struct"), map[string]interface{}{}) + t, testPolyStructNestedSchema("struct"), map[string]interface{}{}) elem := reflect.ValueOf(&obj).Elem() - err := StructToSchema(elem, d, testPolyStructWrappedExtSchema("list", "PolyList"), "", nil) + err := StructToSchema(elem, d, testPolyStructNestedExtSchema("list", "PolyList"), "", nil) assert.NoError(t, err, "unexpected error calling StructToSchema") assert.Len(t, d.Get("poly_struct"), 2) // idx 0: coffee @@ -359,10 +359,10 @@ func TestPolyStructToWrappedSchema(t *testing.T) { assert.Nil(t, errors, "unexpected error calling ConvertToGolang") obj.PolyList[0] = dv.(*data.StructValue) d := schema.TestResourceDataRaw( - t, testPolyStructWrappedSchema("struct"), map[string]interface{}{}) + t, testPolyStructNestedSchema("struct"), map[string]interface{}{}) elem := reflect.ValueOf(&obj).Elem() - err := StructToSchema(elem, d, testPolyStructWrappedExtSchema("set", "PolyList"), "", nil) + err := StructToSchema(elem, d, testPolyStructNestedExtSchema("set", "PolyList"), "", nil) assert.NoError(t, err, "unexpected error calling StructToSchema") assert.Len(t, d.Get("poly_struct"), 2) for _, v := range d.Get("poly_struct").([]interface{}) { @@ -386,10 +386,10 @@ func TestPolyStructToWrappedSchema(t *testing.T) { }) } -func TestWrappedSchemaToPolyStruct(t *testing.T) { +func TestNestedSchemaToPolyStruct(t *testing.T) { t.Run("cat struct", func(t *testing.T) { d := schema.TestResourceDataRaw( - t, testPolyStructWrappedSchema("struct"), map[string]interface{}{ + t, testPolyStructNestedSchema("struct"), map[string]interface{}{ "poly_struct": []interface{}{ map[string]interface{}{ "cat": []interface{}{ @@ -404,7 +404,7 @@ func TestWrappedSchemaToPolyStruct(t *testing.T) { obj := testPolyStruct{} elem := reflect.ValueOf(&obj).Elem() - err := SchemaToStruct(elem, d, testPolyStructWrappedExtSchema("struct", "PolyStruct"), "", nil) + err := SchemaToStruct(elem, d, testPolyStructNestedExtSchema("struct", "PolyStruct"), "", nil) assert.NoError(t, err, "unexpected error calling SchemaToStruct") converter := vapiBindings_.NewTypeConverter() @@ -417,7 +417,7 @@ func TestWrappedSchemaToPolyStruct(t *testing.T) { t.Run("coffee struct", func(t *testing.T) { d := schema.TestResourceDataRaw( - t, testPolyStructWrappedSchema("struct"), map[string]interface{}{ + t, testPolyStructNestedSchema("struct"), map[string]interface{}{ "poly_struct": []interface{}{ map[string]interface{}{ "coffee": []interface{}{ @@ -432,7 +432,7 @@ func TestWrappedSchemaToPolyStruct(t *testing.T) { obj := testPolyStruct{} elem := reflect.ValueOf(&obj).Elem() - err := SchemaToStruct(elem, d, testPolyStructWrappedExtSchema("struct", "PolyStruct"), "", nil) + err := SchemaToStruct(elem, d, testPolyStructNestedExtSchema("struct", "PolyStruct"), "", nil) assert.NoError(t, err, "unexpected error calling SchemaToStruct") converter := vapiBindings_.NewTypeConverter() @@ -445,7 +445,7 @@ func TestWrappedSchemaToPolyStruct(t *testing.T) { t.Run("mixed list", func(t *testing.T) { d := schema.TestResourceDataRaw( - t, testPolyStructWrappedSchema("list"), map[string]interface{}{ + t, testPolyStructNestedSchema("list"), map[string]interface{}{ "poly_struct": []interface{}{ map[string]interface{}{ "coffee": []interface{}{ @@ -468,7 +468,7 @@ func TestWrappedSchemaToPolyStruct(t *testing.T) { obj := testPolyListStruct{} elem := reflect.ValueOf(&obj).Elem() - err := SchemaToStruct(elem, d, testPolyStructWrappedExtSchema("list", "PolyList"), "", nil) + err := SchemaToStruct(elem, d, testPolyStructNestedExtSchema("list", "PolyList"), "", nil) assert.NoError(t, err, "unexpected error calling SchemaToStruct") converter := vapiBindings_.NewTypeConverter() @@ -487,7 +487,7 @@ func TestWrappedSchemaToPolyStruct(t *testing.T) { t.Run("mixed set", func(t *testing.T) { d := schema.TestResourceDataRaw( - t, testPolyStructWrappedSchema("set"), map[string]interface{}{ + t, testPolyStructNestedSchema("set"), map[string]interface{}{ "poly_struct": []interface{}{ map[string]interface{}{ "cat": []interface{}{ @@ -510,7 +510,7 @@ func TestWrappedSchemaToPolyStruct(t *testing.T) { obj := testPolyListStruct{} elem := reflect.ValueOf(&obj).Elem() - err := SchemaToStruct(elem, d, testPolyStructWrappedExtSchema("set", "PolyList"), "", nil) + err := SchemaToStruct(elem, d, testPolyStructNestedExtSchema("set", "PolyList"), "", nil) assert.NoError(t, err, "unexpected error calling SchemaToStruct") converter := vapiBindings_.NewTypeConverter() @@ -533,7 +533,7 @@ func TestWrappedSchemaToPolyStruct(t *testing.T) { }) } -func testPolyStructFlatSchema() map[string]*schema.Schema { +func testPolyStructFlattenSchema() map[string]*schema.Schema { return map[string]*schema.Schema{ "cat": { Type: schema.TypeList, @@ -564,7 +564,7 @@ func testPolyStructFlatSchema() map[string]*schema.Schema { } } -func testPolyStructFlatExtSchema(sdkName string) map[string]*ExtendedSchema { +func testPolyStructFlattenExtSchema(sdkName string) map[string]*ExtendedSchema { typeIdentifier := TypeIdentifier{ SdkName: "Type", APIFieldName: "type", @@ -586,7 +586,7 @@ func testPolyStructFlatExtSchema(sdkName string) map[string]*ExtendedSchema { ReflectType: reflect.TypeOf(testCatStruct{}), BindingType: testCatStructBindingType(), TypeIdentifier: typeIdentifier, - PolymorphicType: PolymorphicTypeFlat, + PolymorphicType: PolymorphicTypeFlatten, ResourceType: "FakeCat", SdkFieldName: sdkName, }, @@ -606,7 +606,7 @@ func testPolyStructFlatExtSchema(sdkName string) map[string]*ExtendedSchema { ReflectType: reflect.TypeOf(testCoffeeStruct{}), BindingType: testCoffeeStructBindingType(), TypeIdentifier: typeIdentifier, - PolymorphicType: PolymorphicTypeFlat, + PolymorphicType: PolymorphicTypeFlatten, ResourceType: "FakeCoffee", SdkFieldName: sdkName, }, @@ -614,7 +614,7 @@ func testPolyStructFlatExtSchema(sdkName string) map[string]*ExtendedSchema { } } -func TestPolyStructToFlatSchema(t *testing.T) { +func TestPolyStructToFlattenSchema(t *testing.T) { t.Run("mixed list", func(t *testing.T) { catName := "oolong" coffeeName := "mocha" @@ -643,10 +643,10 @@ func TestPolyStructToFlatSchema(t *testing.T) { assert.Nil(t, errors, "unexpected error calling ConvertToGolang") obj.PolyList[1] = dv.(*data.StructValue) d := schema.TestResourceDataRaw( - t, testPolyStructFlatSchema(), map[string]interface{}{}) + t, testPolyStructFlattenSchema(), map[string]interface{}{}) elem := reflect.ValueOf(&obj).Elem() - err := StructToSchema(elem, d, testPolyStructFlatExtSchema("PolyList"), "", nil) + err := StructToSchema(elem, d, testPolyStructFlattenExtSchema("PolyList"), "", nil) assert.NoError(t, err, "unexpected error calling StructToSchema") assert.Len(t, d.Get("coffee"), 1) @@ -663,10 +663,10 @@ func TestPolyStructToFlatSchema(t *testing.T) { }) } -func TestFlatSchemaToPolyStruct(t *testing.T) { +func TestFlattenSchemaToPolyStruct(t *testing.T) { t.Run("mixed list", func(t *testing.T) { d := schema.TestResourceDataRaw( - t, testPolyStructFlatSchema(), map[string]interface{}{ + t, testPolyStructFlattenSchema(), map[string]interface{}{ "coffee": []interface{}{ map[string]interface{}{ "name": "mocha", @@ -683,7 +683,7 @@ func TestFlatSchemaToPolyStruct(t *testing.T) { obj := testPolyListStruct{} elem := reflect.ValueOf(&obj).Elem() - err := SchemaToStruct(elem, d, testPolyStructFlatExtSchema("PolyList"), "", nil) + err := SchemaToStruct(elem, d, testPolyStructFlattenExtSchema("PolyList"), "", nil) assert.NoError(t, err, "unexpected error calling SchemaToStruct") converter := vapiBindings_.NewTypeConverter()