<commit_before>module ManageIQ::Providers::AnsibleTower::Shared::AutomationManager::Credential
  extend ActiveSupport::Concern

  module ClassMethods
    def create_in_provider(manager_id, params)
      manager = ExtManagementSystem.find(manager_id)
      credential = manager.with_provider_connection do |connection|
        connection.api.credentials.create!(params)
      end

      # Get the record in our database
      # TODO: This needs to be targeted refresh so it doesn't take too long
      task_ids = EmsRefresh.queue_refresh_task(manager)
      task_ids.each { |tid| MiqTask.wait_for_taskid(tid) }

      find_by!(:resource_id => manager.id, :manager_ref => credential.id)
    end

    def create_in_provider_queue(manager_id, params)
      task_opts = {
        :action => "Creating #{name}",
        :userid => "system"
      }

      manager = ExtManagementSystem.find(manager_id)

      queue_opts = {
        :args        => [manager_id, params],
        :class_name  => name,
        :method_name => "create_in_provider",
        :priority    => MiqQueue::HIGH_PRIORITY,
        :role        => "ems_operations",
        :zone        => manager.my_zone
      }

      MiqTask.generic_action_with_callback(task_opts, queue_opts)
    end
  end
end<commit_msg>Ansible Credential to update/delete in Tower<commit_after>module ManageIQ::Providers::AnsibleTower::Shared::AutomationManager::Credential
  extend ActiveSupport::Concern

  module ClassMethods
    def create_in_provider(manager_id, params)
      manager = ExtManagementSystem.find(manager_id)
      credential = manager.with_provider_connection do |connection|
        connection.api.credentials.create!(params)
      end

      refresh(manager)
      find_by!(:resource_id => manager.id, :manager_ref => credential.id)
    end

    def create_in_provider_queue(manager_id, params)
      manager = ExtManagementSystem.find(manager_id)
      queue(manager.my_zone, nil, "create_in_provider", [manager_id, params], "Creating #{name}")
    end

    private

    def refresh(manager)
      # Get the record in our database
      # TODO: This needs to be targeted refresh so it doesn't take too long
      task_ids = EmsRefresh.queue_refresh_task(manager)
      task_ids.each { |tid| MiqTask.wait_for_taskid(tid) }
    end

    def queue(zone, instance_id, method_name, args, action)
      task_opts = {
        :action => action,
        :userid => "system"
      }

      queue_opts = {
        :args        => args,
        :class_name  => name,
        :method_name => method_name,
        :priority    => MiqQueue::HIGH_PRIORITY,
        :role        => "ems_operations",
        :zone        => zone
      }
      queue_opts[:instance_id] = instance_id if instance_id
      MiqTask.generic_action_with_callback(task_opts, queue_opts)
    end
  end

  def update_in_provider(params)
    params.delete(:task_id) # in case this is being called through update_in_provider_queue which will stick in a :task_id
    resource.with_provider_connection do |connection|
      connection.api.credentials.find(manager_ref).update_attributes!(params)
    end
    self.class.send('refresh', resource)
    reload!
  end

  def update_in_provider_queue(params)
    self.class.send('queue', resource.my_zone, id, "update_in_provider", [params], "Updating #{self.class.name}")
  end

  def delete_in_provider
    params.delete(:task_id) # in case this is being called through update_in_provider_queue which will stick in a :task_id
    resource.with_provider_connection do |connection|
      connection.api.credentials.find(manager_ref).destroy!
    end
    self.class.send('refresh', resource)
  end

  def delete_in_provider_queue
    self.class.send('queue', resource.my_zone, id, "delete_in_provider", [], "Deleting #{self.class.name}")
  end
end
{'fraction_non_alphanumeric': 0.09666403992645127, 'fraction_numerical': 0.0, 'mean_word_length': 3.2979683972911964, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5531329', 'n_tokens_mistral': 1163, 'n_tokens_neox': 1136, 'n_words': 247}