Class: Bosh::Director::DeploymentPlan::ManualNetworkSubnet

Inherits:
Object
  • Object
show all
Extended by:
IpUtil, ValidationHelper
Defined in:
lib/bosh/director/deployment_plan/manual_network_subnet.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ValidationHelper

safe_property

Methods included from IpUtil

each_ip, format_ip, ip_to_i, ip_to_netaddr

Constructor Details

#initialize(network_name, range, gateway, dns, cloud_properties, netmask, availability_zone_names, restricted_ips, static_ips) ⇒ ManualNetworkSubnet

Returns a new instance of ManualNetworkSubnet.



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/bosh/director/deployment_plan/manual_network_subnet.rb', line 92

def initialize(network_name, range, gateway, dns, cloud_properties, netmask, availability_zone_names, restricted_ips, static_ips)
  @network_name = network_name
  @range = range
  @gateway = gateway
  @dns = dns
  @cloud_properties = cloud_properties
  @netmask = netmask
  @availability_zone_names = availability_zone_names
  @restricted_ips = restricted_ips
  @static_ips = static_ips
end

Instance Attribute Details

#availability_zone_namesObject (readonly)

Returns the value of attribute availability_zone_names.



7
8
9
# File 'lib/bosh/director/deployment_plan/manual_network_subnet.rb', line 7

def availability_zone_names
  @availability_zone_names
end

#cloud_propertiesObject (readonly)

Returns the value of attribute cloud_properties.



7
8
9
# File 'lib/bosh/director/deployment_plan/manual_network_subnet.rb', line 7

def cloud_properties
  @cloud_properties
end

#dnsObject (readonly)

Returns the value of attribute dns.



7
8
9
# File 'lib/bosh/director/deployment_plan/manual_network_subnet.rb', line 7

def dns
  @dns
end

#gatewayObject (readonly)

Returns the value of attribute gateway.



7
8
9
# File 'lib/bosh/director/deployment_plan/manual_network_subnet.rb', line 7

def gateway
  @gateway
end

#netmaskObject (readonly)

Returns the value of attribute netmask.



7
8
9
# File 'lib/bosh/director/deployment_plan/manual_network_subnet.rb', line 7

def netmask
  @netmask
end

#network_nameObject (readonly)

Returns the value of attribute network_name.



7
8
9
# File 'lib/bosh/director/deployment_plan/manual_network_subnet.rb', line 7

def network_name
  @network_name
end

#rangeObject (readonly)

Returns the value of attribute range.



7
8
9
# File 'lib/bosh/director/deployment_plan/manual_network_subnet.rb', line 7

def range
  @range
end

#restricted_ipsObject (readonly)

Returns the value of attribute restricted_ips.



7
8
9
# File 'lib/bosh/director/deployment_plan/manual_network_subnet.rb', line 7

def restricted_ips
  @restricted_ips
end

#static_ipsObject (readonly)

Returns the value of attribute static_ips.



7
8
9
# File 'lib/bosh/director/deployment_plan/manual_network_subnet.rb', line 7

def static_ips
  @static_ips
end

Class Method Details

.parse(network_name, subnet_spec, availability_zones, legacy_reserved_ranges) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/bosh/director/deployment_plan/manual_network_subnet.rb', line 10

def self.parse(network_name, subnet_spec, availability_zones, legacy_reserved_ranges)
  @logger = Config.logger

  @logger.debug("reserved ranges #{legacy_reserved_ranges.map {|r| r.first == r.last ? "#{r.first}" : "#{r.first}-#{r.last}"}.join(', ')}")
  range_property = safe_property(subnet_spec, "range", :class => String)
  range = NetAddr::CIDR.create(range_property)

  if range.size <= 1
    raise NetworkInvalidRange,
      "Invalid network range '#{range_property}', " +
        "should include at least 2 IPs"
  end

  netmask = range.wildcard_mask
  network_id = range.network(:Objectify => true)
  broadcast = range.broadcast(:Objectify => true)

  ignore_missing_gateway = Bosh::Director::Config.ignore_missing_gateway
  gateway_property = safe_property(subnet_spec, "gateway", class: String, optional: ignore_missing_gateway)
  if gateway_property
    gateway = NetAddr::CIDR.create(gateway_property)
    unless gateway.size == 1
      invalid_gateway(network_name, "must be a single IP")
    end
    unless range.contains?(gateway)
      invalid_gateway(network_name, "must be inside the range")
    end
    if gateway == network_id
      invalid_gateway(network_name, "can't be the network id")
    end
    if gateway == broadcast
      invalid_gateway(network_name, "can't be the broadcast IP")
    end
  end

  dns_manager = DnsManagerProvider.create
  dns_spec = safe_property(subnet_spec, 'dns', :class => Array, :optional => true)
  dns = dns_manager.dns_servers(network_name, dns_spec)

  availability_zone_names = parse_availability_zones(subnet_spec, network_name, availability_zones)

  cloud_properties = safe_property(subnet_spec, "cloud_properties", class: Hash, default: {})

  reserved_property = safe_property(subnet_spec, "reserved", :optional => true)
  static_property = safe_property(subnet_spec, "static", :optional => true)

  restricted_ips = Set.new
  restricted_ips.add(gateway.to_i) if gateway
  restricted_ips.add(network_id.to_i)
  restricted_ips.add(broadcast.to_i)

  each_ip(reserved_property) do |ip|
    unless range.contains?(ip)
      raise NetworkReservedIpOutOfRange,
        "Reserved IP '#{format_ip(ip)}' is out of " +
          "network '#{network_name}' range"
    end
    restricted_ips.add(ip)
  end

  static_ips = Set.new
  each_ip(static_property) do |ip|
    if restricted_ips.include?(ip)
      raise NetworkStaticIpOutOfRange,
        "Static IP '#{format_ip(ip)}' is in network '#{network_name}' reserved range"
    end
    unless range.contains?(ip)
      raise NetworkStaticIpOutOfRange,
        "Static IP '#{format_ip(ip)}' is out of network '#{network_name}' range"
    end
    static_ips.add(ip)
  end

  legacy_reserved_ranges.each do |cidr_range|
    cidr_range.range(0, nil, Objectify: true).each do |ip|
      restricted_ips.add(ip.to_i) unless static_ips.include?(ip.to_i)
    end
  end

  new(network_name, range, gateway, dns, cloud_properties, netmask, availability_zone_names, restricted_ips, static_ips)
end

Instance Method Details

#is_reservable?(ip) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/bosh/director/deployment_plan/manual_network_subnet.rb', line 110

def is_reservable?(ip)
  range.contains?(ip) && !restricted_ips.include?(ip.to_i)
end

#overlaps?(subnet) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
107
108
# File 'lib/bosh/director/deployment_plan/manual_network_subnet.rb', line 104

def overlaps?(subnet)
  range == subnet.range ||
    range.contains?(subnet.range) ||
    subnet.range.contains?(range)
end