Class: NetConfGen::BlockContext

Inherits:
Object
  • Object
show all
Defined in:
lib/netconfgen/netconfgen.rb

Instance Method Summary collapse

Constructor Details

#initialize(blockengine, settings = nil) ⇒ BlockContext

Returns a new instance of BlockContext.



33
34
35
36
# File 'lib/netconfgen/netconfgen.rb', line 33

def initialize(blockengine, settings=nil)
  @blockengine = blockengine
  @settings = settings
end

Instance Method Details

#include(name) ⇒ Object



38
39
40
41
# File 'lib/netconfgen/netconfgen.rb', line 38

def include(name)
  block = @blockengine.load(name)
  return block.render
end

#megaexcel(name) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/netconfgen/netconfgen.rb', line 91

def megaexcel(name)
  url = @settings["megaexcel"]["url"]
  name_field = @settings["megaexcel"]["name_field"]
  column_definition_row_number = @settings["megaexcel"]["column_definition_row_number"]

  ret = Net::HTTP.get(URI.parse(url))
  data = JSON.parse(ret)

  row = megaexcel_find_row_by_column_value(data, column_definition_row_number, name_field, name)
  pp row
end

#megaexcel_find_row_by_column_value(data, column_definition_row_number, column_name, column_value) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/netconfgen/netconfgen.rb', line 66

def megaexcel_find_row_by_column_value(data, column_definition_row_number, column_name, column_value)

  # The 3rd row is known to be containing the column names
  columns = data["values"][column_definition_row_number]

  # Set i to the INDEX of the column which we are searching
  i = columns.index(column_name)
  if i == nil
    raise "Column #{column_name} not found from megaexcel"
  end

  # Find the row where the INDEX column contains the value we are looking for
  row = data["values"].find do |x|
    x[i] == column_value
  end

  # Convert the indexed row into an object which keys are the column names
  obj = {}
  columns.each_with_index do |key, j|
    obj[key] = row[j]
  end

  return obj
end

#megaexcel_vlans(data) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/netconfgen/netconfgen.rb', line 103

def megaexcel_vlans(data)
  vlans_column_name = @settings["megaexcel"]["vlans_column_name"]
  str = data[vlans_column_name]
  vlans = []
  str.each_line do |line|
    if m = line.match(/((?:Fa|Gi|Te)[^ ]+).+?vlan\s?(\d+)/)
      vlans << {
        "ports" => m[1],
        "vlan" => m[2],
        "description" => line.chomp.strip,
      }
    else
      # Warning. unknown vlan setup #{line}
    end
  end
  return vlans
end

#portrange(portrange) ⇒ Object

Creates an array of individual ports from a port range string



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/netconfgen/netconfgen.rb', line 44

def portrange(portrange)
  if m = portrange.match(/((?:Gi|Fa|Te)(?:[0-9]+\/)+)(\d+)(?:-(\d+))?/) # eg. Gi1/0/27-28 or Gi1/0/27
    ports = []
    if m[3]
      (m[2]..m[3]).each do |i|
        ports << (m[1] + i)
      end
    else
      ports << (m[1] + m[2])
    end

    return ports
  elsif m = portrange.match(/(Gi|Fa|Te)\[(\d)(\d)\](\/\d+)/) # eg. Gi[34]/1
    ports = []
    (m[2]..m[3]).each do |i|
      ports << (m[1] + i + m[4])
    end
    return ports
  end
  return []
end