From a3d71cae86cea2d44b53a208318cf9f296d9e0df Mon Sep 17 00:00:00 2001 From: Liam Young Date: Wed, 30 Nov 2016 20:26:08 +0000 Subject: [PATCH] #4: Gather CharmStore endpoint and timeout from environment This change allows a user to set the environment variables CS_API_URL and CS_API_TIMEOUT to repectively set the Charm Store endpoint URL and how long to wait before a Charm Store query times out. This could also be a step towards removing the DEFAULT_CS_API_URL from libcharmstore. * Increment version --- README.md | 9 +++++++++ charmstore/lib.py | 17 +++++++++++------ setup.py | 2 +- tests/test_entity.py | 21 +++++++++++++++++++-- 4 files changed, 40 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index cb3dd9b..0f9afc8 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,14 @@ # CharmStore Python Library +The environment variables CS\_API\_URL and CS\_API\_TIMEOUT can be set to +repectively set the Charm Store endpoint URL and how long to wait before a +Charm Store query times out. + +```bash +export CS_API_URL=https://api.jujucharms.com/v4 +export CS_API_TIMEOUT=200 +``` + ```python from charmstore import CharmStore, Charm, Bundle ``` diff --git a/charmstore/lib.py b/charmstore/lib.py index 7f53ce4..d71cc9b 100644 --- a/charmstore/lib.py +++ b/charmstore/lib.py @@ -1,3 +1,4 @@ +import os import re import json @@ -30,8 +31,10 @@ class CharmStore(object): - def __init__(self, api=DEFAULT_CS_API_URL): + def __init__(self, api=None): super(CharmStore, self).__init__() + if not api: + api = os.environ.get('CS_API_URL', DEFAULT_CS_API_URL) self.theblues = charmstore.CharmStore(url=api) def requires(self, interfaces=[], limit=None): @@ -82,8 +85,11 @@ def from_data(cls, data): return e - def __init__(self, id=None, api=DEFAULT_CS_API_URL, - timeout=DEFAULT_TIMEOUT): + def __init__(self, id=None, api=None, timeout=None): + if not api: + api = os.environ.get('CS_API_URL', DEFAULT_CS_API_URL) + if not timeout: + timeout = float(os.environ.get('CS_API_TIMEOUT', DEFAULT_TIMEOUT)) self.id = None self.name = None self.owner = None @@ -141,8 +147,7 @@ def load(self, data): class Charm(Entity): - def __init__(self, id=None, api=DEFAULT_CS_API_URL, - timeout=DEFAULT_TIMEOUT): + def __init__(self, id=None, api=None, timeout=None): self.summary = None self.description = None @@ -210,7 +215,7 @@ def __repr__(self): class Bundle(Entity): - def __init__(self, id=None, timeout=DEFAULT_TIMEOUT): + def __init__(self, id=None, timeout=None): self.count = {'machines': 0, 'units': 0} self.relations = [] self.services = None diff --git a/setup.py b/setup.py index 4f8d2d1..5f92213 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ setup( name='libcharmstore', - version='0.0.5', + version='0.0.6', packages=['charmstore'], maintainer='Marco Ceppi', install_requires=[ diff --git a/tests/test_entity.py b/tests/test_entity.py index 7e7b448..8a90842 100644 --- a/tests/test_entity.py +++ b/tests/test_entity.py @@ -4,7 +4,7 @@ from mock import patch -from charmstore.lib import Entity +import charmstore.lib from util import CHARM, BUNDLE @@ -14,7 +14,7 @@ def setUpClass(cls): pass def test_entity_load(self): - c = Entity.from_data(CHARM.get('Meta')) + c = charmstore.lib.Entity.from_data(CHARM.get('Meta')) self.assertEqual(c.id, 'trusty/benchmark-gui-0') self.assertEqual(c.url, 'cs:trusty/benchmark-gui-0') self.assertEqual(c.series, 'trusty') @@ -31,3 +31,20 @@ def test_entity_load(self): self.assertEqual(c.stats, {}) self.assertEqual(c.raw, CHARM.get('Meta')) + + @patch.object(charmstore.lib, 'charmstore') + def test_entity_default_cs_params(self, _charmstore): + charmstore.lib.Entity.from_data(CHARM.get('Meta')) + _charmstore.CharmStore.assert_called_once_with( + timeout=10.0, + url='https://api.jujucharms.com/v4') + + @patch.dict(charmstore.lib.os.environ, { + 'CS_API_URL': 'alturl', + 'CS_API_TIMEOUT': '200'}) + @patch.object(charmstore.lib, 'charmstore') + def test_entity_env(self, _charmstore): + charmstore.lib.Entity.from_data(CHARM.get('Meta')) + _charmstore.CharmStore.assert_called_once_with( + timeout=200.0, + url='alturl')