From e6e805c9fb2d9d73a40fd388d5d172cf51ef1e22 Mon Sep 17 00:00:00 2001 From: Adam Cooke Date: Thu, 15 Apr 2021 12:01:15 +0100 Subject: [PATCH] feat: add record types --- README.md | 5 ++ lib/dennis/client.rb | 5 ++ lib/dennis/record_type.rb | 85 ++++++++++++++++++++ spec/fixtures/vcr_cassettes/record-types.yml | 50 ++++++++++++ spec/specs/record_type_spec.rb | 80 ++++++++++++++++++ 5 files changed, 225 insertions(+) create mode 100644 lib/dennis/record_type.rb create mode 100644 spec/fixtures/vcr_cassettes/record-types.yml create mode 100644 spec/specs/record_type_spec.rb diff --git a/README.md b/README.md index 6b7387d..d459e18 100644 --- a/README.md +++ b/README.md @@ -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'] ``` diff --git a/lib/dennis/client.rb b/lib/dennis/client.rb index 455f130..20d1e40 100644 --- a/lib/dennis/client.rb +++ b/lib/dennis/client.rb @@ -5,6 +5,7 @@ require 'dennis/zone' require 'dennis/nameserver' require 'dennis/record' +require 'dennis/record_type' module Dennis class Client @@ -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 diff --git a/lib/dennis/record_type.rb b/lib/dennis/record_type.rb new file mode 100644 index 0000000..9257be1 --- /dev/null +++ b/lib/dennis/record_type.rb @@ -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 diff --git a/spec/fixtures/vcr_cassettes/record-types.yml b/spec/fixtures/vcr_cassettes/record-types.yml new file mode 100644 index 0000000..0b4f5e8 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/record-types.yml @@ -0,0 +1,50 @@ +--- +http_interactions: +- request: + method: get + uri: https://dennis.dev.adam.ac/api/v1/record_types?_arguments=%7B%7D + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + Authorization: + - Bearer test + response: + status: + code: 200 + message: OK + headers: + Access-Control-Allow-Methods: + - "*" + Access-Control-Allow-Origin: + - "*" + Cache-Control: + - max-age=0, private, must-revalidate + Content-Length: + - '2579' + Content-Type: + - application/json + Etag: + - W/"8693566aaf06a84c77a2f3d60425203b" + Server: + - Caddy + X-Request-Id: + - 42ff7fa1-fd45-45c5-89c5-a335c2d026ca + X-Runtime: + - '0.008390' + Date: + - Thu, 15 Apr 2021 10:51:35 GMT + body: + encoding: UTF-8 + string: '{"record_types":[{"name":"A","requires_priority":false,"managed_only":false,"content_attributes":[{"name":"ip_address","label":"IPv4 + address","type":"text","options":null}]},{"name":"AAAA","requires_priority":false,"managed_only":false,"content_attributes":[{"name":"ip_address","label":"IPv6 + address","type":"text","options":null}]},{"name":"ALIAS","requires_priority":false,"managed_only":false,"content_attributes":[{"name":"hostname","label":"Hostname","type":"text","options":null}]},{"name":"CAA","requires_priority":false,"managed_only":false,"content_attributes":[{"name":"flag","label":"Flag","type":"text","options":null},{"name":"tag","label":"Tag","type":"options","options":[{"label":"issue","value":"issue"},{"label":"issuewild","value":"issuewild"},{"label":"iodef","value":"iodef"},{"label":"contactemail","value":"contactemail"},{"label":"contactphone","value":"contactphone"}]},{"name":"value","label":"Value","type":"text","options":null}]},{"name":"CNAME","requires_priority":false,"managed_only":false,"content_attributes":[{"name":"hostname","label":"Hostname","type":"text","options":null}]},{"name":"MX","requires_priority":true,"managed_only":false,"content_attributes":[{"name":"hostname","label":"Hostname","type":"text","options":null}]},{"name":"NS","requires_priority":false,"managed_only":false,"content_attributes":[{"name":"hostname","label":"Hostname","type":"text","options":null}]},{"name":"PTR","requires_priority":false,"managed_only":false,"content_attributes":[{"name":"hostname","label":"Hostname","type":"text","options":null}]},{"name":"SOA","requires_priority":false,"managed_only":true,"content_attributes":[]},{"name":"SRV","requires_priority":true,"managed_only":false,"content_attributes":[{"name":"weight","label":"Weight","type":"text","options":null},{"name":"port","label":"Port","type":"text","options":null},{"name":"target","label":"Target","type":"text","options":null}]},{"name":"SSHFP","requires_priority":false,"managed_only":false,"content_attributes":[{"name":"algorithm","label":"Algorithm","type":"options","options":[{"label":"RSA","value":"1"},{"label":"DSA","value":"2"},{"label":"ECDSA","value":"3"},{"label":"Ed25519","value":"4"}]},{"name":"fingerprint_type","label":"Fingerprint + type","type":"options","options":[{"label":"SHA1","value":"1"},{"label":"SHA-256","value":"2"}]},{"name":"fingerprint","label":"Fingerprint","type":"text","options":null}]},{"name":"TXT","requires_priority":false,"managed_only":false,"content_attributes":[{"name":"content","label":"Content","type":"long_text","options":null}]}]}' + recorded_at: Thu, 15 Apr 2021 10:51:35 GMT +recorded_with: VCR 6.0.0 diff --git a/spec/specs/record_type_spec.rb b/spec/specs/record_type_spec.rb new file mode 100644 index 0000000..b70c3f3 --- /dev/null +++ b/spec/specs/record_type_spec.rb @@ -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