def _aggregate_update_to_db(context, aggregate_id, values): aggregate = _aggregate_get_from_db(context, aggregate_id) set_delete = True if "availability_zone" in values: az = values.pop('availability_zone') if 'metadata' not in values: values['metadata'] = {'availability_zone': az} set_delete = False else: values['metadata']['availability_zone'] = az metadata = values.get('metadata') if metadata is not None: _metadata_add_to_db(context, aggregate_id, values.pop('metadata'), set_delete=set_delete) aggregate.update(values) try: aggregate.save(context.session) except db_exc.DBDuplicateEntry: if 'name' in values: raise exception.AggregateNameExists(aggregate_name=values['name']) else: raise return _aggregate_get_from_db(context, aggregate_id)
def _aggregate_create_in_db(context, values, metadata=None): query = context.session.query(api_models.Aggregate) query = query.filter(api_models.Aggregate.name == values['name']) aggregate = query.first() if not aggregate: aggregate = api_models.Aggregate() # These created_at and updated_at was added because the original values # contained time zones and tried to push does values into the database. # it might be that the output to the database will be incorrect after this. created_at = values['created_at'].replace(tzinfo=None) updated_at = values['updated_at'].replace(tzinfo=None) values['created_at'] = created_at values['updated_at'] = updated_at aggregate.update(values) aggregate.save(context.session) # We don't want these to be lazy loaded later. We know there is # nothing here since we just created this aggregate. aggregate._hosts = [] aggregate._metadata = [] else: raise exception.AggregateNameExists(aggregate_name=values['name']) if metadata: _metadata_add_to_db(context, aggregate.id, metadata) context.session.expire(aggregate, ['_metadata']) aggregate._metadata return aggregate
def test_create_with_duplicate_aggregate_name(self): side_effect = exception.AggregateNameExists(aggregate_name="test") with mock.patch.object(self.controller.api, 'create_aggregate', side_effect=side_effect) as mock_create: self.assertRaises(exc.HTTPConflict, self.controller.create, self.req, body={"aggregate": {"name": "test", "availability_zone": "nova1"}}) mock_create.assert_called_once_with(self.context, 'test', 'nova1')
def test_update_with_duplicated_name(self): body = {"aggregate": {"name": "test_name"}} side_effect = exception.AggregateNameExists(aggregate_name="test_name") with mock.patch.object(self.controller.api, 'update_aggregate', side_effect=side_effect) as mock_update: self.assertRaises(exc.HTTPConflict, self.controller.update, self.req, "2", body=body) mock_update.assert_called_once_with(self.context, '2', body["aggregate"])
def _aggregate_create_in_db(context, values, metadata=None): query = context.session.query(api_models.Aggregate) query = query.filter(api_models.Aggregate.name == values['name']) aggregate = query.first() if not aggregate: aggregate = api_models.Aggregate() aggregate.update(values) aggregate.save(context.session) # We don't want these to be lazy loaded later. We know there is # nothing here since we just created this aggregate. aggregate._hosts = [] aggregate._metadata = [] else: raise exception.AggregateNameExists(aggregate_name=values['name']) if metadata: _metadata_add_to_db(context, aggregate.id, metadata) context.session.expire(aggregate, ['_metadata']) aggregate._metadata return aggregate
def stub_update_aggregate(context, aggregate, metadata): raise exception.AggregateNameExists(aggregate_name="test_name")
def stub_create_aggregate(context, name, availability_zone): raise exception.AggregateNameExists(aggregate_name=name)