Class: Construqt::Addresses::Address

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

Defined Under Namespace

Classes: Route, TagRoute

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAddress

Returns a new instance of Address.



96
97
98
99
100
101
102
103
104
# File 'lib/construqt/addresses.rb', line 96

def initialize()
  self.ips = []
  self.host = nil
  self.interface = nil
  @routes = []
  self.tags = []
  @loopback = @dhcpv4 = @dhcpv6 = false
  @name = nil
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



80
81
82
# File 'lib/construqt/addresses.rb', line 80

def host
  @host
end

#interfaceObject

Returns the value of attribute interface.



81
82
83
# File 'lib/construqt/addresses.rb', line 81

def interface
  @interface
end

#ipsObject

Returns the value of attribute ips.



82
83
84
# File 'lib/construqt/addresses.rb', line 82

def ips
  @ips
end

#tagsObject

Returns the value of attribute tags.



83
84
85
# File 'lib/construqt/addresses.rb', line 83

def tags
  @tags
end

Instance Method Details

#add_ip(ip, region = "") ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/construqt/addresses.rb', line 161

def add_ip(ip, region = "")
  throw "please give a ip #{ip}" if ip.nil?
  if ip
    #puts ">>>>> #{ip} #{ip.class.name}"
    if DHCPV4 == ip
      @dhcpv4 = true
    elsif DHCPV6 == ip
      @dhcpv6 = true
    elsif LOOOPBACK == ip
      @loopback = true
    else
      throw "please give a ip #{ip} as string!" unless ip.kind_of?(String)
      (unused, ip) = self.merge_tag(ip) { |ip| CqIpAddress.new(IPAddress.parse(ip), self) }
      self.ips << ip
    end
  end

  self
end

#add_route(dst, via, option = {}) ⇒ Object



272
273
274
275
# File 'lib/construqt/addresses.rb', line 272

def add_route(dst, via, option = {})
  @routes << build_route(dst, via, option)
  self
end

#add_route_from_tags(dst_tags, src_tags, options = {}) ⇒ Object



221
222
223
224
# File 'lib/construqt/addresses.rb', line 221

def add_route_from_tags(dst_tags, src_tags, options = {})
  @routes << TagRoute.new(dst_tags, src_tags, options)
  self
end

#add_routes(addr_s, via, options = {}) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/construqt/addresses.rb', line 226

def add_routes(addr_s, via, options = {})
  addrs = addr_s.kind_of?(Array) ? addr_s : [addr_s]
  addrs.each do |addr|
    if addr.respond_to? :ips
      ips = addr.ips if addr.respond_to? :ips
    else
      ips = [addr]
    end
    ips.each do |i|
      add_route(i.to_string, via, options)
    end
  end
  self
end

#build_route(dst, via, option = {}) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/construqt/addresses.rb', line 252

def build_route(dst, via, option = {})
  #puts "DST => "+dst.class.name+":"+dst.to_s
  (unused, dst) = self.merge_tag(dst) { |dst| CqIpAddress.new(IPAddress.parse(dst), self) }
  metric = option['metric']
  if via == UNREACHABLE
    via = nil
    type = 'unreachable'
  else
    if via.nil?
      via = nil
    else
      (unused, via) = self.merge_tag(via) { |via| CqIpAddress.new(IPAddress.parse(via), self) }
      throw "different type #{dst} #{via}" unless dst.ipv4? == via.ipv4? && dst.ipv6? == via.ipv6?
    end
    type = nil
  end
  Route.new(dst, via, type, metric, option["routing-table"])
end

#dhcpv4?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/construqt/addresses.rb', line 84

def dhcpv4?
  @dhcpv4
end

#dhcpv6?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/construqt/addresses.rb', line 88

def dhcpv6?
  @dhcpv6
end

#first_ipv4Object



122
123
124
# File 'lib/construqt/addresses.rb', line 122

def first_ipv4
  v4s.first
end

#first_ipv6Object



126
127
128
# File 'lib/construqt/addresses.rb', line 126

def first_ipv6
  v6s.first
end

#loopback?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/construqt/addresses.rb', line 92

def loopback?
  @loopback
end

#match_network(ip) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/construqt/addresses.rb', line 106

def match_network(ip)
  if ip.ipv4?
    self.v4s.find{|nip| nip.include?(ip) }
  else
    self.v6s.find{|nip| nip.include?(ip) }
  end
end

#merge_tag(name, &block) ⇒ Object



130
131
132
# File 'lib/construqt/addresses.rb', line 130

def merge_tag(name, &block)
  Construqt::Tags.add(([name]+self.tags).join("#")) { |name| block.call(name) }
end

#nameObject



148
149
150
151
152
# File 'lib/construqt/addresses.rb', line 148

def name
  ret = self.name!
  throw "unreferenced address [#{self.ips.map{|i| i.to_string }}]" unless ret
  ret
end

#name!Object



154
155
156
157
158
159
# File 'lib/construqt/addresses.rb', line 154

def name!
  return @name if @name
  return "#{self.interface.name}-#{self.interface.host.name}" if self.interface
  return self.host.name if self.host
  nil
end

#name=(name) ⇒ Object



144
145
146
# File 'lib/construqt/addresses.rb', line 144

def name=(name)
  set_name(name)
end

#routesObject



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/construqt/addresses.rb', line 182

def routes
  @routes.map do |i|
      if i.kind_of?(Route)
        i
      else
        ret = []
        [OpenStruct.new(:dsts => Construqt::Tags.ips_net(i.dst_tag, Construqt::Addresses::IPV6),
                        :vias => Construqt::Tags.ips_hosts(i.via_tag, Construqt::Addresses::IPV6)),
         OpenStruct.new(:dsts => Construqt::Tags.ips_net(i.dst_tag, Construqt::Addresses::IPV4),
                        :vias => Construqt::Tags.ips_hosts(i.via_tag, Construqt::Addresses::IPV4))].each do |blocks|
           next unless blocks.vias
           next unless blocks.dsts
           next if blocks.dsts.empty?
           blocks.vias.each do |via|
             blocks.dsts.each do |dst|
               ret << build_route(dst.to_string, via.to_s, i.options)
             end
           end
        end
        ret
      end
  end.flatten
end

#set_name(xname) ⇒ Object



139
140
141
142
# File 'lib/construqt/addresses.rb', line 139

def set_name(xname)
  (@name, obj) = self.merge_tag(xname) { |xname| self }
  self
end

#tag(tag) ⇒ Object



134
135
136
137
# File 'lib/construqt/addresses.rb', line 134

def tag(tag)
  self.tags += tag.split("#")
  self
end

#to_sObject



277
278
279
# File 'lib/construqt/addresses.rb', line 277

def to_s
  "<Address:Address #{@name}=>#{self.ips.map{|i| i.to_s}.join(":")}>"
end

#v4sObject



118
119
120
# File 'lib/construqt/addresses.rb', line 118

def v4s
  self.ips.select{|ip| ip.ipv4? }
end

#v6sObject



114
115
116
# File 'lib/construqt/addresses.rb', line 114

def v6s
  self.ips.select{|ip| ip.ipv6? }
end