Class: Puppet::Provider::NetworkVlan::CiscoIos

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

Overview

Network Vlan Puppet Provider for Cisco IOS devices

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.commands_hashObject



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

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

.create_commands_from_instance(instance) ⇒ Object



38
39
40
# File 'lib/puppet/provider/network_vlan/cisco_ios.rb', line 38

def self.create_commands_from_instance(instance)
  [PuppetX::CiscoIOS::Utility.set_values(instance, commands_hash)]
end

.instances_from_cli(output) ⇒ Object



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

def self.instances_from_cli(output)
  new_instance_fields = []
  get_values = PuppetX::CiscoIOS::Utility.get_values(commands_hash)
  header_rows = PuppetX::CiscoIOS::Utility.value_foraged_from_command_hash(commands_hash, 'header_rows')
  output = output.sub(%r{(#{get_values}\n\n)#{header_rows}}, '')
  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.first, commands_hash)
    new_instance[:ensure] = 'present'
    # convert cli values to puppet values
    new_instance[:shutdown] = !new_instance[:shutdown].nil?
    new_instance.delete_if { |_k, v| v.nil? }
    new_instance_fields << new_instance
  end
  new_instance_fields
end

.update_commands_from_instance(instance) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/puppet/provider/network_vlan/cisco_ios.rb', line 42

def self.update_commands_from_instance(instance)
  array_of_commands = []
  # if we create / delete only send a single command
  if instance[:ensure] == 'absent'
    array_of_commands.push(PuppetX::CiscoIOS::Utility.set_values(instance, commands_hash))
  else
    instance[:shutdown] = if !instance[:shutdown]
                            'no'
                          else
                            ''
                          end
    array_of_commands = PuppetX::CiscoIOS::Utility.build_commmands_from_attribute_set_values(instance, commands_hash)
  end
  array_of_commands
end

.validate_vlan(id, action_verb) ⇒ Object



29
30
31
32
# File 'lib/puppet/provider/network_vlan/cisco_ios.rb', line 29

def self.validate_vlan(id, action_verb)
  return unless ['1', '1002', '1003', '1004', '1005'].include?(id)
  raise "VLAN #{id} is a Cisco default VLAN and may not be #{action_verb}."
end

Instance Method Details

#canonicalize(_context, resources) ⇒ Object



93
94
95
# File 'lib/puppet/provider/network_vlan/cisco_ios.rb', line 93

def canonicalize(_context, resources)
  resources
end

#commands_hashObject



58
59
60
# File 'lib/puppet/provider/network_vlan/cisco_ios.rb', line 58

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

#create(context, name, should) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/puppet/provider/network_vlan/cisco_ios.rb', line 69

def create(context, name, should)
  validate_vlan(name, 'created')
  create_hash = { id: name, ensure: 'present' }
  self.class.create_commands_from_instance(create_hash).each do |command|
    context.transport.run_command_conf_t_mode(command)
  end
  update(context, name, should)
end

#delete(context, name) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/puppet/provider/network_vlan/cisco_ios.rb', line 85

def delete(context, name)
  validate_vlan(name, 'deleted')
  delete_hash = { id: name, ensure: 'absent' }
  self.class.update_commands_from_instance(delete_hash).each do |command|
    context.transport.run_command_conf_t_mode(command)
  end
end

#get(context, _names = nil) ⇒ Object



62
63
64
65
66
67
# File 'lib/puppet/provider/network_vlan/cisco_ios.rb', line 62

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 = self.class.instances_from_cli(output)
  PuppetX::CiscoIOS::Utility.enforce_simple_types(context, return_value)
end

#update(context, name, should) ⇒ Object



78
79
80
81
82
83
# File 'lib/puppet/provider/network_vlan/cisco_ios.rb', line 78

def update(context, name, should)
  validate_vlan(name, 'updated')
  self.class.update_commands_from_instance(should).each do |command|
    context.transport.run_command_vlan_mode(name, command)
  end
end

#validate_vlan(id, action_verb) ⇒ Object



34
35
36
# File 'lib/puppet/provider/network_vlan/cisco_ios.rb', line 34

def validate_vlan(id, action_verb)
  self.class.validate_vlan(id, action_verb)
end