Class: Puppet::Provider::NtpServer::CiscoIos

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/provider/ntp_server/cisco_ios.rb

Overview

NTP Server Puppet Provider for Cisco IOS devices

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.commands_from_instance(property_hash) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/puppet/provider/ntp_server/cisco_ios.rb', line 26

def self.commands_from_instance(property_hash)
  commands_array = []
  command = PuppetX::CiscoIOS::Utility.set_values(property_hash, commands_hash)
  # special adjustments
  command = command.to_s.gsub(%r{name }, '')
  command = command.to_s.gsub(%r{source_interface}, 'source')
  command = command.to_s.gsub(%r{prefer true}, 'prefer')
  commands_array.push(command)
  commands_array
end

.commands_hashObject



9
10
11
# File 'lib/puppet/provider/ntp_server/cisco_ios.rb', line 9

def self.commands_hash
  @commands_hash ||= PuppetX::CiscoIOS::Utility.load_yaml(File.expand_path(__dir__) + '/command.yaml')
end

.instances_from_cli(output) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/puppet/provider/ntp_server/cisco_ios.rb', line 13

def self.instances_from_cli(output)
  new_instance_fields = []
  output.scan(%r{#{PuppetX::CiscoIOS::Utility.get_instances(commands_hash)}}).each do |raw_instance_fields|
    new_instance = PuppetX::CiscoIOS::Utility.parse_resource(raw_instance_fields, commands_hash)
    new_instance[:ensure] = 'present'
    new_instance[:prefer] = !new_instance[:prefer].nil? # true if the keyword exists
    new_instance.delete_if { |_k, v| v.nil? }

    new_instance_fields << new_instance
  end
  new_instance_fields
end

Instance Method Details

#canonicalize(_context, resources) ⇒ Object



96
97
98
# File 'lib/puppet/provider/ntp_server/cisco_ios.rb', line 96

def canonicalize(_context, resources)
  resources
end

#commands_hashObject



37
38
39
# File 'lib/puppet/provider/ntp_server/cisco_ios.rb', line 37

def commands_hash
  Puppet::Provider::NtpServer::CiscoIos.commands_hash
end

#delete(context, name, should) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/puppet/provider/ntp_server/cisco_ios.rb', line 80

def delete(context, name, should)
  clear_hash = { name: name, ensure: 'absent', vrf: should[:vrf] }
  array_of_commands_to_run = Puppet::Provider::NtpServer::CiscoIos.commands_from_instance(clear_hash)
  array_of_commands_to_run.each do |command|
    context.transport.run_command_conf_t_mode(command)
  end
end

#get(context, _names = nil) ⇒ Object



73
74
75
76
77
78
# File 'lib/puppet/provider/ntp_server/cisco_ios.rb', line 73

def get(context, _names = nil)
  output = context.transport.run_command_enable_mode(PuppetX::CiscoIOS::Utility.get_values(commands_hash))
  return [] if output.nil?
  return_value = Puppet::Provider::NtpServer::CiscoIos.instances_from_cli(output)
  PuppetX::CiscoIOS::Utility.enforce_simple_types(context, return_value)
end

#set(context, changes) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/puppet/provider/ntp_server/cisco_ios.rb', line 41

def set(context, changes)
  changes.each do |name, change|
    is = if context.type.feature?('simple_get_filter')
           change.key?(:is) ? change[:is] : (get(context, [name]) || []).find { |r| r[:name] == name }
         else
           change.key?(:is) ? change[:is] : (get(context) || []).find { |r| r[:name] == name }
         end
    context.type.check_schema(is) unless change.key?(:is)

    should = change[:should]

    raise 'SimpleProvider cannot be used with a Type that is not ensurable' unless context.type.ensurable?

    is = SimpleProvider.create_absent(:name, name) if is.nil?
    should = { name: name, ensure: 'absent', vrf: should[:vrf] } if should.nil?

    if is[:ensure].to_s == 'absent' && should[:ensure].to_s == 'present'
      context.creating(name) do
        create(context, name, should)
      end
    elsif is[:ensure].to_s == 'present' && should[:ensure].to_s == 'present'
      context.updating(name) do
        update(context, name, should)
      end
    elsif is[:ensure].to_s == 'present' && should[:ensure].to_s == 'absent'
      context.deleting(name) do
        delete(context, name, should)
      end
    end
  end
end

#update(context, _name, should) ⇒ Object Also known as: create



88
89
90
91
92
93
# File 'lib/puppet/provider/ntp_server/cisco_ios.rb', line 88

def update(context, _name, should)
  array_of_commands_to_run = Puppet::Provider::NtpServer::CiscoIos.commands_from_instance(should)
  array_of_commands_to_run.each do |command|
    context.transport.run_command_conf_t_mode(command)
  end
end