Merge "added mongodb auth"
This commit is contained in:
commit
fdcd37f6a3
@ -23,14 +23,16 @@ import pkg_resources
|
|||||||
from ceilometer.openstack.common import log
|
from ceilometer.openstack.common import log
|
||||||
from ceilometer.openstack.common import cfg
|
from ceilometer.openstack.common import cfg
|
||||||
|
|
||||||
|
from urlparse import urlparse
|
||||||
|
|
||||||
LOG = log.getLogger(__name__)
|
LOG = log.getLogger(__name__)
|
||||||
|
|
||||||
STORAGE_ENGINE_NAMESPACE = 'ceilometer.storage'
|
STORAGE_ENGINE_NAMESPACE = 'ceilometer.storage'
|
||||||
|
|
||||||
STORAGE_OPTS = [
|
STORAGE_OPTS = [
|
||||||
cfg.StrOpt('metering_storage_engine',
|
cfg.StrOpt('database_connection',
|
||||||
default='mongodb',
|
default='mongodb://localhost:27017/ceilometer',
|
||||||
help='The name of the storage engine to use',
|
help='Database connection string',
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -48,7 +50,7 @@ def register_opts(conf):
|
|||||||
def get_engine(conf):
|
def get_engine(conf):
|
||||||
"""Load the configured engine and return an instance.
|
"""Load the configured engine and return an instance.
|
||||||
"""
|
"""
|
||||||
engine_name = conf.metering_storage_engine
|
engine_name = urlparse(conf.database_connection).scheme
|
||||||
LOG.debug('looking for %r driver in %r',
|
LOG.debug('looking for %r driver in %r',
|
||||||
engine_name, STORAGE_ENGINE_NAMESPACE)
|
engine_name, STORAGE_ENGINE_NAMESPACE)
|
||||||
for ep in pkg_resources.iter_entry_points(STORAGE_ENGINE_NAMESPACE,
|
for ep in pkg_resources.iter_entry_points(STORAGE_ENGINE_NAMESPACE,
|
||||||
|
@ -27,6 +27,9 @@ from ceilometer.storage import base
|
|||||||
|
|
||||||
import bson.code
|
import bson.code
|
||||||
import pymongo
|
import pymongo
|
||||||
|
import re
|
||||||
|
|
||||||
|
from urlparse import urlparse
|
||||||
|
|
||||||
LOG = log.getLogger(__name__)
|
LOG = log.getLogger(__name__)
|
||||||
|
|
||||||
@ -57,20 +60,7 @@ class MongoDBStorage(base.StorageEngine):
|
|||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
OPTIONS = [
|
OPTIONS = []
|
||||||
cfg.StrOpt('mongodb_dbname',
|
|
||||||
default='ceilometer',
|
|
||||||
help='Database name',
|
|
||||||
),
|
|
||||||
cfg.StrOpt('mongodb_host',
|
|
||||||
default='localhost',
|
|
||||||
help='hostname or IP of server running MongoDB',
|
|
||||||
),
|
|
||||||
cfg.IntOpt('mongodb_port',
|
|
||||||
default=27017,
|
|
||||||
help='port number where MongoDB is running',
|
|
||||||
),
|
|
||||||
]
|
|
||||||
|
|
||||||
def register_opts(self, conf):
|
def register_opts(self, conf):
|
||||||
"""Register any configuration options used by this engine.
|
"""Register any configuration options used by this engine.
|
||||||
@ -169,10 +159,12 @@ class Connection(base.Connection):
|
|||||||
""")
|
""")
|
||||||
|
|
||||||
def __init__(self, conf):
|
def __init__(self, conf):
|
||||||
LOG.info('connecting to MongoDB on %s:%s',
|
opts = self._parse_connection_url(conf.database_connection)
|
||||||
conf.mongodb_host, conf.mongodb_port)
|
LOG.info('connecting to MongoDB on %s:%s', opts['host'], opts['port'])
|
||||||
self.conn = self._get_connection(conf)
|
self.conn = self._get_connection(opts)
|
||||||
self.db = getattr(self.conn, conf.mongodb_dbname)
|
self.db = getattr(self.conn, opts['dbname'])
|
||||||
|
if 'username' in opts:
|
||||||
|
self.db.authenticate(opts['username'], opts['password'])
|
||||||
|
|
||||||
# Establish indexes
|
# Establish indexes
|
||||||
#
|
#
|
||||||
@ -195,7 +187,7 @@ class Connection(base.Connection):
|
|||||||
])
|
])
|
||||||
return
|
return
|
||||||
|
|
||||||
def _get_connection(self, conf):
|
def _get_connection(self, opts):
|
||||||
"""Return a connection to the database.
|
"""Return a connection to the database.
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
@ -203,10 +195,25 @@ class Connection(base.Connection):
|
|||||||
The tests use a subclass to override this and return an
|
The tests use a subclass to override this and return an
|
||||||
in-memory connection.
|
in-memory connection.
|
||||||
"""
|
"""
|
||||||
return pymongo.Connection(conf.mongodb_host,
|
return pymongo.Connection(opts['host'], opts['port'], safe=True)
|
||||||
conf.mongodb_port,
|
|
||||||
safe=True,
|
def _parse_connection_url(self, url):
|
||||||
)
|
opts = {}
|
||||||
|
result = urlparse(url)
|
||||||
|
opts['dbtype'] = result.scheme
|
||||||
|
opts['dbname'] = result.path.replace('/', '')
|
||||||
|
netloc_match = re.match(r'(?:(\w+:\w+)@)?(.*)', result.netloc)
|
||||||
|
auth = netloc_match.group(1)
|
||||||
|
netloc = netloc_match.group(2)
|
||||||
|
if auth:
|
||||||
|
opts['username'], opts['password'] = auth.split(':')
|
||||||
|
if ':' in netloc:
|
||||||
|
opts['host'], port = netloc.split(':')
|
||||||
|
else:
|
||||||
|
opts['host'] = netloc
|
||||||
|
port = 27017
|
||||||
|
opts['port'] = port and int(port) or 27017
|
||||||
|
return opts
|
||||||
|
|
||||||
def record_metering_data(self, data):
|
def record_metering_data(self, data):
|
||||||
"""Write the data to the backend storage system.
|
"""Write the data to the backend storage system.
|
||||||
|
@ -61,9 +61,7 @@ class TestBase(unittest.TestCase):
|
|||||||
self.test_app = self.app.test_client()
|
self.test_app = self.app.test_client()
|
||||||
self.conf = mock.Mock()
|
self.conf = mock.Mock()
|
||||||
self.conf.metering_storage_engine = 'mongodb'
|
self.conf.metering_storage_engine = 'mongodb'
|
||||||
self.conf.mongodb_host = 'localhost'
|
self.conf.database_connection = 'mongodb://localhost/%s' % self.DBNAME
|
||||||
self.conf.mongodb_port = 27017
|
|
||||||
self.conf.mongodb_dbname = self.DBNAME
|
|
||||||
self.conn = Connection(self.conf)
|
self.conn = Connection(self.conf)
|
||||||
self.conn.conn.drop_database(self.DBNAME)
|
self.conn.conn.drop_database(self.DBNAME)
|
||||||
self.conn.conn[self.DBNAME]
|
self.conn.conn[self.DBNAME]
|
||||||
|
@ -26,14 +26,14 @@ from ceilometer.storage import impl_log
|
|||||||
|
|
||||||
def test_get_engine():
|
def test_get_engine():
|
||||||
conf = mox.Mox().CreateMockAnything()
|
conf = mox.Mox().CreateMockAnything()
|
||||||
conf.metering_storage_engine = 'log'
|
conf.database_connection = 'log://localhost'
|
||||||
engine = storage.get_engine(conf)
|
engine = storage.get_engine(conf)
|
||||||
assert isinstance(engine, impl_log.LogStorage)
|
assert isinstance(engine, impl_log.LogStorage)
|
||||||
|
|
||||||
|
|
||||||
def test_get_engine_no_such_engine():
|
def test_get_engine_no_such_engine():
|
||||||
conf = mox.Mox().CreateMockAnything()
|
conf = mox.Mox().CreateMockAnything()
|
||||||
conf.metering_storage_engine = 'no-such-engine'
|
conf.database_connection = 'no-such-engine://localhost'
|
||||||
try:
|
try:
|
||||||
storage.get_engine(conf)
|
storage.get_engine(conf)
|
||||||
except RuntimeError as err:
|
except RuntimeError as err:
|
||||||
|
@ -89,13 +89,10 @@ class MongoDBEngineTestBase(unittest.TestCase):
|
|||||||
super(MongoDBEngineTestBase, self).setUp()
|
super(MongoDBEngineTestBase, self).setUp()
|
||||||
|
|
||||||
self.conf = mox.Mox().CreateMockAnything()
|
self.conf = mox.Mox().CreateMockAnything()
|
||||||
self.conf.metering_storage_engine = 'mongodb'
|
self.conf.database_connection = 'mongodb://localhost/testdb'
|
||||||
self.conf.mongodb_host = 'localhost'
|
|
||||||
self.conf.mongodb_port = 27017
|
|
||||||
self.conf.mongodb_dbname = 'testdb'
|
|
||||||
self.conn = Connection(self.conf)
|
self.conn = Connection(self.conf)
|
||||||
self.conn.conn.drop_database(self.conf.mongodb_dbname)
|
self.conn.conn.drop_database('testdb')
|
||||||
self.db = self.conn.conn[self.conf.mongodb_dbname]
|
self.db = self.conn.conn['testdb']
|
||||||
self.conn.db = self.db
|
self.conn.db = self.db
|
||||||
|
|
||||||
self.counter = counter.Counter(
|
self.counter = counter.Counter(
|
||||||
|
Loading…
Reference in New Issue
Block a user