Class: DynectRest

Inherits:
Object
  • Object
show all
Defined in:
lib/dynect_rest.rb,
lib/dynect_rest/resource.rb,
lib/dynect_rest/exceptions.rb

Overview

Author

Adam Jacob (<adam@opscode.com>)

Copyright

Copyright (c) 2010 Opscode, Inc.

License

Apache License, Version 2.0

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Defined Under Namespace

Classes: Exceptions, Resource

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (DynectRest) initialize(customer_name, user_name, password, zone = nil, connect = true)

Creates a new base object for interacting with Dynect's REST API



35
36
37
38
39
40
41
42
# File 'lib/dynect_rest.rb', line 35

def initialize(customer_name, user_name, password, zone=nil, connect=true)
  @customer_name = customer_name
  @user_name = user_name
  @password = password
  @rest = RestClient::Resource.new('https://api2.dynect.net/REST/', :headers => { :content_type => 'application/json' })
  @zone = zone 
   if connect
end

Instance Attribute Details

- (Object) customer_name

Returns the value of attribute customer_name



26
27
28
# File 'lib/dynect_rest.rb', line 26

def customer_name
  @customer_name
end

- (Object) password

Returns the value of attribute password



26
27
28
# File 'lib/dynect_rest.rb', line 26

def password
  @password
end

- (Object) rest

Returns the value of attribute rest



26
27
28
# File 'lib/dynect_rest.rb', line 26

def rest
  @rest
end

- (Object) user_name

Returns the value of attribute user_name



26
27
28
# File 'lib/dynect_rest.rb', line 26

def user_name
  @user_name
end

- (Object) zone

Returns the value of attribute zone



26
27
28
# File 'lib/dynect_rest.rb', line 26

def zone
  @zone
end

Instance Method Details

- (Object) api_request(&block)

Handles making Dynect API requests and formatting the responses properly.



171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/dynect_rest.rb', line 171

def api_request(&block)
  response_body = begin
    response = block.call
    response.body
  rescue RestClient::Exception => e
    puts "I have #{e.inspect} with #{e.http_code}"
    if e.http_code == 307
      get(e.response)
    end
    e.response
  end
  parse_response(JSON.parse(response_body))
end

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

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

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



140
141
142
# File 'lib/dynect_rest.rb', line 140

def delete(path_part, additional_headers = {}, &block)
  api_request { @rest[path_part].delete(additional_headers, &block) }
end

- (Hash) freeze(zone = nil)



99
100
101
102
# File 'lib/dynect_rest.rb', line 99

def freeze(zone=nil)
  zone ||= @zone 
  put("Zone/#{zone}", { "freeze" => true })
end

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

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

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



130
131
132
# File 'lib/dynect_rest.rb', line 130

def get(path_part, additional_headers = {}, &block)
  api_request { @rest[path_part].get(additional_headers, &block) }
end

- (Hash) get_zone(zone = nil)



77
78
79
80
# File 'lib/dynect_rest.rb', line 77

def get_zone(zone=nil)  
  zone ||= @zone
  get("Zone/#{zone}")
end

- (Hash) login

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

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



53
54
55
56
57
# File 'lib/dynect_rest.rb', line 53

def 
  response = post('Session', { 'customer_name' => @customer_name, 'user_name' => @user_name, 'password' => @password })
  @rest.headers[:auth_token] = response["token"]
  response
end

- (Hash) logout

Logout of Dynect - must be done before any other methods called.

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



64
65
66
# File 'lib/dynect_rest.rb', line 64

def logout
  delete('Session')
end

- (Object) parse_response(response)



185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/dynect_rest.rb', line 185

def parse_response(response)
  case response["status"]
  when "success"
    response["data"]
  when "failure", "incomplete"
    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 DynectRest::Exceptions::RequestFailed, "Request failed: #{error_messages.join("\n")}" 
  end
end

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

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.



153
154
155
# File 'lib/dynect_rest.rb', line 153

def post(path_part, payload, additional_headers = {}, &block)
  api_request { @rest[path_part].post(payload.to_json, additional_headers, &block) }
end

- (Hash) publish(zone = nil)

Publish any pending changes to the zone - required to make any alterations permanent.

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



88
89
90
91
# File 'lib/dynect_rest.rb', line 88

def publish(zone=nil)
  zone ||= @zone 
  put("Zone/#{zone}", { "publish" => true })
end

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

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.



166
167
168
# File 'lib/dynect_rest.rb', line 166

def put(path_part, payload, additional_headers = {}, &block)
  api_request { @rest[path_part].put(payload.to_json, additional_headers, &block) }
end

- (Hash) thaw(zone = nil)



110
111
112
113
# File 'lib/dynect_rest.rb', line 110

def thaw(zone=nil)
  zone ||= @zone 
  put("Zone/#{zone}", { "freeze" => true })
end