Project

General

Profile

Download (5.74 KB) Statistics
| Branch: | Tag: | Revision:
086ec942 Ohad Levy
class HostgroupsController < ApplicationController
e5d3f34e Ohad Levy
include Foreman::Controller::HostDetails
b80fa6f1 Ohad Levy
include Foreman::Controller::AutoCompleteSearch
8b737c9c Joseph Magen
before_filter :find_resource, :only => [:nest, :clone, :edit, :update, :destroy]
e14b6968 Daniel Lobato
before_filter :ajax_request, :only => [:process_hostgroup, :current_parameters, :puppetclass_parameters]
before_filter :taxonomy_scope, :only => [:new, :edit, :process_hostgroup]
221fd050 Tom Caspy
before_filter :update_parent_params!, :only => [:update, :create]
e5d3f34e Ohad Levy
bd46fa41 Ohad Levy
def index
acfbc458 Marek Hulan
@hostgroups = resource_base.search_for(params[:search], :order => params[:order]).paginate :page => params[:page]
086ec942 Ohad Levy
end

bd46fa41 Ohad Levy
def new
@hostgroup = Hostgroup.new
end

4d4b84f6 Ohad Levy
def nest
acfbc458 Marek Hulan
@parent = @hostgroup
34897490 Joseph Magen
@hostgroup = Hostgroup.new(:parent_id => @parent.id)
e0f4f396 Joseph Mitchell Magen
load_vars_for_ajax
@hostgroup.puppetclasses = @parent.puppetclasses
@hostgroup.locations = @parent.locations
@hostgroup.organizations = @parent.organizations
# Clone any parameters as well
@hostgroup.group_parameters.each{|param| @parent.group_parameters << param.dup}
4d4b84f6 Ohad Levy
render :action => :new
end

4899723c Ohad Levy
# Clone the hostgroup
def clone
5eb88cf5 Ori Rabin
new = @hostgroup.clone
4899723c Ohad Levy
load_vars_for_ajax
05ab4c16 Ohad Levy
new.valid?
4899723c Ohad Levy
@hostgroup = new
bfbf7ed8 Lukas Zapletal
notice _("The following fields would need reviewing")
4899723c Ohad Levy
render :action => :new
end

bd46fa41 Ohad Levy
def create
@hostgroup = Hostgroup.new(params[:hostgroup])
if @hostgroup.save
b28fdce4 Ohad Levy
process_success
bd46fa41 Ohad Levy
else
743a5d9c Ohad Levy
load_vars_for_ajax
b28fdce4 Ohad Levy
process_error
bd46fa41 Ohad Levy
end
end

def edit
743a5d9c Ohad Levy
load_vars_for_ajax
bd46fa41 Ohad Levy
end

def update
3595a70c Joseph Mitchell Magen
# remove from hash :root_pass if blank?
params[:hostgroup].except!(:root_pass) if params[:hostgroup][:root_pass].blank?
bd46fa41 Ohad Levy
if @hostgroup.update_attributes(params[:hostgroup])
b28fdce4 Ohad Levy
process_success
bd46fa41 Ohad Levy
else
e14b6968 Daniel Lobato
taxonomy_scope
743a5d9c Ohad Levy
load_vars_for_ajax
b28fdce4 Ohad Levy
process_error
bd46fa41 Ohad Levy
end
end

def destroy
8c68024a Daniel Lobato
begin
if @hostgroup.destroy
process_success
else
load_vars_for_ajax
process_error
end
rescue Ancestry::AncestryException
62e5c7d2 Dominic Cleal
process_error(:error_msg => _("Cannot delete group %{current} because it has nested groups.") % { :current => @hostgroup.title } )
bd46fa41 Ohad Levy
end
end
e5d3f34e Ohad Levy
e14b6968 Daniel Lobato
def current_parameters
Taxonomy.as_taxonomy @organization, @location do
e1aec1dc Tomer Brisker
inherited_parameters = Hostgroup.find(params['hostgroup_parent_id']).inherited_params(true) if params['hostgroup_parent_id']
e14b6968 Daniel Lobato
render :partial => "common_parameters/inherited_parameters",
e1aec1dc Tomer Brisker
:locals => { :inherited_parameters => inherited_parameters }
e14b6968 Daniel Lobato
end
end

def puppetclass_parameters
ae7d8d5f Ondrej Prazak
@obj = params[:hostgroup][:id].empty? ? Hostgroup.new(params[:hostgroup]) : Hostgroup.find(params[:hostgroup_id])
e14b6968 Daniel Lobato
Taxonomy.as_taxonomy @organization, @location do
render :partial => "puppetclasses/classes_parameters",
ae7d8d5f Ondrej Prazak
:locals => { :obj => @obj }
e14b6968 Daniel Lobato
end
end

b26ddb7d Ohad Levy
def environment_selected
67799065 Ohad Levy
return not_found unless (@environment = Environment.find(params[:environment_id])) if params[:environment_id].to_i > 0
b26ddb7d Ohad Levy
@hostgroup ||= Hostgroup.new
@hostgroup.environment = @environment if @environment
8b98d8e0 Ondrej Prazak
@hostgroup.puppetclasses = Puppetclass.where(:id => params[:hostgroup][:puppetclass_ids])
111cde57 Joseph Magen
render :partial => 'puppetclasses/class_selection', :locals => {:obj => (@hostgroup), :type => 'hostgroup'}
b26ddb7d Ohad Levy
end

1bd19611 Joseph Mitchell Magen
def process_hostgroup
e14b6968 Daniel Lobato
define_parent
define_hostgroup
inherit_parent_attributes
1bd19611 Joseph Mitchell Magen
load_vars_for_ajax

e14b6968 Daniel Lobato
render :partial => "form"
01122c8b Joseph Mitchell Magen
end

743a5d9c Ohad Levy
private

def load_vars_for_ajax
e14b6968 Daniel Lobato
return unless @hostgroup.present?

743a5d9c Ohad Levy
@architecture = @hostgroup.architecture
@operatingsystem = @hostgroup.operatingsystem
8dec3f5b Ohad Levy
@domain = @hostgroup.domain
1bd19611 Joseph Mitchell Magen
@subnet = @hostgroup.subnet
@environment = @hostgroup.environment
e14b6968 Daniel Lobato
@realm = @hostgroup.realm
e5d3f34e Ohad Levy
end

7d8c71bc Daniel Lobato
def users_in_ancestors
@hostgroup.ancestors.map do |ancestor|
ancestor.users.reject { |u| @hostgroup.users.include?(u) }
end.flatten.uniq
end

acfbc458 Marek Hulan
def action_permission
case params[:action]
when 'nest', 'clone'
'view'
else
super
end
end

e14b6968 Daniel Lobato
def parse_parent_params(parameters)
parameter_keys = parameters.select { |p| p.match(/key/) }.values
parameter_values = parameters.select { |p| p.match(/value/) }.values
parameters = {}
221fd050 Tom Caspy
parameters_hash = parameter_keys.zip(parameter_values).reject{|k, v| v.blank?}
e14b6968 Daniel Lobato
221fd050 Tom Caspy
parameters_hash.each_with_index do |val, i|
key, value = val[0], val[1]
id = GroupParameter.last.id + 1 + i
parameters[id] = { 'name' => key, 'value' => value, 'nested' => true } if value.present?
e14b6968 Daniel Lobato
end
parameters
end

221fd050 Tom Caspy
def update_parent_params!
parent_params = parse_parent_params(params.select { |k| k.match(/parent.*/) } )
if params[:hostgroup] && (params[:hostgroup][:group_parameters_attributes].present? || parent_params.present?)
params[:hostgroup][:group_parameters_attributes] ||= {}
params[:hostgroup][:group_parameters_attributes].merge!(parent_params)
end
end

e14b6968 Daniel Lobato
def define_parent
if params[:hostgroup][:parent_id].present?
@parent = Hostgroup.authorized(:view_hostgroups).find(params[:hostgroup][:parent_id])
end
end

def define_hostgroup
if params[:hostgroup][:id].present?
@hostgroup = Hostgroup.authorized(:view_hostgroups).find(params[:hostgroup][:id])
@hostgroup.attributes = params[:hostgroup]
else
@hostgroup = Hostgroup.new(params[:hostgroup])
end
end

def inherit_parent_attributes
return unless @parent.present?

@hostgroup.architecture ||= @parent.architecture
@hostgroup.operatingsystem ||= @parent.operatingsystem
@hostgroup.domain ||= @parent.domain
@hostgroup.subnet ||= @parent.subnet
@hostgroup.realm ||= @parent.realm
@hostgroup.environment ||= @parent.environment
end
086ec942 Ohad Levy
end