Sha256: 01dbaad22e72c491ae6f2ce5f3741bb3df0d597859382878dc3464b858183d39

Contents?: true

Size: 1.01 KB

Versions: 58

Compression:

Stored size: 1.01 KB

Contents

module Mutations
  module Hosts
    class AssignAnsibleRoles < ::Mutations::UpdateMutation
      resource_class Host::Managed

      argument :ansible_role_ids, [Integer], required: true

      field :host, Types::Host, 'The updated host.', null: false

      def resolve(id:, ansible_role_ids:)
        host = load_object_by(id: id)
        authorize!(host, :edit)

        existing = host.host_ansible_roles
        updated_ids = []
        attrs = []

        ansible_role_ids.each do |role_id|
          current = existing.find_by :ansible_role_id => role_id
          attrs << { :id => current&.id, :position => attrs.count + 1, :ansible_role_id => role_id }
          updated_ids << current.id if current
        end

        existing.where.not(:id => updated_ids).each do |item|
          attrs << { :id => item.id, :position => attrs.count + 1, :_destroy => true }
        end

        host.host_ansible_roles_attributes = attrs
        save_object(host)
      end

      def result_key
        :host
      end
    end
  end
end

Version data entries

58 entries across 58 versions & 1 rubygems

Version Path
foreman_ansible-15.0.2 app/graphql/mutations/hosts/assign_ansible_roles.rb
foreman_ansible-15.0.1 app/graphql/mutations/hosts/assign_ansible_roles.rb
foreman_ansible-15.0.0 app/graphql/mutations/hosts/assign_ansible_roles.rb
foreman_ansible-14.2.2 app/graphql/mutations/hosts/assign_ansible_roles.rb
foreman_ansible-14.2.1 app/graphql/mutations/hosts/assign_ansible_roles.rb
foreman_ansible-13.0.7 app/graphql/mutations/hosts/assign_ansible_roles.rb
foreman_ansible-14.2.0 app/graphql/mutations/hosts/assign_ansible_roles.rb
foreman_ansible-14.1.1 app/graphql/mutations/hosts/assign_ansible_roles.rb
foreman_ansible-14.1.0 app/graphql/mutations/hosts/assign_ansible_roles.rb
foreman_ansible-13.0.5 app/graphql/mutations/hosts/assign_ansible_roles.rb
foreman_ansible-14.0.0 app/graphql/mutations/hosts/assign_ansible_roles.rb
foreman_ansible-13.0.4 app/graphql/mutations/hosts/assign_ansible_roles.rb
foreman_ansible-13.0.3 app/graphql/mutations/hosts/assign_ansible_roles.rb
foreman_ansible-13.0.2 app/graphql/mutations/hosts/assign_ansible_roles.rb
foreman_ansible-13.0.1 app/graphql/mutations/hosts/assign_ansible_roles.rb
foreman_ansible-13.0.0 app/graphql/mutations/hosts/assign_ansible_roles.rb
foreman_ansible-12.0.7 app/graphql/mutations/hosts/assign_ansible_roles.rb
foreman_ansible-10.4.4 app/graphql/mutations/hosts/assign_ansible_roles.rb
foreman_ansible-7.1.8 app/graphql/mutations/hosts/assign_ansible_roles.rb
foreman_ansible-12.0.6 app/graphql/mutations/hosts/assign_ansible_roles.rb