Class: Tinet::Node

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

Defined Under Namespace

Classes: Interfase

Constant Summary collapse

TYPES =
%w(docker netns).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, image, build, interfaces, cmds) ⇒ Node

Returns a new instance of Node.

Parameters:

  • name (String)
  • type (Symbol)
  • image (String, nil)
  • build (String, nil)
  • interfaces (Array<Tinet::Node::Interfase>)
  • cmds (Array<String>)


26
27
28
29
30
31
32
33
34
# File 'lib/tinet/node.rb', line 26

def initialize(name, type, image, build, interfaces, cmds)
  @name = name
  @type = type
  @image = image
  @build = build
  @interfaces = interfaces
  @cmds = cmds
  interfaces.each { |interface| interface.node = self }
end

Instance Attribute Details

#buildObject (readonly)

Returns the value of attribute build.



18
19
20
# File 'lib/tinet/node.rb', line 18

def build
  @build
end

#cmdsObject (readonly)

Returns the value of attribute cmds.



18
19
20
# File 'lib/tinet/node.rb', line 18

def cmds
  @cmds
end

#imageObject (readonly)

Returns the value of attribute image.



18
19
20
# File 'lib/tinet/node.rb', line 18

def image
  @image
end

#interfacesObject (readonly)

Returns the value of attribute interfaces.



18
19
20
# File 'lib/tinet/node.rb', line 18

def interfaces
  @interfaces
end

#nameObject (readonly)

Returns the value of attribute name.



18
19
20
# File 'lib/tinet/node.rb', line 18

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



18
19
20
# File 'lib/tinet/node.rb', line 18

def type
  @type
end

Class Method Details

.parse(node_hash) ⇒ Tinet::Node

Parameters:

  • node_hash (Hash)

Returns:

Raises:



7
8
9
10
11
12
13
14
15
16
# File 'lib/tinet/node.rb', line 7

def self.parse(node_hash)
  name, type, interfaces = node_hash['name'], node_hash['type'], node_hash['interfaces']
  raise InvalidYAMLError, "Node name is missing" if name.nil? || name.empty?
  raise InvalidTypeError, "Unknown node type: #{type}" unless TYPES.include?(type)
  raise InvalidYAMLError, "Node interfaces must be array" unless interfaces.is_a?(Array)

  interfaces = interfaces.map { |interface| Interfase.parse(interface) }
  cmds = node_hash.fetch('cmds', []).map { |cmd| cmd['cmd'] || cmd }
  self.new(name, type.to_sym, node_hash['image'], node_hash['build'], interfaces, cmds)
end