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
This commit is contained in:
Igor Degtiarov 2015-06-23 13:38:49 +03:00
parent 1ffc1c4664
commit f0598b213d
5 changed files with 20 additions and 22 deletions

View File

@ -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,

View File

@ -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,

View File

@ -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.

View File

@ -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: '

View File

@ -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'})