-
Notifications
You must be signed in to change notification settings - Fork 118
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
Add checks for retirement of correct child service in bundle #284
Conversation
@miq_bot add_label bug |
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.
@miq-bot add_label enhancement |
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.
@d-m-u Looks good.
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 |
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.
@d-m-u
Can we add a spec for this.
@tinaafitz could you take another look for me at this please? |
end | ||
|
||
def main | ||
service = $evm.root['service'] |
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.
@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' |
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.
@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 |
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.
@d-m-u You could put the result logic in a method instead of doing it inline here.
@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, |
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.
@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 |
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.
@d-m-u What is the coverage on the method after you run the spec?
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.
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) |
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.
@d-m-u
Should the check_retired be returning a true/false and exhibiting different behaviour based on some earlier setting.
@tinaafitz bump |
end | ||
|
||
def main | ||
service |
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.
@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) |
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.
@d-m-u Can we pass the service argument here?
end | ||
|
||
@handle.log('info', "Service: #{service.name} Resource retirement check returned <#{result}>") | ||
result(result) |
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.
@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) |
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.
@d-m-u Can we add the service arg here?
|
||
result = 'ok' | ||
def check_service_resource_retirement(sr) |
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.
@d-m-u Can we add the service arg here?
|
||
$evm.log('info', "Checking if all service resources have been retired.") | ||
def service_obj |
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.
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) |
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.
@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") |
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.
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? |
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.
@tinaafitz do we need an error check here? Madhu seems to think so.
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.
@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) |
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.
@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) |
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.
@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'
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.
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? |
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.
@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 |
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.
@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) } | ||
|
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.
@d-m-u
Can you please add this test
it_behaves_like "automate_engine_call", domain_file
|
||
result = 'ok' | ||
def check_all_service_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.
@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' |
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.
@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' |
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.
|
||
def check_all_service_resources | ||
if all_service_resources_done? | ||
@handle.log('info', "Service: #{service.name} Resource retirement check returned <#{@result}>") |
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.
@d-m-u Can we remove this log message? The next log message has all of the necessary information.
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 |
We should only retire service bundle children that have resource_type of service and that have a parent.