Class: ForemanSalt::SmartProxies::SaltKeys

Inherits:
Object
  • Object
show all
Defined in:
app/services/foreman_salt/smart_proxies/salt_keys.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ SaltKeys

Returns a new instance of SaltKeys.



6
7
8
# File 'app/services/foreman_salt/smart_proxies/salt_keys.rb', line 6

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

Instance Attribute Details

#fingerprintObject (readonly)

Returns the value of attribute fingerprint.



4
5
6
# File 'app/services/foreman_salt/smart_proxies/salt_keys.rb', line 4

def fingerprint
  @fingerprint
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'app/services/foreman_salt/smart_proxies/salt_keys.rb', line 4

def name
  @name
end

#smart_proxy_idObject (readonly)

Returns the value of attribute smart_proxy_id.



4
5
6
# File 'app/services/foreman_salt/smart_proxies/salt_keys.rb', line 4

def smart_proxy_id
  @smart_proxy_id
end

#stateObject (readonly)

Returns the value of attribute state.



4
5
6
# File 'app/services/foreman_salt/smart_proxies/salt_keys.rb', line 4

def state
  @state
end

Class Method Details

.all(proxy) ⇒ Object

Raises:

  • (::Foreman::Exception)


11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/services/foreman_salt/smart_proxies/salt_keys.rb', line 11

def all(proxy)
  raise ::Foreman::Exception, 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

.find(proxy, name) ⇒ Object



25
26
27
# File 'app/services/foreman_salt/smart_proxies/salt_keys.rb', line 25

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

.find_by_state(proxy, state) ⇒ Object



29
30
31
# File 'app/services/foreman_salt/smart_proxies/salt_keys.rb', line 29

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

Instance Method Details

#<=>(other) ⇒ Object



60
61
62
# File 'app/services/foreman_salt/smart_proxies/salt_keys.rb', line 60

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

#acceptObject



34
35
36
37
38
# File 'app/services/foreman_salt/smart_proxies/salt_keys.rb', line 34

def accept
  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

#deleteObject



46
47
48
49
50
# File 'app/services/foreman_salt/smart_proxies/salt_keys.rb', line 46

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

#rejectObject



40
41
42
43
44
# File 'app/services/foreman_salt/smart_proxies/salt_keys.rb', line 40

def reject
  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

#to_paramObject



52
53
54
# File 'app/services/foreman_salt/smart_proxies/salt_keys.rb', line 52

def to_param
  name
end

#to_sObject



56
57
58
# File 'app/services/foreman_salt/smart_proxies/salt_keys.rb', line 56

def to_s
  name
end