Example #1
0
    def __init__(self, imageElementName, endpointElementName):
        """
        Creates an instance that will manage appliances of a given type
        on a specific cloud infrastructure.  Separate instances must be
        created for different cloud infrastructures and different
        appliances.

        The configuration is validated only when the connect() method is
        called.  This method MUST be called before any of the other
        methods.

        :Parameters:
          **imageElementName** - `string`
            element name in CS:/Resources/VirtualMachines/Images describing
            the type of appliance (image) to instantiate

          **endpointElementName** - `string`
            element name in CS:/Resources/VirtualMachines/CloudEndpoint
            giving the configuration parameters for the StratusLab cloud
            endpoint

        """

        self.log = gLogger.getSubLogger('StratusLabImage_%s_%s: ' %
                                        (endpointElementName, imageElementName))

        self._imageConfig = ImageConfiguration(imageElementName)
        self._endpointConfig = StratusLabEndpointConfiguration(endpointElementName)

        self._impl = None
    def test_defined_path_returns_copy_of_correct_config(self):
        elementName = 'copy_correct'
        path = '%s/%s' % (StratusLabEndpointConfiguration.ENDPOINT_PATH, elementName)
        value = {'a': '1'}
        gConfig.HOLDER.add_options(path, value)

        cfg = StratusLabEndpointConfiguration(elementName)
        self.assertEqual(cfg.config(), value)
        self.assertIsNot(cfg.config(), value)
        self.assertFalse(cfg.validate()['OK'])
    def test_all_required_parameters(self):
        all_keys = StratusLabEndpointConfiguration.DIRAC_REQUIRED_KEYS
        all_keys = all_keys.union(StratusLabEndpointConfiguration.STRATUSLAB_REQUIRED_KEYS)
        value = {}
        for key in all_keys:
            value[key] = key

        elementName = 'all_req_keys'
        path = '%s/%s' % (StratusLabEndpointConfiguration.ENDPOINT_PATH, elementName)
        gConfig.HOLDER.add_options(path, value)

        cfg = StratusLabEndpointConfiguration(elementName)
        self.assertEqual(cfg.config(), value)
        self.assertIsNot(cfg.config(), value)
        self.assertTrue(cfg.validate()['OK'])
    def test_missing_required_parameter(self):
        all_keys = StratusLabEndpointConfiguration.DIRAC_REQUIRED_KEYS
        all_keys = all_keys.union(StratusLabEndpointConfiguration.STRATUSLAB_REQUIRED_KEYS)
        complete = {}
        for key in all_keys:
            complete[key] = key

        for key in all_keys:

            missing = copy.copy(complete)
            del missing[key]

            elementName = 'missing_%s' % key
            path = '%s/%s' % (StratusLabEndpointConfiguration.ENDPOINT_PATH, elementName)
            gConfig.HOLDER.add_options(path, missing)

            cfg = StratusLabEndpointConfiguration(elementName)
            self.assertEqual(cfg.config(), missing)
            self.assertIsNot(cfg.config(), missing)
            self.assertFalse(cfg.validate()['OK'])
    def test_invalid_credentials(self):
        all_keys = StratusLabEndpointConfiguration.DIRAC_REQUIRED_KEYS
        all_keys = all_keys.union(StratusLabEndpointConfiguration.STRATUSLAB_REQUIRED_KEYS)
        all_keys = all_keys.union(StratusLabEndpointConfiguration.STRATUSLAB_OPTIONAL_KEYS)
        complete = {}
        for key in all_keys:
            complete[key] = key

        cred_keys = 'ex_username', 'ex_password', 'ex_pem_key', 'ex_pem_certificate'

        for key in cred_keys:

            missing = copy.copy(complete)
            del missing[key]

            elementName = 'invalid_cred_%s' % key
            path = '%s/%s' % (StratusLabEndpointConfiguration.ENDPOINT_PATH, elementName)
            gConfig.HOLDER.add_options(path, missing)

            cfg = StratusLabEndpointConfiguration(elementName)
            self.assertEqual(cfg.config(), missing)
            self.assertIsNot(cfg.config(), missing)
            self.assertFalse(cfg.validate()['OK'])
 def test_empty_config_from_non_existent_path(self):
     cfg = StratusLabEndpointConfiguration('non-existent')
     self.assertEqual(cfg.config(), {})
Example #7
0
class StratusLabImage:
    """
    Provides interface for managing virtual machine instances of a
    particular appliance on a StratusLab cloud infrastructure.
    """

    def __init__(self, imageElementName, endpointElementName):
        """
        Creates an instance that will manage appliances of a given type
        on a specific cloud infrastructure.  Separate instances must be
        created for different cloud infrastructures and different
        appliances.

        The configuration is validated only when the connect() method is
        called.  This method MUST be called before any of the other
        methods.

        :Parameters:
          **imageElementName** - `string`
            element name in CS:/Resources/VirtualMachines/Images describing
            the type of appliance (image) to instantiate

          **endpointElementName** - `string`
            element name in CS:/Resources/VirtualMachines/CloudEndpoint
            giving the configuration parameters for the StratusLab cloud
            endpoint

        """

        self.log = gLogger.getSubLogger('StratusLabImage_%s_%s: ' %
                                        (endpointElementName, imageElementName))

        self._imageConfig = ImageConfiguration(imageElementName)
        self._endpointConfig = StratusLabEndpointConfiguration(endpointElementName)

        self._impl = None

    def connect(self):
        """
        Tests the connection to the StratusLab cloud infrastructure.  Validates
        the configuration and then makes a request to list active virtual
        machine instances to ensure that the connection works.

        :return: S_OK | S_ERROR
        """

        result = self._imageConfig.validate()
        self._logResult(result, 'image configuration check')
        if not result['OK']:
            return result

        result = self._endpointConfig.validate()
        self._logResult(result, 'endpoint configuration check')
        if not result['OK']:
            return result

        try:
            self._impl = StratusLabClient(self._endpointConfig, self._imageConfig)
        except Exception, e:
            return S_ERROR(e)

        result = self._impl.check_connection()
        return self._logResult(result, 'connect')