<s> import collections <EOL> import logging <EOL> import base64 <EOL> import json <EOL> import os <EOL> import six <EOL> log = logging . getLogger ( __name__ ) <EOL> class ConsulException ( Exception ) : <EOL> pass <EOL> class ACLDisabled ( ConsulException ) : <EOL> pass <EOL> class ACLPermissionDenied ( ConsulException ) : <EOL> pass <EOL> class NotFound ( ConsulException ) : <EOL> pass <EOL> class Timeout ( ConsulException ) : <EOL> pass <EOL> class Check ( object ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def script ( klass , script , interval ) : <EOL> """<STR_LIT>""" <EOL> return { '<STR_LIT>' : script , '<STR_LIT>' : interval } <EOL> @ classmethod <EOL> def http ( klass , url , interval , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> ret = { '<STR_LIT:http>' : url , '<STR_LIT>' : interval } <EOL> if timeout : <EOL> ret [ '<STR_LIT>' ] = timeout <EOL> return ret <EOL> @ classmethod <EOL> def tcp ( klass , host , port , interval , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> ret = { <EOL> '<STR_LIT>' : '<STR_LIT>' . format ( host = host , port = port ) , <EOL> '<STR_LIT>' : interval <EOL> } <EOL> if timeout : <EOL> ret [ '<STR_LIT>' ] = timeout <EOL> return ret <EOL> @ classmethod <EOL> def ttl ( klass , ttl ) : <EOL> """<STR_LIT>""" <EOL> return { '<STR_LIT>' : ttl } <EOL> @ classmethod <EOL> def docker ( klass , container_id , shell , script , interval ) : <EOL> """<STR_LIT>""" <EOL> ret = { <EOL> '<STR_LIT>' : container_id , <EOL> '<STR_LIT>' : shell , <EOL> '<STR_LIT>' : script , <EOL> '<STR_LIT>' : interval <EOL> } <EOL> return ret <EOL> @ classmethod <EOL> def _compat ( <EOL> self , <EOL> script = None , <EOL> interval = None , <EOL> ttl = None , <EOL> http = None , <EOL> timeout = None ) : <EOL> if not script and not http and not ttl : <EOL> return { } <EOL> log . warn ( <EOL> '<STR_LIT>' ) <EOL> ret = { '<STR_LIT>' : { } } <EOL> if script : <EOL> assert interval and not ( ttl or http ) <EOL> ret [ '<STR_LIT>' ] = { '<STR_LIT>' : script , '<STR_LIT>' : interval } <EOL> if ttl : <EOL> assert not ( interval or script or http ) <EOL> ret [ '<STR_LIT>' ] = { '<STR_LIT>' : ttl } <EOL> if http : <EOL> assert interval and not ( script or ttl ) <EOL> ret [ '<STR_LIT>' ] = { '<STR_LIT:http>' : http , '<STR_LIT>' : interval } <EOL> if timeout : <EOL> assert http <EOL> ret [ '<STR_LIT>' ] [ '<STR_LIT>' ] = timeout <EOL> return ret <EOL> Response = collections . namedtuple ( '<STR_LIT>' , [ '<STR_LIT:code>' , '<STR_LIT>' , '<STR_LIT:body>' ] ) <EOL> def callback ( <EOL> map = None , <EOL> is_200 = False , <EOL> is_json = False , <EOL> index = False , <EOL> one = False , <EOL> allow_404 = True ) : <EOL> def cb ( response ) : <EOL> if response . code >= <NUM_LIT> and response . code < <NUM_LIT> : <EOL> raise ConsulException ( "<STR_LIT>" % ( response . code , response . body ) ) <EOL> if response . code == <NUM_LIT> : <EOL> raise ACLPermissionDenied ( response . body ) <EOL> if response . code == <NUM_LIT> and not allow_404 : <EOL> raise NotFound ( response . body ) <EOL> if is_200 : <EOL> data = response . code == <NUM_LIT:200> <EOL> elif is_json : <EOL> data = json . loads ( response . body ) <EOL> else : <EOL> data = response <EOL> if one : <EOL> if data == [ ] : <EOL> data = None <EOL> if data is not None : <EOL> data = data [ <NUM_LIT:0> ] <EOL> if map : <EOL> data = map ( data ) <EOL> if index : <EOL> return response . headers [ '<STR_LIT>' ] , data <EOL> return data <EOL> return cb <EOL> class Consul ( object ) : <EOL> def __init__ ( <EOL> self , <EOL> host = '<STR_LIT:127.0.0.1>' , <EOL> port = <NUM_LIT> , <EOL> token = None , <EOL> scheme = '<STR_LIT:http>' , <EOL> consistency = '<STR_LIT:default>' , <EOL> dc = None , <EOL> verify = True ) : <EOL> """<STR_LIT>""" <EOL> if os . getenv ( '<STR_LIT>' ) : <EOL> host , port = os . getenv ( '<STR_LIT>' ) . split ( '<STR_LIT::>' ) <EOL> use_ssl = os . getenv ( '<STR_LIT>' ) <EOL> if use_ssl is not None : <EOL> scheme = '<STR_LIT>' if use_ssl == '<STR_LIT:true>' else '<STR_LIT:http>' <EOL> if os . getenv ( '<STR_LIT>' ) is not None : <EOL> verify = os . getenv ( '<STR_LIT>' ) == '<STR_LIT:true>' <EOL> self . http = self . connect ( host , port , scheme , verify ) <EOL> self . token = os . getenv ( '<STR_LIT>' , token ) <EOL> self . scheme = scheme <EOL> self . dc = dc <EOL> assert consistency in ( '<STR_LIT:default>' , '<STR_LIT>' , '<STR_LIT>' ) , '<STR_LIT>' <EOL> self . consistency = consistency <EOL> self . event = Consul . Event ( self ) <EOL> self . kv = Consul . KV ( self ) <EOL> self . agent = Consul . Agent ( self ) <EOL> self . catalog = Consul . Catalog ( self ) <EOL> self . health = Consul . Health ( self ) <EOL> self . session = Consul . Session ( self ) <EOL> self . acl = Consul . ACL ( self ) <EOL> self . status = Consul . Status ( self ) <EOL> class Event ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , agent ) : <EOL> self . agent = agent <EOL> def fire ( <EOL> self , <EOL> name , <EOL> body = "<STR_LIT>" , <EOL> node = None , <EOL> service = None , <EOL> tag = None ) : <EOL> """<STR_LIT>""" <EOL> assert not name . startswith ( '<STR_LIT:/>' ) , '<STR_LIT>' <EOL> params = { } <EOL> if node is not None : <EOL> params [ '<STR_LIT>' ] = node <EOL> if service is not None : <EOL> params [ '<STR_LIT>' ] = service <EOL> if tag is not None : <EOL> params [ '<STR_LIT>' ] = tag <EOL> return self . agent . http . put ( <EOL> callback ( is_json = True ) , <EOL> '<STR_LIT>' % name , params = params , data = body ) <EOL> def list ( <EOL> self , <EOL> name = None , <EOL> index = None , <EOL> wait = None ) : <EOL> """<STR_LIT>""" <EOL> params = { } <EOL> if name is not None : <EOL> params [ '<STR_LIT:name>' ] = name <EOL> if index : <EOL> params [ '<STR_LIT:index>' ] = index <EOL> if wait : <EOL> params [ '<STR_LIT>' ] = wait <EOL> def callback ( response ) : <EOL> data = json . loads ( response . body ) <EOL> for item in data : <EOL> if item . get ( '<STR_LIT>' ) is not None : <EOL> item [ '<STR_LIT>' ] = base64 . b64decode ( item [ '<STR_LIT>' ] ) <EOL> return response . headers [ '<STR_LIT>' ] , data <EOL> return self . agent . http . get ( <EOL> callback , '<STR_LIT>' , params = params ) <EOL> class KV ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , agent ) : <EOL> self . agent = agent <EOL> def get ( <EOL> self , <EOL> key , <EOL> index = None , <EOL> recurse = False , <EOL> wait = None , <EOL> token = None , <EOL> consistency = None , <EOL> keys = False , <EOL> separator = None , <EOL> dc = None ) : <EOL> """<STR_LIT>""" <EOL> assert not key . startswith ( '<STR_LIT:/>' ) , '<STR_LIT>' <EOL> params = { } <EOL> if index : <EOL> params [ '<STR_LIT:index>' ] = index <EOL> if wait : <EOL> params [ '<STR_LIT>' ] = wait <EOL> if recurse : <EOL> params [ '<STR_LIT>' ] = '<STR_LIT:1>' <EOL> token = token or self . agent . token <EOL> if token : <EOL> params [ '<STR_LIT>' ] = token <EOL> dc = dc or self . agent . dc <EOL> if dc : <EOL> params [ '<STR_LIT>' ] = dc <EOL> if keys : <EOL> params [ '<STR_LIT>' ] = True <EOL> if separator : <EOL> params [ '<STR_LIT>' ] = separator <EOL> consistency = consistency or self . agent . consistency <EOL> if consistency in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> params [ consistency ] = '<STR_LIT:1>' <EOL> def callback ( response ) : <EOL> if response . code == <NUM_LIT> : <EOL> raise ConsulException ( response . body ) <EOL> elif response . code == <NUM_LIT> : <EOL> data = None <EOL> else : <EOL> data = json . loads ( response . body ) <EOL> if not keys : <EOL> for item in data : <EOL> if item . get ( '<STR_LIT>' ) is not None : <EOL> item [ '<STR_LIT>' ] = base64 . b64decode ( item [ '<STR_LIT>' ] ) <EOL> if not recurse : <EOL> data = data [ <NUM_LIT:0> ] <EOL> return response . headers [ '<STR_LIT>' ] , data <EOL> return self . agent . http . get ( <EOL> callback , '<STR_LIT>' % key , params = params ) <EOL> def put ( <EOL> self , <EOL> key , <EOL> value , <EOL> cas = None , <EOL> flags = None , <EOL> acquire = None , <EOL> release = None , <EOL> token = None , <EOL> dc = None ) : <EOL> """<STR_LIT>""" <EOL> assert not key . startswith ( '<STR_LIT:/>' ) , '<STR_LIT>' <EOL> assert value is None or isinstance ( value , ( six . string_types , six . binary_type ) ) , "<STR_LIT>" <EOL> params = { } <EOL> if cas is not None : <EOL> params [ '<STR_LIT>' ] = cas <EOL> if flags is not None : <EOL> params [ '<STR_LIT>' ] = flags <EOL> if acquire : <EOL> params [ '<STR_LIT>' ] = acquire <EOL> if release : <EOL> params [ '<STR_LIT>' ] = release <EOL> token = token or self . agent . token <EOL> if token : <EOL> params [ '<STR_LIT>' ] = token <EOL> dc = dc or self . agent . dc <EOL> if dc : <EOL> params [ '<STR_LIT>' ] = dc <EOL> return self . agent . http . put ( <EOL> callback ( is_json = True ) , <EOL> '<STR_LIT>' % key , params = params , data = value ) <EOL> def delete ( self , key , recurse = None , cas = None , token = None , dc = None ) : <EOL> """<STR_LIT>""" <EOL> assert not key . startswith ( '<STR_LIT:/>' ) , '<STR_LIT>' <EOL> params = { } <EOL> if recurse : <EOL> params [ '<STR_LIT>' ] = '<STR_LIT:1>' <EOL> if cas is not None : <EOL> params [ '<STR_LIT>' ] = cas <EOL> token = token or self . agent . token <EOL> if token : <EOL> params [ '<STR_LIT>' ] = token <EOL> dc = dc or self . agent . dc <EOL> if dc : <EOL> params [ '<STR_LIT>' ] = dc <EOL> def callback ( response ) : <EOL> if response . code == <NUM_LIT> : <EOL> raise ACLPermissionDenied ( response . body ) <EOL> return json . loads ( response . body ) <EOL> return self . agent . http . delete ( <EOL> callback , '<STR_LIT>' % key , params = params ) <EOL> class Agent ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , agent ) : <EOL> self . agent = agent <EOL> self . service = Consul . Agent . Service ( agent ) <EOL> self . check = Consul . Agent . Check ( agent ) <EOL> def self ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . agent . http . get ( <EOL> lambda x : json . loads ( x . body ) , '<STR_LIT>' ) <EOL> def services ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . agent . http . get ( <EOL> lambda x : json . loads ( x . body ) , '<STR_LIT>' ) <EOL> def checks ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . agent . http . get ( <EOL> lambda x : json . loads ( x . body ) , '<STR_LIT>' ) <EOL> def members ( self , wan = False ) : <EOL> """<STR_LIT>""" <EOL> params = { } <EOL> if wan : <EOL> params [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> return self . agent . http . get ( <EOL> lambda x : json . loads ( x . body ) , <EOL> '<STR_LIT>' , <EOL> params = params ) <EOL> def maintenance ( self , enable , reason = None ) : <EOL> """<STR_LIT>""" <EOL> params = { } <EOL> params [ '<STR_LIT>' ] = enable <EOL> if reason : <EOL> params [ '<STR_LIT>' ] = reason <EOL> return self . agent . http . put ( <EOL> lambda x : x . code == <NUM_LIT:200> , <EOL> '<STR_LIT>' , <EOL> params = params ) <EOL> def join ( self , address , wan = False ) : <EOL> """<STR_LIT>""" <EOL> params = { } <EOL> if wan : <EOL> params [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> return self . agent . http . get ( <EOL> lambda x : x . code == <NUM_LIT:200> , <EOL> '<STR_LIT>' % address , <EOL> params = params ) <EOL> def force_leave ( self , node ) : <EOL> """<STR_LIT>""" <EOL> return self . agent . http . get ( <EOL> lambda x : x . code == <NUM_LIT:200> , <EOL> '<STR_LIT>' % node ) <EOL> class Service ( object ) : <EOL> def __init__ ( self , agent ) : <EOL> self . agent = agent <EOL> def register ( <EOL> self , <EOL> name , <EOL> service_id = None , <EOL> address = None , <EOL> port = None , <EOL> tags = None , <EOL> check = None , <EOL> token = None , <EOL> script = None , <EOL> interval = None , <EOL> ttl = None , <EOL> http = None , <EOL> timeout = None ) : <EOL> """<STR_LIT>""" <EOL> payload = { '<STR_LIT:name>' : name } <EOL> if service_id : <EOL> payload [ '<STR_LIT:id>' ] = service_id <EOL> if address : <EOL> payload [ '<STR_LIT:address>' ] = address <EOL> if port : <EOL> payload [ '<STR_LIT:port>' ] = port <EOL> if tags : <EOL> payload [ '<STR_LIT>' ] = tags <EOL> if check : <EOL> payload [ '<STR_LIT>' ] = check <EOL> else : <EOL> payload . update ( Check . _compat ( <EOL> script = script , <EOL> interval = interval , <EOL> ttl = ttl , <EOL> http = http , <EOL> timeout = timeout ) ) <EOL> params = { } <EOL> token = token or self . agent . token <EOL> if token : <EOL> params [ '<STR_LIT>' ] = token <EOL> return self . agent . http . put ( <EOL> lambda x : x . code == <NUM_LIT:200> , <EOL> '<STR_LIT>' , <EOL> params = params , <EOL> data = json . dumps ( payload ) ) <EOL> def deregister ( self , service_id ) : <EOL> """<STR_LIT>""" <EOL> return self . agent . http . get ( <EOL> lambda x : x . code == <NUM_LIT:200> , <EOL> '<STR_LIT>' % service_id ) <EOL> def maintenance ( self , service_id , enable , reason = None ) : <EOL> """<STR_LIT>""" <EOL> params = { } <EOL> params [ '<STR_LIT>' ] = enable <EOL> if reason : <EOL> params [ '<STR_LIT>' ] = reason <EOL> return self . agent . http . put ( <EOL> lambda x : x . code == <NUM_LIT:200> , <EOL> '<STR_LIT>' . format ( service_id ) , <EOL> params = params ) <EOL> class Check ( object ) : <EOL> def __init__ ( self , agent ) : <EOL> self . agent = agent <EOL> def register ( <EOL> self , <EOL> name , <EOL> check = None , <EOL> check_id = None , <EOL> notes = None , <EOL> service_id = None , <EOL> token = None , <EOL> script = None , <EOL> interval = None , <EOL> ttl = None , <EOL> http = None , <EOL> timeout = None ) : <EOL> """<STR_LIT>""" <EOL> payload = { '<STR_LIT:name>' : name } <EOL> assert check or script or ttl or http , '<STR_LIT>' <EOL> if check : <EOL> payload . update ( check ) <EOL> else : <EOL> payload . update ( Check . _compat ( <EOL> script = script , <EOL> interval = interval , <EOL> ttl = ttl , <EOL> http = http , <EOL> timeout = timeout ) [ '<STR_LIT>' ] ) <EOL> if check_id : <EOL> payload [ '<STR_LIT:id>' ] = check_id <EOL> if notes : <EOL> payload [ '<STR_LIT>' ] = notes <EOL> if service_id : <EOL> payload [ '<STR_LIT>' ] = service_id <EOL> params = { } <EOL> token = token or self . agent . token <EOL> if token : <EOL> params [ '<STR_LIT>' ] = token <EOL> return self . agent . http . put ( <EOL> lambda x : x . code == <NUM_LIT:200> , <EOL> '<STR_LIT>' , <EOL> params = params , <EOL> data = json . dumps ( payload ) ) <EOL> def deregister ( self , check_id ) : <EOL> """<STR_LIT>""" <EOL> return self . agent . http . get ( <EOL> lambda x : x . code == <NUM_LIT:200> , <EOL> '<STR_LIT>' % check_id ) <EOL> def ttl_pass ( self , check_id , notes = None ) : <EOL> """<STR_LIT>""" <EOL> params = { } <EOL> if notes : <EOL> params [ '<STR_LIT>' ] = notes <EOL> return self . agent . http . get ( <EOL> lambda x : x . code == <NUM_LIT:200> , <EOL> '<STR_LIT>' % check_id , <EOL> params = params ) <EOL> def ttl_fail ( self , check_id , notes = None ) : <EOL> """<STR_LIT>""" <EOL> params = { } <EOL> if notes : <EOL> params [ '<STR_LIT>' ] = notes <EOL> return self . agent . http . get ( <EOL> lambda x : x . code == <NUM_LIT:200> , <EOL> '<STR_LIT>' % check_id , <EOL> params = params ) <EOL> def ttl_warn ( self , check_id , notes = None ) : <EOL> """<STR_LIT>""" <EOL> params = { } <EOL> if notes : <EOL> params [ '<STR_LIT>' ] = notes <EOL> return self . agent . http . get ( <EOL> lambda x : x . code == <NUM_LIT:200> , <EOL> '<STR_LIT>' % check_id , <EOL> params = params ) <EOL> class Catalog ( object ) : <EOL> def __init__ ( self , agent ) : <EOL> self . agent = agent <EOL> def register ( self , node , address , service = None , check = None , dc = None ) : <EOL> """<STR_LIT>""" <EOL> data = { '<STR_LIT>' : node , '<STR_LIT:address>' : address } <EOL> dc = dc or self . agent . dc <EOL> if dc : <EOL> data [ '<STR_LIT>' ] = dc <EOL> if service : <EOL> data [ '<STR_LIT>' ] = service <EOL> if check : <EOL> data [ '<STR_LIT>' ] = check <EOL> return self . agent . http . put ( <EOL> callback ( is_200 = True ) , <EOL> '<STR_LIT>' , data = json . dumps ( data ) ) <EOL> def deregister ( self , node , service_id = None , check_id = None , dc = None ) : <EOL> """<STR_LIT>""" <EOL> assert not ( service_id and check_id ) <EOL> data = { '<STR_LIT>' : node } <EOL> dc = dc or self . agent . dc <EOL> if dc : <EOL> data [ '<STR_LIT>' ] = dc <EOL> if service_id : <EOL> data [ '<STR_LIT>' ] = service_id <EOL> if check_id : <EOL> data [ '<STR_LIT>' ] = check_id <EOL> return self . agent . http . put ( <EOL> callback ( is_200 = True ) , <EOL> '<STR_LIT>' , data = json . dumps ( data ) ) <EOL> def datacenters ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . agent . http . get ( <EOL> lambda x : json . loads ( x . body ) , '<STR_LIT>' ) <EOL> def nodes ( self , index = None , wait = None , consistency = None , dc = None ) : <EOL> """<STR_LIT>""" <EOL> params = { } <EOL> dc = dc or self . agent . dc <EOL> if dc : <EOL> params [ '<STR_LIT>' ] = dc <EOL> if index : <EOL> params [ '<STR_LIT:index>' ] = index <EOL> if wait : <EOL> params [ '<STR_LIT>' ] = wait <EOL> consistency = consistency or self . agent . consistency <EOL> if consistency in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> params [ consistency ] = '<STR_LIT:1>' <EOL> return self . agent . http . get ( <EOL> callback ( is_json = True , index = True ) , <EOL> '<STR_LIT>' , params = params ) <EOL> def services ( self , index = None , wait = None , consistency = None , dc = None ) : <EOL> """<STR_LIT>""" <EOL> params = { } <EOL> dc = dc or self . agent . dc <EOL> if dc : <EOL> params [ '<STR_LIT>' ] = dc <EOL> if index : <EOL> params [ '<STR_LIT:index>' ] = index <EOL> if wait : <EOL> params [ '<STR_LIT>' ] = wait <EOL> consistency = consistency or self . agent . consistency <EOL> if consistency in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> params [ consistency ] = '<STR_LIT:1>' <EOL> return self . agent . http . get ( <EOL> callback ( is_json = True , index = True ) , <EOL> '<STR_LIT>' , params = params ) <EOL> def node ( self , node , index = None , wait = None , consistency = None , dc = None ) : <EOL> """<STR_LIT>""" <EOL> params = { } <EOL> dc = dc or self . agent . dc <EOL> if dc : <EOL> params [ '<STR_LIT>' ] = dc <EOL> if index : <EOL> params [ '<STR_LIT:index>' ] = index <EOL> if wait : <EOL> params [ '<STR_LIT>' ] = wait <EOL> consistency = consistency or self . agent . consistency <EOL> if consistency in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> params [ consistency ] = '<STR_LIT:1>' <EOL> return self . agent . http . get ( <EOL> callback ( is_json = True , index = True ) , <EOL> '<STR_LIT>' % node , params = params ) <EOL> def service ( <EOL> self , <EOL> service , <EOL> index = None , <EOL> wait = None , <EOL> tag = None , <EOL> consistency = None , <EOL> dc = None ) : <EOL> """<STR_LIT>""" <EOL> params = { } <EOL> dc = dc or self . agent . dc <EOL> if dc : <EOL> params [ '<STR_LIT>' ] = dc <EOL> if tag : <EOL> params [ '<STR_LIT>' ] = tag <EOL> if index : <EOL> params [ '<STR_LIT:index>' ] = index <EOL> if wait : <EOL> params [ '<STR_LIT>' ] = wait <EOL> consistency = consistency or self . agent . consistency <EOL> if consistency in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> params [ consistency ] = '<STR_LIT:1>' <EOL> return self . agent . http . get ( <EOL> callback ( is_json = True , index = True ) , <EOL> '<STR_LIT>' % service , params = params ) <EOL> class Health ( object ) : <EOL> def __init__ ( self , agent ) : <EOL> self . agent = agent <EOL> def service ( self , <EOL> service , <EOL> index = None , <EOL> wait = None , <EOL> passing = None , <EOL> tag = None , <EOL> dc = None ) : <EOL> """<STR_LIT>""" <EOL> params = { } <EOL> if index : <EOL> params [ '<STR_LIT:index>' ] = index <EOL> if wait : <EOL> params [ '<STR_LIT>' ] = wait <EOL> if passing : <EOL> params [ '<STR_LIT>' ] = '<STR_LIT:1>' <EOL> if tag is not None : <EOL> params [ '<STR_LIT>' ] = tag <EOL> dc = dc or self . agent . dc <EOL> if dc : <EOL> params [ '<STR_LIT>' ] = dc <EOL> def callback ( response ) : <EOL> data = json . loads ( response . body ) <EOL> return response . headers [ '<STR_LIT>' ] , data <EOL> return self . agent . http . get ( <EOL> callback , <EOL> '<STR_LIT>' % service , params = params ) <EOL> def checks ( self , service , index = None , wait = None , dc = None ) : <EOL> """<STR_LIT>""" <EOL> params = { } <EOL> if index : <EOL> params [ '<STR_LIT:index>' ] = index <EOL> if wait : <EOL> params [ '<STR_LIT>' ] = wait <EOL> dc = dc or self . agent . dc <EOL> if dc : <EOL> params [ '<STR_LIT>' ] = dc <EOL> def callback ( response ) : <EOL> data = json . loads ( response . body ) <EOL> return response . headers [ '<STR_LIT>' ] , data <EOL> return self . agent . http . get ( <EOL> callback , <EOL> '<STR_LIT>' % service , params = params ) <EOL> def state ( self , name , index = None , wait = None , dc = None ) : <EOL> """<STR_LIT>""" <EOL> assert name in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> params = { } <EOL> if index : <EOL> params [ '<STR_LIT:index>' ] = index <EOL> if wait : <EOL> params [ '<STR_LIT>' ] = wait <EOL> dc = dc or self . agent . dc <EOL> if dc : <EOL> params [ '<STR_LIT>' ] = dc <EOL> def callback ( response ) : <EOL> data = json . loads ( response . body ) <EOL> return response . headers [ '<STR_LIT>' ] , data <EOL> return self . agent . http . get ( <EOL> callback , <EOL> '<STR_LIT>' % name , params = params ) <EOL> def node ( self , node , index = None , wait = None , dc = None ) : <EOL> """<STR_LIT>""" <EOL> params = { } <EOL> if index : <EOL> params [ '<STR_LIT:index>' ] = index <EOL> if wait : <EOL> params [ '<STR_LIT>' ] = wait <EOL> dc = dc or self . agent . dc <EOL> if dc : <EOL> params [ '<STR_LIT>' ] = dc <EOL> def callback ( response ) : <EOL> data = json . loads ( response . body ) <EOL> return response . headers [ '<STR_LIT>' ] , data <EOL> return self . agent . http . get ( <EOL> callback , <EOL> '<STR_LIT>' % node , params = params ) <EOL> class Session ( object ) : <EOL> def __init__ ( self , agent ) : <EOL> self . agent = agent <EOL> def create ( <EOL> self , <EOL> name = None , <EOL> node = None , <EOL> checks = None , <EOL> lock_delay = <NUM_LIT:15> , <EOL> behavior = '<STR_LIT>' , <EOL> ttl = None , <EOL> dc = None ) : <EOL> """<STR_LIT>""" <EOL> params = { } <EOL> dc = dc or self . agent . dc <EOL> if dc : <EOL> params [ '<STR_LIT>' ] = dc <EOL> data = { } <EOL> if name : <EOL> data [ '<STR_LIT:name>' ] = name <EOL> if node : <EOL> data [ '<STR_LIT>' ] = node <EOL> if checks is not None : <EOL> data [ '<STR_LIT>' ] = checks <EOL> if lock_delay != <NUM_LIT:15> : <EOL> data [ '<STR_LIT>' ] = '<STR_LIT>' % lock_delay <EOL> assert behavior in ( '<STR_LIT>' , '<STR_LIT>' ) , '<STR_LIT>' <EOL> if behavior != '<STR_LIT>' : <EOL> data [ '<STR_LIT>' ] = behavior <EOL> if ttl : <EOL> assert <NUM_LIT:10> <= ttl <= <NUM_LIT> <EOL> data [ '<STR_LIT>' ] = '<STR_LIT>' % ttl <EOL> if data : <EOL> data = json . dumps ( data ) <EOL> else : <EOL> data = '<STR_LIT>' <EOL> return self . agent . http . put ( <EOL> callback ( lambda x : json . loads ( x . body ) [ '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' , params = params , data = data ) <EOL> def destroy ( self , session_id , dc = None ) : <EOL> """<STR_LIT>""" <EOL> params = { } <EOL> dc = dc or self . agent . dc <EOL> if dc : <EOL> params [ '<STR_LIT>' ] = dc <EOL> return self . agent . http . put ( <EOL> callback ( is_200 = True ) , <EOL> '<STR_LIT>' % session_id , params = params ) <EOL> def list ( self , index = None , wait = None , consistency = None , dc = None ) : <EOL> """<STR_LIT>""" <EOL> params = { } <EOL> dc = dc or self . agent . dc <EOL> if dc : <EOL> params [ '<STR_LIT>' ] = dc <EOL> if index : <EOL> params [ '<STR_LIT:index>' ] = index <EOL> if wait : <EOL> params [ '<STR_LIT>' ] = wait <EOL> consistency = consistency or self . agent . consistency <EOL> if consistency in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> params [ consistency ] = '<STR_LIT:1>' <EOL> return self . agent . http . get ( <EOL> callback ( is_json = True , index = True ) , <EOL> '<STR_LIT>' , params = params ) <EOL> def node ( self , node , index = None , wait = None , consistency = None , dc = None ) : <EOL> """<STR_LIT>""" <EOL> params = { } <EOL> dc = dc or self . agent . dc <EOL> if dc : <EOL> params [ '<STR_LIT>' ] = dc <EOL> if index : <EOL> params [ '<STR_LIT:index>' ] = index <EOL> if wait : <EOL> params [ '<STR_LIT>' ] = wait <EOL> consistency = consistency or self . agent . consistency <EOL> if consistency in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> params [ consistency ] = '<STR_LIT:1>' <EOL> return self . agent . http . get ( <EOL> callback ( is_json = True , index = True ) , <EOL> '<STR_LIT>' % node , params = params ) <EOL> def info ( self , <EOL> session_id , <EOL> index = None , <EOL> wait = None , <EOL> consistency = None , <EOL> dc = None ) : <EOL> """<STR_LIT>""" <EOL> params = { } <EOL> dc = dc or self . agent . dc <EOL> if dc : <EOL> params [ '<STR_LIT>' ] = dc <EOL> if index : <EOL> params [ '<STR_LIT:index>' ] = index <EOL> if wait : <EOL> params [ '<STR_LIT>' ] = wait <EOL> consistency = consistency or self . agent . consistency <EOL> if consistency in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> params [ consistency ] = '<STR_LIT:1>' <EOL> return self . agent . http . get ( <EOL> callback ( is_json = True , index = True , one = True ) , <EOL> '<STR_LIT>' % session_id , params = params ) <EOL> def renew ( self , session_id , dc = None ) : <EOL> """<STR_LIT>""" <EOL> params = { } <EOL> dc = dc or self . agent . dc <EOL> if dc : <EOL> params [ '<STR_LIT>' ] = dc <EOL> return self . agent . http . put ( <EOL> callback ( is_json = True , one = True , allow_404 = False ) , <EOL> '<STR_LIT>' % session_id , params = params ) <EOL> class ACL ( object ) : <EOL> def __init__ ( self , agent ) : <EOL> self . agent = agent <EOL> def list ( self , token = None ) : <EOL> """<STR_LIT>""" <EOL> params = { } <EOL> token = token or self . agent . token <EOL> if token : <EOL> params [ '<STR_LIT>' ] = token <EOL> def callback ( response ) : <EOL> if response . code == <NUM_LIT> : <EOL> raise ACLDisabled ( response . body ) <EOL> if response . code == <NUM_LIT> : <EOL> raise ACLPermissionDenied ( response . body ) <EOL> return json . loads ( response . body ) <EOL> return self . agent . http . get ( callback , '<STR_LIT>' , params = params ) <EOL> def info ( self , acl_id , token = None ) : <EOL> """<STR_LIT>""" <EOL> params = { } <EOL> token = token or self . agent . token <EOL> if token : <EOL> params [ '<STR_LIT>' ] = token <EOL> def callback ( response ) : <EOL> if response . code == <NUM_LIT> : <EOL> raise ACLDisabled ( response . body ) <EOL> response = json . loads ( response . body ) <EOL> if response : <EOL> return response [ <NUM_LIT:0> ] <EOL> return self . agent . http . get ( <EOL> callback , '<STR_LIT>' % acl_id , params = params ) <EOL> def create ( self , <EOL> name = None , <EOL> type = '<STR_LIT>' , <EOL> rules = None , <EOL> acl_id = None , <EOL> token = None ) : <EOL> """<STR_LIT>""" <EOL> params = { } <EOL> token = token or self . agent . token <EOL> if token : <EOL> params [ '<STR_LIT>' ] = token <EOL> payload = { } <EOL> if name : <EOL> payload [ '<STR_LIT:Name>' ] = name <EOL> if type : <EOL> assert type in ( '<STR_LIT>' , '<STR_LIT>' ) , '<STR_LIT>' <EOL> payload [ '<STR_LIT>' ] = type <EOL> if rules : <EOL> assert isinstance ( rules , str ) , '<STR_LIT>' <EOL> payload [ '<STR_LIT>' ] = rules <EOL> if acl_id : <EOL> payload [ '<STR_LIT>' ] = acl_id <EOL> if payload : <EOL> data = json . dumps ( payload ) <EOL> else : <EOL> data = '<STR_LIT>' <EOL> def callback ( response ) : <EOL> if response . code == <NUM_LIT> : <EOL> raise ACLDisabled ( response . body ) <EOL> if response . code == <NUM_LIT> : <EOL> raise ACLPermissionDenied ( response . body ) <EOL> return json . loads ( response . body ) [ '<STR_LIT>' ] <EOL> return self . agent . http . put ( <EOL> callback , '<STR_LIT>' , params = params , data = data ) <EOL> def update ( self , acl_id , name = None , type = None , rules = None , token = None ) : <EOL> """<STR_LIT>""" <EOL> params = { } <EOL> token = token or self . agent . token <EOL> if token : <EOL> params [ '<STR_LIT>' ] = token <EOL> payload = { '<STR_LIT>' : acl_id } <EOL> if name : <EOL> payload [ '<STR_LIT:Name>' ] = name <EOL> if type : <EOL> assert type in ( '<STR_LIT>' , '<STR_LIT>' ) , '<STR_LIT>' <EOL> payload [ '<STR_LIT>' ] = type <EOL> if rules : <EOL> assert isinstance ( rules , str ) , '<STR_LIT>' <EOL> payload [ '<STR_LIT>' ] = rules <EOL> data = json . dumps ( payload ) <EOL> def callback ( response ) : <EOL> if response . code == <NUM_LIT> : <EOL> raise ACLDisabled ( response . body ) <EOL> if response . code == <NUM_LIT> : <EOL> raise ACLPermissionDenied ( response . body ) <EOL> return json . loads ( response . body ) [ '<STR_LIT>' ] <EOL> return self . agent . http . put ( <EOL> callback , '<STR_LIT>' , params = params , data = data ) <EOL> def clone ( self , acl_id , token = None ) : <EOL> """<STR_LIT>""" <EOL> params = { } <EOL> token = token or self . agent . token <EOL> if token : <EOL> params [ '<STR_LIT>' ] = token <EOL> def callback ( response ) : <EOL> if response . code == <NUM_LIT> : <EOL> raise ACLDisabled ( response . body ) <EOL> if response . code == <NUM_LIT> : <EOL> raise ACLPermissionDenied ( response . body ) <EOL> return json . loads ( response . body ) [ '<STR_LIT>' ] <EOL> return self . agent . http . put ( <EOL> callback , '<STR_LIT>' % acl_id , params = params ) <EOL> def destroy ( self , acl_id , token = None ) : <EOL> """<STR_LIT>""" <EOL> params = { } <EOL> token = token or self . agent . token <EOL> if token : <EOL> params [ '<STR_LIT>' ] = token <EOL> def callback ( response ) : <EOL> if response . code == <NUM_LIT> : <EOL> raise ACLDisabled ( response . body ) <EOL> if response . code == <NUM_LIT> : <EOL> raise ACLPermissionDenied ( response . body ) <EOL> return json . loads ( response . body ) <EOL> return self . agent . http . put ( <EOL> callback , '<STR_LIT>' % acl_id , params = params ) <EOL> class Status ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , agent ) : <EOL> self . agent = agent <EOL> def leader ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . agent . http . get ( <EOL> lambda x : json . loads ( x . body ) , '<STR_LIT>' ) <EOL> def peers ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . agent . http . get ( <EOL> lambda x : json . loads ( x . body ) , '<STR_LIT>' ) </s>
