-
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 reflect lib for auto generation #1205
Conversation
d372589
to
2fa3325
Compare
/test-all |
1 similar comment
/test-all |
@@ -98,28 +163,20 @@ func resourceNsxtPolicyMacDiscoveryProfileCreate(d *schema.ResourceData, m inter | |||
return err | |||
} | |||
|
|||
// TODO - consider including standard object attributes in the schema | |||
tags := getPolicyTagsFromSchema(d) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to include tags
, display_name
and description
in the extended schema?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not hard to improve GetExtendedSchema
and make it return common extended schema for some well-known policy attrs, but it'll need some refactor on the test side of stuff to make everything e2e working. Can we tackle this after the polymorphic struct is addressed?
Lets make sure we cover the following use cases, either in unit tests or with a resource:
|
0dabad9
to
fe820f1
Compare
/test-all |
nsxt/metadata/metadata.go
Outdated
} else { | ||
sliceElem.Index(i).Set(reflect.ValueOf(v)) | ||
} | ||
log.Printf("[INFO] appending %v to %s", v, key) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think those printouts should be TRACE
level, and we might want to preface them with some keyword so that the context of conversion is clear
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved most to TRACE, except for the ones logged before returning an error.
nsxt/metadata/metadata.go
Outdated
} | ||
|
||
if len(itemList) == 0 { | ||
continue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if here we might run into a problem of assigning nil
value to array instead of assigning empty array?
We need to be able to remove a list of values from a struct, and in some cases nil
value would be ignored by the Patch call.
We can leave this as is for now, and consider adding another metadata value that would control this behavior when the need arises.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch. I actually think there is no harm in passing an empty slice if tf schema is empty. For create this is a no op, and for update this is an explict erasure. We can revisit this part if other handling for erasing is needed for some NSX resources.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test module looks good enough. Only a few questions inline.
if len(parent) > 0 { | ||
log.Printf("[INFO] parent %s key %s", parent, key) | ||
} | ||
if elem.FieldByName(item.Metadata.SdkFieldName).IsNil() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to golang doc, FieldByName will return 0 if the field is not found.
If the method is invoked with the right parameters this should never happen, but in theory it should be possible to have a condition where item.Metadata.SdkFieldName
is not defined in elem
. If that's correct, adding an additional check would not hurt.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree with Salvatore, we should ease troubleshooting for bad metadata arguments that will inevitably occur
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added check for elem.FieldByName(item.Metadata.SdkFieldName).IsValid()
and returns error if the field is not found
nsxt/metadata/metadata.go
Outdated
|
||
log.Printf("[INFO] inspecting key %s with type %s", key, item.Metadata.SchemaType) | ||
if len(parent) > 0 { | ||
log.Printf("[INFO] parent %s key %s", parent, key) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there is value in this log line, we should consider printing something also if len(parent)<=0
since we are also emitting key
in the log statement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added the info of parent as part of context prefix as Anna suggested.
Signed-off-by: Anna Khmelnitsky <[email protected]>
Signed-off-by: Anna Khmelnitsky <[email protected]>
Signed-off-by: Anna Khmelnitsky <[email protected]>
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]>
Signed-off-by: Shawn Wang <[email protected]>
In terraform, optional fields all comes with default value. No need to leave them as empty pointer in the reflected struct. Signed-off-by: Shawn Wang <[email protected]>
Signed-off-by: Shawn Wang <[email protected]>
Signed-off-by: Shawn Wang <[email protected]>
This change improves logging with: - include filename and line for log print in metadata pkg - switch to TRACE - add context of the struct being handled Signed-off-by: Shawn Wang <[email protected]>
If tf schema is updated to empty, the sdk obj should be updated to empty slice instead of nil slice, so that it will be in the PATCH call to remove and clear the list on NSX Signed-off-by: Shawn Wang <[email protected]>
This change improves error checking and reporting in the reflect lib. Now an error will be returned on both convert directions if either: - The name of a field is not found in the passed in struct - reflect called panic for whatever operation failed Signed-off-by: Shawn Wang <[email protected]>
/test-all |
This PR is a continuation of previous work by @annakhm on test-reflect branch on the following aspects.
Functional
SchemaType
set tostruct
*ExtendedSchema
asSchema.Elem
*ExtendedResource
asSchema.Elem
, and setMetadata.ReflectType
to the type of element type in the slice, rather than a slice type.metadata/metaddata_test.go
can be referenced as example use cases.Non-Functional
utils
package, so bothnsxt
andmetadata
package can share the codeTBD in a future PR