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

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/zerigo/requests/dns/get_host.rb,
lib/fog/zerigo/dns.rb,
lib/fog/zerigo/requests/dns/get_zone.rb,
lib/fog/zerigo/requests/dns/find_hosts.rb,
lib/fog/zerigo/requests/dns/list_hosts.rb,
lib/fog/zerigo/requests/dns/list_zones.rb,
lib/fog/zerigo/requests/dns/count_hosts.rb,
lib/fog/zerigo/requests/dns/count_zones.rb,
lib/fog/zerigo/requests/dns/create_host.rb,
lib/fog/zerigo/requests/dns/create_zone.rb,
lib/fog/zerigo/requests/dns/delete_host.rb,
lib/fog/zerigo/requests/dns/delete_zone.rb,
lib/fog/zerigo/requests/dns/update_host.rb,
lib/fog/zerigo/requests/dns/update_zone.rb,
lib/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.



42
43
44
45
# File 'lib/fog/zerigo/dns.rb', line 42

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

Class Method Details

.dataObject



32
33
34
35
36
# File 'lib/fog/zerigo/dns.rb', line 32

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

.resetObject



38
39
40
# File 'lib/fog/zerigo/dns.rb', line 38

def self.reset
  @data = nil
end

Instance Method Details

#count_hosts(zone_id) ⇒ Object



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

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



26
27
28
29
30
31
32
33
# File 'lib/fog/zerigo/requests/dns/count_zones.rb', line 26

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



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
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
# File 'lib/fog/zerigo/requests/dns/create_host.rb', line 63

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



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/fog/zerigo/requests/dns/create_zone.rb', line 86

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



47
48
49
# File 'lib/fog/zerigo/dns.rb', line 47

def data
  self.class.data
end

#delete_host(host_id) ⇒ Object



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

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



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

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



59
60
61
# File 'lib/fog/zerigo/dns.rb', line 59

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

#find_by_zone_id(zone_id) ⇒ Object



55
56
57
# File 'lib/fog/zerigo/dns.rb', line 55

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

#find_host(host_id) ⇒ Object



63
64
65
# File 'lib/fog/zerigo/dns.rb', line 63

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

#find_hosts(fqdn, zone_id = nil) ⇒ Object



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

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].map { |z| z['hosts'].find { |h| h['fqdn'] == fqdn } }.compact

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

  response
end

#get_host(host_id) ⇒ Object



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

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



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

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



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

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, options = {}) ⇒ Object



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
# File 'lib/fog/zerigo/requests/dns/list_hosts.rb', line 43

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

  response = Excon::Response.new

  if zone
    if options.empty?
      response.status = 200
      response.body = {
        "hosts" => zone["hosts"]
      }
    else
      hosts = zone["hosts"]
      hosts = hosts.select {|h| h["fqdn"] == options["fqdn"]} if options["fqdn"]
      hosts = options["per_page"] ? hosts.each_slice(options["per_page"] - 1).to_a : hosts.each_slice(100).to_a
      hosts = options["page"] ? hosts[options["page"]] : hosts[0]
      response.status = 200
      response.body = {
        "hosts" => hosts
      }
    end
  else
    response.status = 404
  end

  response
end

#list_zonesObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/fog/zerigo/requests/dns/list_zones.rb', line 49

def list_zones
  response = Excon::Response.new

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

  response
end

#reset_dataObject



51
52
53
# File 'lib/fog/zerigo/dns.rb', line 51

def reset_data
  self.class.reset
end

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



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

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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/fog/zerigo/requests/dns/update_zone.rb', line 67

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



59
60
61
# File 'lib/fog/zerigo/requests/dns/create_host.rb', line 59

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