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

Refactor amazon_customizerequest method for VM Provisioning. #628

Merged

Conversation

pkomanek
Copy link
Contributor

@pkomanek pkomanek commented Jan 9, 2020

Refactoring the method and creating tests.

This PR is based on the issue below.
ManageIQ/manageiq#12038

@miq-bot add_label refactoring
@miq-bot assign @tinaafitz

@coveralls
Copy link

Pull Request Test Coverage Report for Build 4104

  • 82 of 82 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.08%) to 97.456%

Totals Coverage Status
Change from base Build 4096: 0.08%
Covered Lines: 3103
Relevant Lines: 3184

💛 - Coveralls

@coveralls
Copy link

coveralls commented Jan 9, 2020

Pull Request Test Coverage Report for Build 4151

  • 84 of 84 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.06%) to 97.448%

Totals Coverage Status
Change from base Build 4129: 0.06%
Covered Lines: 3093
Relevant Lines: 3174

💛 - Coveralls

@pkomanek pkomanek force-pushed the amazon_customize_request_refactoring branch 2 times, most recently from 5a9d5e6 to 093bd03 Compare January 10, 2020 15:59
end
end
end
def main(mapping: false)
Copy link
Member

@lfu lfu Jan 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is better keep the main method signature same as other ruby methods.

def main
  ...
  mapping = false
  process_customization if mapping
 ...
end

def process_customization
   set_options
   set_customization_template
end

end

def log(level, msg, update_message = false)
@handle.log(level, msg.to_s)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is to_s necessary? How about @handle.log(level, msg)?


def log(level, msg, update_message = false)
@handle.log(level, msg.to_s)
@handle.root['miq_provision'].message = msg.to_s if @handle.root['miq_provision'] && update_message
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here for to_s.

@pkomanek pkomanek force-pushed the amazon_customize_request_refactoring branch 2 times, most recently from f4a4cab to 6afe308 Compare January 21, 2020 14:18
@pkomanek pkomanek changed the title [WIP]Refactor amazon_customizerequest method for VM Provisioning. Refactor amazon_customizerequest method for VM Provisioning. Jan 21, 2020
@miq-bot miq-bot removed the wip label Jan 21, 2020
end

def product
@product ||= template.operating_system['product_name'].downcase rescue nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pkomanek This refactoring, while it did not introduce the rescue statements, should be able to easily remove them.

For most the initial object is guaranteed to be valid because the accessor method is raising an error. Here the template method will always return a template object or raise an error. So we just need to worry about operating_system and the remaining items being nil.

If we use the . notation instead of ['product_name'] to reference the property you can use safe navigation to achieve this:

@product ||= template.operating_system&.product_name&.downcase

return
end

customization_template_search_by_function = "#{prov.type}_#{prov.get_tags[:function]}" rescue nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The prov method will raise an error if it cannot return the object, so we can just remove the rescue nil at the end.


customization_template_search_by_function = "#{prov.type}_#{prov.get_tags[:function]}" rescue nil
customization_template_search_by_template_name = template.name
customization_template_search_by_ws_values = ws_values[:customization_template] rescue nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ws_values method defaults to an empty hash so we can just remove the rescue nil at the end.

:name => customization_template.name,
:desc => customization_template.description
})
prov.set_customization_template(customization_template) rescue nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only one I'm not really sure about and may want to keep in this refactoring and address later.

@pkomanek pkomanek force-pushed the amazon_customize_request_refactoring branch from 6afe308 to cfb36fd Compare January 28, 2020 18:16
@pkomanek pkomanek force-pushed the amazon_customize_request_refactoring branch from cfb36fd to c4e93c0 Compare February 6, 2020 14:25
@miq-bot
Copy link
Member

miq-bot commented Feb 6, 2020

Some comments on commits pkomanek/manageiq-content@2843cd3~...c4e93c0

spec/content/automate/ManageIQ/Cloud/VM/Provisioning/StateMachines/Methods.class/methods/amazon_customizerequest_spec.rb

  • ⚠️ - 137 - Detected allow_any_instance_of. This RSpec method is highly discouraged, please only use when absolutely necessary.

@miq-bot
Copy link
Member

miq-bot commented Feb 6, 2020

Checked commits pkomanek/manageiq-content@2843cd3~...c4e93c0 with ruby 2.5.5, rubocop 0.69.0, haml-lint 0.20.0, and yamllint 1.10.0
2 files checked, 1 offense detected

content/automate/ManageIQ/Cloud/VM/Provisioning/StateMachines/Methods.class/methods/amazon_customizerequest.rb

@tinaafitz tinaafitz merged commit cbe5517 into ManageIQ:master Mar 24, 2020
@pkomanek pkomanek deleted the amazon_customize_request_refactoring branch March 24, 2020 15:01
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