def test_az(self, mock_get_server_external_ipv4): mock_get_server_external_ipv4.return_value = PUBLIC_V4 server = FakeServer() server.__dict__["OS-EXT-AZ:availability_zone"] = "az1" hostvars = meta.get_hostvars_from_server(FakeCloud(), meta.obj_to_dict(server)) self.assertEquals("az1", hostvars["az"])
def _exit_hostvars_count(module, cloud, servers, changed=True): hostvars = [] for server in servers: hostvars.append(meta.get_hostvars_from_server(cloud, server)) server_ids = [server.id for server in servers] module.exit_json( changed=changed, servers=servers, ids=server_ids, openstack=hostvars)
def test_basic_hostvars( self, mock_get_server_external_ipv4, mock_get_server_external_ipv6): mock_get_server_external_ipv4.return_value = PUBLIC_V4 mock_get_server_external_ipv6.return_value = PUBLIC_V6 hostvars = meta.get_hostvars_from_server( FakeCloud(), _utils.normalize_server( meta.obj_to_dict(FakeServer()), cloud_name='CLOUD_NAME', region_name='REGION_NAME')) self.assertNotIn('links', hostvars) self.assertEqual(PRIVATE_V4, hostvars['private_v4']) self.assertEqual(PUBLIC_V4, hostvars['public_v4']) self.assertEqual(PUBLIC_V6, hostvars['public_v6']) self.assertEqual(PUBLIC_V6, hostvars['interface_ip']) self.assertEquals('REGION_NAME', hostvars['region']) self.assertEquals('CLOUD_NAME', hostvars['cloud']) self.assertEquals("test-image-name", hostvars['image']['name']) self.assertEquals(FakeServer.image['id'], hostvars['image']['id']) self.assertNotIn('links', hostvars['image']) self.assertEquals(FakeServer.flavor['id'], hostvars['flavor']['id']) self.assertEquals("test-flavor-name", hostvars['flavor']['name']) self.assertNotIn('links', hostvars['flavor']) # test having volumes # test volume exception self.assertEquals([], hostvars['volumes'])
def test_image_string(self, mock_get_server_external_ipv4): mock_get_server_external_ipv4.return_value = PUBLIC_V4 server = FakeServer() server.image = "fake-image-id" hostvars = meta.get_hostvars_from_server(FakeCloud(), meta.obj_to_dict(server)) self.assertEquals("fake-image-id", hostvars["image"]["id"])
def test_basic_hostvars( self, mock_get_server_external_ipv4, mock_get_server_external_ipv6): mock_get_server_external_ipv4.return_value = PUBLIC_V4 mock_get_server_external_ipv6.return_value = PUBLIC_V6 hostvars = meta.get_hostvars_from_server( FakeCloud(), self.cloud._normalize_server( meta.obj_to_munch(standard_fake_server))) self.assertNotIn('links', hostvars) self.assertEqual(PRIVATE_V4, hostvars['private_v4']) self.assertEqual(PUBLIC_V4, hostvars['public_v4']) self.assertEqual(PUBLIC_V6, hostvars['public_v6']) self.assertEqual(PUBLIC_V6, hostvars['interface_ip']) self.assertEqual('RegionOne', hostvars['region']) self.assertEqual('_test_cloud_', hostvars['cloud']) self.assertIn('location', hostvars) self.assertEqual('_test_cloud_', hostvars['location']['cloud']) self.assertEqual('RegionOne', hostvars['location']['region_name']) self.assertEqual('admin', hostvars['location']['project']['name']) self.assertEqual("test-image-name", hostvars['image']['name']) self.assertEqual( standard_fake_server['image']['id'], hostvars['image']['id']) self.assertNotIn('links', hostvars['image']) self.assertEqual( standard_fake_server['flavor']['id'], hostvars['flavor']['id']) self.assertEqual("test-flavor-name", hostvars['flavor']['name']) self.assertNotIn('links', hostvars['flavor']) # test having volumes # test volume exception self.assertEqual([], hostvars['volumes'])
def test_private_interface_ip(self, mock_get_server_external_ipv4): mock_get_server_external_ipv4.return_value = PUBLIC_V4 cloud = FakeCloud() cloud.private = True hostvars = meta.get_hostvars_from_server(cloud, meta.obj_to_dict(FakeServer())) self.assertEqual(PRIVATE_V4, hostvars["interface_ip"])
def test_has_no_volume_service(self): mock_cloud = mock.MagicMock() def side_effect(*args): raise exc.OpenStackCloudException("No Volumes") mock_cloud.get_volumes.side_effect = side_effect hostvars = meta.get_hostvars_from_server(mock_cloud, FakeServer()) self.assertEquals([], hostvars['volumes'])
def test_private_interface_ip(self, mock_get_server_external_ipv4): mock_get_server_external_ipv4.return_value = PUBLIC_V4 cloud = FakeCloud() cloud.private = True hostvars = meta.get_hostvars_from_server( cloud, meta.obj_to_munch(standard_fake_server)) self.assertEqual(PRIVATE_V4, hostvars['interface_ip'])
def test_image_string(self, mock_get_server_external_ipv4): mock_get_server_external_ipv4.return_value = PUBLIC_V4 server = standard_fake_server server.image = 'fake-image-id' hostvars = meta.get_hostvars_from_server( FakeCloud(), meta.obj_to_dict(server)) self.assertEquals('fake-image-id', hostvars['image']['id'])
def test_ipv4_hostvars( self, mock_get_server_external_ipv4, mock_get_server_external_ipv6): mock_get_server_external_ipv4.return_value = PUBLIC_V4 mock_get_server_external_ipv6.return_value = PUBLIC_V6 fake_cloud = FakeCloud() fake_cloud.force_ipv4 = True hostvars = meta.get_hostvars_from_server( fake_cloud, meta.obj_to_dict(FakeServer())) self.assertEqual(PUBLIC_V4, hostvars['interface_ip'])
def test_has_volume(self): mock_cloud = mock.MagicMock() mock_volume = mock.MagicMock() mock_volume.id = "volume1" mock_volume.status = "available" mock_volume.display_name = "Volume 1 Display Name" mock_volume.attachments = [{"device": "/dev/sda0"}] mock_volume_dict = meta.obj_to_dict(mock_volume) mock_cloud.get_volumes.return_value = [mock_volume_dict] hostvars = meta.get_hostvars_from_server(mock_cloud, meta.obj_to_dict(FakeServer())) self.assertEquals("volume1", hostvars["volumes"][0]["id"]) self.assertEquals("/dev/sda0", hostvars["volumes"][0]["device"])
def test_has_volume(self): mock_cloud = mock.MagicMock() mock_volume = mock.MagicMock() mock_volume.id = 'volume1' mock_volume.status = 'available' mock_volume.display_name = 'Volume 1 Display Name' mock_volume.attachments = [{'device': '/dev/sda0'}] mock_volume_dict = meta.obj_to_dict(mock_volume) mock_cloud.get_volumes.return_value = [mock_volume_dict] hostvars = meta.get_hostvars_from_server(mock_cloud, FakeServer()) self.assertEquals('volume1', hostvars['volumes'][0]['id']) self.assertEquals('/dev/sda0', hostvars['volumes'][0]['device'])
def test_has_volume(self): mock_cloud = mock.MagicMock() fake_volume = fakes.FakeVolume( id='volume1', status='available', name='Volume 1 Display Name', attachments=[{'device': '/dev/sda0'}]) fake_volume_dict = meta.obj_to_dict(fake_volume) mock_cloud.get_volumes.return_value = [fake_volume_dict] hostvars = meta.get_hostvars_from_server( mock_cloud, meta.obj_to_dict(standard_fake_server)) self.assertEquals('volume1', hostvars['volumes'][0]['id']) self.assertEquals('/dev/sda0', hostvars['volumes'][0]['device'])
def test_get_security_groups(self, mock_list_server_security_groups): '''This test verifies that calling get_hostvars_froms_server ultimately calls list_server_security_groups, and that the return value from list_server_security_groups ends up in server['security_groups'].''' mock_list_server_security_groups.return_value = [ {'name': 'testgroup', 'id': '1'}] server = meta.obj_to_dict(FakeServer()) hostvars = meta.get_hostvars_from_server(FakeCloud(), server) mock_list_server_security_groups.assert_called_once_with(server) self.assertEqual('testgroup', hostvars['security_groups'][0]['name'])
def test_has_volume(self): mock_cloud = mock.MagicMock() fake_volume = fakes.FakeVolume(id='volume1', status='available', name='Volume 1 Display Name', attachments=[{ 'device': '/dev/sda0' }]) fake_volume_dict = meta.obj_to_dict(fake_volume) mock_cloud.get_volumes.return_value = [fake_volume_dict] hostvars = meta.get_hostvars_from_server( mock_cloud, meta.obj_to_dict(standard_fake_server)) self.assertEqual('volume1', hostvars['volumes'][0]['id']) self.assertEqual('/dev/sda0', hostvars['volumes'][0]['device'])
def test_get_security_groups(self, mock_list_server_security_groups): '''This test verifies that calling get_hostvars_froms_server ultimately calls list_server_security_groups, and that the return value from list_server_security_groups ends up in server['security_groups'].''' mock_list_server_security_groups.return_value = [{ 'name': 'testgroup', 'id': '1' }] server = meta.obj_to_dict(standard_fake_server) hostvars = meta.get_hostvars_from_server(FakeCloud(), server) mock_list_server_security_groups.assert_called_once_with(server) self.assertEqual('testgroup', hostvars['security_groups'][0]['name'])
def test_basic_hostvars(self): hostvars = meta.get_hostvars_from_server(FakeCloud(), FakeServer()) self.assertNotIn('links', hostvars) self.assertEqual(PRIVATE_V4, hostvars['private_v4']) self.assertEqual(PUBLIC_V4, hostvars['public_v4']) self.assertEqual(PUBLIC_V4, hostvars['interface_ip']) self.assertEquals(FakeCloud.region_name, hostvars['region']) self.assertEquals(FakeCloud.name, hostvars['cloud']) self.assertEquals("test-image-name", hostvars['image']['name']) self.assertEquals(FakeServer.image['id'], hostvars['image']['id']) self.assertNotIn('links', hostvars['image']) self.assertEquals(FakeServer.flavor['id'], hostvars['flavor']['id']) self.assertEquals("test-flavor-name", hostvars['flavor']['name']) self.assertNotIn('links', hostvars['flavor']) # test having volumes # test volume exception self.assertEquals([], hostvars['volumes'])
def main(): argument_spec = openstack_full_argument_spec(server=dict(required=True), ) module_kwargs = openstack_module_kwargs() module = AnsibleModule(argument_spec, **module_kwargs) if not HAS_SHADE: module.fail_json(msg='shade is required for this module') try: cloud = shade.openstack_cloud(**module.params) server = cloud.get_server(module.params['server']) hostvars = dict(openstack=meta.get_hostvars_from_server(cloud, server)) module.exit_json(changed=False, ansible_facts=hostvars) except shade.OpenStackCloudException as e: module.fail_json(msg=e.message)
def test_basic_hostvars(self, mock_get_server_external_ipv4): mock_get_server_external_ipv4.return_value = PUBLIC_V4 hostvars = meta.get_hostvars_from_server(FakeCloud(), meta.obj_to_dict(FakeServer())) self.assertNotIn("links", hostvars) self.assertEqual(PRIVATE_V4, hostvars["private_v4"]) self.assertEqual(PUBLIC_V4, hostvars["public_v4"]) self.assertEqual(PUBLIC_V4, hostvars["interface_ip"]) self.assertEquals(FakeCloud.region_name, hostvars["region"]) self.assertEquals(FakeCloud.name, hostvars["cloud"]) self.assertEquals("test-image-name", hostvars["image"]["name"]) self.assertEquals(FakeServer.image["id"], hostvars["image"]["id"]) self.assertNotIn("links", hostvars["image"]) self.assertEquals(FakeServer.flavor["id"], hostvars["flavor"]["id"]) self.assertEquals("test-flavor-name", hostvars["flavor"]["name"]) self.assertNotIn("links", hostvars["flavor"]) # test having volumes # test volume exception self.assertEquals([], hostvars["volumes"])
def main(): argument_spec = openstack_full_argument_spec( server=dict(required=True), ) module_kwargs = openstack_module_kwargs() module = AnsibleModule(argument_spec, **module_kwargs) if not HAS_SHADE: module.fail_json(msg='shade is required for this module') try: cloud = shade.openstack_cloud(**module.params) server = cloud.get_server(module.params['server']) hostvars = dict(openstack=meta.get_hostvars_from_server( cloud, server)) module.exit_json(changed=False, ansible_facts=hostvars) except shade.OpenStackCloudException as e: module.fail_json(msg=e.message)
def test_basic_hostvars(self, mock_get_server_external_ipv4, mock_get_server_external_ipv6): mock_get_server_external_ipv4.return_value = PUBLIC_V4 mock_get_server_external_ipv6.return_value = PUBLIC_V6 hostvars = meta.get_hostvars_from_server( FakeCloud(), meta.obj_to_dict(FakeServer())) self.assertNotIn('links', hostvars) self.assertEqual(PRIVATE_V4, hostvars['private_v4']) self.assertEqual(PUBLIC_V4, hostvars['public_v4']) self.assertEqual(PUBLIC_V6, hostvars['public_v6']) self.assertEqual(PUBLIC_V4, hostvars['interface_ip']) self.assertEquals(FakeCloud.region_name, hostvars['region']) self.assertEquals(FakeCloud.name, hostvars['cloud']) self.assertEquals("test-image-name", hostvars['image']['name']) self.assertEquals(FakeServer.image['id'], hostvars['image']['id']) self.assertNotIn('links', hostvars['image']) self.assertEquals(FakeServer.flavor['id'], hostvars['flavor']['id']) self.assertEquals("test-flavor-name", hostvars['flavor']['name']) self.assertNotIn('links', hostvars['flavor']) # test having volumes # test volume exception self.assertEquals([], hostvars['volumes'])
def _exit_hostvars(module, cloud, server, changed=True): hostvars = meta.get_hostvars_from_server(cloud, server) module.exit_json(changed=changed, id=server.id, openstack=hostvars)
def _exit_hostvars(module, cloud, server, changed=True): hostvars = meta.get_hostvars_from_server(cloud, server) module.exit_json( changed=changed, server=server, id=server.id, openstack=hostvars)
def test_has_no_volume_service(self): fake_cloud = FakeCloud() fake_cloud.service_val = False hostvars = meta.get_hostvars_from_server( fake_cloud, meta.obj_to_munch(standard_fake_server)) self.assertEqual([], hostvars['volumes'])
def main(): argument_spec = openstack_full_argument_spec( server=dict(required=True), volume=dict(required=True), device=dict(default=None), # None == auto choose device name state=dict(default='present', choices=['absent', 'present']), ) module_kwargs = openstack_module_kwargs() module = AnsibleModule(argument_spec, supports_check_mode=True, **module_kwargs) if not HAS_SHADE: module.fail_json(msg='shade is required for this module') state = module.params['state'] wait = module.params['wait'] timeout = module.params['timeout'] try: cloud = shade.openstack_cloud(**module.params) server = cloud.get_server(module.params['server']) volume = cloud.get_volume(module.params['volume']) dev = cloud.get_volume_attach_device(volume, server.id) if module.check_mode: module.exit_json(changed=_system_state_change(state, dev)) if state == 'present': if dev: # Volume is already attached to this server module.exit_json(changed=False) cloud.attach_volume(server, volume, module.params['device'], wait=wait, timeout=timeout) server = cloud.get_server(module.params['server']) # refresh volume = cloud.get_volume(module.params['volume']) # refresh hostvars = meta.get_hostvars_from_server(cloud, server) module.exit_json( changed=True, id=volume['id'], attachments=volume['attachments'], openstack=hostvars ) elif state == 'absent': if not dev: # Volume is not attached to this server module.exit_json(changed=False) cloud.detach_volume(server, volume, wait=wait, timeout=timeout) module.exit_json( changed=True, result='Detached volume from server' ) except (shade.OpenStackCloudException, shade.OpenStackCloudTimeout) as e: module.fail_json(msg=e.message)
def test_has_no_volume_service(self): fake_cloud = FakeCloud() fake_cloud.service_val = False hostvars = meta.get_hostvars_from_server( fake_cloud, meta.obj_to_dict(FakeServer())) self.assertEquals([], hostvars['volumes'])
def test_image_string(self): server = FakeServer() server.image = 'fake-image-id' hostvars = meta.get_hostvars_from_server(FakeCloud(), server) self.assertEquals('fake-image-id', hostvars['image']['id'])
def test_az(self): server = FakeServer() server.__dict__['OS-EXT-AZ:availability_zone'] = 'az1' hostvars = meta.get_hostvars_from_server(FakeCloud(), server) self.assertEquals('az1', hostvars['az'])
def test_private_interface_ip(self): cloud = FakeCloud() cloud.private = True hostvars = meta.get_hostvars_from_server(cloud, FakeServer()) self.assertEqual(PRIVATE_V4, hostvars['interface_ip'])