Ejemplo n.º 1
0
    def test_load_nova2(self):
        """test_load_nova check that we could build a map from nova resources using Direct_objects directive."""

        openstackmap = OpenStackMap(
            auto_load=False, objects_strategy=OpenStackMap.DIRECT_OBJECTS)
        openstackmap.load_nova()
        self.assertIsNotNone(openstackmap)
Ejemplo n.º 2
0
    def setUp(self):
        """Create fake pickle objects to be used as cache"""
        self.tmpdir = tempfile.mkdtemp()
        os.mkdir(self.tmpdir + os.path.sep + 'keystone')
        os.mkdir(self.tmpdir + os.path.sep + 'region1')
        os.mkdir(self.tmpdir + os.path.sep + 'region2')
        sample_dict = {id: 'value1'}
        for resource in self.keystone_objects:
            with open(self.tmpdir + '/keystone/' + resource + '.pickle',
                      'wb') as f:
                pickle.dump(sample_dict, f, protocol=-1)

        for resource in OpenStackMap.resources_region:
            with open(self.tmpdir + '/region1/' + resource + '.pickle',
                      'wb') as f:
                pickle.dump(sample_dict, f, protocol=-1)

        with open(self.tmpdir + '/region2/vms.pickle', 'wb') as f:
            pickle.dump(sample_dict, f, protocol=-1)

        self.map = OpenStackMap(
            self.tmpdir,
            region='region1',
            auto_load=False,
            objects_strategy=OpenStackMap.USE_CACHE_OBJECTS_ONLY)
Ejemplo n.º 3
0
    def test_load_cinder(self):
        """test_load_cinder check that we could build a map from cinder resources using Direct_objects directive."""
        environ.setdefault('KEYSTONE_ADMIN_ENDPOINT', self.OS_AUTH_URL)

        openstackmap = OpenStackMap(
            auto_load=False, objects_strategy=OpenStackMap.DIRECT_OBJECTS)
        openstackmap.load_cinder()
        self.assertIsNotNone(openstackmap)
Ejemplo n.º 4
0
    def test_implement_openstackmap_without_region_name(self):
        """test_implement_openstackmap_without_region_name check that we could not build an empty map without providing
        a region Name."""

        del os.environ['OS_REGION_NAME']

        with self.assertRaises(Exception):
            OpenStackMap(auth_url=self.OS_AUTH_URL, auto_load=False)
Ejemplo n.º 5
0
 def test_implement_openstackmap(self, mock_exists, mock_os):
     """test_implement_openstackmap check that we could build an empty map from the resources (VMs, networks, images,
     volumes, users, tenants, roles...) in an OpenStack infrastructure."""
     mock_exists.return_value = False
     mock_os.return_value = True
     openstackmap = OpenStackMap(region=self.OS_REGION_NAME,
                                 auto_load=False)
     self.assertTrue(mock_os.called)
     self.assertIsNotNone(openstackmap)
 def __init__(self):
     """constructor"""
     self.logger = logging.getLogger(__name__)
     OpenStackMap.load_filters = False
     osmap = OpenStackMap(objects_strategy=OpenStackMap.NO_CACHE_OBJECTS,
                          auto_load=False)
     osmap.load_keystone()
     osmap.load_neutron()
     self.map = osmap
     self.neutron = self.map.osclients.get_neutronclient()
Ejemplo n.º 7
0
def get_email_osclient(username, password, region):
    """
    Get the list of user of one region taking into account a bottom-up analysis.

    :param username: The name of the admin user that launch the request.
    :param password: The password of the admin user.
    :param region: The region in which we want to obtain the data.
    :return: The emaillist.
    """
    print("Making analysis bottom-up...")

    # Set environment variables
    os.environ['OS_USERNAME'] = username
    os.environ['OS_PASSWORD'] = password
    os.environ['OS_TENANT_NAME'] = 'admin'
    os.environ['OS_REGION_NAME'] = region
    os.environ['KEYSTONE_ADMIN_ENDPOINT'] = 'http://cloud.lab.fiware.org:4730/'
    os.environ['OS_AUTH_URL'] = 'http://cloud.lab.fiware.org:4730/v2.0'

    # load data from servers
    map = OpenStackMap('tmp_cache', auto_load=False)
    map.load_keystone()

    # Get region filters and empty filter
    regions_filters = dict()
    empty_filter = None

    for filter in map.filters.values():
        if 'region_id' in filter['filters']:
            regions_filters[filter['filters']['region_id']] = filter['id']
        elif not filter['filters']:
            empty_filter = filter['id']

    useremail = dict()
    # Get users. Genuine FIWARE Users should have cloud_project_id. Be aware that
    # there are users without this field (administrators and also other users)
    for user in map.users.values():
        if 'cloud_project_id' not in user:
            continue

        project_id = user['cloud_project_id']
        found = False

        if project_id not in map.filters_by_project:
            found = False
        else:
            for filter in map.filters_by_project[project_id]:
                if filter == regions_filters[region] or filter == empty_filter:
                    found = True
                    break

        if found:
            useremail[user.id] = user.name

    return useremail
Ejemplo n.º 8
0
    def test_implement_openstackmap_with_keystone_admin_endpoint(
            self, mock_exists, mock_os):
        """test_implement_openstackmap_with_keystone_admin_endpoint check that we could build an empty map from the
         resources providing a keystone admin endpoint environment variable."""

        environ.setdefault('KEYSTONE_ADMIN_ENDPOINT', self.OS_AUTH_URL)

        mock_exists.return_value = False
        mock_os.return_value = True
        openstackmap = OpenStackMap(auto_load=False)
        self.assertTrue(mock_os.called)
        self.assertIsNotNone(openstackmap)
Ejemplo n.º 9
0
    def test_load_nova(self, mock_exists, mock_os):
        """test_load_nova check that we could build an empty map from nova resources."""

        environ.setdefault('KEYSTONE_ADMIN_ENDPOINT', self.OS_AUTH_URL)

        mock_exists.return_value = False
        mock_os.return_value = True
        openstackmap = OpenStackMap(
            auto_load=False, objects_strategy=OpenStackMap.NO_CACHE_OBJECTS)

        openstackmap.load_nova()
        self.assertTrue(mock_os.called)
        self.assertIsNotNone(openstackmap)
    def __init__(self, cache_dir, regions=None, offline_mode=False):
        """Constructor
        It also build the sets about users and tenants
        :param cache_dir: the directory where the data is cached.
        :param regions: a list with the regions whose maps are preload. If None
          only the current region.
        :param offline_mode: if True, never connect with servers, use only the
                             cached data.
        """
        self.logger = logging.getLogger(__name__)
        if offline_mode:
            strategy = OpenStackMap.USE_CACHE_OBJECTS_ONLY
        else:
            strategy = OpenStackMap.USE_CACHE_OBJECTS

        if regions:
            self.map = OpenStackMap(cache_dir,
                                    objects_strategy=strategy,
                                    auto_load=False)
            self.map.preload_regions(regions)
        else:
            self.map = OpenStackMap(cache_dir, objects_strategy=strategy)

        # This groups should be disjoint
        self.admin_users = set()
        self.trial_users = set()
        self.community_users = set()
        self.basic_users = set()
        self.other_users = set()
        self.not_found_users = set()
        # Broken users can be of any of the other types (not found, basic...)
        self.broken_users = set()

        self.user_cloud_projects = set()
        self.user_default_projects = set()

        self.comtrialadmin_cloud_projects = set()
        self.basic_cloud_projects = set()
        self.community_cloud_projects = set()
        self.trial_cloud_projects = set()
        self.admin_cloud_projects = set()
        self.other_users_cloud_projects = set()
        self.not_found_cloud_projects = set()
        self.broken_users_projects = set()

        self._process_users()

        # Read all the filters
        self.filter_by_region = dict()
        self.empty_filter = None
        for filter in self.map.filters.values():
            filter_cond = filter['filters']
            if not filter_cond:
                self.empty_filter = filter.id
            elif 'region_id' in filter_cond:
                self.filter_by_region[filter_cond['region_id']] = filter.id

        if regions:
            self.regions = regions
        else:
            self.regions = [self.map.osclients.region]