Skip to content

Commit

Permalink
feat: add record types
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcooke committed Apr 15, 2021
1 parent 902bcb2 commit e6e805c
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,9 @@ record.update(content: { ip_address: '185.22.208.5'} )

# Get all records with any of the given tags
client.tagged_records(['tag1'])

# Get a list of all the types of records that are supported and the attributes
# needed for them. This will return a hash keyed with the name of the type.
client.record_types
client.record_types['A']
```
5 changes: 5 additions & 0 deletions lib/dennis/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'dennis/zone'
require 'dennis/nameserver'
require 'dennis/record'
require 'dennis/record_type'

module Dennis
class Client
Expand All @@ -21,6 +22,10 @@ def initialize(hostname, api_key, **options)
@api.headers['Authorization'] = "Bearer #{api_key}"
end

def record_types
RecordType.all(self)
end

def groups
Group.all(self)
end
Expand Down
85 changes: 85 additions & 0 deletions lib/dennis/record_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# frozen_string_literal: true

module Dennis
class RecordType

class << self

def all(client)
request = client.api.create_request(:get, 'record_types')
request.perform.hash['record_types'].each_with_object({}) do |rt, hash|
type = new(rt)
hash[type.name] = type
end
end

end

def initialize(hash)
@hash = hash
end

def name
@hash['name']
end

def requires_priority?
@hash['requires_priority'] == true
end

def managed_only?
@hash['managed_only'] == true
end

def content_attributes
@hash['content_attributes'].map do |hash|
ContentAttribute.new(hash)
end
end

class ContentAttribute

def initialize(hash)
@hash = hash
end

def name
@hash['name']
end

def label
@hash['label']
end

def type
@hash['type']
end

def options
return [] if @hash['options'].nil?

@hash['options'].map do |hash|
ContentAttributeOption.new(hash)
end
end

end

class ContentAttributeOption

def initialize(hash)
@hash = hash
end

def label
@hash['label']
end

def value
@hash['value']
end

end

end
end
50 changes: 50 additions & 0 deletions spec/fixtures/vcr_cassettes/record-types.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 80 additions & 0 deletions spec/specs/record_type_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# frozen_string_literal: true

require 'spec_helper'

module Dennis

describe RecordType do
include_context 'with client'

describe '.all' do
it 'returns all the record types' do
VCR.use_cassette('record-types') do
types = described_class.all(@client)
expect(types).to be_a Hash
expect(types).to match hash_including(
'A' => have_attributes(
name: 'A',
requires_priority?: false,
managed_only?: false,
content_attributes: [
have_attributes(
name: 'ip_address',
type: 'text',
options: []
)
]
),
'SOA' => have_attributes(
name: 'SOA',
requires_priority?: false,
managed_only?: true,
content_attributes: []
),
'MX' => have_attributes(
name: 'MX',
requires_priority?: true,
managed_only?: false,
content_attributes: [
have_attributes(
name: 'hostname',
type: 'text',
options: []
)
]
),
'CAA' => have_attributes(
name: 'CAA',
requires_priority?: false,
managed_only?: false,
content_attributes: [
have_attributes(
name: 'flag',
type: 'text',
options: []
),
have_attributes(
name: 'tag',
type: 'options',
options: [
have_attributes(label: 'issue', value: 'issue'),
have_attributes(label: 'issuewild', value: 'issuewild'),
have_attributes(label: 'iodef', value: 'iodef'),
have_attributes(label: 'contactemail', value: 'contactemail'),
have_attributes(label: 'contactphone', value: 'contactphone')
]
),
have_attributes(
name: 'value',
type: 'text',
options: []
)
]
)
)
end
end
end
end

end

0 comments on commit e6e805c

Please sign in to comment.