Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Commit

Permalink
#4: Gather CharmStore endpoint and timeout from environment
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Liam Young authored and marcoceppi committed Nov 30, 2016
1 parent 44143bc commit a3d71ca
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
```
17 changes: 11 additions & 6 deletions charmstore/lib.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import re
import json

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setup(
name='libcharmstore',
version='0.0.5',
version='0.0.6',
packages=['charmstore'],
maintainer='Marco Ceppi',
install_requires=[
Expand Down
21 changes: 19 additions & 2 deletions tests/test_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from mock import patch

from charmstore.lib import Entity
import charmstore.lib
from util import CHARM, BUNDLE


Expand All @@ -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')
Expand All @@ -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')

0 comments on commit a3d71ca

Please sign in to comment.