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 checks for retirement of correct child service in bundle #284

Merged
merged 2 commits into from
Jul 9, 2018
Merged

Add checks for retirement of correct child service in bundle #284

merged 2 commits into from
Jul 9, 2018

Conversation

d-m-u
Copy link
Contributor

@d-m-u d-m-u commented Apr 19, 2018

We should only retire service bundle children that have resource_type of service and that have a parent.

@d-m-u
Copy link
Contributor Author

d-m-u commented Apr 19, 2018

@tinaafitz

@d-m-u
Copy link
Contributor Author

d-m-u commented Apr 19, 2018

@miq_bot add_label bug

Copy link
Member

@tinaafitz tinaafitz left a comment

Choose a reason for hiding this comment

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

@d-m-u Looks good. We'll need to add tests in the ManageIQ PR for retire_service_resources.
@mkanoor Please review.

@tinaafitz tinaafitz self-assigned this Apr 19, 2018
@d-m-u
Copy link
Contributor Author

d-m-u commented Apr 20, 2018

@miq-bot add_label enhancement

Copy link
Member

@tinaafitz tinaafitz left a comment

Choose a reason for hiding this comment

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

@d-m-u Looks good.

@tinaafitz
Copy link
Member

bump @mkanoor @gmcculloug

@@ -14,6 +14,7 @@

service.service_resources.each do |sr|
next if sr.resource.nil?
next if sr.resource_type == "Service" && $evm.vmdb(:service).find(sr[:resource_id]).has_parent
Copy link
Contributor

Choose a reason for hiding this comment

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

@d-m-u
Can we add a spec for this.

@d-m-u
Copy link
Contributor Author

d-m-u commented May 10, 2018

@tinaafitz could you take another look for me at this please?

end

def main
service = $evm.root['service']
Copy link
Member

Choose a reason for hiding this comment

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

@d-m-u You might want to have a method for service, something like this:

def service
   @handle.root["service"].tap do |service|
     if service.nil?
      @handle.log(:error, 'Service is nil')
       raise 'Service not found'
     end
  end
end

if sr.resource.retired?
@handle.log('info', "resource: #{sr.resource.name} is already retired.")
else
result = 'retry'
Copy link
Member

Choose a reason for hiding this comment

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

@d-m-u You could put the log message first here, then return retry

@handle.log('info', "resource: #{sr.resource.name} is not retired, setting retry.")
'retry'


$evm.log('info', "Checking if all service resources have been retired.")
@handle.log('info', "Service: #{service.name} Resource retirement check returned <#{result}>")
case result
Copy link
Member

Choose a reason for hiding this comment

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

@d-m-u You could put the result logic in a method instead of doing it inline here.

@d-m-u
Copy link
Contributor Author

d-m-u commented May 15, 2018

@tinaafitz uno mas, por favor

let(:retired_vm) { FactoryGirl.create(:vm, :retired => true) }
let(:root_object) do
Spec::Support::MiqAeMockObject.new('service' => svc_service,
'service_retire_task' => task,
Copy link
Contributor

Choose a reason for hiding this comment

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

@d-m-u
Everything fed into Automate is a Service Object. service_retire_task should point to svc_task instead of task

expect { described_class.new(ae_service).main }.to raise_error('Service not found')
end
end
end
Copy link
Contributor

Choose a reason for hiding this comment

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

@d-m-u What is the coverage on the method after you run the spec?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The only line I'm missing is the one at the end that I haven't had time to finish yet, yarps.

it "check" do
service.service_resources << FactoryGirl.create(:service_resource, :resource_type => "VmOrTemplate", :service_id => service.id, :resource_id => vm.id)
service.save
allow(svc_service).to receive(:check_retired)
Copy link
Contributor

Choose a reason for hiding this comment

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

@d-m-u
Should the check_retired be returning a true/false and exhibiting different behaviour based on some earlier setting.

@d-m-u
Copy link
Contributor Author

d-m-u commented May 17, 2018

@tinaafitz bump

end

def main
service
Copy link
Member

Choose a reason for hiding this comment

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

@d-m-u Could we set a local service attribute here so we can pass it into the check_service_resource_retirement and result methods?

next if sr.resource.nil?
next if sr.resource_type == "Service" && @handle.vmdb(:service).find(sr[:resource_id]).has_parent
next unless sr.resource.respond_to?(:retired?)
result = check_service_resource_retirement(sr)
Copy link
Member

Choose a reason for hiding this comment

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

@d-m-u Can we pass the service argument here?

end

@handle.log('info', "Service: #{service.name} Resource retirement check returned <#{result}>")
result(result)
Copy link
Member

Choose a reason for hiding this comment

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

@d-m-u Can we pass the service argument to result?

else
result = 'retry'
$evm.log('info', "resource: #{sr.resource.name} is not retired, setting retry.")
def result(param)
Copy link
Member

Choose a reason for hiding this comment

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

@d-m-u Can we add the service arg here?


result = 'ok'
def check_service_resource_retirement(sr)
Copy link
Member

Choose a reason for hiding this comment

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

@d-m-u Can we add the service arg here?


$evm.log('info', "Checking if all service resources have been retired.")
def service_obj
Copy link
Contributor

Choose a reason for hiding this comment

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

@d-m-u

 def service
    @service ||= @handle.root['service'].tap do |obj|
                           if obj.nil?
                               @handle.log(:error, 'Service object not provided')
                               raise 'Service object has not been provided'
                            end
                         end
                  end

If you did it this way it will cache the service object in an instance variable once after all the validation and for subsequent calls you can just use service every where.

You can remove the
service = service_obj line
Not pass the service into check_retired_state

next if sr.resource.nil?
next if sr.resource_type == "Service" && @handle.vmdb(:service).find(sr[:resource_id]).has_parent
next unless sr.resource.respond_to?(:retired?)
result = check_service_resource_retirement(sr, service)
Copy link
Contributor

Choose a reason for hiding this comment

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

@d-m-u
Since this is in a loop is the result changing for each iteration, what happens if the first one has an error and the second one has a success, we will report the success but not the first failure


result = 'ok'
def check_service_resource_retirement(sr)
@handle.log('info', "Checking if service resource for service: #{@service.name} resource ID: #{sr.id} is retired")
Copy link
Contributor

Choose a reason for hiding this comment

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

@d-m-u this should just be service instead of @service.

result = 'ok'
def check_service_resource_retirement(sr)
@handle.log('info', "Checking if service resource for service: #{service.name} resource ID: #{sr.id} is retired")
if sr.resource.retired?
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@tinaafitz do we need an error check here? Madhu seems to think so.

Copy link
Member

Choose a reason for hiding this comment

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

@mkanoor What type of checking would you want here? The max_retries would stop this eventually.

next unless sr.resource.respond_to?(:retired?)
result = check_service_resource_retirement(sr)
if result == 'retry'
check_retired_state(result)
Copy link
Member

Choose a reason for hiding this comment

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

@d-m-u Since we know that the result == retry, we can set the retry attributes and log message here.
@handle.log('info', "Service: #{service.name} resource is not retired, setting retry.")
@handle.root['ae_result'] = 'retry'
@handle.root['ae_retry_interval'] = '1.minute'

return result
else
@handle.log('info', "Service: #{service.name} Resource retirement check returned <#{result}>")
check_retired_state(result)
Copy link
Member

Choose a reason for hiding this comment

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

@d-m-u Since we know the result != retry, we can add the log message and set the result here:
@handle.log('info', "All resources are retired for service: #{service.name}. ")
@handle.root['ae_result'] = 'ok'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You had originally told me to break it out into separate methods.

result = 'ok'
def check_service_resource_retirement(sr)
@handle.log('info', "Checking if service resource for service: #{service.name} resource ID: #{sr.id} is retired")
if sr.resource.retired?
Copy link
Member

Choose a reason for hiding this comment

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

@mkanoor What type of checking would you want here? The max_retries would stop this eventually.

result = check_service_resource_retirement(sr)
if result == 'retry'
check_retired_state(result)
return result
Copy link
Member

Choose a reason for hiding this comment

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

@d-m-u I don't think we need to return result here.

'service_action' => 'Retirement')
end
let(:ae_service) { Spec::Support::MiqAeMockService.new(root_object) }

Copy link
Contributor

Choose a reason for hiding this comment

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

@d-m-u
Can you please add this test

it_behaves_like "automate_engine_call", domain_file


result = 'ok'
def check_all_service_resources
Copy link
Contributor

Choose a reason for hiding this comment

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

@d-m-u
Can we try something like this

              def check_all_service_resources
                if all_service_resources_done?
                  @handle.log('info', "Service: #{service.name} Resource retirement check returned <#{result}>")
                  @handle.log('info', "All resources are retired for service: #{service.name}. ")
                  @handle.root['ae_result'] = 'ok'
                else
                  @handle.log('info', "Service: #{service.name} resource is not retired, setting retry.")
                  @handle.root['ae_result'] = 'retry'
                  @handle.root['ae_retry_interval'] = '1.minute'
                end
              end

              def check_service_resource_retirement(sr)
                return 'ignore' unless process?(sr)
                @handle.log('info', "Checking if service resource for service: #{service.name} resource ID: #{sr.id} is retired")
                if sr.resource.retired?
                  @handle.log('info', "resource: #{sr.resource.name} is already retired.")
                  'ok'
                else
                  @handle.log('info', "resource: #{sr.resource.name} is not retired, setting retry.")
                  'retry'
                end
              end
          
              def process?(sr)
                return false if sr.resource.nil?
                return false if sr.resource_type == "Service" && @handle.vmdb(:service).find(sr[:resource_id]).has_parent
                sr.resource.respond_to?(:retired?)
              end

              def all_service_resources_done?
                service.service_resources.all? { |sr| %w(ok ignore).include?(check_service_resource_retirement(sr)) }
              end


def main
@handle.log('info', "Checking if all service resources have been retired.")
@result = 'ok'
Copy link
Contributor

Choose a reason for hiding this comment

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

@d-m-u
This instance variable is not getting used anymore, can we delete it.


def main
@handle.log('info', "Checking if all service resources have been retired.")
@result = 'ok'
Copy link
Member

Choose a reason for hiding this comment

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

@d-m-u We can remove the @Result = 'ok'


def check_all_service_resources
if all_service_resources_done?
@handle.log('info', "Service: #{service.name} Resource retirement check returned <#{@result}>")
Copy link
Member

Choose a reason for hiding this comment

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

@d-m-u Can we remove this log message? The next log message has all of the necessary information.

@miq-bot
Copy link
Member

miq-bot commented Jun 20, 2018

Checked commits d-m-u/manageiq-content@96ce7c6~...2ef160f 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. 👍

@mkanoor mkanoor merged commit d5d6329 into ManageIQ:master Jul 9, 2018
@mkanoor mkanoor added this to the Sprint 90 Ending Jul 16, 2018 milestone Jul 9, 2018
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.

None yet

4 participants