Class: Bosh::Director::DeploymentPlan::DynamicNetwork

Inherits:
NetworkWithSubnets show all
Extended by:
ValidationHelper
Includes:
IpUtil
Defined in:
lib/bosh/director/deployment_plan/dynamic_network.rb

Constant Summary

Constants inherited from Network

Network::VALID_DEFAULTS

Instance Attribute Summary collapse

Attributes inherited from Network

#canonical_name, #name

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

Methods inherited from NetworkWithSubnets

#availability_zones, #has_azs?, #supports_azs?

Methods inherited from Network

#has_azs?, #manual?, #supports_azs?

Constructor Details

#initialize(name, subnets, logger) ⇒ DynamicNetwork

Returns a new instance of DynamicNetwork.



81
82
83
84
# File 'lib/bosh/director/deployment_plan/dynamic_network.rb', line 81

def initialize(name, subnets, logger)
  super(name, logger)
  @subnets = subnets
end

Instance Attribute Details

#subnetsObject (readonly)

Returns the value of attribute subnets.



86
87
88
# File 'lib/bosh/director/deployment_plan/dynamic_network.rb', line 86

def subnets
  @subnets
end

Class Method Details

.check_validity_of_availability_zone(availability_zone, availability_zones, name) ⇒ Object



75
76
77
78
79
# File 'lib/bosh/director/deployment_plan/dynamic_network.rb', line 75

def self.check_validity_of_availability_zone(availability_zone, availability_zones, name)
  unless availability_zone.nil? || availability_zones.any? { |az| az.name == availability_zone }
    raise Bosh::Director::NetworkSubnetUnknownAvailabilityZone, "Network '#{name}' refers to an unknown availability zone '#{availability_zone}'"
  end
end

.parse(network_spec, availability_zones, logger) ⇒ Object



7
8
9
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
# File 'lib/bosh/director/deployment_plan/dynamic_network.rb', line 7

def self.parse(network_spec, availability_zones, logger)
  name = safe_property(network_spec, 'name', :class => String)
  Canonicalizer.canonicalize(name)
  logger = TaggedLogger.new(logger, 'network-configuration')
  dns_manager = DnsManagerProvider.create

  validate_network_has_no_key('az', name, network_spec)
  validate_network_has_no_key('azs', name, network_spec)

  if network_spec.has_key?('subnets')
    validate_network_has_no_key_while_subnets_present('dns', name, network_spec)
    validate_network_has_no_key_while_subnets_present('cloud_properties', name, network_spec)

    subnets = network_spec['subnets'].map do |subnet_properties|
      dns_spec = safe_property(subnet_properties, 'dns', :class => Array, :optional => true)
      dns = dns_manager.dns_servers(subnet_properties['name'], dns_spec)
      cloud_properties = safe_property(subnet_properties, 'cloud_properties', class: Hash, default: {})
      subnet_availability_zones = parse_availability_zones(subnet_properties, availability_zones, name)
      DynamicNetworkSubnet.new(dns, cloud_properties, subnet_availability_zones)
    end
  else
    cloud_properties = safe_property(network_spec, 'cloud_properties', class: Hash, default: {})
    dns_spec = safe_property(network_spec, 'dns', :class => Array, :optional => true)
    dns = dns_manager.dns_servers(network_spec['name'], dns_spec)
    subnets = [DynamicNetworkSubnet.new(dns, cloud_properties, nil)]
  end

  new(name, subnets, logger)
end

.parse_availability_zones(spec, availability_zones, name) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/bosh/director/deployment_plan/dynamic_network.rb', line 50

def self.parse_availability_zones(spec, availability_zones, name)

  has_availability_zones_key = spec.has_key?('azs')
  has_availability_zone_key = spec.has_key?('az')

  if has_availability_zone_key && has_availability_zones_key
    raise Bosh::Director::NetworkInvalidProperty, "Network '#{name}' contains both 'az' and 'azs'. Choose one."
  end

  if has_availability_zones_key
    subnet_availability_zones = safe_property(spec, 'azs', class: Array, optional: true)
    if subnet_availability_zones.empty?
      raise Bosh::Director::NetworkInvalidProperty, "Network '#{name}' refers to an empty 'azs' array"
    end
    subnet_availability_zones.each do |zone|
      check_validity_of_availability_zone(zone, availability_zones, name)
    end
    subnet_availability_zones
  else
    availability_zone = safe_property(spec, 'az', class: String, optional: true)
    check_validity_of_availability_zone(availability_zone, availability_zones, name)
    availability_zone.nil? ? nil : [availability_zone]
  end
end

.validate_network_has_no_key(key, name, network_spec) ⇒ Object



44
45
46
47
48
# File 'lib/bosh/director/deployment_plan/dynamic_network.rb', line 44

def self.validate_network_has_no_key(key, name, network_spec)
  if network_spec.has_key?(key)
    raise NetworkInvalidProperty, "Network '#{name}' must not specify '#{key}'."
  end
end

.validate_network_has_no_key_while_subnets_present(key, name, network_spec) ⇒ Object



37
38
39
40
41
42
# File 'lib/bosh/director/deployment_plan/dynamic_network.rb', line 37

def self.validate_network_has_no_key_while_subnets_present(key, name, network_spec)
  if network_spec.has_key?(key)
    raise NetworkInvalidProperty, "Network '#{name}' must not specify '#{key}' when also specifying 'subnets'. " +
        "Instead, '#{key}' should be specified on subnet entries."
  end
end

Instance Method Details

#find_az_names_for_ip(ip) ⇒ Object



131
132
133
134
# File 'lib/bosh/director/deployment_plan/dynamic_network.rb', line 131

def find_az_names_for_ip(ip)
  # On dynamic network ip does not belong to any subnet
  availability_zones
end

#network_settings(reservation, default_properties = Network::VALID_DEFAULTS, availability_zone = nil) ⇒ Hash

Returns the network settings for the specific reservation.

Parameters:

Returns:

  • (Hash)

    network settings that will be passed to the BOSH Agent



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
120
121
122
# File 'lib/bosh/director/deployment_plan/dynamic_network.rb', line 95

def network_settings(reservation, default_properties = Network::VALID_DEFAULTS, availability_zone = nil)
  unless reservation.dynamic?
    raise NetworkReservationWrongType,
      "IP '#{format_ip(reservation.ip)}' on network '#{reservation.network.name}' does not belong to dynamic pool"
  end

  if availability_zone.nil?
    subnet = subnets.first
  else
    subnet = find_subnet_for_az(availability_zone.name)
    unless subnet
      raise NetworkSubnetInvalidAvailabilityZone,
        "Network '#{name}' has no matching subnet for availability zone '#{availability_zone.name}'"
    end
  end

  config = {
    "type" => "dynamic",
    "cloud_properties" => subnet.cloud_properties
  }
  config["dns"] = subnet.dns if subnet.dns

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

  config
end

#validate_reference_from_job!(job_network_spec, job_name) ⇒ Object



124
125
126
127
128
129
# File 'lib/bosh/director/deployment_plan/dynamic_network.rb', line 124

def validate_reference_from_job!(job_network_spec, job_name)
  if job_network_spec.has_key?('static_ips')
    raise JobStaticIPNotSupportedOnDynamicNetwork,
      "Instance group '#{job_name}' using dynamic network '#{name}' cannot specify static IP(s)"
  end
end