Class: Puppet::Util::NetworkDevice::Cisco::Interface

Inherits:
Object
  • Object
show all
Extended by:
IPCalc
Includes:
IPCalc
Defined in:
lib/puppet/util/network_device/cisco/interface.rb

Overview

this manages setting properties to an interface in a cisco switch or router

Constant Summary collapse

COMMANDS =
{
  # property     => order, ios command/block/array
  :description   => [1, "description %s"],
  :speed         => [2, "speed %s"],
  :duplex        => [3, "duplex %s"],
  :encapsulation => [4, "switchport trunk encapsulation %s"],
  :mode          => [5, "switchport mode %s"],
  :access_vlan   => [6, "switchport access vlan %s"],
  :native_vlan   => [7, "switchport trunk native vlan %s"],
  :allowed_trunk_vlans => [8, "switchport trunk allowed vlan %s"],
  :etherchannel  => [9, ["channel-group %s", "port group %s"]],
  :ipaddress     => [10,
    lambda do |prefix,ip,option|
      ip.ipv6? ? "ipv6 address #{ip.to_s}/#{prefix} #{option}" :
                 "ip address #{ip.to_s} #{netmask(Socket::AF_INET,prefix)}"
    end],
  :ensure        => [11, lambda { |value| value == :present ? "no shutdown" : "shutdown" } ]
}

Constants included from IPCalc

IPCalc::IP, IPCalc::IPv4, IPCalc::IPv6_full, IPCalc::IPv6_partial, IPCalc::Octet

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from IPCalc

bits, fullmask, linklocal?, mask, netmask, parse, prefix_length, wildmask

Constructor Details

#initialize(name, transport) ⇒ Interface

Returns a new instance of Interface.



12
13
14
15
# File 'lib/puppet/util/network_device/cisco/interface.rb', line 12

def initialize(name, transport)
  @name = name
  @transport = transport
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/puppet/util/network_device/cisco/interface.rb', line 10

def name
  @name
end

#transportObject (readonly)

Returns the value of attribute transport.



10
11
12
# File 'lib/puppet/util/network_device/cisco/interface.rb', line 10

def transport
  @transport
end

Instance Method Details

#command(command) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/puppet/util/network_device/cisco/interface.rb', line 85

def command(command)
  transport.command(command) do |out|
    if out =~ /^%/mo or out =~ /^Command rejected:/mo
      # strip off the command just sent
      error = out.sub(command,'')
      Puppet.err _("Error while executing '%{command}', device returned: %{error}") % { command: command, error: error }
    end
  end
end

#execute(property, value, prefix = '') ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/puppet/util/network_device/cisco/interface.rb', line 67

def execute(property, value, prefix='')
  case COMMANDS[property][1]
  when Array
    COMMANDS[property][1].each do |command|
      transport.command(prefix + command % value) do |out|
        break unless out =~ /^%/
      end
    end
  when String
    command(prefix + COMMANDS[property][1] % value)
  when Proc
    value = [value] unless value.is_a?(Array)
    value.each do |v|
      command(prefix + COMMANDS[property][1].call(*v))
    end
  end
end

#update(is = {}, should = {}) ⇒ Object



36
37
38
39
40
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
# File 'lib/puppet/util/network_device/cisco/interface.rb', line 36

def update(is={}, should={})
  Puppet.debug("Updating interface #{name}")
  command("conf t")
  command("interface #{name}")

  # apply changes in a defined order for cisco IOS devices
  [is.keys, should.keys].flatten.uniq.sort {|a,b| COMMANDS[a][0] <=> COMMANDS[b][0] }.each do |property|
    # Work around for old documentation which shows :native_vlan used for access vlan
    if property == :access_vlan and should[:mode] != :trunk and should[:access_vlan].nil?
      should[:access_vlan] = should[:native_vlan]
    end

    Puppet.debug("comparing #{property}: #{is[property]} == #{should[property]}")

    # They're equal, so do nothing.
    next if is[property] == should[property]

    # We're deleting it
    if should[property] == :absent or should[property].nil?
      execute(property, is[property], "no ")
      next
    end

    # We're replacing an existing value or creating a new one
    execute(property, should[property])
  end

  command("exit")
  command("exit")
end