Class: TungstenAPI::TungstenDataserviceManager

Inherits:
Object
  • Object
show all
Defined in:
lib/tungsten/api.rb

Overview

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

Direct Known Subclasses

TungstenReplicator

Instance Method Summary collapse

Constructor Details

#initialize(api_server) ⇒ TungstenDataserviceManager

Registers all the known API calls for Tungsten data service



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# 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

Instance Method Details

#add_api_call(api_call) ⇒ Object

Registers a given API call into the service It is safe to use in derived classes



389
390
391
# File 'lib/tungsten/api.rb', line 389

def add_api_call (api_call)
    @api_calls[api_call.name()] = api_call
end

#call(service, name, type = nil, api_server = nil) ⇒ Object

Calls a named service with explicit mode (:get or :post)



480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# 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

#call_default(service, name, api_server = nil) ⇒ Object

Calls the API using the method for which the call was registered. There is no need to specify :get or :post



467
468
469
470
471
472
473
474
475
# 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

#dashesObject

returns the sub-header dashes for the api list It must be overriden by derived classes



405
406
407
# File 'lib/tungsten/api.rb', line 405

def dashes
    return APICall.dashes
end

#get(service, name, api_server = nil) ⇒ Object

Runs a ‘get’ call with a given API



450
451
452
453
# 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

#headerObject

returns the header for the api list It must be overriden by derived classes



397
398
399
# File 'lib/tungsten/api.rb', line 397

def header
    return APICall.header
end

#list(display_mode = :text) ⇒ Object

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



418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# 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

#post(service, name, api_server = nil) ⇒ Object

Runs a ‘post’ call with a given API



458
459
460
461
# 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

#set_server(api_server) ⇒ Object

Changes the default api_server



381
382
383
# File 'lib/tungsten/api.rb', line 381

def set_server (api_server)
    @api_server = api_server
end

#to_hashObject

Returns a Hash with the list of API calls



439
440
441
442
443
444
445
# 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