Class: Bosh::Director::DeploymentPlan::ManualNetwork

Inherits:
Network show all
Includes:
Bosh::Director::DnsHelper, IpUtil, ValidationHelper
Defined in:
lib/bosh/director/deployment_plan/manual_network.rb

Overview

Represents a explicitly configured network.

Constant Summary

Constants included from Bosh::Director::DnsHelper

Bosh::Director::DnsHelper::SOA, Bosh::Director::DnsHelper::TTL_4H, Bosh::Director::DnsHelper::TTL_5M

Constants inherited from Network

Network::VALID_DEFAULTS

Instance Attribute Summary

Attributes inherited from Network

#canonical_name, #deployment, #name

Instance Method Summary collapse

Methods included from ValidationHelper

#invalid_type, #safe_property

Methods included from Bosh::Director::DnsHelper

#add_default_dns_server, #canonical, #default_dns_server, #delete_dns_records, #delete_empty_domain, #dns_domain_name, #dns_ns_record, #dns_servers, #flush_dns_cache, #invalid_dns, #reverse_domain, #reverse_host, #update_dns_a_record, #update_dns_ptr_record

Methods included from IpUtil

#each_ip, #format_ip, #ip_to_i, #ip_to_netaddr, #process_range

Methods inherited from Network

#reserve!

Constructor Details

#initialize(deployment, network_spec) ⇒ ManualNetwork

Creates a new network.

Parameters:

  • deployment (DeploymentPlan)

    associated deployment plan

  • network_spec (Hash)

    parsed deployment manifest network section



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/bosh/director/deployment_plan/manual_network.rb', line 17

def initialize(deployment, network_spec)
  super

  @subnets = []
  subnets = safe_property(network_spec, "subnets", :class => Array)

  subnets.each do |subnet_spec|
    new_subnet = NetworkSubnet.new(self, subnet_spec)
    @subnets.each do |subnet|
      if subnet.overlaps?(new_subnet)
        raise NetworkOverlappingSubnets,
              "Network `#{name}' has overlapping subnets"
      end
    end
    @subnets << new_subnet
  end

  # Uncomment line below when integration tests is fixed
  # raise "Must specify at least one subnet" if @subnets.empty?
end

Instance Method Details

#find_subnet(ip) { ... } ⇒ Object

Parameters:

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

Yields:

  • the subnet that contains the IP.



127
128
129
130
131
132
133
134
# File 'lib/bosh/director/deployment_plan/manual_network.rb', line 127

def find_subnet(ip)
  @subnets.each do |subnet|
    if subnet.range.contains?(ip)
      yield subnet
      break
    end
  end
end

#network_settings(reservation, default_properties = VALID_DEFAULTS) ⇒ Hash

Returns the network settings for the specific reservation.

Parameters:

Returns:

  • (Hash)

    network settings that will be passed to the BOSH Agent



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/bosh/director/deployment_plan/manual_network.rb', line 99

def network_settings(reservation, default_properties = VALID_DEFAULTS)
  unless reservation.ip
    raise NetworkReservationIpMissing,
          "Can't generate network settings without an IP"
  end

  config = nil
  find_subnet(reservation.ip) do |subnet|
    ip = ip_to_netaddr(reservation.ip)
    config = {
        "ip" => ip.ip,
        "netmask" => subnet.netmask,
        "cloud_properties" => subnet.cloud_properties
    }

    if default_properties
      config["default"] = default_properties.sort
    end

    config["dns"] = subnet.dns if subnet.dns
    config["gateway"] = subnet.gateway.ip if subnet.gateway
  end
  config
end

#release(reservation) ⇒ void

This method returns an undefined value.

Releases a previous reservation that had been fulfilled.

Parameters:



82
83
84
85
86
87
88
89
90
91
# File 'lib/bosh/director/deployment_plan/manual_network.rb', line 82

def release(reservation)
  unless reservation.ip
    raise NetworkReservationIpMissing,
          "Can't release reservation without an IP"
  end

  find_subnet(reservation.ip) do |subnet|
    subnet.release_ip(reservation.ip)
  end
end

#reserve(reservation) ⇒ Boolean

Reserves a network resource.

This is either an already used reservation being verified or a new one waiting to be fulfilled.

Parameters:

Returns:

  • (Boolean)

    true if the reservation was fulfilled



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/bosh/director/deployment_plan/manual_network.rb', line 45

def reserve(reservation)
  reservation.reserved = false
  if reservation.ip
    find_subnet(reservation.ip) do |subnet|
      type = subnet.reserve_ip(reservation.ip)
      if type.nil?
        reservation.error = NetworkReservation::USED
      elsif reservation.type && reservation.type != type
        reservation.error = NetworkReservation::WRONG_TYPE
      else
        reservation.type = type
        reservation.reserved = true
      end
    end
  else
    unless reservation.dynamic?
      raise NetworkReservationInvalidType,
            "New reservations without IPs must be dynamic"
    end
    @subnets.each do |subnet|
      reservation.ip = subnet.allocate_dynamic_ip
      if reservation.ip
        reservation.reserved = true
        break
      end
    end
    unless reservation.reserved?
      reservation.error = NetworkReservation::CAPACITY
    end
  end
  reservation.reserved?
end