pec 0.2.3 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aadbb308e49e11ca2d2ced6b275a93fd70c4485f
4
- data.tar.gz: 4f1d3be1d106fbcf7b4adb7b595f568468245b94
3
+ metadata.gz: 40647fc979435e780b1ed75ef3d5d5c69cef8430
4
+ data.tar.gz: bd561098ca4192c67f9ddb18f2b0f1dd0874ad7d
5
5
  SHA512:
6
- metadata.gz: 38a7d1d3f93112fb776545753b0c52d465883fc5e3184ec5149202243f8c70d73e6c6542c7b72b58016b4f0efaea8cd01f2703453d40a356d0f78a76fe49215c
7
- data.tar.gz: fe83c5437379bc16a8691cf596a0835c1070f5e7e84d9f4c83730e93eb48ea6862b8f315b22e7e629264a64b374c0e4eb812128d9062692f02e8b880f4b8394e
6
+ metadata.gz: caaf7f0b8c4b9f453734816999f23241ed1a319abc73c4c08b9cd545b08176f9fdc4c74aea9f400c07ec91244913ca13b26ef5046b10ee2a3cf2cd72aa5c6cdd
7
+ data.tar.gz: 2e70c4393bbb7cad5c0ab3b2485ddd78e4e59f7412ab9edeb9465ea51277be691387611937022528fb9f457b2189bb4102ef2873c23e22c7a9cb270c2f156d6a
data/lib/pec.rb CHANGED
@@ -23,6 +23,7 @@ require "pec/compute/image"
23
23
  require "pec/compute/tenant"
24
24
  require "pec/compute/security_group"
25
25
  require "pec/network/port"
26
+ require "pec/network/port_state"
26
27
  require "pec/network/subnet"
27
28
  require "pec/cli"
28
29
 
@@ -3,16 +3,16 @@ module Pec
3
3
  class Ethernet
4
4
  attr_reader :name, :bootproto, :ip_address, :options
5
5
  def initialize(config)
6
- @name = config[0];
7
- @bootproto = config[1]["bootproto"];
6
+ @name = config[0];
7
+ @bootproto = config[1]["bootproto"];
8
8
  @ip_address = config[1]["ip_address"];
9
- @options = config[1].select do |k,v|
9
+ @options = config[1].select do |k,v|
10
10
  { k => v } if k != "bootproto" && k != "ip_address"
11
11
  end
12
12
  end
13
13
 
14
14
  def find_port(ports)
15
- ports.find { |p| p.name == @name }
15
+ ports.find { |p| p.device_name == @name }
16
16
  end
17
17
 
18
18
  class << self
@@ -14,7 +14,7 @@ module Pec
14
14
  port_subnet = Pec::Network::Subnet.fetch(ip.network.to_s)
15
15
  raise(Pec::Errors::Subnet, "subnet:#{ip.network.to_s} is not fond!") unless port_subnet
16
16
 
17
- port = Pec::Network::Port.new.assign(ether.name, ip, port_subnet, get_security_group_id(host.security_group))
17
+ port = Pec::Network::Port.assign(ether.name, ip, port_subnet, get_security_group_id(host.security_group))
18
18
  raise(Pec::Errors::Port, "ip addess:#{ip.to_addr} can't create port!") unless port
19
19
 
20
20
  puts "#{host.name}: assingn ip #{port.ip_address}".green
@@ -3,96 +3,73 @@ module Pec
3
3
  class Network
4
4
  class Port
5
5
  extend Query
6
- attr_reader :name, :subnet
7
- @@use_ip_list = []
8
-
9
- def assign(name, ip, subnet, security_group_ids)
10
- @name = name
11
- @subnet = subnet
12
-
13
- # dhcp ip recycle
14
- if request_any_address?(ip)
15
- @port = fetch_free_port
16
- ip = IP.new("#{@port["fixed_ips"][0]["ip_address"]}/#{ip.pfxlen}") unless @port.nil?
6
+ class << self
7
+ @@use_ip_list = []
8
+
9
+ def assign(name, ip, subnet, security_group_ids)
10
+ ip = get_free_port_ip(ip, subnet) if request_any_address?(ip, subnet)
11
+ port_state = Pec::Network::PortState.new(name, fetch_by_ip(ip.to_addr))
12
+
13
+ assign_port = case
14
+ when port_state.exists? && !port_state.used?
15
+ recreate(ip, subnet, security_group_ids)
16
+ when !port_state.exists?
17
+ create(ip, subnet, security_group_ids)
18
+ when port_state.used?
19
+ raise(Pec::Errors::Port, "ip:#{ip.to_addr} is used!")
20
+ end
21
+ Pec::Network::PortState.new(name, assign_port)
17
22
  end
18
- case
19
- when exists? && !used?
20
- recreate(ip, subnet, security_group_ids)
21
- when !exists?
22
- create(ip, subnet, security_group_ids)
23
- when used?
24
- raise(Pec::Errors::Port, "ip:#{ip.to_addr} is used!")
25
- end
26
- self
27
- end
28
23
 
29
- def create(ip, subnet, security_group_ids)
30
- options = { security_groups: security_group_ids }
31
- options.merge!({ fixed_ips: [{ subnet_id: subnet["id"], ip_address: ip.to_addr}]}) if ip.to_s != subnet["cidr"]
32
- response = Pec::Resource.get.create_port(subnet["network_id"], options)
33
- if response
34
- @port = response.data[:body]["port"]
35
- @@use_ip_list << response.data[:body]["port"]["fixed_ips"][0]["ip_address"]
36
- response.data[:body]["port"]["id"]
24
+ def get_free_port_ip(ip, subnet)
25
+ port = fetch_free_port(subnet)
26
+ port ? IP.new("#{port["fixed_ips"][0]["ip_address"]}/#{ip.pfxlen}") : ip
37
27
  end
38
- end
39
28
 
40
- def delete(ip)
41
- target_port = fetch_by_ip(ip.to_addr)
42
- response = Pec::Resource.get.delete_port(target_port["id"]) if target_port
43
- end
29
+ def create(ip, subnet, security_group_ids)
30
+ options = set_security_group(security_group_ids)
31
+ options = set_fixed_ip(options, subnet, ip)
32
+ response = Pec::Resource.get.create_port(subnet["network_id"], options)
44
33
 
45
- def recreate(ip, subnet, security_group_ids)
46
- create(ip, subnet, security_group_ids) if delete(ip)
47
- end
34
+ raise(Pec::Errors::Port, "ip:#{ip.to_addr} is not created!") unless response
48
35
 
49
- def request_any_address?(ip)
50
- ip.to_s == subnet["cidr"]
51
- end
52
-
53
- def list
54
- Pec::Resource.get.port_list
55
- end
56
-
57
- def fetch_by_ip(ip_addr)
58
- list.find {|p| p["fixed_ips"][0]["ip_address"] == ip_addr }
59
- end
60
-
61
- def fetch_free_port
62
- list.find do |p|
63
- p["fixed_ips"][0]["subnet_id"] == @subnet["id"] &&
64
- p["device_owner"].empty? &&
65
- p["admin_state_up"] &&
66
- !@@use_ip_list.include?(p["fixed_ips"][0]["ip_address"])
36
+ @@use_ip_list << response.data[:body]["port"]["fixed_ips"][0]["ip_address"]
37
+ response.data[:body]["port"]
67
38
  end
68
- end
69
39
 
70
- def exists?
71
- !@port.nil?
72
- end
40
+ def set_security_group(security_group_ids)
41
+ { security_groups: security_group_ids }
42
+ end
73
43
 
74
- def used?
75
- @port && !@port["device_owner"].empty?
76
- end
44
+ def set_fixed_ip(options, subnet, ip)
45
+ ip.to_s != subnet["cidr"] ? options.merge({ fixed_ips: [{ subnet_id: subnet["id"], ip_address: ip.to_addr}]}) : options
46
+ end
77
47
 
78
- def id
79
- @port["id"]
80
- end
48
+ def delete(ip)
49
+ target_port = fetch_by_ip(ip.to_addr)
50
+ response = Pec::Resource.get.delete_port(target_port["id"]) if target_port
51
+ end
81
52
 
82
- def mac_address
83
- @port["mac_address"]
84
- end
53
+ def recreate(ip, subnet, security_group_ids)
54
+ create(ip, subnet, security_group_ids) if delete(ip)
55
+ end
85
56
 
86
- def ip_address
87
- @port["fixed_ips"][0]["ip_address"]
88
- end
57
+ def request_any_address?(ip, subnet)
58
+ ip.to_s == subnet["cidr"]
59
+ end
89
60
 
90
- def network_id
91
- @port["network_id"]
92
- end
61
+ def fetch_by_ip(ip_addr)
62
+ list.find {|p| p["fixed_ips"][0]["ip_address"] == ip_addr }
63
+ end
93
64
 
94
- def netmask
95
- IP.new(@port["fixed_ips"][0]["ip_address"]).netmask.to_s
65
+ def fetch_free_port(subnet)
66
+ list.find do |p|
67
+ p["fixed_ips"][0]["subnet_id"] == subnet["id"] &&
68
+ p["device_owner"].empty? &&
69
+ p["admin_state_up"] &&
70
+ !@@use_ip_list.include?(p["fixed_ips"][0]["ip_address"])
71
+ end
72
+ end
96
73
  end
97
74
  end
98
75
  end
@@ -0,0 +1,40 @@
1
+ require 'json'
2
+ module Pec
3
+ class Network
4
+ class PortState
5
+ attr_reader :device_name
6
+ def initialize(device_name, port)
7
+ @device_name = device_name
8
+ @port = port
9
+ end
10
+
11
+ def exists?
12
+ !@port.nil?
13
+ end
14
+
15
+ def used?
16
+ @port && !@port["device_owner"].empty?
17
+ end
18
+
19
+ def id
20
+ @port["id"]
21
+ end
22
+
23
+ def mac_address
24
+ @port["mac_address"]
25
+ end
26
+
27
+ def ip_address
28
+ @port["fixed_ips"][0]["ip_address"]
29
+ end
30
+
31
+ def network_id
32
+ @port["network_id"]
33
+ end
34
+
35
+ def netmask
36
+ IP.new(@port["fixed_ips"][0]["ip_address"]).netmask.to_s
37
+ end
38
+ end
39
+ end
40
+ end
data/lib/pec/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Pec
2
- VERSION = "0.2.3"
2
+ VERSION = "0.2.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - kazuhiko yamashita
@@ -162,6 +162,7 @@ files:
162
162
  - lib/pec/errors.rb
163
163
  - lib/pec/init.rb
164
164
  - lib/pec/network/port.rb
165
+ - lib/pec/network/port_state.rb
165
166
  - lib/pec/network/subnet.rb
166
167
  - lib/pec/query.rb
167
168
  - lib/pec/resource.rb