Class: Bosh::Director::NetworkReservation
- Includes:
- IpUtil
- Defined in:
- lib/bosh/director/network_reservation.rb
Overview
Network resolution, either existing or one to be fulfilled by Network
Constant Summary collapse
- STATIC =
:static
- DYNAMIC =
:dynamic
- USED =
:used
- CAPACITY =
:capacity
- WRONG_TYPE =
:wrong_type
Instance Attribute Summary collapse
-
#error ⇒ Symbol?
Reservation error.
-
#ip ⇒ Integer?
Ip.
-
#reserved ⇒ Boolean
Reserved.
-
#type ⇒ Symbol?
Type.
Class Method Summary collapse
Instance Method Summary collapse
-
#dynamic? ⇒ Boolean
Returns true if this is a dynamic reservation.
-
#handle_error(origin) ⇒ Object
Handles network reservation error and re-raises the proper exception.
-
#initialize(options = {}) ⇒ NetworkReservation
constructor
Creates a new network reservation.
-
#reserved? ⇒ Boolean
Returns true if this reservation was fulfilled.
-
#static? ⇒ Boolean
Returns true if this is a static reservation.
-
#take(other) ⇒ void
Tries to take the provided reservation if it meets the requirements.
Methods included from IpUtil
#each_ip, #format_ip, #ip_to_i, #ip_to_netaddr, #process_range
Constructor Details
#initialize(options = {}) ⇒ NetworkReservation
Creates a new network reservation
41 42 43 44 45 46 47 48 |
# File 'lib/bosh/director/network_reservation.rb', line 41 def initialize( = {}) @ip = [:ip] @type = [:type] @reserved = false @error = nil @ip = ip_to_i(@ip) if @ip end |
Instance Attribute Details
#error ⇒ Symbol?
Returns reservation error.
26 27 28 |
# File 'lib/bosh/director/network_reservation.rb', line 26 def error @error end |
#ip ⇒ Integer?
Returns ip.
17 18 19 |
# File 'lib/bosh/director/network_reservation.rb', line 17 def ip @ip end |
#reserved ⇒ Boolean
Returns reserved.
23 24 25 |
# File 'lib/bosh/director/network_reservation.rb', line 23 def reserved @reserved end |
#type ⇒ Symbol?
Returns type.
20 21 22 |
# File 'lib/bosh/director/network_reservation.rb', line 20 def type @type end |
Class Method Details
.new_dynamic(ip = nil) ⇒ Object
28 29 30 |
# File 'lib/bosh/director/network_reservation.rb', line 28 def self.new_dynamic(ip = nil) new(:type => NetworkReservation::DYNAMIC, :ip => ip) end |
.new_static(ip = nil) ⇒ Object
32 33 34 |
# File 'lib/bosh/director/network_reservation.rb', line 32 def self.new_static(ip = nil) new(:type => NetworkReservation::STATIC, :ip => ip) end |
Instance Method Details
#dynamic? ⇒ Boolean
Returns true if this is a dynamic reservation
58 59 60 |
# File 'lib/bosh/director/network_reservation.rb', line 58 def dynamic? @type == DYNAMIC end |
#handle_error(origin) ⇒ Object
Handles network reservation error and re-raises the proper exception
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/bosh/director/network_reservation.rb', line 90 def handle_error(origin) if static? formatted_ip = ip_to_netaddr(@ip).ip case @error when NetworkReservation::USED raise NetworkReservationAlreadyInUse, "#{origin} asked for a static IP #{formatted_ip} " + "but it's already reserved/in use" when NetworkReservation::WRONG_TYPE raise NetworkReservationWrongType, "#{origin} asked for a static IP #{formatted_ip} " + "but it's in the dynamic pool" else raise NetworkReservationError, "#{origin} failed to reserve static IP " + "#{formatted_ip}: #{@error}" end else case @error when NetworkReservation::CAPACITY raise NetworkReservationNotEnoughCapacity, "#{origin} asked for a dynamic IP " + "but there were no more available" else raise NetworkReservationError, "#{origin} failed to reserve dynamic IP " + "#{formatted_ip}: #{@error}" end end end |
#reserved? ⇒ Boolean
Returns true if this reservation was fulfilled
64 65 66 |
# File 'lib/bosh/director/network_reservation.rb', line 64 def reserved? !!@reserved end |
#static? ⇒ Boolean
Returns true if this is a static reservation
52 53 54 |
# File 'lib/bosh/director/network_reservation.rb', line 52 def static? @type == STATIC end |
#take(other) ⇒ void
This method returns an undefined value.
Tries to take the provided reservation if it meets the requirements
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/bosh/director/network_reservation.rb', line 71 def take(other) if other.reserved? if @type == other.type if dynamic? || (static? && @ip == other.ip) @ip = other.ip @reserved = true end end end end |