Skip to content

Commit

Permalink
Add TemplatesService records
Browse files Browse the repository at this point in the history
  • Loading branch information
weppos committed Dec 15, 2014
1 parent cef3081 commit e13e493
Show file tree
Hide file tree
Showing 13 changed files with 424 additions and 79 deletions.
10 changes: 5 additions & 5 deletions lib/dnsimple/client/records_service.rb
Expand Up @@ -2,7 +2,7 @@ module Dnsimple
class Client
class RecordsService < ClientService

# Lists the records in the account.
# Lists the records for a domain.
#
# @see http://developer.dnsimple.com/domains/records/#list
#
Expand All @@ -18,7 +18,7 @@ def list(domain, options = {})
response.map { |r| Record.new(r["record"]) }
end

# Creates a record in the account.
# Creates a record for a domain.
#
# @see http://developer.dnsimple.com/domains/records/#create
#
Expand All @@ -36,7 +36,7 @@ def create(domain, attributes = {})
Record.new(response["record"])
end

# Gets a record from the account.
# Gets a record for a domain.
#
# @see http://developer.dnsimple.com/domains/records/#get
#
Expand All @@ -52,7 +52,7 @@ def find(domain, record)
Record.new(response["record"])
end

# Updates a record in the account.
# Updates a record for a domain.
#
# @see http://developer.dnsimple.com/domains/records/#update
#
Expand All @@ -70,7 +70,7 @@ def update(domain, record, attributes = {})
Record.new(response["record"])
end

# Deletes a record from the account.
# Deletes a record for a domain.
#
# WARNING: this cannot be undone.
#
Expand Down
82 changes: 82 additions & 0 deletions lib/dnsimple/client/templates_service.rb
Expand Up @@ -77,6 +77,88 @@ def delete(template)
client.delete("v1/templates/#{template}")
end


# Lists the records for a template.
#
# @see http://developer.dnsimple.com/templates/records/#list
#
# @param [#to_s] template The template id or short-name.
#
# @return [Array<TemplateRecord>]
# @raise [RecordNotFound]
# @raise [RequestError] When the request fails.
def list_records(template)
response = client.get("v1/templates/#{template}/records")

response.map { |r| TemplateRecord.new(r["dns_template_record"]) }
end

# Creates a record for a template.
#
# @see http://developer.dnsimple.com/templates/records/#create
#
# @param [#to_s] template The template id or short-name.
# @param [Hash] attributes
#
# @return [Record]
# @raise [RecordNotFound]
# @raise [RequestError] When the request fails.
def create_record(domain, attributes = {})
validate_mandatory_attributes(attributes, [:name, :record_type, :content])
options = { body: { dns_template_record: attributes }}
response = client.post("v1/templates/#{domain}/records", options)

TemplateRecord.new(response["dns_template_record"])
end

# Gets a record for a template.
#
# @see http://developer.dnsimple.com/templates/records/#delete
#
# @param [#to_s] template The template id or short-name.
# @param [Fixnum] record The record id.
#
# @return [TemplateRecord]
# @raise [RecordNotFound]
# @raise [RequestError] When the request fails.
def find_record(template, record)
response = client.get("v1/templates/#{template}/records/#{record}")

TemplateRecord.new(response["dns_template_record"])
end

# Updates a record for a template.
#
# @see http://developer.dnsimple.com/templates/#update
#
# @param [#to_s] template The template id or short-name.
# @param [Fixnum] record The record id.
# @param [Hash] attributes
#
# @return [TemplateRecord]
# @raise [RecordNotFound]
# @raise [RequestError] When the request fails.
def update_record(template, record, attributes = {})
options = { body: { dns_template_record: attributes }}
response = client.put("v1/templates/#{template}/records/#{record}", options)

TemplateRecord.new(response["dns_template_record"])
end

# Deletes a record for a template.
#
# @see http://developer.dnsimple.com/templates/records/#get
#
# @param [#to_s] template The template id or short-name.
# @param [Fixnum] record The record id.
#
# @return [Template]
# @raise [RecordNotFound]
# @raise [RequestError] When the request fails.
def delete_record(template, record)
client.delete("v1/templates/#{template}/records/#{record}")
end

end
end
end
84 changes: 13 additions & 71 deletions lib/dnsimple/template_record.rb
@@ -1,80 +1,22 @@
module Dnsimple

# A single record in a template.
class TemplateRecord < Base

# The id of the template record
attr_accessor :id

# The template the record belongs to
attr_accessor :template

# The name the record points to. This may be blank.
attr_accessor :dns_template_id
attr_accessor :name

# The content for the record.
attr_accessor :type
attr_accessor :content

# The record type
attr_accessor :record_type

# The time-to-live
attr_accessor :ttl

# The priority (only for MX records)
attr_accessor :prio

def delete(options={})
Client.delete("v1/templates/#{template.id}/template_records/#{id}", options)
end
alias :destroy :delete

def self.create(short_name, name, record_type, content, options={})
template = Template.find(short_name)

record_hash = {:name => name, :record_type => record_type, :content => content}
record_hash[:ttl] = options.delete(:ttl) || 3600
record_hash[:prio] = options.delete(:prio) || ''

options.merge!({:query => {:dns_template_record => record_hash}})

response = Client.post("v1/templates/#{template.id}/template_records", options)

case response.code
when 201
new({:template => template}.merge(response["dns_template_record"]))
else
raise RequestError.new("Error creating template record", response)
end
end

def self.find(short_name, id, options={})
template = Template.find(short_name)
response = Client.get("v1/templates/#{template.id}/template_records/#{id}", options)

case response.code
when 200
new({:template => template}.merge(response["dns_template_record"]))
when 404
raise RecordNotFound, "Could not find template record #{id} for template #{template.id}"
else
raise RequestError.new("Error finding template record", response)
end
end

# Get all of the template records for the template with the
# given short name.
def self.all(short_name, options={})
template = Template.find(short_name)
response = Client.get("v1/templates/#{template.id}/template_records", options)

case response.code
when 200
response.map { |r| new({:template => template}.merge(r["dns_template_record"])) }
else
raise RequestError.new("Error listing template records", response)
end
end

attr_accessor :priority
attr_accessor :created_at
attr_accessor :updated_at

alias :template_id :dns_template_id
alias :template_id= :dns_template_id=
alias :prio :priority
alias :prio= :priority=
alias :record_type :type
alias :record_type= :type=
end

end

0 comments on commit e13e493

Please sign in to comment.