Class: Puppet::Provider::NetworkTrunk::CiscoIos

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

Overview

Network Trunk Puppet Provider for Cisco IOS devices

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.commands_from_instance(property_hash) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/puppet/provider/network_trunk/cisco_ios.rb', line 35

def self.commands_from_instance(property_hash)
  commands_array = []
  ensure_command = if PuppetX::CiscoIOS::Utility.attribute_safe_to_run(commands_hash, 'ensure')
                     PuppetX::CiscoIOS::Utility.attribute_value_foraged_from_command_hash(commands_hash, 'ensure', 'set_value')
                   else
                     ''
                   end
  if property_hash[:ensure] == 'absent'
    # delete with a 'no'
    ensure_command = PuppetX::CiscoIOS::Utility.insert_attribute_into_command_line(ensure_command, 'state', 'no', false)
    commands_array.push(ensure_command) if ensure_command != ''
  else
    ensure_command = PuppetX::CiscoIOS::Utility.insert_attribute_into_command_line(ensure_command, 'state', '', false)
    commands_array.push(ensure_command.strip) if ensure_command != ''
    if property_hash[:mode]
      property_hash[:mode] = PuppetX::CiscoIOS::Utility.convert_network_trunk_mode_modelled(property_hash[:mode])
    end
    commands_array += PuppetX::CiscoIOS::Utility.build_commmands_from_attribute_set_values(property_hash, commands_hash)
  end
  commands_array
end

.commands_hashObject



10
11
12
# File 'lib/puppet/provider/network_trunk/cisco_ios.rb', line 10

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

.instance_from_cli(output) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/puppet/provider/network_trunk/cisco_ios.rb', line 22

def self.instance_from_cli(output)
  new_instance = PuppetX::CiscoIOS::Utility.parse_resource(output, commands_hash)
  new_instance[:name] = PuppetX::CiscoIOS::Utility.shorthand_to_full(new_instance[:name])
  new_instance[:mode] = PuppetX::CiscoIOS::Utility.convert_network_trunk_mode_cli(new_instance[:mode])
  new_instance.delete_if { |_k, v| v.nil? }
  new_instance[:ensure] = if new_instance[:ensure] || new_instance.size > 1
                            'present'
                          else
                            'absent'
                          end
  new_instance
end

.interface_names_from_cli(name_output) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/puppet/provider/network_trunk/cisco_ios.rb', line 14

def self.interface_names_from_cli(name_output)
  interface_names = []
  name_output.scan(%r{#{commands_hash['get_interfaces_get_value']['default']}}).each do |interface_name|
    interface_names << interface_name.first
  end
  interface_names
end

Instance Method Details

#canonicalize(_context, resources) ⇒ Object



88
89
90
# File 'lib/puppet/provider/network_trunk/cisco_ios.rb', line 88

def canonicalize(_context, resources)
  resources
end

#commands_hashObject



57
58
59
# File 'lib/puppet/provider/network_trunk/cisco_ios.rb', line 57

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

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



77
78
79
# File 'lib/puppet/provider/network_trunk/cisco_ios.rb', line 77

def create(context, name, should)
  context.transport.run_command_interface_mode(name, Puppet::Provider::NetworkTrunk::CiscoIos.commands_from_instance(should).join("\n"))
end

#delete(context, name) ⇒ Object



83
84
85
86
# File 'lib/puppet/provider/network_trunk/cisco_ios.rb', line 83

def delete(context, name)
  delete_hash = { name: name, ensure: 'absent' }
  context.transport.run_command_interface_mode(name, Puppet::Provider::NetworkTrunk::CiscoIos.commands_from_instance(delete_hash).join("\n"))
end

#get(context, _names = nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/puppet/provider/network_trunk/cisco_ios.rb', line 61

def get(context, _names = nil)
  output = context.transport.run_command_enable_mode(PuppetX::CiscoIOS::Utility.get_values(commands_hash))
  # convert the output to an array, breaking at `Name:`
  output_array = output.split("\n").slice_before(%r{Name:(.*)}).to_a

  return_instances = []
  # drop the first item in the array which is the command...
  output_array.drop(1).each do |interface|
    interface_output = interface.join("\n")
    unless interface_output.include? ' is not a switchable port'
      return_instances << Puppet::Provider::NetworkTrunk::CiscoIos.instance_from_cli(interface_output)
    end
  end
  PuppetX::CiscoIOS::Utility.enforce_simple_types(context, return_instances)
end