Module: GandiV5::LiveDNS::HasZoneRecords

Included in:
Domain, Zone
Defined in:
lib/gandi_v5/live_dns/has_zone_records.rb

Overview

Methods for handling record related requests in both GandiV5::LiveDNS::Domain and GandiV5::LiveDNS::Zone.

Instance Method Summary collapse

Instance Method Details

#add_record(name, type, ttl, *values) ⇒ String

Add record to this domain.

Parameters:

  • name (String)
  • type (String)
  • ttl (Integer)
  • values (Array<String>)

Returns:

  • (String)

    The confirmation message from Gandi.

Raises:



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/gandi_v5/live_dns/has_zone_records.rb', line 59

def add_record(name, type, ttl, *values)
  GandiV5::LiveDNS.require_valid_record_type type
  fail ArgumentError, 'ttl must be positive and non-zero' unless ttl.positive?
  fail ArgumentError, 'there must be at least one value' if values.none?

  body = {
    rrset_name: name,
    rrset_type: type,
    rrset_ttl: ttl,
    rrset_values: values
  }.to_json
  _response, data = GandiV5.post "#{url}/records", body
  data['message']
end

#delete_recordsnil #delete_records(name) ⇒ nil #delete_records(name, type) ⇒ nil

Overloads:

  • #delete_recordsnil

    Delete all records for this domain.

  • #delete_records(name) ⇒ nil

    Delete records for a name.

    Parameters:

    • name (String)

      the name to delete records for.

  • #delete_records(name, type) ⇒ nil

    Delete records of a type for a name.

    Parameters:

    • name (String)

      the name to delete records for.

    • type (String)

      the record type to delete.

Returns:

  • (nil)

Raises:



85
86
87
88
89
90
91
92
# File 'lib/gandi_v5/live_dns/has_zone_records.rb', line 85

def delete_records(name = nil, type = nil)
  GandiV5::LiveDNS.require_valid_record_type(type) if type

  url_ = "#{url}/records"
  url_ += "/#{CGI.escape name}" if name
  url_ += "/#{CGI.escape type}" if type
  GandiV5.delete(url_).last
end

#fetch_recordsArray<GandiV5::LiveDNS::RecordSet> #fetch_records(name) ⇒ Array<GandiV5::LiveDNS::RecordSet> #fetch_records(name, type) ⇒ Array<GandiV5::LiveDNS::RecordSet>

Overloads:

  • #fetch_recordsArray<GandiV5::LiveDNS::RecordSet>

    Fetch all records for this domain.

  • #fetch_records(name) ⇒ Array<GandiV5::LiveDNS::RecordSet>

    Fetch records for a name.

    Parameters:

    • name (String)

      the name to fetch records for.

  • #fetch_records(name, type) ⇒ Array<GandiV5::LiveDNS::RecordSet>

    Fetch records of a type for a name.

    Parameters:

    • name (String)

      the name to fetch records for.

    • type (String)

      the record type to fetch.

Returns:

Raises:



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/gandi_v5/live_dns/has_zone_records.rb', line 19

def fetch_records(name = nil, type = nil)
  GandiV5::LiveDNS.require_valid_record_type type if type

  url_ = "#{url}/records"
  url_ += "/#{CGI.escape name}" if name
  url_ += "/#{CGI.escape type}" if type

  _response, data = GandiV5.get url_
  data = [data] unless data.is_a?(Array)
  data.map { |item| GandiV5::LiveDNS::RecordSet.from_gandi item }
end

#fetch_zone_linesString #fetch_zone_lines(name) ⇒ String #fetch_zone_lines(name, type) ⇒ String

Overloads:

  • #fetch_zone_linesString

    Fetch all records for this domain.

  • #fetch_zone_lines(name) ⇒ String

    Fetch records for a name.

    Parameters:

    • name (String)

      the name to fetch records for.

  • #fetch_zone_lines(name, type) ⇒ String

    Fetch records of a type for a name.

    Parameters:

    • name (String)

      the name to fetch records for.

    • type (String)

      the record type to fetch.

Returns:

  • (String)

Raises:



42
43
44
45
46
47
48
49
50
# File 'lib/gandi_v5/live_dns/has_zone_records.rb', line 42

def fetch_zone_lines(name = nil, type = nil)
  GandiV5::LiveDNS.require_valid_record_type type if type

  url_ = "#{url}/records"
  url_ += "/#{CGI.escape name}" if name
  url_ += "/#{CGI.escape type}" if type

  GandiV5.get(url_, accept: 'text/plain').last
end

#replace_records(records: nil, text: nil) ⇒ String

Replace all records for this domain.

Parameters:

  • records (defaults to: nil)
    Array<Hash<:name, :type => String, :ttl => Integer, :values => Array<String>>>

    the records to add.

  • text (String) (defaults to: nil)

    zone file lines to replace the records with.

Returns:

  • (String)

    The confirmation message from Gandi.

Raises:



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/gandi_v5/live_dns/has_zone_records.rb', line 102

def replace_records(records: nil, text: nil)
  unless [records, text].count(&:nil?).eql?(1)
    fail ArgumentError, 'you must pass ONE of records: or text:'
  end

  if records
    body = {
      items: records.map { |r| r.transform_keys { |k| "rrset_#{k}" } }
    }.to_json
    _response, data = GandiV5.put "#{url}/records", body
  elsif text
    _response, data = GandiV5.put "#{url}/records", text, 'content-type': 'text/plain'
  end
  data['message']
end

#replace_records_for(name, records, type: nil, ttl: nil) ⇒ String

Returns The confirmation message from Gandi.

Returns:

  • (String)

    The confirmation message from Gandi.

Raises:



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/gandi_v5/live_dns/has_zone_records.rb', line 133

def replace_records_for(name, records, type: nil, ttl: nil)
  fail ArgumentError, 'missing keyword: type' if ttl && type.nil?

  if type
    GandiV5::LiveDNS.require_valid_record_type type
    body = { rrset_values: records, rrset_ttl: ttl }
    # body[:rrset_ttl] = ttl if ttl
    _response, data = GandiV5.put "#{url}/records/#{name}/#{type}", body.to_json

  else
    body = {
      items: records.map { |r| r.transform_keys { |k| "rrset_#{k}" } }
    }
    _response, data = GandiV5.put "#{url}/records/#{name}", body.to_json
  end

  data['message']
end