Sha256: e4c9ac4c6df8234551cfb34cd1cd601a864f4fa90410a4e689354f0a160825d5

Contents?: true

Size: 1.88 KB

Versions: 12

Compression:

Stored size: 1.88 KB

Contents

module ForemanAnsible
  module Api
    module V2
      # Extends the hostgroups controller to support playing ansible roles
      module HostgroupsControllerExtensions
        extend ActiveSupport::Concern
        include ForemanTasks::Triggers
        include ::ForemanAnsible::Concerns::JobInvocationHelper

        # Included blocks shouldn't be bound by length, as otherwise concerns
        # cannot extend the method properly.
        included do
          api :POST, '/hostgroups/:id/play_roles',
              N_('Plays Ansible roles on a hostgroup')
          param :id, String, :required => true

          def play_roles
            find_resource
            composer = job_composer(:ansible_run_host, @hostgroup.hosts)
            process_response composer.trigger!, composer.job_invocation
          end

          api :POST, '/hostgroups/play_roles',
              N_('Plays Ansible roles on hostgroups')
          param :id, Array, :required => true

          def multiple_play_roles
            find_multiple
            composer = job_composer(:ansible_run_host,
                                    @hostgroups.map(&:host_ids).flatten.uniq)
            process_response composer.trigger!, composer.job_invocation
          end
        end

        private

        def find_multiple
          hostgroup_ids = params.fetch(:hostgroup_ids, [])
          hostgroup_names = params.fetch(:hostgroup_names, [])

          @hostgroups = []
          hostgroup_ids.uniq.each do |hostgroup_id|
            @hostgroups.append(Hostgroup.find(hostgroup_id))
          end
          hostgroup_names.uniq.each do |hostgroup_name|
            @hostgroups.append(Hostgroup.find_by(:name => hostgroup_name))
          end
        end

        def action_permission
          case params[:action]
          when 'play_roles'
            :view
          else
            super
          end
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
foreman_ansible-2.2.9 app/controllers/foreman_ansible/api/v2/hostgroups_controller_extensions.rb
foreman_ansible-2.2.8 app/controllers/foreman_ansible/api/v2/hostgroups_controller_extensions.rb
foreman_ansible-2.2.7 app/controllers/foreman_ansible/api/v2/hostgroups_controller_extensions.rb
foreman_ansible-2.2.6 app/controllers/foreman_ansible/api/v2/hostgroups_controller_extensions.rb
foreman_ansible-2.2.5 app/controllers/foreman_ansible/api/v2/hostgroups_controller_extensions.rb
foreman_ansible-2.2.4 app/controllers/foreman_ansible/api/v2/hostgroups_controller_extensions.rb
foreman_ansible-2.2.3 app/controllers/foreman_ansible/api/v2/hostgroups_controller_extensions.rb
foreman_ansible-2.2.2 app/controllers/foreman_ansible/api/v2/hostgroups_controller_extensions.rb
foreman_ansible-2.2.1 app/controllers/foreman_ansible/api/v2/hostgroups_controller_extensions.rb
foreman_ansible-2.2.0 app/controllers/foreman_ansible/api/v2/hostgroups_controller_extensions.rb
foreman_ansible-2.1.2 app/controllers/foreman_ansible/api/v2/hostgroups_controller_extensions.rb
foreman_ansible-2.1.1 app/controllers/foreman_ansible/api/v2/hostgroups_controller_extensions.rb