module ForemanSalt::Concerns::HostManagedExtensions

def configuration_with_salt?

def configuration_with_salt?
  configuration_without_salt? || !!salt_proxy
end

def handle_salt

def handle_salt
  return true unless salt?
  set_salt_autosign
end

def params_with_salt_proxy

def params_with_salt_proxy
  params = params_without_salt_proxy
  params['salt_master'] = salt_master unless salt_master.blank?
  params
end

def salt_master

def salt_master
  salt_proxy.to_s
end

def salt_modules_for_enc

def salt_modules_for_enc
  return [] unless salt_environment
  modules = salt_modules + (hostgroup ? hostgroup.all_salt_modules : [])
  ForemanSalt::SaltModule.in_environment(salt_environment).where(:id => modules).pluck("salt_modules.name").uniq
end

def salt_modules_in_host_environment

def salt_modules_in_host_environment
  return unless self.salt_modules.any?
  if self.salt_environment
    errors.add(:base, _('Salt states must be in the environment of the host')) unless (self.salt_modules - self.salt_environment.salt_modules).empty?
  else
    errors.add(:base, _('Host must have an environment in order to set salt states'))
  end
end

def saltrun!

def saltrun!
  unless salt_proxy.present?
    errors.add(:base, _("No Salt master defined - can't continue"))
    logger.warn 'Unable to execute salt run, no salt proxies defined'
    return false
  end
  ProxyAPI::Salt.new(:url => salt_proxy.url).highstate name
rescue => e
  errors.add(:base, _('Failed to execute state.highstate: %s') % e)
  false
end

def set_hostgroup_defaults_with_salt_proxy

def set_hostgroup_defaults_with_salt_proxy
  return unless hostgroup
  assign_hostgroup_attributes(%w(salt_proxy_id salt_environment_id))
  set_hostgroup_defaults_without_salt_proxy
end

def smart_proxy_ids_with_salt_proxy

def smart_proxy_ids_with_salt_proxy
  ids = smart_proxy_ids_without_salt_proxy
  [salt_proxy, hostgroup.try(:salt_proxy)].compact.each do |proxy|
    ids << proxy.id
  end
  ids
end