<s> '''<STR_LIT>''' <EOL> import xmlrpclib <EOL> import logging <EOL> import urllib2 <EOL> import requests <EOL> import mc_states . api <EOL> from salt . utils . odict import OrderedDict <EOL> import salt . exceptions <EOL> class ClientNotActivated ( salt . exceptions . SaltException ) : <EOL> """<STR_LIT:.>""" <EOL> __name = '<STR_LIT>' <EOL> _marker = object ( ) <EOL> log = logging . getLogger ( __name__ ) <EOL> try : <EOL> import ovh <EOL> HAS_OVH = True <EOL> except ImportError : <EOL> HAS_OVH = False <EOL> def settings ( ) : <EOL> '''<STR_LIT>''' <EOL> @ mc_states . api . lazy_subregistry_get ( __salt__ , __name ) <EOL> def _settings ( ) : <EOL> grains = __grains__ <EOL> is_ovh = '<STR_LIT>' in __grains__ [ '<STR_LIT:id>' ] <EOL> is_sys = __grains__ [ '<STR_LIT:id>' ] . startswith ( '<STR_LIT>' ) <EOL> is_online = '<STR_LIT>' in __grains__ [ '<STR_LIT:id>' ] <EOL> have_rpn = None <EOL> ifaces = grains [ '<STR_LIT>' ] . items ( ) <EOL> data = __salt__ [ '<STR_LIT>' ] ( <EOL> '<STR_LIT>' , { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:default>' : { <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : None , <EOL> } <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:default>' : { <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT:password>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> } <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : is_online , <EOL> '<STR_LIT>' : is_ovh , <EOL> '<STR_LIT>' : is_sys , <EOL> } , <EOL> '<STR_LIT>' : have_rpn , <EOL> } ) <EOL> if data [ '<STR_LIT>' ] [ '<STR_LIT>' ] and data [ '<STR_LIT>' ] is None : <EOL> for ifc in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if True in [ ifc == a [ <NUM_LIT:0> ] for a in ifaces ] : <EOL> data [ '<STR_LIT>' ] = True <EOL> return data <EOL> return _settings ( ) <EOL> def get_provider_opt ( provider , opt , default = _marker , domain = None ) : <EOL> data = settings ( ) [ provider ] <EOL> val = _marker <EOL> if domain : <EOL> val = data . get ( domain , { } ) . get ( opt , _marker ) <EOL> if val is _marker : <EOL> val = data [ '<STR_LIT:default>' ] . get ( opt , _marker ) <EOL> if val is _marker : <EOL> if default is not _marker : <EOL> val = default <EOL> else : <EOL> raise KeyError ( opt ) <EOL> return val <EOL> def get_ovh_opt ( opt , default = None , domain = None ) : <EOL> return get_provider_opt ( '<STR_LIT>' , opt , domain = domain ) <EOL> def get_gandi_opt ( opt , default = None , domain = None ) : <EOL> return get_provider_opt ( '<STR_LIT>' , opt , domain = domain ) <EOL> def ovh_auth ( app_key = None ) : <EOL> if not app_key : <EOL> app_key = get_ovh_opt ( '<STR_LIT>' ) <EOL> headers = { "<STR_LIT>" : app_key , <EOL> "<STR_LIT>" : "<STR_LIT:application/json>" } <EOL> end_point = get_ovh_opt ( '<STR_LIT>' ) + '<STR_LIT>' <EOL> payload = __salt__ [ '<STR_LIT>' ] ( { <EOL> "<STR_LIT>" : [ <EOL> { "<STR_LIT>" : "<STR_LIT:POST>" , "<STR_LIT:path>" : "<STR_LIT>" } , <EOL> { "<STR_LIT>" : "<STR_LIT:GET>" , "<STR_LIT:path>" : "<STR_LIT>" } , <EOL> { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:path>" : "<STR_LIT>" } , <EOL> { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:path>" : "<STR_LIT>" } <EOL> ] , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } ) <EOL> try : <EOL> res = requests . post ( end_point , data = payload , headers = headers ) <EOL> data = res . json ( ) <EOL> except ( urllib2 . HTTPError , ) as exc : <EOL> if exc . getcode ( ) == <NUM_LIT> : <EOL> raise Exception ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return data <EOL> def gandi_client ( ** kw ) : <EOL> domain = kw . get ( '<STR_LIT>' , None ) <EOL> if not get_gandi_opt ( '<STR_LIT>' , domain = domain ) : <EOL> raise ClientNotActivated ( '<STR_LIT>' ) <EOL> uapi = get_gandi_opt ( '<STR_LIT>' , domain = domain ) <EOL> apikey = get_gandi_opt ( '<STR_LIT>' , domain = domain ) <EOL> api = xmlrpclib . ServerProxy ( uapi ) <EOL> return api , apikey <EOL> def ovh_client ( ** kw ) : <EOL> domain = kw . pop ( '<STR_LIT>' , None ) <EOL> if not get_ovh_opt ( '<STR_LIT>' , domain = domain ) : <EOL> raise ClientNotActivated ( '<STR_LIT>' ) <EOL> if not HAS_OVH : <EOL> raise ValueError ( <EOL> '<STR_LIT>' ) <EOL> ckw = { } <EOL> for i in [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] : <EOL> ckw [ i ] = kw . setdefault ( i , get_ovh_opt ( i , domain = domain ) ) <EOL> client = ovh . Client ( ** ckw ) <EOL> return client <EOL> def have_rpn ( ) : <EOL> _s = __salt__ <EOL> providers = _s [ '<STR_LIT>' ] ( ) <EOL> have_rpn = providers [ '<STR_LIT>' ] <EOL> return have_rpn </s>
