Skip to content

Commit

Permalink
Fixes #22973 - add custom insights notification
Browse files Browse the repository at this point in the history
  • Loading branch information
ares authored and dLobatog committed Mar 22, 2018
1 parent 4c58c8b commit d37ad23
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# Offense count: 1
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 13
Max: 14

# Offense count: 1
# Configuration parameters: Include.
Expand Down
64 changes: 64 additions & 0 deletions app/services/foreman_ansible/insights_notification_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
module ForemanAnsible
# A class that builds custom notificaton for REX job if it's insights
# remediation feature
# rubocop:disable LineLength
class InsightsNotificationBuilder < ::UINotifications::RemoteExecutionJobs::BaseJobFinish
# rubocop:enable LineLength
def deliver!
::Notification.create!(
:audience => Notification::AUDIENCE_USER,
:notification_blueprint => blueprint,
:initiator => initiator,
:message => message,
:subject => subject,
:actions => {
:links => links
}
)
end

def blueprint
name = 'insights_remediation_successful'
@blueprint ||= NotificationBlueprint.unscoped.find_by(:name => name)
end

def hosts_count
@hosts_count ||= subject.template_invocations_hosts.size
end

def message
UINotifications::StringParser.new(blueprint.message,
:hosts_count => hosts_count)
end

def links
job_links + insights_links
end

def insights_links
pattern_template = subject.pattern_template_invocations.first
plan_id = pattern_template.input_values.
joins(:template_input).
where('template_inputs.name' => 'plan_id').
first.try(:value)
return [] if plan_id.nil?

[
{
:href => "/redhat_access/insights/planner/#{plan_id}",
:title => _('Remediation Plan')
}
]
end

def job_links
UINotifications::URLResolver.new(
subject,
:links => [{
:path_method => :job_invocation_path,
:title => _('Job Details')
}]
).actions[:links]
end
end
end
17 changes: 17 additions & 0 deletions db/seeds.d/90_notification_blueprints.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
blueprints = [
{
:group => N_('Jobs'),
:name => 'insights_remediation_successful',
:message => N_('Insights remediation on %{hosts_count}' \
' host(s) has finished successfully'),
:level => 'success',
:actions => {
:links => [
:path_method => :job_invocation_path,
:title => N_('Job Details')
]
}
}
]

blueprints.each { |blueprint| UINotifications::Seed.new(blueprint).configure }
5 changes: 4 additions & 1 deletion lib/foreman_ansible/remote_execution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class Engine < ::Rails::Engine
:Ansible,
ForemanAnsible::AnsibleProvider
)

ForemanAnsible::Engine.register_rex_feature
end

def self.register_rex_feature
Expand All @@ -22,7 +24,8 @@ def self.register_rex_feature
N_('Ansible: Run Insights maintenance plan'),
:description => N_('Runs a given maintenance plan from Red Hat '\
'Access Insights given an ID.'),
:provided_inputs => %w[organization_id plan_id]
:provided_inputs => %w[organization_id plan_id],
:notification_builder => ForemanAnsible::InsightsNotificationBuilder
)
end
end
Expand Down

0 comments on commit d37ad23

Please sign in to comment.