This repository has been archived by the owner on Feb 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from ashcrow/config
Works as expected
- Loading branch information
Showing
10 changed files
with
185 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
commissaire.config module | ||
========================= | ||
|
||
.. automodule:: commissaire.config | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# Copyright (C) 2016 Red Hat, Inc | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
""" | ||
Configuration. | ||
""" | ||
|
||
import logging | ||
|
||
import etcd | ||
|
||
|
||
class Config(dict): | ||
""" | ||
Configuration container. | ||
""" | ||
|
||
def __init__(self, listen={}, etcd={}, kubernetes={}): | ||
""" | ||
Creates an instance of the Config class. | ||
:param listen: Structure containing the server listening data. | ||
:type listen: dict | ||
:param etcd: Structure containing the etcd connection data. | ||
:type etcd: dict | ||
:param kubernetes: Structure containing the kubernetes connection data. | ||
:type kubernets: dict | ||
:returns: commissaire.config.Config | ||
""" | ||
self.listen = listen | ||
self.etcd = etcd | ||
self.kubernetes = kubernetes | ||
|
||
|
||
def cli_etcd_or_default(name, cli, default, ds): | ||
""" | ||
Returns the value for an option in the following order: | ||
CLI switch, etcd value, default. | ||
:param name: The name of the switch/etcd key. | ||
:type name: str | ||
:param cli: The argparse value. | ||
:type cli: list | ||
:param default: The default value if CLI and etcd have no values. | ||
:param ds: Etcd client | ||
:type ds: etcd.Client | ||
""" | ||
result = None | ||
if cli: | ||
result = cli[0] | ||
logging.info('Using CLI for {0} configuration.'.format(name)) | ||
else: | ||
try: | ||
result = ds.get('/commissaire/config/{0}'.format(name)).value | ||
logging.info('Using Etcd for {0} configuration.'.format(name)) | ||
except etcd.EtcdKeyNotFound: | ||
logging.info( | ||
'No CLI or etcd defined for {0}.' | ||
' Using default of {1}.'.format(name, default)) | ||
result = default | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Copyright (C) 2016 Red Hat, Inc | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
""" | ||
Test cases for the commissaire.script module. | ||
""" | ||
|
||
import etcd | ||
|
||
from . import TestCase | ||
from argparse import Namespace | ||
from mock import MagicMock | ||
from commissaire import config | ||
|
||
|
||
class Test_CliEtcdOrDefault(TestCase): | ||
""" | ||
Tests for the cli_etcd_or_default function. | ||
""" | ||
|
||
def test_cli_etcd_or_default_with_cli_input(self): | ||
""" | ||
Verify cli_etcd_or_default works with cli input. | ||
""" | ||
cli = Namespace(test=['test']) | ||
ds = MagicMock(get=MagicMock(side_effect=etcd.EtcdKeyNotFound)) | ||
self.assertEquals( | ||
'test', | ||
config.cli_etcd_or_default('test', cli.test, 'default', ds)) | ||
|
||
def test_cli_etcd_or_default_with_default_fallback(self): | ||
""" | ||
Verify cli_etcd_or_default falls to default with no other input. | ||
""" | ||
cli = Namespace(test=None) | ||
ds = MagicMock(get=MagicMock(side_effect=etcd.EtcdKeyNotFound)) | ||
self.assertEquals( | ||
'default', | ||
config.cli_etcd_or_default('test', cli.test, 'default', ds)) | ||
|
||
def test_cli_etcd_or_default_with_etcd_result(self): | ||
""" | ||
Verify cli_etcd_or_default uses etcd result when present. | ||
""" | ||
cli = Namespace(test=None) | ||
ds = MagicMock( | ||
get=MagicMock(return_value=MagicMock(value='frometcd'))) | ||
self.assertEquals( | ||
'frometcd', | ||
config.cli_etcd_or_default('test', cli.test, 'default', ds)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.