Module: BoshVerifyManifest::Assertions::Helpers

Included in:
BoshVerifyManifest::Assertions
Defined in:
lib/bosh-verify-manifest/helpers.rb

Instance Method Summary collapse

Instance Method Details

#addresses_in_range?(addresses, range) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/bosh-verify-manifest/helpers.rb', line 7

def addresses_in_range?(addresses, range)
	unless range.include?('/') and range =~ %r{/[0-9]+$}
    raise ArgumentError, 'Range must be specified in CIDR format.'
  end
  # Can't get the netmask from an IPAddr instance without monkey patching
	# Consider using an alternate library for this functionality
  mask = range.split('/').last.to_i
  if addresses.include?('-')
    first_and_last_addresses = addresses.split('-').map do |address|
	    IPAddr.new(address.strip).to_s
	  end.uniq
    expanded_range = IPAddr.new(range).to_range.to_a.map{|i| i.to_s}
    (first_and_last_addresses & expanded_range) == first_and_last_addresses
  else
    Array(addresses).all? do |address|
      if address.include?('-')
        addresses_in_range?(address, range)
      else
        IPAddr.new(range).include?(address)
      end
    end
  end
end

#network(manifest, name) ⇒ Object



31
32
33
34
# File 'lib/bosh-verify-manifest/helpers.rb', line 31

def network(manifest, name)
  manifest['networks'].find{|n| n['name'] == name} || raise(
	  "Network '#{name}' not found in the manifest")
end

#pool_names(manifest) ⇒ Object



36
37
38
# File 'lib/bosh-verify-manifest/helpers.rb', line 36

def pool_names(manifest)
  manifest['resource_pools'].map{|pool| pool['name']}
end

#pool_sizes(manifest) ⇒ Object



40
41
42
# File 'lib/bosh-verify-manifest/helpers.rb', line 40

def pool_sizes(manifest)
  Hash[manifest['resource_pools'].map{|pool| [pool['name'], pool['size']]}]
end

#pool_usage(manifest) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/bosh-verify-manifest/helpers.rb', line 44

def pool_usage(manifest)
  Hash[manifest['jobs'].group_by do |job|
    job['resource_pool']
  end.map do |pool, jobs|
    [pool, jobs.inject(0){|count, job| count += job['instances']}]
  end]
end

#undeclared_pools(manifest) ⇒ Object



52
53
54
# File 'lib/bosh-verify-manifest/helpers.rb', line 52

def undeclared_pools(manifest)
  pool_usage(manifest).keys.sort - pool_names(manifest).sort
end