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.



48
49
50
# File 'lib/fog/hp/dns.rb', line 48

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

Class Method Details

.dataObject



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

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

.resetObject



44
45
46
# File 'lib/fog/hp/dns.rb', line 44

def self.reset
  @data = nil
end

Instance Method Details

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



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

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



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

def create_record(domain_id, name, type, data, options={})
  response        = Excon::Response.new
  if list_domains.body['domains'].detect {|_| _['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



52
53
54
# File 'lib/fog/hp/dns.rb', line 52

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

#delete_domain(domain_id) ⇒ Object



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

def delete_domain(domain_id)
  response = Excon::Response.new
  if list_domains.body['domains'].detect { |_| _['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'].detect { |_| _['id'] == record_id }
    response.status = 200
    response
  else
    raise Fog::HP::DNS::NotFound
  end
  response
end

#dummy_serversObject



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

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



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

def get_domain(domain_id)
  response = Excon::Response.new
  if domain = list_domains.body['domains'].detect { |_| _['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



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

def get_record(domain_id, record_id)
  response = Excon::Response.new
  if record = list_records_in_a_domain(domain_id).body['records'].detect { |_| _['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



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

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

#list_domainsObject



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

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



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

def list_records_in_a_domain(domain_id)
  response = Excon::Response.new
  if domain = list_domains.body['domains'].detect { |_| _['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



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

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



56
57
58
# File 'lib/fog/hp/dns.rb', line 56

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

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



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

def update_domain(domain_id, options={})
  response = Excon::Response.new
  if domain = list_domains.body['domains'].detect { |_| _['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



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

def update_record(domain_id, record_id, options)
  response = Excon::Response.new
  if record = list_records_in_a_domain(domain_id).body['records'].detect { |_| _['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