@ -45,6 +45,7 @@ nios_provider_spec = {
' username ' : dict ( ) ,
' username ' : dict ( ) ,
' password ' : dict ( no_log = True ) ,
' password ' : dict ( no_log = True ) ,
' ssl_verify ' : dict ( type = ' bool ' , default = False ) ,
' ssl_verify ' : dict ( type = ' bool ' , default = False ) ,
' silent_ssl_warnings ' : dict ( type = ' bool ' , default = True ) ,
' http_request_timeout ' : dict ( type = ' int ' , default = 10 ) ,
' http_request_timeout ' : dict ( type = ' int ' , default = 10 ) ,
' http_pool_connections ' : dict ( type = ' int ' , default = 10 ) ,
' http_pool_connections ' : dict ( type = ' int ' , default = 10 ) ,
' http_pool_maxsize ' : dict ( type = ' int ' , default = 10 ) ,
' http_pool_maxsize ' : dict ( type = ' int ' , default = 10 ) ,
@ -53,11 +54,14 @@ nios_provider_spec = {
}
}
def get_ provider_spec( ) :
def get_ connector( * args , * * kwargs ) :
return { ' provider ' : dict ( type = ' dict ' , options = nios_provider_spec ) }
''' Returns an instance of infoblox_client.connector.Connector
: params args : positional arguments are silently ignored
: params kwargs : dict that is passed to Connector init
def get_connector ( * args , * * kwargs ) :
: returns : Connector
'''
if not HAS_INFOBLOX_CLIENT :
if not HAS_INFOBLOX_CLIENT :
raise Exception ( ' infoblox-client is required but does not appear '
raise Exception ( ' infoblox-client is required but does not appear '
' to be installed. It can be installed using the '
' to be installed. It can be installed using the '
@ -66,8 +70,15 @@ def get_connector(*args, **kwargs):
if not set ( kwargs . keys ( ) ) . issubset ( nios_provider_spec . keys ( ) ) :
if not set ( kwargs . keys ( ) ) . issubset ( nios_provider_spec . keys ( ) ) :
raise Exception ( ' invalid or unsupported keyword argument for connector ' )
raise Exception ( ' invalid or unsupported keyword argument for connector ' )
for key in nios_provider_spec. keys ( ) :
for key , value in iteritems( nios_provider_spec) :
if key not in kwargs :
if key not in kwargs :
# apply default values from nios_provider_spec since we cannot just
# assume the provider values are coming from AnsibleModule
if ' default ' in value :
kwargs [ key ] = value [ ' default ' ]
# override any values with env variables unless they were
# explicitly set
env = ( ' INFOBLOX_ %s ' % key ) . upper ( )
env = ( ' INFOBLOX_ %s ' % key ) . upper ( )
if env in os . environ :
if env in os . environ :
kwargs [ key ] = os . environ . get ( env )
kwargs [ key ] = os . environ . get ( env )
@ -115,14 +126,10 @@ def flatten_extattrs(value):
class WapiBase ( object ) :
class WapiBase ( object ) :
''' Base class for implementing Infoblox WAPI API '''
''' Base class for implementing Infoblox WAPI API '''
def __init__ ( self , module ) :
provider_spec = { ' provider ' : dict ( type = ' dict ' , options = nios_provider_spec ) }
self . module = module
try :
def __init__ ( self , provider ) :
provider = module . params [ ' provider ' ] or { }
self . connector = get_connector ( * * provider )
self . connector = get_connector ( * * provider )
except Exception as exc :
module . fail_json ( msg = to_text ( exc ) )
def __getattr__ ( self , name ) :
def __getattr__ ( self , name ) :
try :
try :
@ -137,20 +144,50 @@ class WapiBase(object):
method = getattr ( self . connector , name )
method = getattr ( self . connector , name )
return method ( * args , * * kwargs )
return method ( * args , * * kwargs )
except InfobloxException as exc :
except InfobloxException as exc :
if hasattr ( self , ' handle_exception ' ) :
self . handle_exception ( name , exc )
else :
raise
class WapiLookup ( WapiBase ) :
''' Implements WapiBase for lookup plugins '''
pass
class WapiInventory ( WapiBase ) :
''' Implements WapiBase for dynamic inventory script '''
pass
class WapiModule ( WapiBase ) :
''' Implements WapiBase for executing a NIOS module '''
def __init__ ( self , module ) :
self . module = module
provider = module . params [ ' provider ' ]
try :
super ( WapiModule , self ) . __init__ ( provider )
except Exception as exc :
self . module . fail_json ( msg = to_text ( exc ) )
def handle_exception ( self , method_name , exc ) :
''' Handles any exceptions raised
This method will be called if an InfobloxException is raised for
any call to the instance of Connector . This method will then
gracefully fail the module .
: args exc : instance of InfobloxException
'''
self . module . fail_json (
self . module . fail_json (
msg = exc . response [ ' text ' ] ,
msg = exc . response [ ' text ' ] ,
type = exc . response [ ' Error ' ] . split ( ' : ' ) [ 0 ] ,
type = exc . response [ ' Error ' ] . split ( ' : ' ) [ 0 ] ,
code = exc . response . get ( ' code ' ) ,
code = exc . response . get ( ' code ' ) ,
action = name
oper ation= method_ name
)
)
def run ( self , ib_obj_type , ib_spec ) :
raise NotImplementedError
class Wapi ( WapiBase ) :
''' Implements WapiBase for executing a NIOS module '''
def run ( self , ib_obj_type , ib_spec ) :
def run ( self , ib_obj_type , ib_spec ) :
''' Runs the module and performans configuration tasks
''' Runs the module and performans configuration tasks