Class: ForemanAnsibleDirector::VariableService
- Inherits:
-
Object
- Object
- ForemanAnsibleDirector::VariableService
- Defined in:
- app/services/foreman_ansible_director/variable_service.rb
Class Method Summary collapse
- .create_override(variable:, value:, matcher:, matcher_value:) ⇒ Object
- .create_variable(key:, type:, default_value:, owner:) ⇒ Object
- .destroy_override(override) ⇒ Object
- .edit_override(override:, value:, matcher:, matcher_value:) ⇒ Object
- .edit_variable(variable:, key:, type:, default_value:, overridable:) ⇒ Object
- .get_overrides_for_target(target, include_overridable: false) ⇒ Object
Class Method Details
.create_override(variable:, value:, matcher:, matcher_value:) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'app/services/foreman_ansible_director/variable_service.rb', line 35 def create_override(variable:, value:, matcher:, matcher_value:) ActiveRecord::Base.transaction do LookupValue.create!( match: "#{matcher}=#{matcher_value}", value: value, lookup_key_id: variable.id ) end end |
.create_variable(key:, type:, default_value:, owner:) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'app/services/foreman_ansible_director/variable_service.rb', line 6 def create_variable(key:, type:, default_value:, owner:) ActiveRecord::Base.transaction do ::ForemanAnsibleDirector::AnsibleVariable.create!( key: key, default_value: default_value, variable_type: type, ownable: owner ) end end |
.destroy_override(override) ⇒ Object
60 61 62 63 64 |
# File 'app/services/foreman_ansible_director/variable_service.rb', line 60 def destroy_override(override) ActiveRecord::Base.transaction do override.destroy! end end |
.edit_override(override:, value:, matcher:, matcher_value:) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 |
# File 'app/services/foreman_ansible_director/variable_service.rb', line 48 def edit_override(override:, value:, matcher:, matcher_value:) ActiveRecord::Base.transaction do override.update!( match: "#{matcher}=#{matcher_value}", value: value ) end end |
.edit_variable(variable:, key:, type:, default_value:, overridable:) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'app/services/foreman_ansible_director/variable_service.rb', line 20 def edit_variable(variable:, key:, type:, default_value:, overridable:) ActiveRecord::Base.transaction do variable.update!( key: key, key_type: type, default_value: default_value, override: overridable ) end end |
.get_overrides_for_target(target, include_overridable: false) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'app/services/foreman_ansible_director/variable_service.rb', line 66 def get_overrides_for_target(target, include_overridable: false) case target when Host::Managed host = target when Hostgroup host = Host.new host.hostgroup = target else raise NotImplementedError, "Unexpected class #{target.class}" end _, resolved_assignments, = ::ForemanAnsibleDirector::AssignmentService.assignments_for( target: target, resolve: true ) return [] if resolved_assignments.blank? variables = [] resolved_assignments.each do |content_assignment| consumable = content_assignment[:cuv] next unless consumable variable_owner = variable_owner_for(consumable) next unless variable_owner overridables = ::ForemanAnsibleDirector::AnsibleVariable.where( ownable_type: variable_owner.class.name, ownable_id: variable_owner.id ).overridables overrides = overridables.values_hash(host).raw overridables.each do |variable| resolved_override = overrides.dig(variable.id, variable.key) next if resolved_override.nil? && !include_overridable override_matcher = matcher_for(resolved_override) variables << { variable_id: variable.id, key: variable.key, key_type: variable.key_type, default_value: variable.default_value, overridable: variable.overridable?, override_id: LookupValue.find_by(match: override_matcher, lookup_key_id: variable.id)&.id, override_matcher: override_matcher, override_value: resolved_override&.dig(:value), } end end variables end |