Class: Bosh::Director::Canonicalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/bosh/director/dns/canonicalizer.rb

Class Method Summary collapse

Class Method Details

.canonicalize(string, opts = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/bosh/director/dns/canonicalizer.rb', line 4

def self.canonicalize(string, opts = {})
  # a-z, 0-9, -, case insensitive, and must start with a letter
  string = string.downcase.gsub(/_/, "-")
  if opts[:allow_dots]
    string = string.gsub(/[^a-z0-9\-\.]/, "")
  else
    string = string.gsub(/[^a-z0-9\-]/, "")
  end

  validate_dns_name(string)
end

.validate_dns_name(string) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/bosh/director/dns/canonicalizer.rb', line 16

def self.validate_dns_name(string)
  if string =~ /^(\d|-)/
    raise DnsInvalidCanonicalName,
          "Invalid DNS canonical name '#{string}', must begin with a letter"
  end
  if string =~ /-$/
    raise DnsInvalidCanonicalName,
          "Invalid DNS canonical name '#{string}', can't end with a hyphen"
  end
  string
end