def main(): args = parse_args() try: config_files = cloud_config.CONFIG_FILES + CONFIG_FILES sdk.enable_logging(debug=args.debug) inventory_args = dict( refresh=args.refresh, config_files=config_files, private=args.private, cloud=args.cloud, ) if hasattr(sdk_inventory.OpenStackInventory, 'extra_config'): inventory_args.update(dict( config_key='ansible', config_defaults={ 'use_hostnames': False, 'expand_hostvars': False, 'fail_on_errors': True, } )) inventory = sdk_inventory.OpenStackInventory(**inventory_args) if args.list: output = get_host_groups(inventory, refresh=args.refresh, cloud=args.cloud) elif args.host: output = to_json(inventory.get_host(args.host)) print(output) except sdk.exceptions.OpenStackCloudException as e: sys.stderr.write('%s\n' % e.message) sys.exit(1) sys.exit(0)
def test__init(self, mock_cloud, mock_config): mock_config.return_value.get_all.return_value = [{}] inv = inventory.OpenStackInventory() mock_config.assert_called_once_with( config_files=openstack.config.loader.CONFIG_FILES) self.assertIsInstance(inv.clouds, list) self.assertEqual(1, len(inv.clouds)) self.assertTrue(mock_config.return_value.get_all.called)
def test__init_one_cloud(self, mock_cloud, mock_config): mock_config.return_value.get_one_cloud.return_value = [{}] inv = inventory.OpenStackInventory(cloud='supercloud') mock_config.assert_called_once_with( config_files=openstack.config.loader.CONFIG_FILES) self.assertIsInstance(inv.clouds, list) self.assertEqual(1, len(inv.clouds)) self.assertFalse(mock_config.return_value.get_all_clouds.called) mock_config.return_value.get_one_cloud.assert_called_once_with( 'supercloud')
def test_get_host(self, mock_cloud, mock_config): mock_config.return_value.get_all.return_value = [{}] inv = inventory.OpenStackInventory() server = dict(id='server_id', name='server_name') self.assertIsInstance(inv.clouds, list) self.assertEqual(1, len(inv.clouds)) inv.clouds[0].list_servers.return_value = [server] inv.clouds[0].get_openstack_vars.return_value = server ret = inv.get_host('server_id') self.assertEqual(server, ret)
def test_list_hosts_no_detail(self, mock_cloud, mock_config): mock_config.return_value.get_all.return_value = [{}] inv = inventory.OpenStackInventory() server = self.cloud._normalize_server( fakes.make_fake_server('1234', 'test', 'ACTIVE', addresses={})) self.assertIsInstance(inv.clouds, list) self.assertEqual(1, len(inv.clouds)) inv.clouds[0].list_servers.return_value = [server] inv.list_hosts(expand=False) inv.clouds[0].list_servers.assert_called_once_with(detailed=False) self.assertFalse(inv.clouds[0].get_openstack_vars.called)
def test_list_hosts(self, mock_cloud, mock_config): mock_config.return_value.get_all.return_value = [{}] inv = inventory.OpenStackInventory() server = dict(id='server_id', name='server_name') self.assertIsInstance(inv.clouds, list) self.assertEqual(1, len(inv.clouds)) inv.clouds[0].list_servers.return_value = [server] inv.clouds[0].get_openstack_vars.return_value = server ret = inv.list_hosts() inv.clouds[0].list_servers.assert_called_once_with(detailed=True) self.assertFalse(inv.clouds[0].get_openstack_vars.called) self.assertEqual([server], ret)
def setUp(self): super(TestInventory, self).setUp() # This needs to use an admin account, otherwise a public IP # is not allocated from devstack. self.inventory = inventory.OpenStackInventory(cloud='devstack-admin') self.server_name = self.getUniqueString('inventory') self.flavor = pick_flavor( self.user_cloud.list_flavors(get_extra=False)) if self.flavor is None: self.assertTrue(False, 'no sensible flavor available') self.image = self.pick_image() self.addCleanup(self._cleanup_server) server = self.operator_cloud.create_server(name=self.server_name, image=self.image, flavor=self.flavor, wait=True, auto_ip=True, network='public') self.server_id = server['id']
def create_inventory(nodes, mgmt): global myservers global mgmt_nodes global node_list mgmt_nodes = mgmt node_list = nodes myservers = collections.OrderedDict({'all': {'children': {}}}) args = Foo() try: # openstacksdk library may write to stdout, so redirect this sys.stdout = StringIO() config_files = cloud_config.CONFIG_FILES + CONFIG_FILES sdk.enable_logging(debug=args.debug) inventory_args = dict( refresh=args.refresh, config_files=config_files, private=args.private, cloud=args.cloud, ) if hasattr(sdk_inventory.OpenStackInventory, 'extra_config'): inventory_args.update( dict(config_key='ansible', config_defaults={ 'use_hostnames': False, 'expand_hostvars': True, 'fail_on_errors': True, })) inventory = sdk_inventory.OpenStackInventory(**inventory_args) sys.stdout = sys.__stdout__ if args.list: output = get_host_groups(inventory, refresh=args.refresh, cloud=args.cloud) return myservers, manager_public except sdk.exceptions.OpenStackCloudException as e: sys.stderr.write('%s\n' % e.message) sys.exit(1)
def main(): args = parse_args() try: # openstacksdk library may write to stdout, so redirect this sys.stdout = StringIO() config_files = cloud_config.CONFIG_FILES + CONFIG_FILES sdk.enable_logging(debug=args.debug) inventory_args = dict( refresh=args.refresh, config_files=config_files, private=args.private, cloud=args.cloud, ) if hasattr(sdk_inventory.OpenStackInventory, 'extra_config'): inventory_args.update( dict( config_key='ansible', config_defaults={ 'use_hostnames': True, #False, (If i leave this as False the quesry will display instance ID rather than Name) 'expand_hostvars': True, 'fail_on_errors': True, })) inventory = sdk_inventory.OpenStackInventory(**inventory_args) sys.stdout = sys.__stdout__ if args.list: output = get_host_groups(inventory, refresh=args.refresh, cloud=args.cloud) elif args.host: output = to_json(inventory.get_host(args.host)) print(output) except sdk.exceptions.OpenStackCloudException as e: sys.stderr.write('%s\n' % e.message) sys.exit(1) sys.exit(0)