Class: Fog::DNS::Dynect::Real

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/dynect/dns.rb,
lib/fog/dynect/requests/dns/get_zone.rb,
lib/fog/dynect/requests/dns/put_zone.rb,
lib/fog/dynect/requests/dns/post_zone.rb,
lib/fog/dynect/requests/dns/get_record.rb,
lib/fog/dynect/requests/dns/put_record.rb,
lib/fog/dynect/requests/dns/delete_zone.rb,
lib/fog/dynect/requests/dns/post_record.rb,
lib/fog/dynect/requests/dns/post_session.rb,
lib/fog/dynect/requests/dns/delete_record.rb,
lib/fog/dynect/requests/dns/get_node_list.rb,
lib/fog/dynect/requests/dns/get_all_records.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/fog/dynect/dns.rb', line 64

def initialize(options={})
  @dynect_customer = options[:dynect_customer]
  @dynect_username = options[:dynect_username]
  @dynect_password = options[:dynect_password]

  @connection_options = options[:connection_options] || {}
  @host       = 'api-v4.dynect.net'
  @port       = options[:port]        || 443
  @path       = options[:path]        || '/REST'
  @persistent = options[:persistent]  || false
  @scheme     = options[:scheme]      || 'https'
  @version    = options[:version]     || '3.5.2'
  @connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
end

Instance Method Details

#auth_tokenObject



79
80
81
# File 'lib/fog/dynect/dns.rb', line 79

def auth_token
  @auth_token ||= post_session.body['data']['token']
end

#delete_record(type, zone, fqdn, record_id) ⇒ Object

Delete a record

Parameters

  • type<~String> - type of record in [‘AAAA’, ‘ANY’, ‘A’, ‘CNAME’, ‘DHCID’, ‘DNAME’, ‘DNSKEY’, ‘DS’, ‘KEY’, ‘LOC’, ‘MX’, ‘NSA’, ‘NS’, ‘PTR’, ‘PX’, ‘RP’, ‘SOA’, ‘SPF’, ‘SRV’, ‘SSHFP’, ‘TXT’]

  • zone<~String> - zone of record

  • fqdn<~String> - fqdn of record

  • record_id<~String> - id of record



14
15
16
17
18
19
20
21
# File 'lib/fog/dynect/requests/dns/delete_record.rb', line 14

def delete_record(type, zone, fqdn, record_id)
  request(
    :expects  => 200,
    :idempotent => true,
    :method   => :delete,
    :path     => ["#{type.to_s.upcase}Record", zone, fqdn, record_id].join('/')
  )
end

#delete_zone(zone) ⇒ Object

Delete a zone

Parameters

  • zone<~String> - zone to host



11
12
13
14
15
16
17
# File 'lib/fog/dynect/requests/dns/delete_zone.rb', line 11

def delete_zone(zone)
  request(
    :expects  => 200,
    :method   => :delete,
    :path     => "Zone/#{zone}"
  )
end

#get_all_records(zone, options = {}) ⇒ Object

Get one or more node lists

Parameters

  • zone<~String> - zone to lookup node lists for

  • options<~Hash>

    • fqdn<~String> - fully qualified domain name of node to lookup



13
14
15
16
17
18
19
20
21
# File 'lib/fog/dynect/requests/dns/get_all_records.rb', line 13

def get_all_records(zone, options = {})
  requested_fqdn = options['fqdn'] || options[:fqdn]
  request(
    :expects  => 200,
    :idempotent => true,
    :method   => :get,
    :path     => ['AllRecord', zone, requested_fqdn].compact.join('/')
  )
end

#get_node_list(zone, options = {}) ⇒ Object

Get one or more node lists

Parameters

  • zone<~String> - zone to lookup node lists for

  • options<~Hash>

    • fqdn<~String> - fully qualified domain name of node to lookup



13
14
15
16
17
18
19
20
21
# File 'lib/fog/dynect/requests/dns/get_node_list.rb', line 13

def get_node_list(zone, options = {})
  requested_fqdn = options['fqdn'] || options[:fqdn]
  request(
    :expects  => 200,
    :idempotent => true,
    :method   => :get,
    :path     => ['AllRecord', zone, requested_fqdn].compact.join('/')
  )
end

#get_record(type, zone, fqdn, options = {}) ⇒ Object

List records of a given type

Parameters

  • type<~String> - type of record in [‘AAAA’, ‘ANY’, ‘A’, ‘CNAME’, ‘DHCID’, ‘DNAME’, ‘DNSKEY’, ‘DS’, ‘KEY’, ‘LOC’, ‘MX’, ‘NSA’, ‘NS’, ‘PTR’, ‘PX’, ‘RP’, ‘SOA’, ‘SPF’, ‘SRV’, ‘SSHFP’, ‘TXT’]

  • zone<~String> - name of zone to lookup

  • fqdn<~String> - name of fqdn to lookup

  • options<~Hash>:

    • record_id<~String> - id of record



15
16
17
18
19
20
21
22
# File 'lib/fog/dynect/requests/dns/get_record.rb', line 15

def get_record(type, zone, fqdn, options = {})
  request(
    :expects  => 200,
    :idempotent => true,
    :method   => :get,
    :path     => ["#{type.to_s.upcase}Record", zone, fqdn, options['record_id']].compact.join('/')
  )
end

#get_zone(options = {}) ⇒ Object

Get one or more zones

Parameters

  • options<~Hash>:

    • zone<~String> - name of zone to lookup, or omit to return list of zones



12
13
14
15
16
17
18
19
# File 'lib/fog/dynect/requests/dns/get_zone.rb', line 12

def get_zone(options = {})
  request(
    :expects  => 200,
    :idempotent => true,
    :method   => :get,
    :path     => ['Zone', options['zone']].compact.join('/')
  )
end

#poll_job(response, original_expects, time_to_wait = 10) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/fog/dynect/dns.rb', line 123

def poll_job(response, original_expects, time_to_wait = 10)
  job_location = response.headers['Location']

  Fog.wait_for(time_to_wait) do
    response = request(:expects => original_expects, :method => :get, :path => job_location)
    response.body['status'] != 'incomplete'
  end

  if response.body['status'] == 'incomplete'
    raise JobIncomplete.new("Job #{response.body['job_id']} is still incomplete")
  end

  response
end

#post_record(type, zone, fqdn, rdata, options = {}) ⇒ Object

Create a record

Parameters

  • type<~String> - type of record in [‘AAAA’, ‘ANY’, ‘A’, ‘CNAME’, ‘DHCID’, ‘DNAME’, ‘DNSKEY’, ‘DS’, ‘KEY’, ‘LOC’, ‘MX’, ‘NSA’, ‘NS’, ‘PTR’, ‘PX’, ‘RP’, ‘SOA’, ‘SPF’, ‘SRV’, ‘SSHFP’, ‘TXT’]

  • zone<~String> - zone of record

  • rdata<~Hash> - rdata for record

  • options<~Hash>: (options vary by type, listing below includes common parameters)

    • ttl<~Integer> - ttl for the record, defaults to zone ttl



15
16
17
18
19
20
21
22
23
# File 'lib/fog/dynect/requests/dns/post_record.rb', line 15

def post_record(type, zone, fqdn, rdata, options = {})
  options.merge!('rdata' => rdata)
  request(
    :body     => Fog::JSON.encode(options),
    :expects  => 200,
    :method   => :post,
    :path     => ["#{type.to_s.upcase}Record", zone, fqdn].join('/')
  )
end

#post_sessionObject



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/fog/dynect/requests/dns/post_session.rb', line 6

def post_session
  request(
    :expects  => 200,
    :idempotent => true,
    :method   => :post,
    :path     => "Session",
    :body     => Fog::JSON.encode({
      :customer_name  => @dynect_customer,
      :user_name      => @dynect_username,
      :password       => @dynect_password
    })
  )
end

#post_zone(rname, ttl, zone, options = {}) ⇒ Object

Create a zone

Parameters

  • rname<~String> - administrative contact

  • ttl<~Integer> - time to live (in seconds) for records in this zone

  • zone<~String> - name of zone to host

  • options<~Hash>:

    • serial_style<~String> - style of serial number, in [‘day’, ‘epoch’, ‘increment’, ‘minute’]. Defaults to increment



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fog/dynect/requests/dns/post_zone.rb', line 15

def post_zone(rname, ttl, zone, options = {})
  body = Fog::JSON.encode({
    :rname  => rname,
    :token  => auth_token,
    :ttl    => ttl
  }.merge!(options))

  request(
    :body     => body,
    :expects  => 200,
    :method   => :post,
    :path     => 'Zone/' << zone
  )
end

#put_record(type, zone, fqdn, rdata, options = {}) ⇒ Object

Update or replace a record

Parameters

  • type<~String> - type of record in [‘AAAA’, ‘ANY’, ‘A’, ‘CNAME’, ‘DHCID’, ‘DNAME’, ‘DNSKEY’, ‘DS’, ‘KEY’, ‘LOC’, ‘MX’, ‘NSA’, ‘NS’, ‘PTR’, ‘PX’, ‘RP’, ‘SOA’, ‘SPF’, ‘SRV’, ‘SSHFP’, ‘TXT’]

  • zone<~String> - zone of record

  • rdata<~Hash> - rdata for record

  • options<~Hash>: (options vary by type, listing below includes common parameters)

    • ttl<~Integer> - ttl for the record, defaults to zone ttl



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fog/dynect/requests/dns/put_record.rb', line 15

def put_record(type, zone, fqdn, rdata, options = {})
  options.merge!('rdata' => rdata)
  type.to_s.upcase!
  options = {"#{type}Records" => [options]} unless options['record_id']
  path = ["#{type}Record", zone, fqdn].join('/')
  path += "/#{options.delete('record_id')}" if options['record_id']
  request(
    :body       => Fog::JSON.encode(options),
    :expects    => 200,
    :idempotent => true,
    :method     => :put,
    :path       => path
  )
end

#put_zone(zone, options = {}) ⇒ Object

Update a zone

Parameters

  • zone<~String> - name or id of zone

  • options<~Hash>:

    • freeze<~Boolean> - causes zone to become frozen

    • publish<~Boolean> - causes all pending changes to be pushed to nameservers

    • thaw<~Boolean> - causes zone to cease being frozen



15
16
17
18
19
20
21
22
23
# File 'lib/fog/dynect/requests/dns/put_zone.rb', line 15

def put_zone(zone, options = {})
  request(
    :body       => Fog::JSON.encode(options),
    :expects    => 200,
    :idempotent => true,
    :method     => :put,
    :path       => 'Zone/' << zone
  )
end

#request(params) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/fog/dynect/dns.rb', line 83

def request(params)
  begin
    # any request could redirect to a job
    params[:expects] = Array(params[:expects]) | [307]

    params[:headers] ||= {}
    params[:headers]['Content-Type'] = 'application/json'
    params[:headers]['API-Version'] = @version
    params[:headers]['Auth-Token'] = auth_token unless params[:path] == 'Session'
    params[:path] = "#{@path}/#{params[:path]}" unless params[:path] =~ %r{^#{Regexp.escape(@path)}/}

    response = @connection.request(params)

    if response.body.empty?
      response.body = {}
    elsif response.headers['Content-Type'] == 'application/json'
      response.body = Fog::JSON.decode(response.body)
    end

    if response.body['status'] == 'failure'
      raise Error, response.body['msgs'].first['INFO']
    end

    if response.status == 307 && params[:path] !~ %r{^/REST/Job/}
      response = poll_job(response, params[:expects])
    end

    response
  rescue Excon::Errors::HTTPStatusError => error
    if @auth_token && error.message =~ /login: (Bad or expired credentials|inactivity logout)/
      @auth_token = nil
      retry
    else
      raise error
    end
  end

  response
end