class TungstenAPI::TungstenDataserviceManager
Container for API calls. It has the definition of the api calls supported through this architecture, and methods to call them easily.
Public instance methods:
* initialize(api_server) * list (display_mode) will show all the API registered with this service * set_server(api_server) * get(service,name) will return the result of a get call * post(service,name) will return the result of a post operation
Public Class Methods
Registers all the known API calls for Tungsten data service
# File lib/tungsten/api.rb, line 346 def initialize(api_server) @api_server = api_server @api_calls = {} # # get # add_api_call( APICall.new('status', 'status', '', 'Show cluster status', :hash, :get) ) add_api_call( APICall.new('policy', 'policy', '', 'Show current policy',:hash, :get) ) add_api_call( APICall.new('routers', '', 'service/router/status', 'Shows the routers for this data service',:hash, :get, true) ) add_api_call( APICall.new('members', '', 'service/members', 'Shows the members for this data service',:hash, :get, true) ) # # post # add_api_call( APICall.new('setmaintenance', 'policy', 'maintenance', 'set policy as maintenance',:hash, :post) ) add_api_call( APICall.new('setautomatic', 'policy', 'automatic', 'set policy as automatic',:hash, :post) ) add_api_call( APICall.new('setmanual', 'policy', 'manual', 'set policy as manual',:hash, :post) ) add_api_call( APICall.new('setarchive', 'control', 'setarchive', 'Sets the archve flag for a slave', :hash, :post) ) add_api_call( APICall.new('cleararchive', 'control', 'cleararchive', 'Clears the archve flag for a slave', :hash, :post) ) add_api_call( APICall.new('promote', 'control', 'promote', 'promotes a slave to master', :hash, :post) ) add_api_call( APICall.new('shun', 'control', 'shun', 'shuns a data source',:hash, :post) ) add_api_call( APICall.new('welcome', 'control', 'welcome', 'welcomes back a data source',:hash, :post) ) add_api_call( APICall.new('backup', 'control', 'backup', 'performs a datasource backup',:hash, :post) ) add_api_call( APICall.new('restore', 'control', 'restore', 'Performs a datasource restore',:hash, :post) ) add_api_call( APICall.new('online', 'control', 'online', 'puts a datasource online',:hash, :post) ) add_api_call( APICall.new('offline', 'control', 'offline', 'Puts a datasource offline',:hash, :post) ) add_api_call( APICall.new('fail', 'control', 'fail', 'fails a datasource',:hash, :post) ) add_api_call( APICall.new('recover', 'control', 'recover', 'recover a failed datasource',:hash, :post) ) add_api_call( APICall.new('heartbeat', 'control', 'heartbeat', 'Issues a heartbeat on the master',:hash, :post) ) end
Public Instance Methods
Registers a given API call into the service It is safe to use in derived classes
# File lib/tungsten/api.rb, line 389 def add_api_call (api_call) @api_calls[api_call.name()] = api_call end
Calls a named service with explicit mode (:get or :post)
# File lib/tungsten/api.rb, line 480 def call (service, name , type=nil, api_server=nil) api_server ||= @api_server api = @api_calls[name] unless api raise SyntaxError, "api call #{name} not found" end if type == nil type = api.type end if type == :get return api.get(@api_server,service) else return api.post(@api_server,service) end end
Calls the API using the method for which the call was registered. There is no need to specify :get or :post
# File lib/tungsten/api.rb, line 467 def call_default (service, name, api_server=nil ) api_server ||= @api_server api = @api_calls[name].to_hash if api.type == :get return call(service,name,:get, api_server) else return call(service,name,:post, api_server) end end
returns the sub-header dashes for the api list It must be overriden by derived classes
# File lib/tungsten/api.rb, line 405 def dashes return APICall.dashes end
Runs a ‘get’ call with a given API
# File lib/tungsten/api.rb, line 450 def get ( service, name, api_server=nil ) api_server ||= @api_server return call(service,name,:get, api_server) end
returns the header for the api list It must be overriden by derived classes
# File lib/tungsten/api.rb, line 397 def header return APICall.header end
Display the list of registered API calls using a given display_mode:
-
:text (default)
-
:hash : good for further usage of the API call within the same application
-
:json : good to export to other applications
Safe to use in derived classes
# File lib/tungsten/api.rb, line 418 def list (display_mode=:text) if display_mode == :text puts header() puts dashes() @api_calls.sort.each do |name,api| puts api end else if display_mode == :hash pp self.to_hash elsif display_mode == :json puts JSON.generate(self.to_hash) else raise SyntaxError, "no suitable display method selected" end end end
Runs a ‘post’ call with a given API
# File lib/tungsten/api.rb, line 458 def post (service, name, api_server = nil) api_server ||= @api_server return call(service,name,:post, api_server) end
Changes the default api_server
# File lib/tungsten/api.rb, line 381 def set_server (api_server) @api_server = api_server end
Returns a Hash with the list of API calls
# File lib/tungsten/api.rb, line 439 def to_hash display_api_calls = {} @api_calls.each do |name,api| display_api_calls[name] = api.to_hash end display_api_calls end