Skip to content
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

Merged

Conversation

abellotti
Copy link
Member

@abellotti abellotti commented Apr 5, 2018

Extends #352

Dependent on ManageIQ/manageiq#17177

Supports import from csv or without any data at all

POST /api/transformation_mappings/:id
{
   "action": "validate_vms"
}
POST /api/transformation_mappings/:id
{
   "action": "validate_vms",
   "import": [
      { "name": "vm_name", "uid": "vm_uid" } 
    ]
}

cc: @gtanzillo @bzwei

@abellotti
Copy link
Member Author

Marking as WIP as we need ManageIQ/manageiq#17177 merged first

resolves issue #352 (comment)

so results now would return vms hrefs, so:

{
    "valid_vms": [
        {
            "href": "http://localhost:3000/api/vms/2222",
            "name": "amaya-at",
            "cluster": "Default",
            "path": "RHV/Default",
            "allocated_size": "32212254720.0",
            "id": "2222",
            "reason": "OK"
        }
    ]
}

Jillian Tullo and others added 3 commits April 26, 2018 18:13
…s vm references

instead of the previously returned transformation_mappings hrefs.
…cluster

and a transformation mapping with the proper item identifying the source cluster.
@abellotti abellotti force-pushed the transformation_mapping_validate_vms branch from b602511 to 3250fe4 Compare April 27, 2018 13:20
@abellotti abellotti changed the title [WIP] Add support for validate_vms action on transformation_mappings Add support for validate_vms action on transformation_mappings Apr 27, 2018
@abellotti abellotti removed the wip label Apr 27, 2018
@abellotti
Copy link
Member Author

@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.

Copy link
Contributor

@imtayadeway imtayadeway left a 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"]) || {})
Copy link
Contributor

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

Copy link
Member Author

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}"
Copy link
Contributor

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.

Copy link
Member Author

@abellotti abellotti Apr 27, 2018

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.
@miq-bot
Copy link
Member

miq-bot commented Apr 27, 2018

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
2 files checked, 0 offenses detected
Everything looks fine. 🍰

@abellotti
Copy link
Member Author

updated & re-pushed @imtayadeway

/cc @AparnaKarve

@gtanzillo
Copy link
Member

@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?
Copy link
Contributor

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 !

Copy link
Contributor

@imtayadeway imtayadeway left a 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 !

@imtayadeway imtayadeway merged commit 9437016 into ManageIQ:master Apr 30, 2018
@imtayadeway imtayadeway added this to the Sprint 85 Ending May 7, 2018 milestone Apr 30, 2018
@gmcculloug
Copy link
Member

@miq-bot add_label transformation

@miq-bot miq-bot added the v2v label May 17, 2018
simaishi pushed a commit that referenced this pull request May 29, 2018
…e_vms

Add support for validate_vms action on transformation_mappings
(cherry picked from commit 9437016)
@simaishi
Copy link
Contributor

Gaprindashvili backport details:

$ git log -1
commit eeeb1992d5ff128ef170517d20d4ee7a50f0ff07
Author: Tim Wade <[email protected]>
Date:   Mon Apr 30 07:40:47 2018 -0700

    Merge pull request #358 from abellotti/transformation_mapping_validate_vms
    
    Add support for validate_vms action on transformation_mappings
    (cherry picked from commit 94370162a7b6e7e6d896fac21f7cc2b09747c127)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants