Skip to content

Commit

Permalink
All Real tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
dldinternet committed Apr 1, 2017
1 parent 64ee727 commit 5f8b97c
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 28 deletions.
3 changes: 3 additions & 0 deletions lib/fog/digitalocean/models/dns/domain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ module Fog
module DNS
class DigitalOcean
class Domain < Fog::Model

has_many :records, :records

identity :name
attribute :ttl
attribute :zone_file
Expand Down
21 changes: 21 additions & 0 deletions lib/fog/digitalocean/models/dns/domains.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,27 @@ def get(id)
rescue Fog::Errors::NotFound
nil
end

def delete(id)
id = id.name if id.is_a?(Fog::Model)
id = id.with_indifferent_access[:name] if id.is_a?(Hash)
return false unless id
response = service.delete_domain(id)
if response.status == 204
self.replace(self.select{ |dom| !dom.name.eql?(id)})
true
else
false
end
rescue Fog::Errors::NotFound
nil
end

def create(attributes = {})
object = super
self << object
object
end
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/fog/digitalocean/models/dns/record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ module Fog
module DNS
class DigitalOcean
class Record < Fog::Model

# provider_class :Record
# collection_name :records

identity :id # number A unique identifier for each domain record.
attribute :type # string The type of the DNS record (ex: A, CNAME, TXT, ...).
attribute :name # string The name to use for the DNS record.
Expand Down Expand Up @@ -46,7 +50,7 @@ def update(delta={})

def save
options = attributes_to_options('CREATE')
data = service.create_record(domain.name, options).body['domain_record']
data = (self.id ? service.update_record(domain.name, options) : service.create_record(domain.name, options)).body['domain_record']
merge_attributes(data)
true
end
Expand Down
25 changes: 24 additions & 1 deletion lib/fog/digitalocean/models/dns/records.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class Records < Fog::Compute::DigitalOcean::PagingCollection
attribute :domain

model Fog::DNS::DigitalOcean::Record
has_one :domain, :domain


# Returns list of records
# @return [Fog::DNS::DigitalOceanV2::Records] Retrieves a list of domains.
Expand All @@ -18,7 +20,7 @@ class Records < Fog::Compute::DigitalOcean::PagingCollection
# @see https://developers.digitalocean.com/documentation/v2/#list-all-keys
def all(filters = {})
data = service.list_records(domain.name, filters)
links = data.body['meta']['links']
links = data.body['links']
get_paged_links(links)
keys = data.body["domain_records"]
load(keys)
Expand Down Expand Up @@ -52,6 +54,27 @@ def get(id)
nil
end

def create(attributes = {})
object = super
self.replace(self.all!)
object
end

def delete(id)
id = id.id if id.is_a?(Fog::Model)
id = id.with_indifferent_access[:id] if id.is_a?(Hash)
return false unless id
response = service.delete_record(self.domain.name, id)
if response.status == 204
self.replace(self.select{ |dom| dom.id != id })
true
else
false
end
rescue Fog::Errors::NotFound
nil
end

def new(attributes = {})
requires :domain
super({ :domain => domain }.merge!(attributes))
Expand Down
29 changes: 16 additions & 13 deletions tests/digitalocean/models/dns/record_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,24 @@
@domain = Fog::DNS[:digitalocean].domains.create(name: generate_unique_domain, ip_address: '5.5.5.5')
end

params = { :name => @domain.name, :type => 'A', :data => '1.2.3.4' }
[
{ :name => "#{@domain.name}.", :type => 'A', :data => '1.2.3.4' },
{ :name => '@', :type => 'A', :data => '1.2.3.4' },
].each do |params|
model_tests(@domain.records, params) do |instance|

# Newly created records should have a change id
tests("#id") do
returns(true) { instance.id != nil }
end

tests("#modify").succeeds do
new_value = '5.5.5.5'
returns(true) { instance.modify('data' => new_value) }
returns(new_value) { instance.data }
end

model_tests(@domain.records, params) do |instance|

# Newly created records should have a change id
tests("#id") do
returns(true) { instance.id != nil }
end

tests("#modify").succeeds do
new_value = '5.5.5.5'
returns(true) { instance.modify('data' => new_value) }
returns(new_value) { instance.data }
end

end

tests("domain.destroy").succeeds do
Expand Down
6 changes: 3 additions & 3 deletions tests/digitalocean/models/dns/records_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
{ :name => "#{@domain.name}.", :type => 'A', :ttl => 3600, :data => '1.2.3.4' },
{ :name => '@', :type => 'A', :ttl => 3600, :data => '5.6.7.8' },
# CNAME record
{ :name => "www", :fqdn => "www.#{@domain.name}.", :type => "CNAME", :ttl => 300, :data => "#{@domain.name}." }
{ :name => "www", :type => "CNAME", :ttl => 300, :data => "#{@domain.name}." }
]

param_groups.each do |params|
Expand All @@ -40,8 +40,8 @@
end

tests("#all wildcard parsing").returns(true) do
set = @domain.records.map(&:name)
set.include?("*.#{@domain.name}.")
set = @domain.records.all!.map(&:name)
set.include?('*') || set.include?("*.#{@domain.name}.")
end

records.each do |record|
Expand Down
6 changes: 3 additions & 3 deletions tests/digitalocean/requests/dns/list_records_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
'type' => String,
'name' => String,
'data' => String,
'priority' => Integer,
'port' => Integer,
'weight' => Integer,
# 'priority' => Integer,
# 'port' => Integer,
# 'weight' => Integer,
}

tests('success') do
Expand Down
16 changes: 9 additions & 7 deletions tests/helpers/collection_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
def collection_tests(collection, params = {}, mocks_implemented = true)
def fqdn(params)
@fqdn ||= params[:fqdn] ? params[:fqdn] : params[:name]
params[:fqdn] ? params[:fqdn] : params[:name]
end

tests('success') do
Expand All @@ -9,16 +9,17 @@ def fqdn(params)
pending if Fog.mocking? && !mocks_implemented
coll = collection.new(params)
@collection_size = coll.collection.size
true
collection.size == coll.collection.size
end

tests("#create(#{params.inspect})").succeeds do
tests("#create(#{params.inspect})").returns(fqdn(params)) do
pending if Fog.mocking? && !mocks_implemented
@instance = collection.create(params)
@instance.name.eql?(fqdn(params)) || @instance.name.eql?('@')
@collection_size = collection.size
@instance.name.eql?('@') ? fqdn(params) : @instance.name
end

tests("#all").returns(@collection_size+1) do
tests("#{collection.class.name}#all").returns(@collection_size) do
pending if Fog.mocking? && !mocks_implemented
coll = collection.all
coll.size
Expand All @@ -31,7 +32,7 @@ def fqdn(params)
tests("#get(#{@identity})").returns(fqdn(params)) do
pending if Fog.mocking? && !mocks_implemented
record = collection.get(@identity)
record.name
record.name.eql?('@') ? fqdn(params) : record.name
end

tests('Enumerable') do
Expand Down Expand Up @@ -79,7 +80,8 @@ def fqdn(params)
end

if !Fog.mocking? || mocks_implemented
@instance.destroy
collection.delete(@instance)
@collection_size = collection.size
end
end

Expand Down

0 comments on commit 5f8b97c

Please sign in to comment.