def setup_security_groups_support(self, reset_state=True):
        """
        We will track security groups -> dvportgroup mapping though the use
        of custom attributes.

        A custom attribute must be defined initially and then queried at
        start for its id, which is then used to map to a particular value.
        """
        service_content = self.connection.vim.retrieve_service_content()
        custom_field_manager = service_content.customFieldsManager

        result = vim_util.get_object_properties(self.connection.vim,
                                                custom_field_manager, "field")
        for field in result[0].propSet[0].val.CustomFieldDef:
            if SECURITY_GROUPS_ATTRIBUTE == field.name:
                LOG.debug("Found custom attribute for security groups with key %s", field.key)
                self.security_groups_attribute_key = field.key
                break
        else:
            LOG.debug("No custom attribute for security groups found, will create one.")
            field = self.connection.invoke_api(self.connection.vim,
                                               "AddCustomFieldDef",
                                               custom_field_manager,
                                               name=SECURITY_GROUPS_ATTRIBUTE,
                                               moType="DistributedVirtualPortgroup")
            LOG.debug("Created custom attribute for security groups with key %s", field.key)
            self.security_groups_attribute_key = field.key

        if reset_state:
            wait_pile = False
            pile = GreenPile(self.pool) if self.pool else GreenPile()
            # Will drop all security group rules from matching dvportgroups and remove empty portgroups
            for uuid, dvs in six.iteritems(self.uuid_dvs_map):
                sg_tagged_pgs = dvs.get_pg_per_sg_attribute(self.security_groups_attribute_key)
                for sg_set, pg in six.iteritems(sg_tagged_pgs):
                    wait_pile = True
                    if len(pg["vm"]) == 0:
                        pile.spawn(dvs._delete_port_group, pg["ref"], pg["name"], ignore_in_use=True)
                    else:
                        port_config = dvs.builder.port_setting()
                        port_config.blocked = dvs.builder.blocked(False)
                        port_config.filterPolicy = dvs.builder.filter_policy([], None)
                        pile.spawn(dvs.update_dvportgroup,
                                   pg["ref"],
                                   pg["configVersion"],
                                   port_config,
                                   name=dvs.dvportgroup_name(sg_set))
            if wait_pile:
                for result in pile:
                    pass
Exemple #2
0
    def test_get_object_properties(self, cancel_retrieval):
        vim = mock.Mock()
        moref = mock.Mock()
        moref._type = "VirtualMachine"
        retrieve_result = mock.Mock()

        def vim_RetrievePropertiesEx_side_effect(pc,
                                                 specSet,
                                                 options,
                                                 skip_op_id=False):
            self.assertTrue(pc is vim.service_content.propertyCollector)
            self.assertEqual(1, options.maxObjects)

            self.assertEqual(1, len(specSet))
            property_filter_spec = specSet[0]

            propSet = property_filter_spec.propSet
            self.assertEqual(1, len(propSet))
            prop_spec = propSet[0]
            self.assertTrue(prop_spec.all)
            self.assertEqual(['name'], prop_spec.pathSet)
            self.assertEqual(moref._type, prop_spec.type)

            objSet = property_filter_spec.objectSet
            self.assertEqual(1, len(objSet))
            obj_spec = objSet[0]
            self.assertEqual(moref, obj_spec.obj)
            self.assertEqual([], obj_spec.selectSet)
            self.assertFalse(obj_spec.skip)

            return retrieve_result

        vim.RetrievePropertiesEx.side_effect = (
            vim_RetrievePropertiesEx_side_effect)

        res = vim_util.get_object_properties(vim, moref, None)
        self.assertEqual(1, vim.RetrievePropertiesEx.call_count)
        self.assertTrue(res is retrieve_result.objects)
        cancel_retrieval.assert_called_once_with(vim, retrieve_result)
    def test_get_object_properties(self, cancel_retrieval):
        vim = mock.Mock()
        moref = mock.Mock()
        moref._type = "VirtualMachine"
        retrieve_result = mock.Mock()

        def vim_RetrievePropertiesEx_side_effect(pc, specSet, options,
                                                 skip_op_id=False):
            self.assertTrue(pc is vim.service_content.propertyCollector)
            self.assertEqual(1, options.maxObjects)

            self.assertEqual(1, len(specSet))
            property_filter_spec = specSet[0]

            propSet = property_filter_spec.propSet
            self.assertEqual(1, len(propSet))
            prop_spec = propSet[0]
            self.assertTrue(prop_spec.all)
            self.assertEqual(['name'], prop_spec.pathSet)
            self.assertEqual(moref._type, prop_spec.type)

            objSet = property_filter_spec.objectSet
            self.assertEqual(1, len(objSet))
            obj_spec = objSet[0]
            self.assertEqual(moref, obj_spec.obj)
            self.assertEqual([], obj_spec.selectSet)
            self.assertFalse(obj_spec.skip)

            return retrieve_result

        vim.RetrievePropertiesEx.side_effect = (
            vim_RetrievePropertiesEx_side_effect)

        res = vim_util.get_object_properties(vim, moref, None)
        self.assertEqual(1, vim.RetrievePropertiesEx.call_count)
        self.assertTrue(res is retrieve_result.objects)
        cancel_retrieval.assert_called_once_with(vim, retrieve_result)
 def test_get_object_properties_with_empty_moref(self):
     vim = mock.Mock()
     ret = vim_util.get_object_properties(vim, None, None)
     self.assertIsNone(ret)
Exemple #5
0
 def test_get_object_properties_with_empty_moref(self):
     vim = mock.Mock()
     ret = vim_util.get_object_properties(vim, None, None)
     self.assertIsNone(ret)