def _create(self, skipcheck=False): # NOTE(danms): This is just for the migration routine, and # can be removed once we're no longer supporting the migration # of instance groups from the main to api database. if self.obj_attr_is_set('id'): raise exception.ObjectActionError(action='create', reason='already created') updates = self.obj_get_changes() payload = dict(updates) updates.pop('id', None) policies = updates.pop('policies', None) members = updates.pop('members', None) if 'uuid' not in updates: self.uuid = uuidutils.generate_uuid() updates['uuid'] = self.uuid if not skipcheck: try: db.instance_group_get(self._context, self.uuid) raise exception.ObjectActionError( action='create', reason='already created in main') except exception.InstanceGroupNotFound: pass db_group = self._create_in_db(self._context, updates, policies=policies, members=members) self._from_db_object(self._context, self, db_group) payload['server_group_id'] = self.uuid compute_utils.notify_about_server_group_update(self._context, "create", payload)
def save(self): """Save updates to this instance group.""" updates = self.obj_get_changes() # NOTE(sbauza): We do NOT save the set of compute nodes that an # instance group is connected to in this method. Instance groups are # implicitly connected to compute nodes when the # InstanceGroup.add_members() method is called, which adds the mapping # table entries. # So, since the only way to have hosts in the updates is to set that # field explicitly, we prefer to raise an Exception so the developer # knows he has to call obj_reset_changes(['hosts']) right after setting # the field. if 'hosts' in updates: raise exception.InstanceGroupSaveException(field='hosts') if not updates: return payload = dict(updates) payload['server_group_id'] = self.uuid try: db_group = self._save_in_db(self._context, self.uuid, updates) except exception.InstanceGroupNotFound: db.instance_group_update(self._context, self.uuid, updates) db_group = db.instance_group_get(self._context, self.uuid) self._from_db_object(self._context, self, db_group) compute_utils.notify_about_server_group_update(self._context, "update", payload)
def save(self): """Save updates to this instance group.""" updates = self.obj_get_changes() # NOTE(sbauza): We do NOT save the set of compute nodes that an # instance group is connected to in this method. Instance groups are # implicitly connected to compute nodes when the # InstanceGroup.add_members() method is called, which adds the mapping # table entries. # So, since the only way to have hosts in the updates is to set that # field explicitely, we prefer to raise an Exception so the developer # knows he has to call obj_reset_changes(['hosts']) right after setting # the field. if 'hosts' in updates: raise exception.InstanceGroupSaveException(field='hosts') if not updates: return payload = dict(updates) payload['server_group_id'] = self.uuid db.instance_group_update(self._context, self.uuid, updates) db_inst = db.instance_group_get(self._context, self.uuid) self._from_db_object(self._context, self, db_inst) compute_utils.notify_about_server_group_update(self._context, "update", payload)
def test_save_members(self): values = self._get_default_values() db_result = self._create_instance_group(self.context, values) obj_result = instance_group.InstanceGroup.get_by_uuid(self.context, db_result.uuid) members = ["instance1", "instance2"] obj_result.members = members obj_result.save() result = db.instance_group_get(self.context, db_result["uuid"]) self.assertEqual(result["members"], members)
def test_save_policies(self): values = self._get_default_values() db_result = self._create_instance_group(self.context, values) obj_result = instance_group.InstanceGroup.get_by_uuid(self.context, db_result.uuid) policies = ["policy1", "policy2"] obj_result.policies = policies obj_result.save() result = db.instance_group_get(self.context, db_result["uuid"]) self.assertEqual(result["policies"], policies)
def test_save_simple(self): values = self._get_default_values() db_result = self._create_instance_group(self.context, values) obj_result = instance_group.InstanceGroup.get_by_uuid(self.context, db_result.uuid) self.assertEqual(obj_result.name, "fake_name") obj_result.name = "new_name" obj_result.save() result = db.instance_group_get(self.context, db_result["uuid"]) self.assertEqual(result["name"], "new_name")
def get_by_uuid(cls, context, uuid): db_group = None try: db_group = cls._get_from_db_by_uuid(context, uuid) except exception.InstanceGroupNotFound: pass if db_group is None: db_group = db.instance_group_get(context, uuid) return cls._from_db_object(context, cls(), db_group)
def test_save_policies(self): values = self._get_default_values() db_result = self._create_instance_group(self.context, values) obj_result = instance_group.InstanceGroup.get_by_uuid(self.context, db_result.uuid) policies = ['policy1', 'policy2'] obj_result.policies = policies obj_result.save() result = db.instance_group_get(self.context, db_result['uuid']) self.assertEqual(result['policies'], policies)
def save(self, context): """Save updates to this instance group.""" updates = self.obj_get_changes() if not updates: return db.instance_group_update(context, self.uuid, updates) db_inst = db.instance_group_get(context, self.uuid) self._from_db_object(context, self, db_inst)
def test_save_simple(self): values = self._get_default_values() db_result = self._create_instance_group(self.context, values) obj_result = instance_group.InstanceGroup.get_by_uuid(self.context, db_result.uuid) self.assertEqual(obj_result.name, 'fake_name') obj_result.name = 'new_name' obj_result.save() result = db.instance_group_get(self.context, db_result['uuid']) self.assertEqual(result['name'], 'new_name')
def test_save_members(self): values = self._get_default_values() db_result = self._create_instance_group(self.context, values) obj_result = instance_group.InstanceGroup.get_by_uuid( self.context, db_result.uuid) members = ['instance1', 'instance2'] obj_result.members = members obj_result.save() result = db.instance_group_get(self.context, db_result['uuid']) self.assertEqual(result['members'], members)
def test_create(self): group1 = instance_group.InstanceGroup() group1.uuid = "fake-uuid" group1.name = "fake-name" group1.create(self.context) group2 = instance_group.InstanceGroup.get_by_uuid(self.context, group1.uuid) self.assertEqual(group1.id, group2.id) self.assertEqual(group1.uuid, group2.uuid) self.assertEqual(group1.name, group2.name) result = db.instance_group_get(self.context, group1.uuid) self.assertEqual(group1.id, result.id) self.assertEqual(group1.uuid, result.uuid) self.assertEqual(group1.name, result.name)
def test_create(self): group1 = instance_group.InstanceGroup() group1.uuid = 'fake-uuid' group1.name = 'fake-name' group1.create(self.context) group2 = instance_group.InstanceGroup.get_by_uuid( self.context, group1.uuid) self.assertEqual(group1.id, group2.id) self.assertEqual(group1.uuid, group2.uuid) self.assertEqual(group1.name, group2.name) result = db.instance_group_get(self.context, group1.uuid) self.assertEqual(group1.id, result.id) self.assertEqual(group1.uuid, result.uuid) self.assertEqual(group1.name, result.name)
def save(self, context): """Save updates to this instance group.""" updates = self.obj_get_changes() if not updates: return metadata = None if 'metadetails' in updates: metadata = updates.pop('metadetails') updates.update({'metadata': metadata}) db.instance_group_update(context, self.uuid, updates) db_inst = db.instance_group_get(context, self.uuid) self._from_db_object(context, self, db_inst)
def test_save_members(self): values = self._get_default_values() db_result = self._create_instance_group(self.context, values) obj_result = instance_group.InstanceGroup.get_by_uuid(self.context, db_result.uuid) members = ['instance1', 'instance2'] obj_result.members = members fake_notifier.NOTIFICATIONS = [] obj_result.save() self.assertEqual(1, len(fake_notifier.NOTIFICATIONS)) msg = fake_notifier.NOTIFICATIONS[0] self.assertEqual('servergroup.update', msg.event_type) self.assertEqual(members, msg.payload['members']) result = db.instance_group_get(self.context, db_result['uuid']) self.assertEqual(result['members'], members)
def save(self): """Save updates to this instance group.""" updates = self.obj_get_changes() if not updates: return payload = dict(updates) payload['server_group_id'] = self.uuid db.instance_group_update(self._context, self.uuid, updates) db_inst = db.instance_group_get(self._context, self.uuid) self._from_db_object(self._context, self, db_inst) compute_utils.notify_about_server_group_update(self._context, "update", payload)
def test_create(self): group1 = instance_group.InstanceGroup(context=self.context) group1.uuid = 'fake-uuid' group1.name = 'fake-name' fake_notifier.NOTIFICATIONS = [] group1.create() self.assertEqual(1, len(fake_notifier.NOTIFICATIONS)) msg = fake_notifier.NOTIFICATIONS[0] self.assertEqual(group1.name, msg.payload['name']) self.assertEqual(group1.uuid, msg.payload['server_group_id']) self.assertEqual('servergroup.create', msg.event_type) group2 = instance_group.InstanceGroup.get_by_uuid(self.context, group1.uuid) self.assertEqual(group1.id, group2.id) self.assertEqual(group1.uuid, group2.uuid) self.assertEqual(group1.name, group2.name) result = db.instance_group_get(self.context, group1.uuid) self.assertEqual(group1.id, result.id) self.assertEqual(group1.uuid, result.uuid) self.assertEqual(group1.name, result.name)
def get_by_uuid(cls, context, uuid): db_inst = db.instance_group_get(context, uuid) return cls._from_db_object(context, cls(), db_inst)