Class: Fog::HP::DNS::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/hp/dns.rb,
lib/fog/hp/requests/dns/get_domain.rb,
lib/fog/hp/requests/dns/get_record.rb,
lib/fog/hp/requests/dns/list_domains.rb,
lib/fog/hp/requests/dns/create_domain.rb,
lib/fog/hp/requests/dns/create_record.rb,
lib/fog/hp/requests/dns/delete_domain.rb,
lib/fog/hp/requests/dns/delete_record.rb,
lib/fog/hp/requests/dns/update_domain.rb,
lib/fog/hp/requests/dns/update_record.rb,
lib/fog/hp/requests/dns/list_records_in_a_domain.rb,
lib/fog/hp/requests/dns/get_servers_hosting_domain.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



46
47
48
# File 'lib/fog/hp/dns.rb', line 46

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

Class Method Details

.dataObject



33
34
35
36
37
38
39
40
# File 'lib/fog/hp/dns.rb', line 33

def self.data
  @data ||= Hash.new do |hash, key|
    hash[key] = {
      :domains => {},
      :records => {}
    }
  end
end

.resetObject



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

def self.reset
  @data = nil
end

Instance Method Details

#create_domain(name, email, options = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fog/hp/requests/dns/create_domain.rb', line 45

def create_domain(name, email, options={})
  response        = Excon::Response.new
  response.status = 200

  data = {
      'id'         => Fog::HP::Mock.uuid.to_s,
      'name'       => name || 'domain1.com.',
      'email'      => email || '[email protected]',
      'description' => options[:description] || 'desc domain1.com.',
      'ttl'        => options[:ttl] || 3600,
      'serial'     => Fog::Mock.random_numbers(10).to_i,
      'created_at' => '2012-01-01T13:32:20Z'
  }
  self.data[:domains][data['id']] = data
  response.body = data
  response
end

#create_record(domain_id, name, type, data, options = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/fog/hp/requests/dns/create_record.rb', line 50

def create_record(domain_id, name, type, data, options={})
  response        = Excon::Response.new
  if list_domains.body['domains'].find {|_| _['id'] == domain_id}
    response.status = 200
    data = {
        'id'           => Fog::HP::Mock.uuid.to_s,
        'domain_id'    => domain_id,
        'name'         => name || 'www.example.com.',
        'type'         => type || 'A',
        'data'         => data || '15.185.172.152',
        'ttl'          => options['ttl'] || 3600,
        'description'  => options['description'] || 'desc for www.example.com.',
        'priority'     => options['priority'] || nil,
        'created_at'   => '2012-11-02T19:56:26.366792',
        'updated_at'   => '2012-11-02T19:56:26.366792'
    }
    self.data[:records][data['id']] = data
    response.body = data
    response
  else
    raise Fog::HP::DNS::NotFound
  end
end

#dataObject



50
51
52
# File 'lib/fog/hp/dns.rb', line 50

def data
  self.class.data[@hp_access_key]
end

#delete_domain(domain_id) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/fog/hp/requests/dns/delete_domain.rb', line 20

def delete_domain(domain_id)
  response = Excon::Response.new
  if list_domains.body['domains'].find { |_| _['id'] == domain_id }
    response.status = 202
    response
  else
    raise Fog::HP::DNS::NotFound
  end
end

#delete_record(domain_id, record_id) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/fog/hp/requests/dns/delete_record.rb', line 21

def delete_record(domain_id, record_id)
  response = Excon::Response.new
  if list_records_in_a_domain(domain_id).body['records'].find { |_| _['id'] == record_id }
    response.status = 200
    response
  else
    raise Fog::HP::DNS::NotFound
  end
  response
end

#dummy_serversObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/fog/hp/requests/dns/get_servers_hosting_domain.rb', line 41

def dummy_servers
  [
      {
          'id'         => Fog::HP::Mock.uuid.to_s,
          'name'       => 'ns1.provider.com.',
          'created_at' => '2012-01-01T13:32:20Z',
          'updated_at' => '2012-01-01T13:32:20Z'
      },
      {
          'id'         => Fog::HP::Mock.uuid.to_s,
          'name'       => 'ns2.provider.com.',
          'created_at' => '2012-01-01T13:32:20Z',
          'updated_at' => '2012-01-01T13:32:20Z'
      },
  ]
end

#get_domain(domain_id) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/fog/hp/requests/dns/get_domain.rb', line 28

def get_domain(domain_id)
  response = Excon::Response.new
  if domain = list_domains.body['domains'].find { |_| _['id'] == domain_id }
    response.status = 200
    response.body = domain
    response
  else
    raise Fog::HP::DNS::NotFound
  end
end

#get_record(domain_id, record_id) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/fog/hp/requests/dns/get_record.rb', line 33

def get_record(domain_id, record_id)
  response = Excon::Response.new
  if record = list_records_in_a_domain(domain_id).body['records'].find { |_| _['id'] == record_id }
    response.status = 200
    response.body = record
    response
  else
    raise Fog::HP::DNS::NotFound
  end
end

#get_servers_hosting_domain(domain_id) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/fog/hp/requests/dns/get_servers_hosting_domain.rb', line 30

def get_servers_hosting_domain(domain_id)
  response = Excon::Response.new
  if list_domains.body['domains'].find { |_| _['id'] == domain_id }
    response.status = 200
    response.body   = { 'servers' => dummy_servers }
    response
  else
    raise Fog::HP::DNS::NotFound
  end
end

#list_domainsObject



26
27
28
29
30
31
32
# File 'lib/fog/hp/requests/dns/list_domains.rb', line 26

def list_domains
  response        = Excon::Response.new
  domains = self.data[:domains].values
  response.status = 200
  response.body = { 'domains' => domains }
  response
end

#list_records_in_a_domain(domain_id) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/fog/hp/requests/dns/list_records_in_a_domain.rb', line 33

def list_records_in_a_domain(domain_id)
  response = Excon::Response.new
  if domain = list_domains.body['domains'].find { |_| _['id'] == domain_id }
    response.status = 200
    response.body = { 'records' => records_for_domain(domain_id) }
  else
    raise Fog::HP::DNS::NotFound
  end
  response
end

#records_for_domain(domain_id) ⇒ Object



44
45
46
47
48
49
# File 'lib/fog/hp/requests/dns/list_records_in_a_domain.rb', line 44

def records_for_domain(domain_id)
  rdata = data[:records].select { |_,v| v['domain_id'] == domain_id}
  records = []
  rdata.each { |_,v| records << v }
  records
end

#reset_dataObject



54
55
56
# File 'lib/fog/hp/dns.rb', line 54

def reset_data
  self.class.data.delete(@hp_access_key)
end

#update_domain(domain_id, options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fog/hp/requests/dns/update_domain.rb', line 42

def update_domain(domain_id, options={})
  response = Excon::Response.new
  if domain = list_domains.body['domains'].find { |_| _['id'] == domain_id }

    domain['name']          = options[:name]   if options[:name]
    domain['description']   = options[:description]   if options[:description]
    domain['ttl']           = options[:ttl]    if options[:ttl]
    domain['email']         = options[:email]  if options[:email]

    response.status = 200
    response.body   = domain
    response
  else
    raise Fog::HP::DNS::NotFound
  end
end

#update_record(domain_id, record_id, options) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fog/hp/requests/dns/update_record.rb', line 48

def update_record(domain_id, record_id, options)
  response = Excon::Response.new
  if record = list_records_in_a_domain(domain_id).body['records'].find { |_| _['id'] == record_id }
    record['name']      = options[:name]      if options[:name]
    record['type']      = options[:type]      if options[:type]
    record['data']      = options[:data]      if options[:data]
    record['priority']  = options[:priority]  if options[:priority]

    response.status = 200
    response.body   = record
    response
  else
    raise Fog::HP::DNS::NotFound
  end
end