Class: Puppet::Provider::TacacsServer::CiscoNexus

Inherits:
ResourceApi::SimpleProvider
  • Object
show all
Defined in:
lib/puppet/provider/tacacs_server/cisco_nexus.rb

Overview

Implementation for the tacacs_server type using the Resource API.

Instance Method Summary collapse

Instance Method Details

#canonicalize(_context, resources) ⇒ Object



18
19
20
# File 'lib/puppet/provider/tacacs_server/cisco_nexus.rb', line 18

def canonicalize(_context, resources)
  resources
end

#create(context, name, should) ⇒ Object



64
65
66
67
68
69
# File 'lib/puppet/provider/tacacs_server/cisco_nexus.rb', line 64

def create(context, name, should)
  context.notice("Creating '#{name}' with #{should.inspect}")
  # need to create a new host if it does not exist
  Cisco::TacacsServerHost.new(name)
  handle_update(name, should)
end

#delete(context, name) ⇒ Object



53
54
55
56
57
# File 'lib/puppet/provider/tacacs_server/cisco_nexus.rb', line 53

def delete(context, name)
  context.notice("Destroying '#{name}'")
  @tacacs_server = Cisco::TacacsServerHost.hosts
  @tacacs_server[name].destroy if @tacacs_server[name]
end

#get(_context, hosts = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/puppet/provider/tacacs_server/cisco_nexus.rb', line 22

def get(_context, hosts=nil)
  require 'cisco_node_utils'
  current_states = []
  # using = instead of ||= due to creation behaviour and having
  # to reevaluate the hosts upon creation
  @tacacs_server = Cisco::TacacsServerHost.hosts
  if hosts.nil? || hosts.empty?
    @tacacs_server.each do |host, instance|
      current_states << get_current_state(host, instance)
    end
  else
    hosts.each do |host|
      individual_host = @tacacs_server[host]
      next if individual_host.nil?
      current_states << get_current_state(host, individual_host)
    end
  end
  current_states
end

#get_current_state(host, instance) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/puppet/provider/tacacs_server/cisco_nexus.rb', line 42

def get_current_state(host, instance)
  {
    ensure:     'present',
    name:       host,
    port:       instance.port,
    timeout:    instance.timeout,
    key_format: instance.encryption_type,
    key:        instance.encryption_password.gsub(/\A"|"\Z/, ''),
  }
end

#handle_update(name, should) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/puppet/provider/tacacs_server/cisco_nexus.rb', line 71

def handle_update(name, should)
  @tacacs_server = Cisco::TacacsServerHost.hosts
  [:port, :timeout].each do |property|
    next unless should[property]
    @tacacs_server[name].send("#{property}=", should[property]) if @tacacs_server[name].respond_to?("#{property}=")
  end
  @tacacs_server[name].encryption_key_set(munge(should[:key_format]), munge(should[:key])) if should[:key]
end

#munge(value) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/puppet/provider/tacacs_server/cisco_nexus.rb', line 80

def munge(value)
  if value.is_a?(String) && value.eql?('unset')
    nil
  elsif value.is_a?(Integer) && value.eql?(-1)
    nil
  else
    value
  end
end

#update(context, name, should) ⇒ Object



59
60
61
62
# File 'lib/puppet/provider/tacacs_server/cisco_nexus.rb', line 59

def update(context, name, should)
  context.notice("Updating '#{name}' with #{should.inspect}")
  handle_update(name, should)
end