-
Notifications
You must be signed in to change notification settings - Fork 993
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b793575
commit 7ccdc13
Showing
35 changed files
with
655 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module Api | ||
module V2 | ||
class RenderStatusesController < V2::BaseController | ||
api :GET, "/hosts/:host_id/render_statuses/", N_("List all render statuses for a given host") | ||
api :GET, "/hostgroups/:host_id/render_statuses/", N_("List all render statuses for a given hostgroup") | ||
api :GET, "/provisioning_templates/:provisioning_template_id/render_statuses/", N_("List all render statuses for a given provisioning template") | ||
|
||
param :host_id, :identifier, desc: N_("ID of host") | ||
param :hostgroup_id, :identifier, desc: N_("ID of hostgroup") | ||
param :provisioning_template_id, :identifier, desc: N_("ID of provisioning template") | ||
param_group :search_and_pagination, ::Api::V2::BaseController | ||
add_scoped_search_description_for(RenderStatus) | ||
|
||
def index | ||
@render_statuses = resource_scope_for_index.includes(:host, :hostgroup, :provisioning_template) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
class RenderStatus < ApplicationRecord | ||
include Authorizable | ||
|
||
belongs_to :host, class_name: 'Host::Managed' | ||
belongs_to :hostgroup | ||
belongs_to :provisioning_template, foreign_key: :template_id | ||
|
||
validate :host_or_hostgroup_presence | ||
validates :host, uniqueness: { scope: [:provisioning_template, :safemode] } | ||
validates :hostgroup, uniqueness: { scope: [:provisioning_template, :safemode] } | ||
validates :provisioning_template, presence: true | ||
validates :safemode, inclusion: [true, false] | ||
validates :success, inclusion: [true, false] | ||
|
||
scoped_search on: :updated_at, default_order: :asc | ||
scoped_search on: :safemode | ||
|
||
private | ||
|
||
def host_or_hostgroup_presence | ||
errors.add(:base, "can only have host or hostgroup") if host.present? && hostgroup.present? | ||
errors.add(:base, "host or hostgroup is required") if host.blank? && hostgroup.blank? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
module Foreman | ||
module Renderer | ||
class RenderStatusService | ||
def self.success(host:, provisioning_template:, safemode:) | ||
call(host, provisioning_template, safemode, true) | ||
end | ||
|
||
def self.error(host:, provisioning_template:, safemode:) | ||
call(host, provisioning_template, safemode, false) | ||
end | ||
|
||
def self.call(host, provisioning_template, safemode, success) | ||
return unless host | ||
|
||
klass = host.persisted? ? ExistingHostService : NewHostService | ||
klass.new(host, provisioning_template, safemode, success).call | ||
end | ||
|
||
class BaseService | ||
def initialize(host, provisioning_template, safemode, success) | ||
@host = host | ||
@provisioning_template = provisioning_template | ||
@safemode = safemode | ||
@success = success | ||
end | ||
|
||
private | ||
|
||
attr_reader :host, :provisioning_template, :safemode, :success | ||
delegate :render_statuses, to: :host | ||
delegate :id, to: :provisioning_template, prefix: true | ||
end | ||
|
||
class ExistingHostService < BaseService | ||
def call | ||
render_statuses.find_or_initialize_by(provisioning_template: provisioning_template, safemode: safemode) | ||
.update(success: success) | ||
end | ||
end | ||
|
||
class NewHostService < BaseService | ||
def call | ||
update || create | ||
end | ||
|
||
private | ||
|
||
def update | ||
render_statuses.find { |status| status.template_id == provisioning_template_id && status.safemode == safemode } | ||
.tap { |status| status&.assign_attributes(success: success) } | ||
end | ||
|
||
def create | ||
render_statuses.new(provisioning_template: provisioning_template, safemode: safemode, success: success) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
object @render_status | ||
|
||
attributes :id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
collection @render_statuses | ||
|
||
extends "api/v2/render_statuses/main" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
object @render_status | ||
|
||
extends "api/v2/render_statuses/base" | ||
|
||
attributes :safemode, :success, :created_at, :updated_at | ||
|
||
child :host do | ||
attributes :id, :name | ||
node :path do |host| | ||
path = host_details_page_path(host) | ||
User.current.allowed_to?(path) ? path : nil | ||
end | ||
end | ||
|
||
child :hostgroup do | ||
attributes :id, :name | ||
node :path do |hostgroup| | ||
path = edit_hostgroup_path(hostgroup) | ||
User.current.allowed_to?(path) ? path : nil | ||
end | ||
end | ||
|
||
child :provisioning_template do | ||
attributes :id, :name | ||
node :path do |provisioning_template| | ||
path = edit_provisioning_template_path(provisioning_template) | ||
User.current.allowed_to?(path) ? path : nil | ||
end | ||
end |
3 changes: 3 additions & 0 deletions
3
app/views/provisioning_templates/_custom_tab_headers.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
<li><a href="#template_type" data-toggle="tab"><%= _("Type") %></a></li> | ||
<li><a href="#template_associations" data-toggle="tab"><%= _("Association") %></a></li> | ||
<% if @template.render_statuses_enabled? %> | ||
<li><a href="#render_statuses" data-toggle="tab"><%= _("Render Statuses") %></a></li> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= react_component('RenderStatuses', { path: api_provisioning_template_render_statuses_path(@template) }.to_json) %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
class CreateRenderStatuses < ActiveRecord::Migration[6.0] | ||
def change | ||
create_table :render_statuses do |t| | ||
t.references :host, type: :integer, foreign_key: true, index: true | ||
t.references :hostgroup, type: :integer, foreign_key: true, index: true | ||
t.references :template, type: :integer, foreign_key: true, index: true, null: false | ||
t.boolean :safemode, null: false | ||
t.boolean :success, null: false | ||
t.timestamps | ||
end | ||
|
||
add_index :render_statuses, [:host_id, :template_id, :safemode], unique: true, name: 'index_render_statuses_on_host_and_template_and_safemode' | ||
add_index :render_statuses, [:hostgroup_id, :template_id, :safemode], unique: true, name: 'index_render_statuses_on_hostgroup_and_template_and_safemode' | ||
|
||
reversible do |dir| | ||
dir.up do | ||
execute <<-SQL | ||
ALTER TABLE render_statuses ADD CONSTRAINT host_or_hostgroup_check CHECK ( | ||
( | ||
(host_id is not null)::integer + | ||
(hostgroup_id is not null)::integer | ||
) = 1 | ||
); | ||
SQL | ||
end | ||
dir.down do | ||
execute <<-SQL | ||
ALTER TABLE render_statuses DROP CONSTRAINT host_or_hostgroup_check; | ||
SQL | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.