From f0598b213dd45b8d5dcd1a773b2022104bf8dad3 Mon Sep 17 00:00:00 2001 From: Igor Degtiarov Date: Tue, 23 Jun 2015 13:38:49 +0300 Subject: [PATCH] Replaces methods deprecated in pymongo3.0 Methods insert, find_and_modify, update and MongoReplicaSetClient are deprecated in pymongo3.0. They were replaced with appropriate insert_one, find_one_and _update, upte_one, MongoClient consistently. Change-Id: I1fdc15ed5d21227d2070052a771ce4f3aec94c47 Closes-Bug: #1467856 --- ceilometer/event/storage/pymongo_base.py | 2 +- ceilometer/storage/impl_db2.py | 14 +++++++------- ceilometer/storage/impl_mongodb.py | 10 +++++----- ceilometer/storage/mongo/utils.py | 12 +++++------- ceilometer/tests/storage/test_impl_db2.py | 4 ++-- 5 files changed, 20 insertions(+), 22 deletions(-) diff --git a/ceilometer/event/storage/pymongo_base.py b/ceilometer/event/storage/pymongo_base.py index e4c620e2..6338231c 100644 --- a/ceilometer/event/storage/pymongo_base.py +++ b/ceilometer/event/storage/pymongo_base.py @@ -58,7 +58,7 @@ class Connection(base.Connection): 'trait_type': trait.dtype, 'trait_value': trait.value}) try: - self.db.event.insert( + self.db.event.insert_one( {'_id': event_model.message_id, 'event_type': event_model.event_type, 'timestamp': event_model.generated, diff --git a/ceilometer/storage/impl_db2.py b/ceilometer/storage/impl_db2.py index 99d7aab6..69613062 100644 --- a/ceilometer/storage/impl_db2.py +++ b/ceilometer/storage/impl_db2.py @@ -179,13 +179,13 @@ class Connection(pymongo_base.Connection): # for their ENV. resource_id = self._generate_random_str( cfg.CONF.database.db2nosql_resource_id_maxlen) - self.db.resource.insert({'_id': resource_id, - 'no_key': resource_id}) + self.db.resource.insert_one({'_id': resource_id, + 'no_key': resource_id}) meter_id = str(bson.objectid.ObjectId()) timestamp = timeutils.utcnow() - self.db.meter.insert({'_id': meter_id, - 'no_key': meter_id, - 'timestamp': timestamp}) + self.db.meter.insert_one({'_id': meter_id, + 'no_key': meter_id, + 'timestamp': timestamp}) self.db.resource.create_index([ ('user_id', pymongo.ASCENDING), @@ -232,7 +232,7 @@ class Connection(pymongo_base.Connection): data = copy.deepcopy(data) data['resource_metadata'] = pymongo_utils.improve_keys( data.pop('resource_metadata')) - self.db.resource.update( + self.db.resource.update_one( {'_id': data['resource_id']}, {'$set': {'project_id': data['project_id'], 'user_id': data['user_id'] or 'null', @@ -257,7 +257,7 @@ class Connection(pymongo_base.Connection): # automatically. if record.get('_id') is None: record['_id'] = str(bson.objectid.ObjectId()) - self.db.meter.insert(record) + self.db.meter.insert_one(record) def get_resources(self, user=None, project=None, source=None, start_timestamp=None, start_timestamp_op=None, diff --git a/ceilometer/storage/impl_mongodb.py b/ceilometer/storage/impl_mongodb.py index 7f8fb8fc..830c65e3 100644 --- a/ceilometer/storage/impl_mongodb.py +++ b/ceilometer/storage/impl_mongodb.py @@ -501,7 +501,7 @@ class Connection(pymongo_base.Connection): data = copy.deepcopy(data) data['resource_metadata'] = pymongo_utils.improve_keys( data.pop('resource_metadata')) - resource = self.db.resource.find_and_modify( + resource = self.db.resource.find_one_and_update( {'_id': data['resource_id']}, {'$set': {'project_id': data['project_id'], 'user_id': data['user_id'], @@ -518,7 +518,7 @@ class Connection(pymongo_base.Connection): }, }, upsert=True, - new=True, + return_document=pymongo.ReturnDocument.AFTER, ) # only update last sample timestamp if actually later (the usual @@ -526,7 +526,7 @@ class Connection(pymongo_base.Connection): last_sample_timestamp = resource.get('last_sample_timestamp') if (last_sample_timestamp is None or last_sample_timestamp <= data['timestamp']): - self.db.resource.update( + self.db.resource.update_one( {'_id': data['resource_id']}, {'$set': {'metadata': data['resource_metadata'], 'last_sample_timestamp': data['timestamp']}} @@ -540,7 +540,7 @@ class Connection(pymongo_base.Connection): first_sample_timestamp = resource.get('first_sample_timestamp') if (first_sample_timestamp is not None and first_sample_timestamp > data['timestamp']): - self.db.resource.update( + self.db.resource.update_one( {'_id': data['resource_id']}, {'$set': {'first_sample_timestamp': data['timestamp']}} ) @@ -550,7 +550,7 @@ class Connection(pymongo_base.Connection): # a new key '_id'). record = copy.copy(data) record['recorded_at'] = timeutils.utcnow() - self.db.meter.insert(record) + self.db.meter.insert_one(record) def clear_expired_metering_data(self, ttl): """Clear expired data from the backend storage system. diff --git a/ceilometer/storage/mongo/utils.py b/ceilometer/storage/mongo/utils.py index 1c2da395..2df78341 100644 --- a/ceilometer/storage/mongo/utils.py +++ b/ceilometer/storage/mongo/utils.py @@ -253,13 +253,11 @@ class ConnectionPool(object): @staticmethod def _mongo_connect(url): try: - if cfg.CONF.database.mongodb_replica_set: - client = MongoProxy( - pymongo.MongoReplicaSetClient( - url, - replicaSet=cfg.CONF.database.mongodb_replica_set)) - else: - client = MongoProxy(pymongo.MongoClient(url)) + client = MongoProxy( + pymongo.MongoClient( + url, replicaSet=cfg.CONF.database.mongodb_replica_set + ) + ) return client except pymongo.errors.ConnectionFailure as e: LOG.warn(_('Unable to connect to the database server: ' diff --git a/ceilometer/tests/storage/test_impl_db2.py b/ceilometer/tests/storage/test_impl_db2.py index 77ad1fbc..2cc86faf 100644 --- a/ceilometer/tests/storage/test_impl_db2.py +++ b/ceilometer/tests/storage/test_impl_db2.py @@ -114,10 +114,10 @@ class ConnectionTest(test_base.BaseTestCase): group='database') impl_db2.Connection('db2://user:pwd@localhost:27017/ceilodb2') resource_id = 'wew' * 247 + 'x' * 3 - conn_mock.ceilodb2.resource.insert.assert_called_with( + conn_mock.ceilodb2.resource.insert_one.assert_called_with( {'_id': resource_id, 'no_key': resource_id}) - conn_mock.ceilodb2.meter.insert.assert_called_with( + conn_mock.ceilodb2.meter.insert_one.assert_called_with( {'_id': '54b8860d75bfe43b54e84ce7', 'no_key': '54b8860d75bfe43b54e84ce7', 'timestamp': 'timestamp'})