Sha256: 2ef785392c7f1049c381d2a6c84be295d124a3661bce4838c26da39db8db9368

Contents?: true

Size: 1.88 KB

Versions: 15

Compression:

Stored size: 1.88 KB

Contents

module ForemanSalt
  class SmartProxies::SaltKeys
    attr_reader :name, :state, :fingerprint, :smart_proxy_id

    def initialize(opts)
      @name, @state, @fingerprint, @smart_proxy_id = opts.flatten
    end

    class << self
      def all(proxy)
        fail ::Foreman::Exception.new(N_('Must specify a Smart Proxy to use')) if proxy.nil?

        unless (keys = Rails.cache.read("saltkeys_#{proxy.id}"))
          api = ProxyAPI::Salt.new(:url => proxy.url)
          keys = api.key_list.map do |name, properties|
            new([name.strip, properties['state'], properties['fingerprint'], proxy.id])
          end.compact

          Rails.cache.write("saltkeys_#{proxy.id}", keys, :expires_in => 1.minute) if Rails.env.production?
        end
        keys
      end

      def find(proxy, name)
        all(proxy).find { |c| c.name == name }
      end

      def find_by_state(proxy, state)
        all(proxy).select { |c| c.state == state }
      end
    end

    def accept
      fail ::Foreman::Exception.new(N_('unable to re-accept an accepted key')) unless state == 'unaccepted'
      proxy = SmartProxy.find(smart_proxy_id)
      Rails.cache.delete("saltkeys_#{proxy.id}") if Rails.env.production?
      ProxyAPI::Salt.new(:url => proxy.url).key_accept name
    end

    def reject
      fail ::Foreman::Exception.new(N_('unable to reject an accepted key')) unless state == 'unaccepted'
      proxy = SmartProxy.find(smart_proxy_id)
      Rails.cache.delete("saltkeys_#{proxy.id}") if Rails.env.production?
      ProxyAPI::Salt.new(:url => proxy.url).key_reject name
    end

    def delete
      proxy = SmartProxy.find(smart_proxy_id)
      Rails.cache.delete("saltkeys_#{proxy.id}") if Rails.env.production?
      ProxyAPI::Salt.new(:url => proxy.url).key_delete name
    end

    def to_param
      name
    end

    def to_s
      name
    end

    def <=>(other)
      name <=> other.name
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
foreman_salt-9.0.0 app/services/foreman_salt/smart_proxies/salt_keys.rb
foreman_salt-8.0.3 app/services/foreman_salt/smart_proxies/salt_keys.rb
foreman_salt-8.0.2 app/services/foreman_salt/smart_proxies/salt_keys.rb
foreman_salt-8.0.1 app/services/foreman_salt/smart_proxies/salt_keys.rb
foreman_salt-7.0.3 app/services/foreman_salt/smart_proxies/salt_keys.rb
foreman_salt-8.0.0 app/services/foreman_salt/smart_proxies/salt_keys.rb
foreman_salt-7.0.2 app/services/foreman_salt/smart_proxies/salt_keys.rb
foreman_salt-6.0.1 app/services/foreman_salt/smart_proxies/salt_keys.rb
foreman_salt-7.0.1 app/services/foreman_salt/smart_proxies/salt_keys.rb
foreman_salt-7.0.0 app/services/foreman_salt/smart_proxies/salt_keys.rb
foreman_salt-6.0.0 app/services/foreman_salt/smart_proxies/salt_keys.rb
foreman_salt-5.0.1 app/services/foreman_salt/smart_proxies/salt_keys.rb
foreman_salt-5.0.0 app/services/foreman_salt/smart_proxies/salt_keys.rb
foreman_salt-4.0.1 app/services/foreman_salt/smart_proxies/salt_keys.rb
foreman_salt-4.0.0 app/services/foreman_salt/smart_proxies/salt_keys.rb