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.
- .completer_scope(options) ⇒ 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).
-
#location_ids ⇒ Object
For Audits to be correctly taxed for Puppetclass creation Puppetclass gets saved before the environment class and thus taxonomy ids are empty We collect the ids from unsaved environment_classes for the Audits correct taxation.
-
#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.
- #organization_ids ⇒ Object
Class Method Details
.classes2hash(classes) ⇒ Object
returns a hash containing modules and associated classes
66 67 68 69 70 71 72 73 74 |
# File 'app/models/foreman_puppet/puppetclass.rb', line 66 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
77 78 79 80 81 82 83 84 85 86 |
# File 'app/models/foreman_puppet/puppetclass.rb', line 77 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 |
.completer_scope(options) ⇒ Object
156 157 158 |
# File 'app/models/foreman_puppet/puppetclass.rb', line 156 def self.completer_scope() super.merge(assigned_to_hosts) end |
.search_by_host(_key, operator, value) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'app/models/foreman_puppet/puppetclass.rb', line 139 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
110 111 112 113 114 115 116 117 118 |
# File 'app/models/foreman_puppet/puppetclass.rb', line 110 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
131 132 133 134 135 136 137 |
# File 'app/models/foreman_puppet/puppetclass.rb', line 131 def hostgroup_ids=(hg_ids) hg_ids = Array(hg_ids).reject(&:blank?) hg_to_facets_ids = HostgroupPuppetFacet.where(hostgroup_id: hg_ids).pluck(:hostgroup_id, :id).to_h 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
120 121 122 123 124 125 126 127 128 |
# File 'app/models/foreman_puppet/puppetclass.rb', line 120 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)
106 107 108 |
# File 'app/models/foreman_puppet/puppetclass.rb', line 106 def klass name.gsub("#{module_name}::", '') end |
#location_ids ⇒ Object
For Audits to be correctly taxed for Puppetclass creation Puppetclass gets saved before the environment class and thus taxonomy ids are empty We collect the ids from unsaved environment_classes for the Audits correct taxation
91 92 93 |
# File 'app/models/foreman_puppet/puppetclass.rb', line 91 def location_ids environment_classes.select(&:new_record?).flat_map { |ec| ec.environment.location_ids }.concat(super).uniq 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
101 102 103 |
# File 'app/models/foreman_puppet/puppetclass.rb', line 101 def module_name (i = name.index('::')) ? name[0..i - 1] : name end |
#organization_ids ⇒ Object
95 96 97 |
# File 'app/models/foreman_puppet/puppetclass.rb', line 95 def organization_ids environment_classes.select(&:new_record?).flat_map { |ec| ec.environment.organization_ids }.concat(super).uniq end |