Skip to content

Commit

Permalink
Merge b8a82a1 into c12de99
Browse files Browse the repository at this point in the history
  • Loading branch information
russell committed Jun 20, 2014
2 parents c12de99 + b8a82a1 commit 429dbbd
Show file tree
Hide file tree
Showing 9 changed files with 310 additions and 43 deletions.
6 changes: 5 additions & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ def api3():
return pypuppetdb.connect(api_version=3)


class TestAPI(pypuppetdb.api.BaseAPI):
api_version = 3


@pytest.fixture
def baseapi():
return pypuppetdb.api.BaseAPI(3)
return TestAPI()


@pytest.fixture
Expand Down
5 changes: 5 additions & 0 deletions pypuppetdb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@

from pypuppetdb.api import v2
from pypuppetdb.api import v3
from pypuppetdb.api import v4
from pypuppetdb.errors import UnsupportedVersionError

try: # Python 2.7+
Expand Down Expand Up @@ -102,6 +103,10 @@ def connect(api_version=3, host='localhost', port=8080, ssl_verify=False,
:raises: :class:`~pypuppetdb.errors.UnsupportedVersionError`
"""
if api_version == 4:
return v4.API(host=host, port=port,
timeout=timeout, ssl_verify=ssl_verify, ssl_key=ssl_key,
ssl_cert=ssl_cert)
if api_version == 3:
return v3.API(host=host, port=port,
timeout=timeout, ssl_verify=ssl_verify, ssl_key=ssl_key,
Expand Down
39 changes: 30 additions & 9 deletions pypuppetdb/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
API_VERSIONS = {
2: 'v2',
3: 'v3',
4: 'v4',
}

ENDPOINTS = {
Expand All @@ -44,6 +45,22 @@
'server-time': 'server-time',
'version': 'version',
},
4: {
'facts': 'facts',
'fact-names': 'fact-names',
'nodes': 'nodes',
'resources': 'resources',
'catalogs': 'catalogs',
'metrics': 'metrics',
'mbean': 'metrics/mbean',
'reports': 'reports',
'events': 'events',
'event-counts': 'event-counts',
'aggregate-event-counts': 'aggregate-event-counts',
'server-time': 'server-time',
'version': 'version',
'environments': 'environments',
},
}

ERROR_STRINGS = {
Expand Down Expand Up @@ -88,24 +105,27 @@ class BaseAPI(object):
:raises: :class:`~pypuppetdb.errors.ImproperlyConfiguredError`
:raises: :class:`~pypuppetdb.errors.UnsupportedVersionError`
"""
def __init__(self, api_version, host='localhost', port=8080,
api_version = None

def __init__(self, host='localhost', port=8080,
ssl_verify=True, ssl_key=None, ssl_cert=None, timeout=10):
"""Initialises our BaseAPI object passing the parameters needed in
order to be able to create the connection strings, set up SSL and
timeouts and so forth."""

if api_version in API_VERSIONS:
self.api_version = API_VERSIONS[api_version]
if self.api_version in API_VERSIONS:
self.api_prefix = API_VERSIONS[self.api_version]
else:
raise UnsupportedVersionError
log.debug('API initialised with {0}'.format(self.api_version))

self.host = host
self.port = port
self.ssl_verify = ssl_verify
self.ssl_key = ssl_key
self.ssl_cert = ssl_cert
self.timeout = timeout
self.endpoints = ENDPOINTS[api_version]
self.endpoints = ENDPOINTS[self.api_version]

if self.ssl_key is not None and self.ssl_cert is not None:
self.protocol = 'https'
Expand All @@ -118,7 +138,7 @@ def version(self):
:returns: Current API version.
:rtype: :obj:`string`"""
return self.api_version
return self.api_prefix

@property
def base_url(self):
Expand Down Expand Up @@ -155,7 +175,7 @@ def _normalize_resource_type(self, type_):
"""
return '::'.join([s.capitalize() for s in type_.split('::')])

def _url(self, endpoint, path=None):
def _url(self, endpoint, path=None, environment=None):
"""The complete URL we will end up querying. Depending on the
endpoint we pass in this will result in different URL's with
different prefixes.
Expand All @@ -176,7 +196,7 @@ def _url(self, endpoint, path=None):
endpoint, path))

if endpoint in self.endpoints:
api_prefix = self.api_version
api_prefix = self.api_prefix
endpoint = self.endpoints[endpoint]
else:
# If we reach this we're trying to query an endpoint that doesn't
Expand All @@ -196,7 +216,8 @@ def _url(self, endpoint, path=None):

def _query(self, endpoint, path=None, query=None,
order_by=None, limit=None, offset=None, include_total=False,
summarize_by=None, count_by=None, count_filter=None):
summarize_by=None, count_by=None, count_filter=None,
environment=None):
"""This method actually querries PuppetDB. Provided an endpoint and an
optional path and/or query it will fire a request at PuppetDB. If
PuppetDB can be reached and answers within the timeout we'll decode
Expand Down Expand Up @@ -241,7 +262,7 @@ def _query(self, endpoint, path=None, query=None,
offset, summarize_by, count_by,
count_filter))

url = self._url(endpoint, path=path)
url = self._url(endpoint, path=path, environment=environment)
headers = {
'content-type': 'application/json',
'accept': 'application/json',
Expand Down
6 changes: 1 addition & 5 deletions pypuppetdb/api/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ class API(BaseAPI):
:param \*\*kwargs: Rest of the keywoard arguments passed on to our parent\
:class:`~pypuppetdb.api.BaseAPI`.
"""

def __init__(self, *args, **kwargs):
"""Initialise the API object."""
super(API, self).__init__(api_version=2, **kwargs)
log.debug('API initialised with {0}'.format(kwargs))
api_version = 2

def node(self, name):
"""Gets a single node from PuppetDB."""
Expand Down
8 changes: 2 additions & 6 deletions pypuppetdb/api/v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ class API(BaseAPI):
:param \*\*kwargs: Rest of the keywoard arguments passed on to our parent\
:class:`~pypuppetdb.api.BaseAPI`.
"""

def __init__(self, *args, **kwargs):
"""Initialise the API object."""
super(API, self).__init__(api_version=3, **kwargs)
log.debug('API initialised with {0}.'.format(kwargs))
api_version = 3

def node(self, name):
"""Gets a single node from PuppetDB."""
Expand Down Expand Up @@ -156,7 +152,7 @@ def resources(self, type_=None, title=None, query=None):

if type_ is not None:
type_ = self._normalize_resource_type(type_)

if title is not None:
path = '{0}/{1}'.format(type_, title)
elif title is None:
Expand Down

0 comments on commit 429dbbd

Please sign in to comment.