Class: ForemanPuppet::Puppetclass
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- ForemanPuppet::Puppetclass
- Extended by:
- FriendlyId
- Includes:
- Authorizable, Parameterizable::ByIdName, ScopedSearchExtensions
- Defined in:
- app/models/foreman_puppet/puppetclass.rb
Class Method Summary collapse
-
.classes2hash(classes) ⇒ Object
returns a hash containing modules and associated classes.
-
.classes2hash_v2(classes) ⇒ Object
For API v2 - eliminate node :puppetclass for each object.
- .search_by_host(_key, operator, value) ⇒ Object
Instance Method Summary collapse
- #all_hostgroups(with_descendants: true, unsorted: false) ⇒ Object
-
#hostgroup_ids=(hg_ids) ⇒ Object
We are going through two associations, so we are on our own to define modifiers.
- #hosts_count ⇒ Object
-
#klass ⇒ Object
returns class name (excluding of the module name).
-
#module_name ⇒ Object
returns module name (excluding of the class name) if class separator does not exists (the “::” chars), then returns the whole class name.
Class Method Details
.classes2hash(classes) ⇒ Object
returns a hash containing modules and associated classes
53 54 55 56 57 58 59 60 61 |
# File 'app/models/foreman_puppet/puppetclass.rb', line 53 def self.classes2hash(classes) hash = {} classes.each do |klass| next unless (mod = klass.module_name) hash[mod] ||= [] hash[mod] << klass end hash end |
.classes2hash_v2(classes) ⇒ Object
For API v2 - eliminate node :puppetclass for each object. returns a hash containing modules and associated classes
64 65 66 67 68 69 70 71 72 73 |
# File 'app/models/foreman_puppet/puppetclass.rb', line 64 def self.classes2hash_v2(classes) hash = {} classes.each do |klass| if (mod = klass.module_name) hash[mod] ||= [] hash[mod] << { id: klass.id, name: klass.name, created_at: klass.created_at, updated_at: klass.updated_at } end end hash end |
.search_by_host(_key, operator, value) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'app/models/foreman_puppet/puppetclass.rb', line 115 def self.search_by_host(_key, operator, value) conditions = sanitize_sql_for_conditions(["hosts.name #{operator} ?", value_to_sql(operator, value)]) direct = Puppetclass.joins(host_puppet_facets: :host).where(conditions).pluck(:id).uniq hostgroup = Hostgroup.joins(:hosts).find_by(conditions) indirect = if hostgroup.blank? [] else HostgroupClass.joins(hostgroup_puppet_facet: :hostgroup) .where(Hostgroup.arel_table[:id].in(hostgroup.path_ids)) .pluck(:puppetclass_id).uniq end return { conditions: '1=0' } if direct.blank? && indirect.blank? puppet_classes = (direct + indirect).uniq { conditions: "puppetclasses.id IN(#{puppet_classes.join(',')})" } end |
Instance Method Details
#all_hostgroups(with_descendants: true, unsorted: false) ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'app/models/foreman_puppet/puppetclass.rb', line 86 def all_hostgroups(with_descendants: true, unsorted: false) hgs = Hostgroup. .left_outer_joins(puppet: [:hostgroup_classes, { config_groups: [:config_group_classes] }]) .where("#{id} IN (hostgroup_classes.puppetclass_id, config_group_classes.puppetclass_id)") .distinct hgs = hgs.reorder('') if unsorted hgs = hgs.flat_map(&:subtree).uniq if with_descendants hgs end |
#hostgroup_ids=(hg_ids) ⇒ Object
We are going through two associations, so we are on our own to define modifiers
107 108 109 110 111 112 113 |
# File 'app/models/foreman_puppet/puppetclass.rb', line 107 def hostgroup_ids=(hg_ids) hg_ids = Array(hg_ids).reject(&:blank?) hg_to_facets_ids = Hash[HostgroupPuppetFacet.where(hostgroup_id: hg_ids).pluck(:hostgroup_id, :id)] missing_facet_ids = hg_ids.map(&:to_i) - hg_to_facets_ids.keys new_facet_ids = missing_facet_ids.map { |hg_id| HostgroupPuppetFacet.create(hostgroup_id: hg_id).id } self.hostgroup_puppet_facet_ids = hg_to_facets_ids.values + new_facet_ids end |
#hosts_count ⇒ Object
96 97 98 99 100 101 102 103 104 |
# File 'app/models/foreman_puppet/puppetclass.rb', line 96 def hosts_count hostgroup_ids = all_hostgroups(unsorted: true).map(&:id) Host::Managed. .reorder(nil) .left_outer_joins(puppet: [:host_classes, { config_groups: [:config_group_classes] }]) .where('(? IN (host_classes.puppetclass_id, config_group_classes.puppetclass_id)) OR (hosts.hostgroup_id IN (?))', id, hostgroup_ids) .distinct .count end |
#klass ⇒ Object
returns class name (excluding of the module name)
82 83 84 |
# File 'app/models/foreman_puppet/puppetclass.rb', line 82 def klass name.gsub("#{module_name}::", '') end |
#module_name ⇒ Object
returns module name (excluding of the class name) if class separator does not exists (the “::” chars), then returns the whole class name
77 78 79 |
# File 'app/models/foreman_puppet/puppetclass.rb', line 77 def module_name (i = name.index('::')) ? name[0..i - 1] : name end |