def test_get_object_properties_dict_missing(self, mock_obj_prop): mock_obj_content = mock.Mock() missing_prop = mock.Mock() missing_prop.path = "name" missing_prop.fault = mock.Mock() mock_obj_content.missingSet = [missing_prop] del mock_obj_content.propSet mock_obj_prop.return_value = [mock_obj_content] vim = mock.Mock() moref = mock.Mock() res = vim_util.get_object_properties_dict(vim, moref, None) self.assertEqual({}, res)
def test_get_object_properties_dict(self, mock_obj_prop): expected_prop_dict = {'name': 'vm01'} mock_obj_content = mock.Mock() prop = mock.Mock() prop.name = "name" prop.val = "vm01" mock_obj_content.propSet = [prop] del mock_obj_content.missingSet mock_obj_prop.return_value = [mock_obj_content] vim = mock.Mock() moref = mock.Mock() res = vim_util.get_object_properties_dict(vim, moref, None) self.assertEqual(expected_prop_dict, res)
def test_get_object_properties_dict_empty(self, mock_obj_prop): mock_obj_prop.return_value = None vim = mock.Mock() moref = mock.Mock() res = vim_util.get_object_properties_dict(vim, moref, None) self.assertEqual({}, res)
def create_dvportgroup(self, sg_attr_key, sg_set, port_config): """ Creates an automatically-named dvportgroup on the dvswitch with the specified sg rules and marks it as such through a custom attribute Returns a dictionary with "key" and "ref" keys. Note, that while a portgroup's key and managed object id have the same string format and appear identical under normal use it is possible to have them diverge by the use of the backup and restore feature of the dvs for example. As such, one should not rely on any equivalence between them. """ # There is a create_network method a few lines above # which seems to be part of a non-used call path # starting from the dvs_agent_rpc_api. TODO - remove it dvpg_name = self.dvportgroup_name(sg_set) try: pg_spec = self.builder.pg_config(port_config) pg_spec.name = dvpg_name pg_spec.numPorts = 0 pg_spec.type = 'earlyBinding' pg_spec.description = sg_set now = timeutils.utcnow() pg_create_task = self.connection.invoke_api( self.connection.vim, 'CreateDVPortgroup_Task', self._dvs, spec=pg_spec) result = self.connection.wait_for_task(pg_create_task) pg_ref = result.result # Tag the portgroup for the specific security group set self.connection.invoke_api( self.connection.vim, "SetField", self._service_content.customFieldsManager, entity=pg_ref, key=sg_attr_key, value=sg_set) props = vim_util.get_object_properties_dict(self.connection.vim, pg_ref, ["key"]) delta = timeutils.utcnow() - now stats.timing('networking_dvs.dvportgroup.created', delta) LOG.debug("Creating portgroup {} took {} seconds.".format(pg_ref, delta.seconds)) return {"key": props["key"], "ref": pg_ref} except vmware_exceptions.DuplicateName as dn: LOG.info("Untagged portgroup with matching name {} found, will update and use.".format(dvpg_name)) portgroups = self._get_portgroups() for existing in portgroups: if existing['name'] != dvpg_name: continue break # found it else: LOG.error("Portgroup with matching name {} not found while expected.".format(dvpg_name)) return self.update_dvportgroup(existing["ref"], existing["config.configVersion"], port_config) # Tag the portgroup for the specific security group set self.connection.invoke_api( self.connection.vim, "SetField", self._service_content.customFieldsManager, entity=existing["ref"], key=sg_attr_key, value=sg_set) return existing except vmware_exceptions.VimException as e: raise exceptions.wrap_wmvare_vim_exception(e)