Class: Bosh::Director::NetworkReservation

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

Parameters:

  • options (Hash) (defaults to: {})

    the options to create the reservation from

Options Hash (options):

  • :ip (Integer, String, NetAddr::CIDR)

    reservation ip

  • :type (Symbol)

    reservation type



41
42
43
44
45
46
47
48
# File 'lib/bosh/director/network_reservation.rb', line 41

def initialize(options = {})
  @ip = options[:ip]
  @type = options[:type]
  @reserved = false
  @error = nil

  @ip = ip_to_i(@ip) if @ip
end

Instance Attribute Details

#errorSymbol?

Returns reservation error.

Returns:

  • (Symbol, nil)

    reservation error



26
27
28
# File 'lib/bosh/director/network_reservation.rb', line 26

def error
  @error
end

#ipInteger?

Returns ip.

Returns:

  • (Integer, nil)

    ip



17
18
19
# File 'lib/bosh/director/network_reservation.rb', line 17

def ip
  @ip
end

#reservedBoolean

Returns reserved.

Returns:

  • (Boolean)

    reserved



23
24
25
# File 'lib/bosh/director/network_reservation.rb', line 23

def reserved
  @reserved
end

#typeSymbol?

Returns type.

Returns:

  • (Symbol, nil)

    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

Returns:

  • (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

Parameters:

  • origin (String)

    Whoever tried to take the reservation

Returns:

  • void

Raises:



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

Returns:

  • (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

Returns:

  • (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