Class: Pec::Handler::Networks

Inherits:
Object
  • Object
show all
Extended by:
Core
Defined in:
lib/pec/handler/networks.rb,
lib/pec/handler/networks/ip_address.rb,
lib/pec/handler/networks/allowed_address_pairs.rb

Defined Under Namespace

Classes: AllowedAddressPairs, IpAddress

Constant Summary collapse

NAME =
0
CONFIG =
1

Instance Attribute Summary

Attributes included from Core

#kind

Class Method Summary collapse

Methods included from Core

build, post_build, recover

Class Method Details

.build(config) ⇒ Object



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

def build(config)
  ports = []
  config.networks.each do |network|
    validate(network)
    Pec::Logger.notice "port create start : #{network[NAME]}"
    port = create_port(config, network)
    Pec::Logger.notice "assgin ip : #{port.fixed_ips.first["ip_address"]}"
    ports << port
  end
  {
    networks: ports.map {|port| { uuid: '', port: port.id }}
  }
rescue Yao::Conflict => e
  raise(Pec::PortError.new(ports), e)
end

.create_port(config, network) ⇒ Object



51
52
53
54
# File 'lib/pec/handler/networks.rb', line 51

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

.gen_port_attribute(config, network) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/pec/handler/networks.rb', line 56

def gen_port_attribute(config, 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(config)
  ) if config.security_group

  Pec.processor_matching(network[CONFIG], Pec::Handler::Networks) do |klass|
    ops = klass.build(network)
    attribute.deep_merge!(ops) if ops
  end

  attribute
end

.recover(attribute) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pec/handler/networks.rb', line 29

def recover(attribute)
  return unless attribute[:networks]

  Pec::Logger.notice "start port recovery"
  attribute[:networks].each do |port|
    if port[:port]
      Yao::Port.destroy(port[:port])
      Pec::Logger.notice "port delete id:#{port[:port]}"
    end
  end
  Pec::Logger.notice "complete port recovery"
end

.security_group(config) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/pec/handler/networks.rb', line 76

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

.validate(network) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/pec/handler/networks.rb', line 42

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