Class: Dyn::Traffic::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/dyn/traffic.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(customer_name, user_name, password, zone = nil, connect = true, verbose = false, max_redirects = 10) ⇒ Client

Creates a new base object for interacting with Dyn’s REST API

Parameters:

  • Your (String)

    dyn customer name

  • Your (String)

    dyn user name

  • Your (String)

    dyn password

  • The (String)

    zone you are going to be editing

  • Whether (Boolean)

    to connect immediately or not - runs login for you

  • Verbosity (Boolean)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/dyn/traffic.rb', line 50

def initialize(customer_name, user_name, password, zone=nil, connect=true, verbose=false, max_redirects=10)
  @customer_name = customer_name
  @user_name = user_name
  @password = password
  @rest = Dyn::HttpClient::DefaultClient.new("api2.dynect.net", "443", "https")
  @rest.default_headers = {
    'User-Agent'   => 'dyn-rb 1.0.3',
    'Content-Type' => 'application/json'
  }
  @zone = zone 
  @verbose = verbose
  @session = Dyn::Traffic::Session.new(self)
   if connect
end

Instance Attribute Details

#auth_tokenObject

Returns the value of attribute auth_token.



40
41
42
# File 'lib/dyn/traffic.rb', line 40

def auth_token
  @auth_token
end

#customer_nameObject

Returns the value of attribute customer_name.



40
41
42
# File 'lib/dyn/traffic.rb', line 40

def customer_name
  @customer_name
end

#passwordObject

Returns the value of attribute password.



40
41
42
# File 'lib/dyn/traffic.rb', line 40

def password
  @password
end

#restObject

Returns the value of attribute rest.



40
41
42
# File 'lib/dyn/traffic.rb', line 40

def rest
  @rest
end

#user_nameObject

Returns the value of attribute user_name.



40
41
42
# File 'lib/dyn/traffic.rb', line 40

def user_name
  @user_name
end

Class Method Details

.underscore(string) ⇒ Object

Convert a CamelCasedString to an under_scored_string.



130
131
132
133
134
135
136
137
138
# File 'lib/dyn/traffic.rb', line 130

def self.underscore(string)
  word = string.dup
  word.gsub!(/::/, '/')
  word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
  word.tr!("-", "_")
  word.downcase!
  word
end

Instance Method Details

#api_request(&block) ⇒ Object

Handles making Dynect API requests and formatting the responses properly.



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/dyn/traffic.rb', line 196

def api_request(&block)
  response_body = begin
    response = block.call
    response.body
  rescue Exception => e
    if @verbose
      puts "I have #{e.inspect} with #{e.http_code}"
    end
    if e.http_code == 307
      e.response.sub!('/REST/','') if e.response =~ /^\/REST\//
      get(e.response)
    end
    e.response
  end
  
  parse_response(JSON.parse(response_body || '{}'))
end

#delete(path_part, additional_headers = {}, &block) ⇒ Object

Raw DELETE request, formatted for Dyn. See list of endpoints at:

manage.dynect.net/help/docs/api2/rest/resources/

Parameters:

  • The (String)

    partial path to DELETE - for example, ‘User’ or ‘Zone’.

  • Additional (Hash)

    HTTP headers



165
166
167
# File 'lib/dyn/traffic.rb', line 165

def delete(path_part, additional_headers = {}, &block)
  api_request { @rest.delete('/REST/' + path_part, "", additional_headers, &block) }
end

#get(path_part, additional_headers = {}, &block) ⇒ Object

Raw GET request, formatted for Dyn. See list of endpoints at:

manage.dynect.net/help/docs/api2/rest/resources/

Parameters:

  • The (String)

    partial path to GET - for example, ‘User’ or ‘Zone’.

  • Additional (Hash)

    HTTP headers



155
156
157
# File 'lib/dyn/traffic.rb', line 155

def get(path_part, additional_headers = {}, &block)
  api_request { @rest.get('/REST/' + path_part, nil, additional_headers, &block) }
end

#gslb(options = {}) ⇒ Object

GSLB API



118
119
120
# File 'lib/dyn/traffic.rb', line 118

def gslb(options = {})
  Dyn::Traffic::GSLB.new(self, @zone, options)
end

#http_redirect(options = {}) ⇒ Object

HTTPRedirect API



104
105
106
# File 'lib/dyn/traffic.rb', line 104

def http_redirect(options = {})
  Dyn::Traffic::HTTPRedirect.new(self, @zone, options)
end

#loginHash

Login to Dyn - must be done before any other methods called.

See: manage.dynect.net/help/docs/api2/rest/resources/Session.html

Returns:

  • (Hash)

    The dynect API response



70
71
72
73
74
75
# File 'lib/dyn/traffic.rb', line 70

def 
 response = @session.create
 @auth_token = response["token"]
 @rest.default_headers = { 'Content-Type' => 'application/json', 'Auth-Token' => @auth_token }
 response
end

#logoutHash

Logout of Dyn - generally the last operation performed

See: manage.dynect.net/help/docs/api2/rest/resources/Session.html

Returns:

  • (Hash)

    The dynect API response



82
83
84
85
86
87
# File 'lib/dyn/traffic.rb', line 82

def logout
 response = @session.delete
 @auth_token = nil
 @rest.default_headers = { 'Content-Type' => 'application/json' }
 response
end

#parse_response(response) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/dyn/traffic.rb', line 215

def parse_response(response)
  case response["status"]
  when "success"
    response["data"]
  when "incomplete"
    # we get 'incomplete' when the API is running slow and claims the session has a previous job running
    # raise an error and return the job ID in case we want to ask the API what the job's status is
    error_messages = []
    error_messages.push( "This session may have a job _still_ running (slowly). Call /REST/Job/#{response["job_id"]} to get its status." )
    response["msgs"].each do |error_message|
      error_messages << "#{error_message["LVL"]} #{error_message["ERR_CD"]} #{error_message["SOURCE"]} - #{error_message["INFO"]}"
    end
    raise Dyn::Exceptions::IncompleteRequest.new( "#{error_messages.join("\n")}", response["job_id"] )
  when "failure"
    error_messages = []
    response["msgs"].each do |error_message|
      error_messages << "#{error_message["LVL"]} #{error_message["ERR_CD"]} #{error_message["SOURCE"]} - #{error_message["INFO"]}"
    end
    raise Dyn::Exceptions::RequestFailed, "Request failed: #{error_messages.join("\n")}" 
  end
end

#post(path_part, payload, additional_headers = {}, &block) ⇒ Object

Raw POST request, formatted for Dyn. See list of endpoints at:

manage.dynect.net/help/docs/api2/rest/resources/

Read the API documentation, and submit the proper data structure from here.

Parameters:

  • The (String)

    partial path to POST - for example, ‘User’ or ‘Zone’.

  • The (Hash)

    data structure to submit as the body, is automatically turned to JSON.

  • Additional (Hash)

    HTTP headers



178
179
180
# File 'lib/dyn/traffic.rb', line 178

def post(path_part, payload, additional_headers = {}, &block)
  api_request { @rest.post('/REST/' + path_part, payload.to_json, additional_headers, &block) }
end

#publishObject

for convenience…



90
91
92
# File 'lib/dyn/traffic.rb', line 90

def publish
  zone.publish
end

#put(path_part, payload, additional_headers = {}, &block) ⇒ Object

Raw PUT request, formatted for Dyn. See list of endpoints at:

manage.dynect.net/help/docs/api2/rest/resources/

Read the API documentation, and submit the proper data structure from here.

Parameters:

  • The (String)

    partial path to PUT - for example, ‘User’ or ‘Zone’.

  • The (Hash)

    data structure to submit as the body, is automatically turned to JSON.

  • Additional (Hash)

    HTTP headers



191
192
193
# File 'lib/dyn/traffic.rb', line 191

def put(path_part, payload, additional_headers = {}, &block)
  api_request { @rest.put('/REST/' + path_part, payload.to_json, additional_headers, &block) }
end

#record_typeObject

Resource Record API



143
144
145
146
147
# File 'lib/dyn/traffic.rb', line 143

%w{AAAA A CNAME DNSKEY DS KEY LOC MX NS PTR RP SOA SRV TXT}.each do |record_type|
  define_method underscore(record_type) do
    Dyn::Traffic::Resource.new(self, @zone, "#{record_type}")
  end
end

#sessionObject

Session API



111
112
113
# File 'lib/dyn/traffic.rb', line 111

def session
  Dyn::Traffic::Session.new(self)
end

#zoneObject

Zone API



125
126
127
# File 'lib/dyn/traffic.rb', line 125

def zone
  Dyn::Traffic::Zone.new(self, @zone)
end

#zone=(zone) ⇒ Object

Zone attribute setter



97
98
99
# File 'lib/dyn/traffic.rb', line 97

def zone=(zone)
  @zone = zone
end