Class: Fog::DNS::Zerigo::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/rackspace-fog/zerigo/requests/dns/get_host.rb,
lib/rackspace-fog/zerigo/dns.rb,
lib/rackspace-fog/zerigo/requests/dns/get_zone.rb,
lib/rackspace-fog/zerigo/requests/dns/find_hosts.rb,
lib/rackspace-fog/zerigo/requests/dns/list_hosts.rb,
lib/rackspace-fog/zerigo/requests/dns/list_zones.rb,
lib/rackspace-fog/zerigo/requests/dns/count_hosts.rb,
lib/rackspace-fog/zerigo/requests/dns/count_zones.rb,
lib/rackspace-fog/zerigo/requests/dns/create_host.rb,
lib/rackspace-fog/zerigo/requests/dns/create_zone.rb,
lib/rackspace-fog/zerigo/requests/dns/delete_host.rb,
lib/rackspace-fog/zerigo/requests/dns/delete_zone.rb,
lib/rackspace-fog/zerigo/requests/dns/update_host.rb,
lib/rackspace-fog/zerigo/requests/dns/update_zone.rb,
lib/rackspace-fog/zerigo/requests/dns/get_zone_stats.rb

Overview

:nodoc:all

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



45
46
47
48
# File 'lib/rackspace-fog/zerigo/dns.rb', line 45

def initialize(options={})
  @zerigo_email = options[:zerigo_email]
  @zerigo_token = options[:zerigo_token]
end

Class Method Details

.dataObject



35
36
37
38
39
# File 'lib/rackspace-fog/zerigo/dns.rb', line 35

def self.data
  @data ||= Hash.new do |hash, key|
    hash[key] = key == :zones ? [] : {}
  end
end

.resetObject



41
42
43
# File 'lib/rackspace-fog/zerigo/dns.rb', line 41

def self.reset
  @data = nil
end

Instance Method Details

#count_hosts(zone_id) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rackspace-fog/zerigo/requests/dns/count_hosts.rb', line 28

def count_hosts(zone_id)
  zone = find_by_zone_id(zone_id)

  response = Excon::Response.new

  if zone
    response.status = 200
    response.body = {
      'count' => zone['hosts'].size
    }
  else
    response.status = 404
  end

  response
end

#count_zonesObject



28
29
30
31
32
33
34
35
# File 'lib/rackspace-fog/zerigo/requests/dns/count_zones.rb', line 28

def count_zones
  response = Excon::Response.new

  response.status = 200
  response.body = { 'count' => self.data[:zones].size }

  response
end

#create_host(zone_id, host_type, data, options = {}) ⇒ Object



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
91
92
93
94
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
123
124
# File 'lib/rackspace-fog/zerigo/requests/dns/create_host.rb', line 66

def create_host(zone_id, host_type, data, options = {})
  zone = find_by_zone_id(zone_id)

  response = Excon::Response.new

  # Handle error cases.

  # Zone doesn't exist.
  unless zone
    response.status = 404
    return response
  end

  # Bad host type.
  unless valid_host_types.include?(host_type)
    response.status = 422
    response.body = {
      'errors' => [
        'error' => 'Host type is not included in the list'
      ]
    }

    return response
  end

  # Missing or bad priority value for MX or SRV records.
  if %w[MX SRV].include?(host_type) && options['priority'].to_s !~ /\d+/
    response.status = 422
    response.body = {
      'errors' => [
        'error' => 'Priority is not a number'
      ]
    }

    return response
  end

  # Successful case.
  now = Time.now
  host = {
    'id'            => rand(10000000),
    'fqdn'          => options[:hostname] ? "#{options[:hostname]}.#{zone['domain']}" : zone['domain'],
    'data'          => data,
    'hostname'      => options[:hostname],
    'ttl'           => options[:ttl].to_i,
    'host-type'     => host_type,
    'created-at'    => now,
    'updated-at'    => now,
    'notes'         => options[:notes],
    'priority'      => options[:priority].to_i,
    'zone-id'       => zone_id
  }

  zone['hosts'] << host
  response.status = 201
  response.body = host

  response
end

#create_zone(domain, default_ttl, ns_type, options = {}) ⇒ Object



89
90
91
92
93
94
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
123
124
125
126
127
# File 'lib/rackspace-fog/zerigo/requests/dns/create_zone.rb', line 89

def create_zone(domain, default_ttl, ns_type, options = {})
  now = Time.now
  zone = {
    'id'                    => rand(10000000),
    'domain'                => domain,
    'created-at'            => now,
    'updated-at'            => now,
    'ns1'                   => options[:ns1],
    'nx-ttl'                => options[:nx_ttl].to_i,
    'default-ttl'           => default_ttl.to_i,
    'ns-type'               => ns_type,
    'hosts'                 => options[:hosts] || [],
    'hosts-count'           => (options[:hosts] || []).size,
    'notes'                 => options[:notes],
    'slave-nameservers'     => options[:slave_nameservers],
    'tag-list'              => options[:tag_list]
  }

  response = Excon::Response.new

  if self.data[:zones].any? {|z| z['domain'] == zone['domain'] }
    response.status = 422
    response.body = {
      'errors' => [
        'error' => 'Domain is already associated to another account',
        'error' => 'Domain already exists. If it was just deleted, wait a minute and try again'
      ]
    }

  else
    self.data[:zones] << zone

    response.status = 201
    response.headers = { 'Location' => "http://ns.zerigo.com/api/1.1/zones/#{zone['id']}" }
    response.body = zone['hosts'].empty? ? zone.merge(:hosts => nil) : zone # Zerigo returns nil, not an empty list only on the create command.
  end

  response
end

#dataObject



50
51
52
# File 'lib/rackspace-fog/zerigo/dns.rb', line 50

def data
  self.class.data
end

#delete_host(host_id) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rackspace-fog/zerigo/requests/dns/delete_host.rb', line 24

def delete_host(host_id)
  host = find_host(host_id)

  response = Excon::Response.new

  if host
    zone = find_by_zone_id(host['zone-id'])
    zone['hosts'].delete(host)

    response.status = 200
  else
    response.status = 404
  end

  response
end

#delete_zone(zone_id) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rackspace-fog/zerigo/requests/dns/delete_zone.rb', line 25

def delete_zone(zone_id)
  zone = find_by_zone_id(zone_id)

  response = Excon::Response.new

  if zone
    self.data[:zones].delete(zone)
    response.status = 200
  else
    response.status = 404
  end

  response
end

#find_by_domain(domain) ⇒ Object



62
63
64
# File 'lib/rackspace-fog/zerigo/dns.rb', line 62

def find_by_domain(domain)
  self.data[:zones].find { |z| z['domain'] == domain }
end

#find_by_zone_id(zone_id) ⇒ Object



58
59
60
# File 'lib/rackspace-fog/zerigo/dns.rb', line 58

def find_by_zone_id(zone_id)
  self.data[:zones].find { |z| z['id'] == zone_id }
end

#find_host(host_id) ⇒ Object



66
67
68
# File 'lib/rackspace-fog/zerigo/dns.rb', line 66

def find_host(host_id)
  self.data[:zones].collect { |z| z['hosts'].find { |h| h['id'] == host_id } }.compact.first
end

#find_hosts(fqdn, zone_id = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rackspace-fog/zerigo/requests/dns/find_hosts.rb', line 55

def find_hosts(fqdn, zone_id = nil)
  response = Excon::Response.new

  zone = find_by_zone_id(zone_id)
  if zone_id && !zone
    response.status = 404
  else
    hosts = zone ? zone['hosts'].select { |z| z['fqdn'] == fqdn } :
                   self.data[:zones].collect { |z| z['hosts'].find { |h| h['fqdn'] == fqdn } }.compact

    response.status = 200
    response.body = {
      'hosts' => hosts
    }
  end

  response
end

#get_host(host_id) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rackspace-fog/zerigo/requests/dns/get_host.rb', line 39

def get_host(host_id)
  host = find_host(host_id)

  response = Excon::Response.new

  if host
    response.status = 200
    response.body = host
  else
    response.status = 404
  end

  response
end

#get_zone(zone_id_or_domain) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rackspace-fog/zerigo/requests/dns/get_zone.rb', line 49

def get_zone(zone_id_or_domain)
  zone = find_by_zone_id(zone_id_or_domain) || find_by_domain(zone_id_or_domain)

  response = Excon::Response.new

  if zone
    response.status = 200
    response.body = zone
  else
    response.status = 404
  end

  response
end

#get_zone_stats(zone_id) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rackspace-fog/zerigo/requests/dns/get_zone_stats.rb', line 36

def get_zone_stats(zone_id)
  zone = find_by_zone_id(zone_id)

  response = Excon::Response.new

  if zone
    response.status = 200
    response.body = {
      'id' => zone,
      'domain' => zone['domain'],
      'period-begin' => zone['created-at'].strftime("%F"),
      'period-end' => Date.today.to_s,
      'queries' => 0
    }
  else
    response.status = 404
  end

  response
end

#list_hosts(zone_id) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rackspace-fog/zerigo/requests/dns/list_hosts.rb', line 40

def list_hosts(zone_id)
  zone = find_by_zone_id(zone_id)

  response = Excon::Response.new

  if zone
    response.status = 200
    response.body = {
      'hosts' => zone['hosts']
    }
  else
    response.status = 404
  end

  response
end

#list_zonesObject



46
47
48
49
50
51
52
53
54
55
# File 'lib/rackspace-fog/zerigo/requests/dns/list_zones.rb', line 46

def list_zones
  response = Excon::Response.new

  response.status = 200
  response.body = {
    'zones' => self.data[:zones]
  }

  response
end

#reset_dataObject



54
55
56
# File 'lib/rackspace-fog/zerigo/dns.rb', line 54

def reset_data
  self.class.reset
end

#update_host(host_id, options = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rackspace-fog/zerigo/requests/dns/update_host.rb', line 52

def update_host(host_id, options = {})
  host = find_host(host_id)

  response = Excon::Response.new

  if host
    options.each { |k, v| host[k.to_s] = v } # Deal with symbols in requests but strings in responses.
    host['updated-at'] = Time.now

    response.status = 200
  else
    response.status = 404
  end

  response
end

#update_zone(zone_id, options = {}) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rackspace-fog/zerigo/requests/dns/update_zone.rb', line 70

def update_zone(zone_id, options = {})
  zone = find_by_zone_id(zone_id)

  response = Excon::Response.new

  if zone
    options.each { |k, v| zone[k.to_s] = v } # Deal with symbols in requests but strings in responses.
    zone['updated-at'] = Time.now

    response.status = 200
  else
    response.status = 404
  end

  response
end

#valid_host_typesObject



62
63
64
# File 'lib/rackspace-fog/zerigo/requests/dns/create_host.rb', line 62

def valid_host_types
  %w[A AAAA CNAME GEO MX NS SPF SRV TXT URL PTR CNAME NS]
end