Skip to content

Commit

Permalink
Creating OS clients from the environment variables
Browse files Browse the repository at this point in the history
Use `osclients.Clients.create_from_env' to create a Clients instance
from the environment variables. Useful for trying things in a Python
shell.

Change-Id: I807176183ff692f15b8700a8db1d0c231af3f5c6
  • Loading branch information
Pavel Boldin committed Feb 24, 2015
1 parent c339eb9 commit 4775640
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
14 changes: 14 additions & 0 deletions rally/osclients.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# License for the specific language governing permissions and limitations
# under the License.

import os

from ceilometerclient import client as ceilometer
from cinderclient import client as cinder
from designateclient import v1 as designate
Expand All @@ -33,6 +35,7 @@
from rally.common import log as logging
from rally import consts
from rally import exceptions
from rally import objects


CONF = cfg.CONF
Expand Down Expand Up @@ -85,6 +88,17 @@ def __init__(self, endpoint):
self.endpoint = endpoint
self.cache = {}

@classmethod
def create_from_env(cls):
return cls(
objects.Endpoint(
os.environ["OS_AUTH_URL"],
os.environ["OS_USERNAME"],
os.environ["OS_PASSWORD"],
os.environ.get("OS_TENANT_NAME"),
region_name=os.environ.get("OS_REGION_NAME")
))

def clear(self):
"""Remove all cached client handles."""
self.cache = {}
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/test_osclients.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ def setUp(self):
def tearDown(self):
super(OSClientsTestCase, self).tearDown()

def test_create_from_env(self):
with mock.patch.dict("os.environ",
{"OS_AUTH_URL": "foo_auth_url",
"OS_USERNAME": "foo_username",
"OS_PASSWORD": "foo_password",
"OS_TENANT_NAME": "foo_tenant_name",
"OS_REGION_NAME": "foo_region_name"}):
clients = osclients.Clients.create_from_env()

self.assertEqual("foo_auth_url", clients.endpoint.auth_url)
self.assertEqual("foo_username", clients.endpoint.username)
self.assertEqual("foo_password", clients.endpoint.password)
self.assertEqual("foo_tenant_name", clients.endpoint.tenant_name)
self.assertEqual("foo_region_name", clients.endpoint.region_name)

def test_keystone(self):
self.assertNotIn("keystone", self.clients.cache)
client = self.clients.keystone()
Expand Down

0 comments on commit 4775640

Please sign in to comment.