-
Notifications
You must be signed in to change notification settings - Fork 141
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 for validate_vms action on transformation_mappings #358
Add support for validate_vms action on transformation_mappings #358
Conversation
Marking as WIP as we need ManageIQ/manageiq#17177 merged first resolves issue #352 (comment) so results now would return vms hrefs, so:
|
431ba75
to
b602511
Compare
…s vm references instead of the previously returned transformation_mappings hrefs.
…cluster and a transformation mapping with the proper item identifying the source cluster.
b602511
to
3250fe4
Compare
@gmcculloug had to update the specs to have the proper wiring for the valid vm case. ping @imtayadeway can we borrow your 👀 for review ? Thanks. |
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.
This mostly looks good although I had some concerns about adding complexity for one edge case (that lives in a concrete sub-type 🎉 ), as well as blindly returning bad requests for any standard error. More details below. Thanks @abellotti ! 🎹
@@ -10,6 +10,13 @@ def create_resource(_type, _id, data = {}) | |||
raise BadRequestError, "Could not create Transformation Mapping - #{err}" | |||
end | |||
|
|||
def validate_vms_resource(type, id, data = {}) | |||
transformation_mapping = resource_search(id, type, collection_class(type)) | |||
normalize_hash(:vms, transformation_mapping.validate_vms(data["import"]) || {}) |
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 seems unfortunate that we have to add more params to the already quite-complex normalize_<thing>
methods for this one edge case. For example, the proliferation of items
and types
in https://github.com/ManageIQ/manageiq-api/pull/358/files#diff-d4f69c54bfacef864c9985012cdb05dfR131 requires a lot of context to grok! This also seems to be obscuring intent - we are saying "normalize this thing as a vm" but this resource isn't a vm, it's a bundle of data that contains some vm attributes, and some others.
I'm assuming then that the reason this is done is to append the href to the individual valid/invalid/conflict groups? WDYT about doing this directly here, removing the need to add more complexity to the above methods:
def validate_vms_resource(type, id, data = {})
transformation_mapping = resource_search(id, type, collection_class(type))
(transformation_mapping.validate_vms(data["import"]) || {}).tap do |res|
%w(valid_vms invalid_vms conflict_vms).each do |key|
next unless res.key?(key)
res[key].each do |v|
v["href"] = normalize_href(:vms, v["id"])
end
end
end
rescue StandardError => err
raise BadRequestError, "Could not validate vms - #{err}"
end
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.
This is fine too for now, but there is still an issue with the normalize_ methods where type is not properly propagated and type is deduced from the request when we explicitly are telling normalize_hash the type.
transformation_mapping = resource_search(id, type, collection_class(type)) | ||
normalize_hash(:vms, transformation_mapping.validate_vms(data["import"]) || {}) | ||
rescue StandardError => err | ||
raise BadRequestError, "Could not validate vms - #{err}" |
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 really want to rescue a StandardError here and tell the user they made a bad request? It seems like we should anticipate the things that could go wrong and respond with a bad request if the user made an error, otherwise the error is on us.
It might be nice to see some tests around this path illustrating what a user would have to do to get this response.
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.
Not sure we can anticipate what can wrong, this is outside API's knowledge and the validate_vms does not raise any malformed input error for now, so really nothing to catch and make a BadRequestError of. we should probably just drop this rescue and resort to the 500.
…agated. - Added the vms href directly in the validate_vms API action. - Removed the invalid rescue for the validate_vms method call made.
Checked commits abellotti/manageiq-api@cceac71~...e1d4eaf with ruby 2.3.3, rubocop 0.52.1, haml-lint 0.20.0, and yamllint 1.10.0 |
updated & re-pushed @imtayadeway /cc @AparnaKarve |
@imtayadeway Can we get this one merged? If there are additional changes that we need, we can do that in a followup PR. |
%w(valid_vms invalid_vms conflict_vms).each do |key| | ||
next unless res.key?(key) | ||
res[key].each do |entry| | ||
entry["href"] = normalize_href(:vms, entry["id"]) if entry["href"].blank? && entry["id"].present? |
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'm not sure why this postfix conditional is needed - isn't it part of the contract of the return value of validate_vms
that it will have an id, and that it won't have an href? If there are cases where this is needed, this should have a test. Perhaps you could handle this in a follow up PR? Thanks @abellotti !
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.
LGTM - I have just one follow-up item here but I think this is good enough to merge. Thanks @abellotti !
@miq-bot add_label transformation |
…e_vms Add support for validate_vms action on transformation_mappings (cherry picked from commit 9437016)
Gaprindashvili backport details:
|
Extends #352
Dependent on ManageIQ/manageiq#17177
Supports import from csv or without any data at all
cc: @gtanzillo @bzwei