Class: Pec::Handler::Networks

Inherits:
Base
  • Object
show all
Defined in:
lib/pec/handler/networks.rb,
lib/pec/handler/networks/ip_address.rb,
lib/pec/handler/networks/option_base.rb,
lib/pec/handler/networks/allowed_address_pairs.rb

Defined Under Namespace

Classes: AllowedAddressPairs, IpAddress, OptionBase

Constant Summary collapse

NAME =
0
CONFIG =
1

Class Method Summary collapse

Class Method Details

.build(host) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/pec/handler/networks.rb', line 12

def build(host)
  ports = []
  host.networks.each do |network|
    validate(network)
    Pec::Logger.notice "port create start : #{network[NAME]}"
    port = create_port(host, network)
    Pec::Logger.notice "assgin ip : #{port.fixed_ips.first["ip_address"]}"
    ports << port
  end
  {
    networks: ports.map {|port| { uuid: nil, port: port.id }}
  }
end

.create_port(host, network) ⇒ Object



35
36
37
38
# File 'lib/pec/handler/networks.rb', line 35

def create_port(host, network)
  attribute = gen_port_attribute(host, network)
  Yao::Port.create(attribute)
end

.gen_port_attribute(host, network) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/pec/handler/networks.rb', line 40

def gen_port_attribute(host, network)
  ip = IP.new(network[CONFIG]['ip_address'])
  subnet = Yao::Subnet.list.find {|s|s.cidr == ip.network.to_s}
  attribute = {
    name: network[NAME],
    network_id: subnet.network_id
  }

  attribute.merge!(
    security_group(host)
  ) if host.security_group

  network[CONFIG].keys.each do |k|
    Pec::Handler::Networks.constants.each do |c|
      if Object.const_get("Pec::Handler::Networks::#{c}").kind == k &&
          ops = Object.const_get("Pec::Handler::Networks::#{c}").build(network)
        attribute.deep_merge!(ops)
      end
    end
  end

  attribute
end

.security_group(host) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/pec/handler/networks.rb', line 64

def security_group(host)
  tenant = Yao::Tenant.list.find {|t| t.name == host.tenant }
  ids = host.security_group.map do |name|
    sg = Yao::SecurityGroup.list.find {|sg| sg.name == name && tenant.id == sg.tenant_id }
    raise "security group #{name} is not found" unless sg
    sg.id
  end
  { security_groups: ids }
end

.validate(network) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/pec/handler/networks.rb', line 26

def validate(network)
  %w(
    bootproto
    ip_address
  ).each do |k|
    raise "network key #{k} is require" unless network[CONFIG][k]
  end
end