Sha256: a9ef2989a20ca79cf37d2ae8354d439159e9cb12288b80db8c24b5a19a4e6f06

Contents?: true

Size: 1.94 KB

Versions: 3

Compression:

Stored size: 1.94 KB

Contents

# frozen_string_literal: true

require 'nifcloud/client'
require 'xmlsimple'
require 'cgi'

module Nifcloud
  module DNS
    # Class that manages DNS record information
    class Record < Client
      RECORD_SET_WITH_ID = 'hostedzone/%s/rrset'
      RECORD_SET_WITH_PARAMS = 'hostedzone/%s/rrset?%s'

      def initialize(zone_id, access_key: nil, secret_key: nil)
        super(access_key, secret_key)
        @zone_id = zone_id
      end

      def list(options: nil)
        if options.nil? || !options.instance_of?(Hash)
          get(RECORD_SET_WITH_ID % @zone_id)
        else
          list_path = format(RECORD_SET_WITH_PARAMS,
                             @zone_id,
                             options.collect { |k, v| "#{k}=#{CGI.escape(v.to_s)}" }.join('&'))
          get(list_path)
        end
      end

      def add(name, type, value, ttl: 86_400, comment: '')
        post(RECORD_SET_WITH_ID % @zone_id, xml('CREATE', name, type, value, ttl, comment))
      end

      def del(name, type, value, ttl: 86_400, comment: '')
        post(RECORD_SET_WITH_ID % @zone_id, xml('DELETE', name, type, value, ttl, comment))
      end

      private

      def xml(action, name, type, value, ttl, comment) # rubocop:disable Metrics/MethodLength, Metrics/ParameterLists
        body = {
          '@xmlns': Nifcloud::Client::NAMESPACE,
          ChangeBatch: {
            Comment: ['content' => comment],
            Changes: [
              Change: {
                Action: action,
                ResourceRecordSet: {
                  Name: name,
                  Type: type,
                  TTL: ttl,
                  ResourceRecords: {
                    Value: value
                  }
                }
              }
            ]
          }
        }
        options = {
          AttrPrefix: true,
          RootName: 'ChangeResourceRecordSetsRequest',
          ContentKey: 'content'
        }
        XmlSimple.xml_out(body, options)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nifcloud-dns-0.3.2 lib/nifcloud/dns/record.rb
nifcloud-dns-0.3.1 lib/nifcloud/dns/record.rb
nifcloud-dns-0.3.0 lib/nifcloud/dns/record.rb