Ejemplo n.º 1
0
def provider_info(providers, request):
    """
    Introspect provider class and return response what contain:
    name - provider.name attribute
    website - provider.website attribute
    x-headers - list of provider API credentials which user should provide
        in request headers  (parsed from from __init__ method docstrings)
    supported_methods - list of all methods information which supported by
        provider, Method information parsed from method docstings
    """
    provider_name = request.args.get('provider_name', '')
    provider_name = provider_name.upper()
    providers = get_providers_dict(providers.DRIVERS, providers.Provider)
    if not provider_name in providers:
        raise ProviderNotSupportedError(provider=provider_name)
    driver = providers[provider_name]
    supported_methods = {}
    for method_name, method in inspect.getmembers(driver, inspect.ismethod):
        if method_name.startswith('_'):
            continue
        try:
            driver_method = DriverMethod(driver, method_name)
        except MethodParsingException, e:
            logger.info(str(e) + ' ' + driver.name + '.' + method_name)
            continue
        supported_methods[method_name] = driver_method.get_description()
Ejemplo n.º 2
0
def provider_info(providers, request):
    """
    Introspect provider class and return response what contain:
    name - provider.name attribute
    website - provider.website attribute
    x-headers - list of provider API credentials which user should provide
        in request headers  (parsed from from __init__ method docstrings)
    supported_methods - list of all methods information which supported by
        provider, Method information parsed from method docstings
    """
    provider_name = request.args.get('provider_name', '')
    provider_name = provider_name.upper()
    providers = get_providers_dict(providers.DRIVERS, providers.Provider)
    if not provider_name in providers:
        raise ProviderNotSupportedError(provider=provider_name)
    driver = providers[provider_name]
    supported_methods = {}
    for method_name, method in inspect.getmembers(driver,
                                                  inspect.ismethod):
        if method_name.startswith('_'):
            continue
        try:
            driver_method = DriverMethod(driver, method_name)
        except MethodParsingException, e:
            logger.info(str(e) + ' ' + driver.name + '.' + method_name)
            continue
        supported_methods[method_name] = driver_method.get_description()
class FakeDriverTests(unittest2.TestCase):
    def setUp(self):
        self.driver_method = DriverMethod(FakeDriver, 'ex_create_fake')

    def test_get_description(self):
        data = self.driver_method.get_description()
        self.assertEqual('ex_create_fake', data['name'])
        self.assertEqual('teachess volume to node.',  data['description'])
        arguments = data['arguments']
        node = {'required': True, 'type': 'str', 'name': 'node_id',
                'description': 'ID of the node which should be used'}
        volume = {'required': True, 'type': 'str', 'name': 'volume',
                  'description': 'Volume to attach'}
        device = {'required': True, 'type': 'str', 'name': 'device',
                  'description': "Where the device is exposed, "
                                 "e.g. '/dev/sdb (required)"}
        extra_dict = {'required': False, 'type': 'dict', 'name': 'extra',
                      'description': 'Extra attributes (driver specific).'}
        extra_str = {'required': False, 'type': 'str', 'name': 'extra',
                     'description': 'Extra attributes (driver specific).'}
        varg = {'default': 'value', 'required': False, 'type': 'str',
                'name': 'varg', 'description': 'with default value'}
        kwarg = {'required': False, 'type': 'str',
                 'name': 'kwarg', 'description': 'Keyword argument'}
        test_args = [node, volume, device, extra_dict, extra_str, varg, kwarg]
        self.assertEqual(arguments, test_args)
Ejemplo n.º 4
0
class FakeDriverTests(unittest2.TestCase):
    def setUp(self):
        self.driver_method = DriverMethod(FakeDriver, 'ex_create_fake')

    def test_get_description(self):
        data = self.driver_method.get_description()
        self.assertEqual('ex_create_fake', data['name'])
        self.assertEqual('teachess volume to node.', data['description'])
        arguments = data['arguments']
        node = {
            'required': True,
            'type': 'str',
            'name': 'node_id',
            'description': 'ID of the node which should be used'
        }
        volume = {
            'required': True,
            'type': 'str',
            'name': 'volume',
            'description': 'Volume to attach'
        }
        device = {
            'required':
            True,
            'type':
            'str',
            'name':
            'device',
            'description':
            "Where the device is exposed, "
            "e.g. '/dev/sdb (required)"
        }
        extra_dict = {
            'required': False,
            'type': 'dict',
            'name': 'extra',
            'description': 'Extra attributes (driver specific).'
        }
        extra_str = {
            'required': False,
            'type': 'str',
            'name': 'extra',
            'description': 'Extra attributes (driver specific).'
        }
        varg = {
            'default': 'value',
            'required': False,
            'type': 'str',
            'name': 'varg',
            'description': 'with default value'
        }
        kwarg = {
            'required': False,
            'type': 'str',
            'name': 'kwarg',
            'description': 'Keyword argument'
        }
        test_args = [node, volume, device, extra_dict, extra_str, varg, kwarg]
        self.assertEqual(arguments, test_args)
Ejemplo n.º 5
0
 def test_method(self):
     self.assertTrue(DriverMethod(FakeDriver, 'ex_create_fake'))
     self.assertRaises(NoSuchOperationError, DriverMethod, FakeDriver,
                       'variable')
     self.assertRaises(MethodParsingException, DriverMethod, FakeDriver,
                       'get_not_documented')
     self.assertRaises(MethodParsingException, DriverMethod, FakeDriver,
                       'get_unknown_argument')
Ejemplo n.º 6
0
def invoke_method(providers, method_name, request, status_code=httplib.OK,
                  data=None, file_result=False):
    """
    Invoke method and return response with result represented as json.

    @param file_result: If True wraps result
    """
    if data is None:
        data = request.data
    driver = get_driver_instance_by_request(providers, request)
    driver_method = DriverMethod(driver, method_name)
    try:
        result = driver_method.invoke(data)
    except Exception, e:
        if e.__class__ in INTERNAL_LIBCLOUD_ERRORS_MAP:
            raise INTERNAL_LIBCLOUD_ERRORS_MAP[e.__class__]()
        if isinstance(e, common_types.LibcloudError):
            raise LibcloudError(detail=str(e))
        raise
Ejemplo n.º 7
0
 def _check_docstrings(cls, providers, drivers):
     Drivers = cls._get_drivers(providers, drivers)
     for Driver in Drivers:
         methods = [mn for mn, _ in getmembers(Driver, ismethod) if
                    not mn.startswith('_')]
         methods.append('__init__')
         for method_name in methods:
             try:
                 DriverMethod(Driver, method_name)
             except Exception, e:
                 print str(e), Driver.name, method_name
Ejemplo n.º 8
0
    provider_name = provider_name.upper()
    providers = get_providers_dict(providers.DRIVERS, providers.Provider)
    if not provider_name in providers:
        raise ProviderNotSupportedError(provider=provider_name)
    driver = providers[provider_name]
    supported_methods = {}
    for method_name, method in inspect.getmembers(driver, inspect.ismethod):
        if method_name.startswith('_'):
            continue
        try:
            driver_method = DriverMethod(driver, method_name)
        except MethodParsingException, e:
            logger.info(str(e) + ' ' + driver.name + '.' + method_name)
            continue
        supported_methods[method_name] = driver_method.get_description()
    init_description = DriverMethod(driver, '__init__').get_description()
    init_arguments = init_description['arguments']
    for arg in init_arguments[:]:
        arg['name'] = ARGS_TO_XHEADERS_DICT[arg['name']]
    result = {
        'name': driver.name,
        'website': driver.website,
        'X-headers': init_arguments,
        'supported_methods': supported_methods
    }
    return JsonResponse(json.dumps(result))


class ServiceHandler(object):
    def __init__(self, url_prefix):
        self.url_prefix = url_prefix
Ejemplo n.º 9
0
 def setUp(self):
     self.driver_method = DriverMethod(FakeDriver, 'ex_create_fake')
 def setUp(self):
     self.driver_method = DriverMethod(FakeDriver, 'ex_create_fake')