Convert configuration object to a dictionary
This change no longer requires users of the Satori module to fake an argparse object to set configuration values. Change-Id: I61a1086bf16befe3dc9e45ba739790ed99eeb446 Implements: blueprint poc-config-object
This commit is contained in:
parent
c9fef42b4e
commit
d80e65b73a
|
@ -32,8 +32,11 @@ from satori import dns
|
|||
from satori import utils
|
||||
|
||||
|
||||
def run(address, config, interactive=False):
|
||||
def run(address, config=None, interactive=False):
|
||||
"""Run discovery and return results."""
|
||||
if config is None:
|
||||
config = {}
|
||||
|
||||
results = {}
|
||||
if utils.is_valid_ip_address(address):
|
||||
ipaddress = address
|
||||
|
@ -53,7 +56,7 @@ def run(address, config, interactive=False):
|
|||
results['address'] = ipaddress
|
||||
|
||||
results['host'] = host = {'type': 'Undetermined'}
|
||||
if config.username is not None:
|
||||
if config.get('username'):
|
||||
server = find_nova_host(ipaddress, config)
|
||||
if server:
|
||||
host['type'] = 'Nova instance'
|
||||
|
@ -62,8 +65,8 @@ def run(address, config, interactive=False):
|
|||
host['name'] = server.name
|
||||
host['id'] = server.id
|
||||
host['addresses'] = server.addresses
|
||||
if config.system_info:
|
||||
module_name = config.system_info.replace("-", "_")
|
||||
if config.get('system_info'):
|
||||
module_name = config['system_info'].replace("-", "_")
|
||||
if '.' not in module_name:
|
||||
module_name = 'satori.sysinfo.%s' % module_name
|
||||
system_info_module = utils.import_object(module_name)
|
||||
|
@ -75,11 +78,11 @@ def run(address, config, interactive=False):
|
|||
|
||||
def find_nova_host(address, config):
|
||||
"""See if a nova instance has the supplied address."""
|
||||
nova = client.Client(config.username,
|
||||
config.password,
|
||||
config.tenant_id,
|
||||
config.authurl,
|
||||
region_name=config.region,
|
||||
nova = client.Client(config['username'],
|
||||
config['password'],
|
||||
config['tenant_id'],
|
||||
config['authurl'],
|
||||
region_name=config['region'],
|
||||
service_type="compute")
|
||||
for server in nova.servers.list():
|
||||
for network_addresses in six.itervalues(server.addresses):
|
||||
|
|
|
@ -43,7 +43,7 @@ def netloc_parser(data):
|
|||
"""
|
||||
if data and '@' in data:
|
||||
first_at = data.index('@')
|
||||
return (data[0:first_at] or None), data[first_at+1:] or None
|
||||
return (data[0:first_at] or None), data[first_at + 1:] or None
|
||||
else:
|
||||
return None, data or None
|
||||
|
||||
|
@ -210,7 +210,7 @@ def parse_args(argv):
|
|||
else:
|
||||
config.host_username = 'root'
|
||||
|
||||
return config
|
||||
return vars(config)
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
|
@ -218,17 +218,18 @@ def main(argv=None):
|
|||
config = parse_args(argv)
|
||||
common_logging.init_logging(config)
|
||||
|
||||
if not (config.format == 'json' or check_format(config.format or "text")):
|
||||
if not (config['format'] == 'json' or
|
||||
check_format(config['format'] or "text")):
|
||||
sys.exit("Output format file (%s) not found or accessible. Try "
|
||||
"specifying raw JSON format using `--format json`" %
|
||||
get_template_path(config.format))
|
||||
get_template_path(config['format']))
|
||||
|
||||
try:
|
||||
results = discovery.run(config.netloc, config, interactive=True)
|
||||
print(format_output(config.netloc, results,
|
||||
template_name=config.format))
|
||||
results = discovery.run(config['netloc'], config, interactive=True)
|
||||
print(format_output(config['netloc'], results,
|
||||
template_name=config['format']))
|
||||
except Exception as exc: # pylint: disable=W0703
|
||||
if config.debug:
|
||||
if config['debug']:
|
||||
LOG.exception(exc)
|
||||
return str(exc)
|
||||
|
||||
|
|
|
@ -45,8 +45,8 @@ def get_systeminfo(ipaddress, config, interactive=False):
|
|||
client.port = 0
|
||||
|
||||
else:
|
||||
client = bash.RemoteShell(ipaddress, username=config.host_username,
|
||||
private_key=config.host_key,
|
||||
client = bash.RemoteShell(ipaddress, username=config['host_username'],
|
||||
private_key=config['host_key'],
|
||||
interactive=interactive)
|
||||
|
||||
install_remote(client)
|
||||
|
|
|
@ -28,9 +28,10 @@ class TestOhaiSolo(utils.TestCase):
|
|||
@mock.patch.object(ohai_solo, 'install_remote')
|
||||
def test_connect_and_run(self, mock_install, mock_sysinfo, mock_bash):
|
||||
address = "192.0.2.2"
|
||||
config = mock.MagicMock()
|
||||
config.host_key = "foo"
|
||||
config.host_username = "bar"
|
||||
config = {
|
||||
'host_key': 'foo',
|
||||
'host_username': 'bar',
|
||||
}
|
||||
mock_sysinfo.return_value = {}
|
||||
result = ohai_solo.get_systeminfo(address, config)
|
||||
self.assertTrue(result is mock_sysinfo.return_value)
|
||||
|
|
Loading…
Reference in New Issue