diff options
author | Dean Troyer <dtroyer@gmail.com> | 2014-10-12 20:06:14 -0500 |
---|---|---|
committer | Dean Troyer <dtroyer@gmail.com> | 2014-10-21 18:10:05 -0500 |
commit | 0ed767e20937a62ed2f190c6645673236b11237a (patch) | |
tree | c3ddb6341fb68f0b1f3bd82f2b72705cbca9ffc1 | |
parent | 470f6ea9358ac6cd6a05a96805d06c3cf8501d97 (diff) | |
download | os-client-config-0ed767e20937a62ed2f190c6645673236b11237a.tar.gz |
Map CloudConfig attributes to CloudConfig.config
Treat the CloudConfig object as if it has the config attributes directly.
And add some simple tests.
This makes it easier to replace an argparse.Namespace() object with a
CloudConfig object. It also might make initialization of some of the
default attributes unnecessary. An example of this usage is in
https://review.openstack.org/#/c/129795/1/openstackclient/shell.py
Change-Id: I00ced540cf94742e8cb738f8f0767445ffeb4bfe
-rw-r--r-- | os_client_config/cloud_config.py | 14 | ||||
-rw-r--r-- | os_client_config/tests/test_cloud_config.py | 44 |
2 files changed, 58 insertions, 0 deletions
diff --git a/os_client_config/cloud_config.py b/os_client_config/cloud_config.py index d0c932f..c17bfc2 100644 --- a/os_client_config/cloud_config.py +++ b/os_client_config/cloud_config.py @@ -18,3 +18,17 @@ class CloudConfig(object): self.name = name self.region = region self.config = config + + def __getattr__(self, key): + """Return arbitrary attributes.""" + + if key.startswith('os_'): + key = key[3:] + + if key in [attr.replace('-', '_') for attr in self.config]: + return self.config[key] + else: + return None + + def __iter__(self): + return self.config.__iter__() diff --git a/os_client_config/tests/test_cloud_config.py b/os_client_config/tests/test_cloud_config.py new file mode 100644 index 0000000..36386e5 --- /dev/null +++ b/os_client_config/tests/test_cloud_config.py @@ -0,0 +1,44 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + + +from os_client_config import cloud_config +from os_client_config.tests import base + + +fake_config_dict = {'a': 1, 'os_b': 2, 'c': 3, 'os_c': 4} + + +class TestCloudConfig(base.TestCase): + + def test_arbitrary_attributes(self): + cc = cloud_config.CloudConfig("test1", "region-al", fake_config_dict) + self.assertEqual("test1", cc.name) + self.assertEqual("region-al", cc.region) + + # Look up straight value + self.assertEqual(1, cc.a) + + # Look up prefixed attribute, fail - returns None + self.assertEqual(None, cc.os_b) + + # Look up straight value, then prefixed value + self.assertEqual(3, cc.c) + self.assertEqual(3, cc.os_c) + + # Lookup mystery attribute + self.assertIsNone(cc.x) + + def test_iteration(self): + cc = cloud_config.CloudConfig("test1", "region-al", fake_config_dict) + self.assertTrue('a' in cc) + self.assertFalse('x' in cc) |