Skip to content

Commit

Permalink
Have most of the Models and Requests in place.
Browse files Browse the repository at this point in the history
  • Loading branch information
dldinternet committed Mar 28, 2017
1 parent 2ef2e68 commit 764ea1f
Show file tree
Hide file tree
Showing 16 changed files with 607 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ruby-2.2.5
ruby-2.3.1
130 changes: 126 additions & 4 deletions lib/fog/digitalocean/dns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ class DigitalOcean < Fog::Service
requires :digitalocean_token

model_path 'fog/digitalocean/models/dns'
model :domain
collection :domains
model :record
collection :records
model :zone
collection :zones

request_path 'fog/digitalocean/requests/dns'
request :list_domains
Expand All @@ -27,8 +27,130 @@ class Mock
def self.data
@data ||= Hash.new do |hash, key|
hash[key] = {
:servers => [],
:ssh_keys => []
:domains => [
{
"name" => "domain.com",
"ttl" => 1800,
"zone_file" => "$ORIGIN domain.com.\n$TTL 1800\ndomain.com. IN SOA ns1.digitalocean.com. hostmaster.domain.com. 1490145863 10800 3600 604800 1800\ndomain.com. 1800 IN NS ns1.digitalocean.com.\ndomain.com. 1800 IN NS ns2.digitalocean.com.\ndomain.com. 1800 IN NS ns3.digitalocean.com.\ndomain.com. 1800 IN A 127.0.0.3\n"
},
{
"name" => "domain.net",
"ttl" => 1800,
"zone_file" => "$ORIGIN domain.net.\n$TTL 1800\ndomain.net. IN SOA ns1.digitalocean.com. hostmaster.domain.net. 1488909707 10800 3600 604800 1800\ndomain.net. 1800 IN NS ns1.digitalocean.com.\ndomain.net. 1800 IN NS ns2.digitalocean.com.\ndomain.net. 1800 IN NS ns3.digitalocean.com.\ndomain.net. 1800 IN A 64.99.64.37\n"
},
{
"name" => "domain.org",
"ttl" => 1800,
"zone_file" => "$ORIGIN domain.org.\n$TTL 1800\ndomain.org. IN SOA ns1.digitalocean.com. hostmaster.domain.org. 1488395060 10800 3600 604800 1800\ndomain.org. 1800 IN NS ns1.digitalocean.com.\ndomain.org. 1800 IN NS ns2.digitalocean.com.\ndomain.org. 1800 IN NS ns3.digitalocean.com.\ndomain.org. 3600 IN A 208.38.128.210\ndomain.org. 1800 IN MX 1 aspmx.l.google.com.\ndomain.org. 1800 IN MX 5 alt1.aspmx.l.google.com.\ndomain.org. 1800 IN MX 5 alt2.aspmx.l.google.com.\ndomain.org. 1800 IN MX 10 alt3.aspmx.l.google.com.\ndomain.org. 1800 IN MX 10 alt4.aspmx.l.google.com.\n"
}
],
:domain_records => {
'domain.com' =>
[
{ "id" => Fog::Mock.random_numbers(8).to_i,
"type" => "NS",
"name" => "@",
"data" => "ns1.digitalocean.com",
"priority" => nil,
"port" => nil,
"weight" => nil
},
{ "id" => Fog::Mock.random_numbers(8).to_i,
"type" => "NS",
"name" => "@",
"data" => "ns2.digitalocean.com",
"priority" => nil,
"port" => nil,
"weight" => nil
},
{ "id" => Fog::Mock.random_numbers(8).to_i,
"type" => "NS",
"name" => "@",
"data" => "ns3.digitalocean.com",
"priority" => nil,
"port" => nil,
"weight" => nil
},
{ "id" => Fog::Mock.random_numbers(8).to_i,
"type" => "A",
"name" => "@",
"data" => "127.0.0.1",
"priority" => nil,
"port" => nil,
"weight" => nil
}
],
'domain.net' =>
[
{ "id" => Fog::Mock.random_numbers(8).to_i,
"type" => "NS",
"name" => "@",
"data" => "ns1.digitalocean.com",
"priority" => nil,
"port" => nil,
"weight" => nil
},
{ "id" => Fog::Mock.random_numbers(8).to_i,
"type" => "NS",
"name" => "@",
"data" => "ns2.digitalocean.com",
"priority" => nil,
"port" => nil,
"weight" => nil
},
{ "id" => Fog::Mock.random_numbers(8).to_i,
"type" => "NS",
"name" => "@",
"data" => "ns3.digitalocean.com",
"priority" => nil,
"port" => nil,
"weight" => nil
},
{ "id" => Fog::Mock.random_numbers(8).to_i,
"type" => "A",
"name" => "@",
"data" => "127.0.0.1",
"priority" => nil,
"port" => nil,
"weight" => nil
}
],
'domain.org' =>
[
{ "id" => Fog::Mock.random_numbers(8).to_i,
"type" => "NS",
"name" => "@",
"data" => "ns1.digitalocean.com",
"priority" => nil,
"port" => nil,
"weight" => nil
},
{ "id" => Fog::Mock.random_numbers(8).to_i,
"type" => "NS",
"name" => "@",
"data" => "ns2.digitalocean.com",
"priority" => nil,
"port" => nil,
"weight" => nil
},
{ "id" => Fog::Mock.random_numbers(8).to_i,
"type" => "NS",
"name" => "@",
"data" => "ns3.digitalocean.com",
"priority" => nil,
"port" => nil,
"weight" => nil
},
{ "id" => Fog::Mock.random_numbers(8).to_i,
"type" => "A",
"name" => "@",
"data" => "127.0.0.1",
"priority" => nil,
"port" => nil,
"weight" => nil
}
],
}
}
end
end
Expand Down
28 changes: 28 additions & 0 deletions lib/fog/digitalocean/models/dns/domain.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Fog
module DNS
class DigitalOcean
class Domain < Fog::Model
identity :name
attribute :ttl
attribute :zone_file
# attribute :ip_address

def create
requires :name, :ip_address
merge_attributes(service.create_domain(name, ip_address).body['name'])
true
end

def delete
requires :name
service.delete_domain name
end

def get
requires :name
service.get_domain name
end
end
end
end
end
40 changes: 40 additions & 0 deletions lib/fog/digitalocean/models/dns/domains.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'fog/digitalocean/models/paging_collection'

module Fog
module DNS
class DigitalOcean
class Domains < Fog::Compute::DigitalOcean::PagingCollection
model Fog::DNS::DigitalOcean::Domain

# Returns list of domains
# @return [Fog::DNS::DigitalOceanV2::Domains] Retrieves a list of ssh keys.
# @raise [Fog::DNS::DigitalOceanV2::NotFound] - HTTP 404
# @raise [Fog::DNS::DigitalOceanV2::BadRequest] - HTTP 400
# @raise [Fog::DNS::DigitalOceanV2::InternalServerError] - HTTP 500
# @raise [Fog::DNS::DigitalOceanV2::ServiceError]
# @see https://developers.digitalocean.com/documentation/v2/#list-all-keys
def all(filters = {})
data = service.list_domains(filters)
links = data.body["links"]
get_paged_links(links)
keys = data.body["domains"]
load(keys)
end

# Returns domain
# @return [Fog::DNS::DigitalOceanV2::Domains] Retrieves a list of ssh keys.
# @raise [Fog::DNS::DigitalOceanV2::NotFound] - HTTP 404
# @raise [Fog::DNS::DigitalOceanV2::BadRequest] - HTTP 400
# @raise [Fog::DNS::DigitalOceanV2::InternalServerError] - HTTP 500
# @raise [Fog::DNS::DigitalOceanV2::ServiceError]
# @see https://developers.digitalocean.com/documentation/v2/#retrieve-an-existing-key
def get(id)
key = service.get_domain(id).body['name']
new(key) if key
rescue Fog::Errors::NotFound
nil
end
end
end
end
end
32 changes: 32 additions & 0 deletions lib/fog/digitalocean/models/dns/record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Fog
module DNS
class DigitalOcean
class Record < Fog::Model
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.
attribute :data # string The value to use for the DNS record.
attribute :priority # nullable number The priority for SRV and MX records.
attribute :port # nullable number The port for SRV records.
attribute :weight # nullable number The weight for SRV records.

def create
requires :name, :type, :data
merge_attributes(service.create_domain_record(name, type, data).body['id'])
true
end

def delete
requires :id
service.delete_domain_record name
end

def update
requires :id, :name, :type, :data
service.create_domain_record name, type, data
service.delete_domain_record id
end
end
end
end
end
40 changes: 40 additions & 0 deletions lib/fog/digitalocean/models/dns/records.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'fog/digitalocean/models/paging_collection'

module Fog
module DNS
class DigitalOcean
class Records < Fog::Compute::DigitalOcean::PagingCollection
model Fog::DNS::DigitalOcean::Record

# Returns list of records
# @return [Fog::DNS::DigitalOceanV2::Records] Retrieves a list of domains.
# @raise [Fog::DNS::DigitalOceanV2::NotFound] - HTTP 404
# @raise [Fog::DNS::DigitalOceanV2::BadRequest] - HTTP 400
# @raise [Fog::DNS::DigitalOceanV2::InternalServerError] - HTTP 500
# @raise [Fog::DNS::DigitalOceanV2::ServiceError]
# @see https://developers.digitalocean.com/documentation/v2/#list-all-keys
def all(filters = {})
data = service.list_domain_records(filters)
links = data.body["links"]
get_paged_links(links)
keys = data.body["records"]
load(keys)
end

# Returns record
# @return [Fog::DNS::DigitalOceanV2::Records] Retrieves a list of records
# @raise [Fog::DNS::DigitalOceanV2::NotFound] - HTTP 404
# @raise [Fog::DNS::DigitalOceanV2::BadRequest] - HTTP 400
# @raise [Fog::DNS::DigitalOceanV2::InternalServerError] - HTTP 500
# @raise [Fog::DNS::DigitalOceanV2::ServiceError]
# @see https://developers.digitalocean.com/documentation/v2/#retrieve-an-existing-key
def get(id)
key = service.get_domain(id).body['id']
new(key) if key
rescue Fog::Errors::NotFound
nil
end
end
end
end
end
42 changes: 42 additions & 0 deletions lib/fog/digitalocean/requests/dns/create_domain.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module Fog
module DNS
class DigitalOcean
# noinspection RubyStringKeysInHashInspection
class Real

def create_domain(name, ip_address)
create_options = {
:name => name,
:ip_address => ip_address,
}

encoded_body = Fog::JSON.encode(create_options)

request(
:expects => [201],
:headers => {
'Content-Type' => "application/json; charset=UTF-8",
},
:method => 'POST',
:path => '/v2/domains',
:body => encoded_body,
)
end
end

# noinspection RubyStringKeysInHashInspection
class Mock
def create_domain(name, ip_address)
response = Excon::Response.new
response.status = 200

response.body ={
'domain' => data[:domains].last
}

response
end
end
end
end
end
44 changes: 44 additions & 0 deletions lib/fog/digitalocean/requests/dns/create_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module Fog
module DNS
class DigitalOcean
# noinspection RubyStringKeysInHashInspection
class Real

def create_record(name, rec={})
create_options = {
:type => rec[:type],
}
%w(name data priority port weight).each do |fld|
create_options[fld.to_sym] = rec[fld.to_sym] if rec[fld.to_sym]
end

encoded_body = Fog::JSON.encode(create_options)

request(
:expects => [201],
:headers => {
'Content-Type' => "application/json; charset=UTF-8",
},
:method => 'POST',
:path => "/v2/domains/#{name}/records",
:body => encoded_body,
)
end
end

# noinspection RubyStringKeysInHashInspection
class Mock
def create_record(name, rec={})
response = Excon::Response.new
response.status = 200

response.body = {
"domain_record" => data[:domain_records][name].last
}

response
end
end
end
end
end
Loading

0 comments on commit 764ea1f

Please sign in to comment.