From 000ab244d6596cd7d4b3451a7f55e8c1121a98af Mon Sep 17 00:00:00 2001 From: Rupak Ganguly Date: Mon, 7 Oct 2013 14:53:50 -0400 Subject: [PATCH] [hp|dns] Add DNS model tests and updated models and mocks. --- lib/fog/hp/models/dns/domain.rb | 55 +++++++++++++++++ lib/fog/hp/models/dns/domains.rb | 20 ++----- lib/fog/hp/models/dns/record.rb | 59 +++++++++++++++++++ lib/fog/hp/models/dns/records.rb | 32 ++++++++++ lib/fog/hp/requests/dns/create_domain.rb | 13 ++-- lib/fog/hp/requests/dns/create_record.rb | 24 +++++--- lib/fog/hp/requests/dns/delete_record.rb | 4 +- lib/fog/hp/requests/dns/get_record.rb | 5 +- .../requests/dns/list_records_in_a_domain.rb | 4 ++ lib/fog/hp/requests/dns/update_domain.rb | 12 ++-- lib/fog/hp/requests/dns/update_record.rb | 7 ++- tests/hp/models/dns/domain_tests.rb | 25 ++++++++ tests/hp/models/dns/domains_tests.rb | 14 +++++ tests/hp/models/dns/record_tests.rb | 28 +++++++++ tests/hp/models/dns/records_tests.rb | 9 +++ tests/hp/requests/dns/domain_tests.rb | 1 + tests/hp/requests/dns/records_tests.rb | 7 ++- 17 files changed, 279 insertions(+), 40 deletions(-) create mode 100644 tests/hp/models/dns/domain_tests.rb create mode 100644 tests/hp/models/dns/domains_tests.rb create mode 100644 tests/hp/models/dns/record_tests.rb create mode 100644 tests/hp/models/dns/records_tests.rb diff --git a/lib/fog/hp/models/dns/domain.rb b/lib/fog/hp/models/dns/domain.rb index e69de29bb..ff5d04d13 100644 --- a/lib/fog/hp/models/dns/domain.rb +++ b/lib/fog/hp/models/dns/domain.rb @@ -0,0 +1,55 @@ +require 'fog/core/model' + +module Fog + module HP + class DNS + + class Domain < Fog::Model + identity :id + + attribute :name + attribute :description + attribute :email + attribute :ttl + attribute :serial + attribute :created_at + attribute :updated_at + + def destroy + requires :id + service.delete_domain(id) + true + end + + def records + @records ||= begin + Fog::HP::DNS::Records.new({ + :service => service, + :domain => self + }) + end + end + + def save + identity ? update : create + end + + private + + def create + ### Inconsistent API behavior - does not return 'domain' + merge_attributes(service.create_domain(self.name, self.email, attributes).body) + true + end + + def update + requires :id + ### Inconsistent API behavior - does not return 'domain' + merge_attributes(service.update_domain(id, attributes).body) + true + end + + end + end + end +end diff --git a/lib/fog/hp/models/dns/domains.rb b/lib/fog/hp/models/dns/domains.rb index 1e2b10a29..4f1aa9811 100644 --- a/lib/fog/hp/models/dns/domains.rb +++ b/lib/fog/hp/models/dns/domains.rb @@ -1,5 +1,5 @@ require 'fog/core/collection' -require 'fog/hp/models/dns/domains' +require 'fog/hp/models/dns/domain' module Fog module HP @@ -7,23 +7,15 @@ module Fog class Domains < Fog::Collection - attribute :filters + model Fog::HP::DNS::Domain - model Fog::HP::DNS::Domains - - def initialize(attributes) - self.filters ||= {} - super - end - - def all(filters = filters) - self.filters = filters - non_aliased_filters = Fog::HP.convert_aliased_attributes_to_original(self.model, filters) - load(service.list_domains(non_aliased_filters).body['domains']) + def all + load(service.list_domains.body['domains']) end def get(domain_id) - if domain = service.get_domain(domain_id).body['domain'] + ### Inconsistent API - does not return a 'domain' + if domain = service.get_domain(domain_id).body new(domain) end rescue Fog::HP::DNS::NotFound diff --git a/lib/fog/hp/models/dns/record.rb b/lib/fog/hp/models/dns/record.rb index e69de29bb..d2bae4232 100644 --- a/lib/fog/hp/models/dns/record.rb +++ b/lib/fog/hp/models/dns/record.rb @@ -0,0 +1,59 @@ +require 'fog/core/model' + +module Fog + module HP + class DNS + + class Record < Fog::Model + identity :id + + attribute :name + attribute :description + attribute :domain_id + attribute :type + attribute :ttl + attribute :data + attribute :priority + attribute :created_at + attribute :updated_at + + def initialize(new_attributes = {}) + super(new_attributes) + self.domain_id = domain.id if domain + self + end + + def destroy + requires :id, :domain_id + service.delete_record(self.domain_id, id) + true + end + + def save + identity ? update : create + end + + private + + def domain + collection.domain + end + + def create + requires :domain_id + ### Inconsistent API behavior - does not return 'record' + merge_attributes(service.create_record(self.domain_id, self.name, self.type, self.data, attributes).body) + true + end + + def update + requires :id, :domain_id + ### Inconsistent API behavior - does not return 'domain' + merge_attributes(service.update_record(self.domain_id, id, attributes).body) + true + end + + end + end + end +end diff --git a/lib/fog/hp/models/dns/records.rb b/lib/fog/hp/models/dns/records.rb index e69de29bb..7752e4d79 100644 --- a/lib/fog/hp/models/dns/records.rb +++ b/lib/fog/hp/models/dns/records.rb @@ -0,0 +1,32 @@ +require 'fog/core/collection' +require 'fog/hp/models/dns/record' + +module Fog + module HP + class DNS + + class Records < Fog::Collection + + model Fog::HP::DNS::Record + + attr_accessor :domain + + def all + requires :domain + load(service.list_records_in_a_domain(domain.id).body['records']) + end + + def get(record_id) + requires :domain + ### Inconsistent API - does not return a 'record' + if record = service.get_record(domain.id, record_id).body + new(record) + end + rescue Fog::HP::DNS::NotFound + nil + end + + end + end + end +end diff --git a/lib/fog/hp/requests/dns/create_domain.rb b/lib/fog/hp/requests/dns/create_domain.rb index ceb3ac367..a0d248849 100644 --- a/lib/fog/hp/requests/dns/create_domain.rb +++ b/lib/fog/hp/requests/dns/create_domain.rb @@ -5,9 +5,10 @@ module Fog # Create a new DNS domain # # ==== Parameters - # * 'name'<~String> - Name of domain - # * 'email'<~String> - email for the domain + # * 'name'<~String> - Name of the domain + # * 'email'<~String> - Email for the domain # * options<~Hash>: + # * 'description'<~String> - Description for the domain # * 'ttl'<~String> - TTL for the domain # # ==== Returns @@ -15,6 +16,7 @@ module Fog # * body<~Hash>: # * 'id'<~String> - UUID of the domain # * 'name'<~String> - Name of the domain + # * 'description'<~String> - Description for the domain # * 'ttl'<~Integer> - TTL for the domain # * 'email'<~String> - Email for the domain # * 'serial'<~Integer> - Serial number for the domain @@ -26,7 +28,7 @@ module Fog :email => email } - l_options = [:ttl] + l_options = [:description, :ttl] l_options.select{|o| options[o]}.each do |key| data[key] = options[key] end @@ -48,9 +50,10 @@ module Fog data = { 'id' => Fog::HP::Mock.uuid.to_s, 'name' => name || 'domain1.com.', - 'ttl' => options[:ttl] || 3600, 'email' => email || 'nsadmin@example.org', - 'serial' => 1351800588, + '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 diff --git a/lib/fog/hp/requests/dns/create_record.rb b/lib/fog/hp/requests/dns/create_record.rb index ed6986018..614a97480 100644 --- a/lib/fog/hp/requests/dns/create_record.rb +++ b/lib/fog/hp/requests/dns/create_record.rb @@ -6,30 +6,38 @@ module Fog # Create a new DNS record # # ==== Parameters + # * 'domain_id'<~String> - UUId of the domain # * 'name'<~String> - Name of record # * 'type'<~String> - Type of the record i.e. 'A' # * 'data'<~String> - Data required by the record - # * 'priority'<~String> - Priority + # * options<~Hash>: + # * 'description'<~String> - Description for the record + # * 'priority'<~Integer> - Priority + # * 'ttl'<~Integer> - TTL of the record # # ==== Returns # * response<~Excon::Response>: # * body<~Hash>: # * 'id'<~String> - UUID of the record # * 'name'<~String> - Name of the record + # * 'description'<~String> - Description for the record # * 'type'<~String> - Type of the record # * 'domain_id'<~String> - UUID of the domain # * 'ttl'<~Integer> - TTL of the record # * 'data'<~String> - Data required by the record - # * 'priority'<~String> - Priority for the record + # * 'priority'<~Integer> - Priority for the record # * 'created_at'<~String> - created date time stamp # * 'updated_at'<~String> - updated date time stamp - def create_record(domain_id, name, type, data, priority=nil) + def create_record(domain_id, name, type, data, options={}) data = { :name => name, :type => type, :data => data } - data[:priority] = priority.to_i unless priority.nil? + l_options = [:description, :priority, :ttl] + l_options.select{|o| options[o]}.each do |key| + data[key] = options[key] + end request( :body => Fog::JSON.encode(data), @@ -41,8 +49,7 @@ module Fog end end class Mock - def create_record(domain_id, name, type, data, priority) - priority = priority.to_i unless priority.nil? + 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 @@ -52,8 +59,9 @@ module Fog 'name' => name || 'www.example.com.', 'type' => type || 'A', 'data' => data || '15.185.172.152', - 'ttl' => 3600, - 'priority' => priority || nil, + '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' } diff --git a/lib/fog/hp/requests/dns/delete_record.rb b/lib/fog/hp/requests/dns/delete_record.rb index ef64de362..a494ad1ae 100644 --- a/lib/fog/hp/requests/dns/delete_record.rb +++ b/lib/fog/hp/requests/dns/delete_record.rb @@ -5,8 +5,8 @@ module Fog # Delete a DNS Record # # ==== Parameters - # * domain_id<~Integer> - Id of domain for record - # * record_id<~Integer> - Id of record to delete + # * 'domain_id'<~String> - UUId of domain for record + # * 'record_id'<~String> - UUId of record to delete # def delete_record(domain_id, record_id) request( diff --git a/lib/fog/hp/requests/dns/get_record.rb b/lib/fog/hp/requests/dns/get_record.rb index d8dd0ccbf..ecef4aef9 100644 --- a/lib/fog/hp/requests/dns/get_record.rb +++ b/lib/fog/hp/requests/dns/get_record.rb @@ -6,14 +6,15 @@ module Fog # Get details of an existing DNS record # # ==== Parameters - # * domain_id<~String> - UUId of domain for record - # * record_id<~String> - UUId of record to get + # * 'domain_id'<~String> - UUId of domain for record + # * 'record_id'<~String> - UUId of record to get # # ==== Returns # * response<~Excon::Response>: # * body<~Hash>: # * 'id'<~String> - UUID of the record # * 'name'<~String> - Name of the record + # * 'description'<~String> - Description for the record # * 'type'<~String> - Type of the record # * 'domain_id'<~String> - UUId of the domain # * 'ttl'<~Integer> - TTL of the record diff --git a/lib/fog/hp/requests/dns/list_records_in_a_domain.rb b/lib/fog/hp/requests/dns/list_records_in_a_domain.rb index ae7d51741..35bb7837c 100644 --- a/lib/fog/hp/requests/dns/list_records_in_a_domain.rb +++ b/lib/fog/hp/requests/dns/list_records_in_a_domain.rb @@ -4,12 +4,16 @@ module Fog class Real # List DNS records for a given domain # + # ==== Parameters + # * 'domain_id'<~String> - UUId of domain for record + # # ==== Returns # * response<~Excon::Response>: # * body<~Hash>: # * 'records'<~Array>: # * 'id'<~String> - UUID of the record # * 'name'<~String> - Name of the record + # * 'description'<~String> - Description for the record # * 'type'<~String> - Type of the record # * 'domain_id'<~String> - UUID of the domain # * 'ttl'<~Integer> - TTL of the record diff --git a/lib/fog/hp/requests/dns/update_domain.rb b/lib/fog/hp/requests/dns/update_domain.rb index a2b5769af..00e52cf96 100644 --- a/lib/fog/hp/requests/dns/update_domain.rb +++ b/lib/fog/hp/requests/dns/update_domain.rb @@ -9,6 +9,7 @@ module Fog # * domain_id<~String> - UUId of domain to delete # * options<~Hash>: # * 'name'<~String> - Name of domain + # * 'description'<~String> - Description for the domain # * 'ttl'<~String> - TTL for the domain # * 'email'<~String> - email for the domain # @@ -17,12 +18,14 @@ module Fog # * body<~Hash>: # * 'id'<~String> - UUID of the domain # * 'name'<~String> - Name of the domain + # * 'description'<~String> - Description for the domain # * 'ttl'<~Integer> - TTL for the domain # * 'email'<~String> - Email for the domain # * 'serial'<~Integer> - Serial number for the domain # * 'created_at'<~String> - created date time stamp def update_domain(domain_id, options={}) - l_options = [:name, :ttl, :email] + data = {} + l_options = [:name, :description, :ttl, :email] l_options.select{|o| options[o]}.each do |key| data[key] = options[key] end @@ -41,9 +44,10 @@ module Fog response = Excon::Response.new if domain = list_domains.body['domains'].detect { |_| _['id'] == domain_id } - domain['name'] = options[:name] if options[:name] - domain['ttl'] = options[:ttl] if options[:ttl] - domain['email'] = options[:email] if options[:email] + 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 diff --git a/lib/fog/hp/requests/dns/update_record.rb b/lib/fog/hp/requests/dns/update_record.rb index d436441ba..553994db9 100644 --- a/lib/fog/hp/requests/dns/update_record.rb +++ b/lib/fog/hp/requests/dns/update_record.rb @@ -6,19 +6,22 @@ module Fog # Update an existing DNS record # # ==== Parameters - # * domain_id<~String> - UUId of domain to delete - # * record_id<~String> - UUId of record to get + # * 'domain_id'<~String> - UUId of domain of record + # * 'record_id'<~String> - UUId of record to update # * options<~Hash>: # * 'name'<~String> - Name of record + # * 'description'<~String> - Description for the record # * 'type'<~String> - Type of the record i.e. 'A' # * 'data'<~String> - Data required by the record # * 'priority'<~Integer> - Priority + # * 'ttl'<~Integer> - TTL of the record # # ==== Returns # * response<~Excon::Response>: # * body<~Hash>: # * 'id'<~String> - UUID of the record # * 'name'<~String> - Name of the record + # * 'description'<~String> - Description for the record # * 'type'<~String> - Type of the record # * 'domain_id'<~String> - UUID of the domain # * 'ttl'<~Integer> - TTL of the record diff --git a/tests/hp/models/dns/domain_tests.rb b/tests/hp/models/dns/domain_tests.rb new file mode 100644 index 000000000..cf69f431e --- /dev/null +++ b/tests/hp/models/dns/domain_tests.rb @@ -0,0 +1,25 @@ +Shindo.tests('HP::DNS | domain model', ['hp', 'dns', 'domain']) do + + attributes = {:name => 'www.fogtest.com.', :email => 'test@fogtest.com'} + model_tests(HP[:dns].domains, attributes, true) + + tests('success') do + + tests('#create').succeeds do + attributes = {:name => 'www.fogtest.com.', :email => 'test@fogtest.com', :ttl => 3600} + @domain = HP[:dns].domains.create(attributes) + !@domain.id.nil? + end + + tests('Update via #save').succeeds do + @domain.email = 'update@fogtest.com' + @domain.save + end + + tests('#destroy').succeeds do + @domain.destroy + end + + end + +end diff --git a/tests/hp/models/dns/domains_tests.rb b/tests/hp/models/dns/domains_tests.rb new file mode 100644 index 000000000..5b92d66ef --- /dev/null +++ b/tests/hp/models/dns/domains_tests.rb @@ -0,0 +1,14 @@ +Shindo.tests('HP::DNS | domains collection', ['hp', 'dns', 'domains']) do + + attributes = {:name => 'www.fogtest.com.', :email => 'test@fogtest.com', :ttl => 3600} + collection_tests(HP[:dns].domains, attributes, true) + + tests('success') do + + tests('#all').succeeds do + HP[:dns].domains.all + end + + end + +end \ No newline at end of file diff --git a/tests/hp/models/dns/record_tests.rb b/tests/hp/models/dns/record_tests.rb new file mode 100644 index 000000000..d9acfcba6 --- /dev/null +++ b/tests/hp/models/dns/record_tests.rb @@ -0,0 +1,28 @@ +Shindo.tests('HP::DNS | record model', ['hp', 'dns', 'record']) do + + @domain = HP[:dns].domains.create({:name => 'www.fogtest.com.', :email => 'test@fogtest.com'}) + + attributes = {:domain_id => @domain.id, :name => 'www.fogtest.com.', :type => 'A', :data => '15.185.100.152'} + model_tests(@domain.records, attributes, true) + + tests('success') do + + tests('#create').succeeds do + attributes = {:domain_id => @domain.id, :name => 'www.fogtest.com.', :type => 'A', :data => '15.185.200.152', :description => 'test record'} + @record = HP[:dns].records.create(attributes) + !@record.id.nil? + end + + tests('Update via #save').succeeds do + @record.name = 'www.fogupdate.com.' + @record.save + end + + tests('#destroy').succeeds do + @record.destroy + end + + end + + @domain.destroy +end diff --git a/tests/hp/models/dns/records_tests.rb b/tests/hp/models/dns/records_tests.rb new file mode 100644 index 000000000..ddf6093e6 --- /dev/null +++ b/tests/hp/models/dns/records_tests.rb @@ -0,0 +1,9 @@ +Shindo.tests('HP::DNS | records collection', ['hp', 'dns', 'records']) do + + @domain = HP[:dns].domains.create({:name => 'www.fogtest.com.', :email => 'test@fogtest.com'}) + + attributes = {:domain_id => @domain.id, :name => 'www.fogtest.com.', :type => 'A', :data => '15.185.172.152'} + collection_tests(@domain.records, attributes, true) + + @domain.destroy +end \ No newline at end of file diff --git a/tests/hp/requests/dns/domain_tests.rb b/tests/hp/requests/dns/domain_tests.rb index c05c0c88a..b33ef756d 100644 --- a/tests/hp/requests/dns/domain_tests.rb +++ b/tests/hp/requests/dns/domain_tests.rb @@ -2,6 +2,7 @@ Shindo.tests("HP::DNS | domain requests", ['hp', 'dns', 'domain']) do @domain_format = { 'id' => String, 'name' => String, + 'description' => String, 'ttl' => Integer, 'serial' => Integer, 'email' => String, diff --git a/tests/hp/requests/dns/records_tests.rb b/tests/hp/requests/dns/records_tests.rb index 48cc1a13e..da3e5be17 100644 --- a/tests/hp/requests/dns/records_tests.rb +++ b/tests/hp/requests/dns/records_tests.rb @@ -2,11 +2,12 @@ Shindo.tests("HP::DNS | record requests", ['hp', 'dns', 'record']) do @record_format = { 'id' => String, 'name' => String, + 'description' => String, 'type' => String, 'domain_id' => String, 'ttl' => Integer, 'data' => String, - 'priority' => Integer, + 'priority' => Fog::Nullable::Integer, 'created_at' => String, 'updated_at' => String } @@ -19,8 +20,8 @@ Shindo.tests("HP::DNS | record requests", ['hp', 'dns', 'record']) do @record_name = 'www.fogtest.com.' @record_data = '15.185.172.152' - tests("#create_record(#{@domain_id}, #{@record_name}, 'A', #{@record_data}, 1)").formats(@record_format) do - data = HP[:dns].create_record(@domain_id, @record_name, 'A', @record_data, 1).body + tests("#create_record(#{@domain_id}, #{@record_name}, 'A', #{@record_data})").formats(@record_format) do + data = HP[:dns].create_record(@domain_id, @record_name, 'A', @record_data).body @record_id = data['id'] data end