Skip to content

Commit

Permalink
Update how API client reads config file
Browse files Browse the repository at this point in the history
Close #107
  • Loading branch information
elyezer committed Jan 5, 2018
1 parent b770e37 commit 9cef1bb
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 34 deletions.
44 changes: 26 additions & 18 deletions camayoc/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import requests

from urllib.parse import urljoin
from urllib.parse import urljoin, urlunparse

from camayoc import config
from camayoc import exceptions
Expand Down Expand Up @@ -79,34 +79,42 @@ def __init__(self, response_handler=None, url=None):
If no response handler is specified, use the `code_handler` which will
raise an exception for 'bad' return codes.
To specify base url with config file, include the following your
camayoc config file:
If no URL is specified, then the config file will be parsed and the URL
will be built by reading the hostname, port and https values. You can
configure the default URL by including the following on your Camayoc
configuration file::
qcs:
hostname: 'http://hostname_or_ip_with_port'
hostname: <machine_hostname_or_ip_address>
port: <port> # if not defined will take the default port
# depending on the https config: 80 if https is
# false and 443 if https is true.
https: false # change to true if server is published over
# https. Defaults to false if not defined
"""
self._cfg = config.get_config()
qcs_settings = self._cfg.get('qcs')
self.url = urljoin(url, QCS_API_VERSION) if url else None
self.url = url

if not self.url:
cfg = config.get_config().get('qcs', {})
hostname = cfg.get('hostname')

if qcs_settings and not url:
if not qcs_settings.get('hostname'):
if not hostname:
raise exceptions.QCSBaseUrlNotFound(
"\n'qcs' section specified in camayoc config file, but"
"no 'hostname' key found."
)
self.url = urljoin(qcs_settings.get('hostname'), QCS_API_VERSION)

scheme = 'https' if cfg.get('https', False) else 'http'
port = str(cfg.get('port', ''))
netloc = hostname + ':{}'.format(port) if port else hostname
self.url = urlunparse(
(scheme, netloc, QCS_API_VERSION, '', '', ''))

if not self.url:
raise exceptions.QCSBaseUrlNotFound(
'\nNo base url was specified to the client'
'\neither with the url="host" option or with the camayoc'
' config file.')
if 'http' not in self.url:
raise exceptions.QCSBaseUrlNotFound(
'A hostname was provided, but we could not use it.'
'\nValid hostnames start with http:// or https://'
)
'No base url was specified to the client either with the '
'url="host" option or with the camayoc config file.')

if response_handler is None:
self.response_handler = code_handler
Expand Down
9 changes: 3 additions & 6 deletions camayoc/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,10 @@ class ConfigFileNotFoundError(Exception):


class QCSBaseUrlNotFound(Exception):
"""No base url was specifed in the camayoc config file.
"""Was not able to build a base URL with the config file information.
Specify a base url to contact the quipucords server in your camayoc
config file in the following manner:
qcs:
hostname: 'http://hostname_or_ip_with_port'
Check the expected configuration file format on the API Client
documentation.
"""


Expand Down
18 changes: 9 additions & 9 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@

CAMAYOC_CONFIG = """
qcs:
hostname: 'http://example.com'
hostname: example.com
https: false
"""

INVALID_HOST_CONFIG = """
qcs:
hostname: 'example.com'
port: 8000
https: true
"""

MOCK_CREDENTIAL = {
Expand Down Expand Up @@ -58,27 +60,25 @@ def test_create_with_config(self):
with mock.patch.object(config, '_CONFIG', self.config):
self.assertEqual(config.get_config(), self.config)
client = api.Client()
cfg_host = self.config['qcs']['hostname']
self.assertEqual(cfg_host, client.url.strip('api/v1/'))
self.assertEqual(client.url, 'http://example.com/api/v1/')

def test_create_no_config(self):
"""If a base url is specified we use it."""
with mock.patch.object(config, '_CONFIG', {}):
self.assertEqual(config.get_config(), {})
other_host = 'http://hostname.com'
client = api.Client(url=other_host)
cfg_host = self.config['qcs']['hostname']
self.assertNotEqual(cfg_host, client.url.strip('/api/v1/'))
self.assertEqual(other_host, client.url.strip('/api/v1/'))
self.assertNotEqual('http://example.com/api/v1/', client.url)
self.assertEqual(other_host, client.url)

def test_create_override_config(self):
"""If a base url is specified, we use that instead of config file."""
with mock.patch.object(config, '_CONFIG', self.config):
other_host = 'http://hostname.com'
client = api.Client(url=other_host)
cfg_host = self.config['qcs']['hostname']
self.assertNotEqual(cfg_host, client.url.strip('/api/v1/'))
self.assertEqual(other_host, client.url.strip('/api/v1/'))
self.assertNotEqual(cfg_host, client.url)
self.assertEqual(other_host, client.url)

def test_negative_create(self):
"""Raise an error if no config entry is found and no url specified."""
Expand Down
4 changes: 3 additions & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
vcenter:
hostname: example1.com
qcs:
hostname: 'http://example2.com'
hostname: example2.com
port: 8000
https: false
"""


Expand Down

0 comments on commit 9cef1bb

Please sign in to comment.