-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support of polymorphic struct in reflect lib #1222
Conversation
Is
I understand this would mean that we will not be able to refactor most existing resources with the new library. |
For the current approach this is true. For some resources like you mentioned, The problem here to address is that we want an explicit (and standard) way to express the two following items:
If we remove the |
@annakhm I've added support for both formats: with or without an wrapping layer. Also cleaned up some exisiting code. As summerized in the internal doc, this should be able to cover >50% of exisiting resource that involves polymorphism. The ones needs further work (probably a future PR when needed) are:
|
Looks great and also thanks for the thorough test coverage! |
Signed-off-by: Shawn Wang <[email protected]>
For certain polymorphic resources, typically minor child classes that are not a standalone resource in NSX, the type identifier could be value other than "ResourceType" / "resource_type". This change adds support of declaring the type identifier in pair. Signed-off-by: Shawn Wang <[email protected]>
Signed-off-by: Shawn Wang <[email protected]>
This change adds support of the "flat" polymorphic list conversion, where different type in a polymorphic SDK list is combined and mapped to a separate tf schema attr. Signed-off-by: Shawn Wang <[email protected]>
Signed-off-by: Shawn Wang <[email protected]>
Signed-off-by: Shawn Wang <[email protected]>
Signed-off-by: Shawn Wang <[email protected]>
@annakhm Thanks. Let's track the test imporvements in a follow-up PR |
This PR adds support of polymorphic struct for the reflect lib. Two approaches used in existing TF resource schema are supported in this PR.
Dedicated attribute wrapping polymorphic classes
Suppose an attr
poly_struct
can accept two types:cat
andcoffee
, the "wrapped" format expects the followings:Suppose
poly_struct
is a list, and resource is defined as below:metadata
definition expects the followings:At
poly_struct
levelPolymorphicType
aswrapped
ResourceTypeMap
, where the key is all potential tf sub-object (in this case,cat
andcoffee
), and value is theResourceType
value reported from NSXTypeIdentifier
providing the SDK field name and API JSON name for the attr to identify the resource type. Defaults to useResourceType
andresource_type
respectivelypoly_struct
should include all sub-objects, while indicating them as mutal exclusive usingConflictsWith
At sub-object level
ReflectType
should be the golang type directly matching with this sub classBindingType
is required from SDK so it can be statically converted as*data.StructValue
(or slice equivalant)TypeIdentifier
to match with the wrapping levelSdkFieldName
will be ignoredNo wrapping level ("flat")
Similiarly, if the SDK field is a polymorphic list, it's also possible to express elements of the same child class as a separate terraform schema attr. In this case, the resource is defined as below:
In this case, multiple attrs will be combined into one SDK list. For each object, the followings are expected:
PolymorphicType
asflat
ReflectType
matching with the corresponding sub classBindingType
matching withReflectType
TypeIdentifier
ResourceType
Refer to
nsxt/metadata/metadata_poly_test.go
for the full example for both use cases.