def test_hosts_get_with_key_value_filters(self): project_id = self.make_project('project_1', foo='P1', zoo='P2') cloud_id = self.make_cloud(project_id, 'cloud_1') region_id = self.make_region(project_id, cloud_id, 'region_1', foo='R1') host1 = self.make_host(project_id, cloud_id, region_id, 'www.example.xyz', IPAddress(u'10.1.2.101'), 'server') variables = {"key1": "example1", "key2": "Tom"} dbapi.variables_update_by_resource_id( self.context, "hosts", host1, variables ) # Second host with own variables host2 = self.make_host(project_id, cloud_id, region_id, 'www.example2.xyz', IPAddress(u'10.1.2.102'), 'server') variables = {"key1": "example2", "key2": "Tom"} dbapi.variables_update_by_resource_id( self.context, "hosts", host2, variables ) filters = {"vars": "key1:example2"} res, _ = dbapi.hosts_get_all(self.context, filters, default_pagination) self.assertEqual(len(res), 1) self.assertEqual('www.example2.xyz', res[0].name) filters = {"vars": "key2:Tom"} res, _ = dbapi.hosts_get_all(self.context, filters, default_pagination) self.assertEqual(len(res), 2)
def test_hosts_get_all_with_filter_cell_id(self): project_id = self.make_project('project_1', foo='P1', zoo='P2') cloud_id = self.make_cloud(project_id, 'cloud_1') region_id = self.make_region(project_id, cloud_id, 'region_1', foo='R1') cell_id1 = self.make_cell(project_id, cloud_id, region_id, 'cell_1', bar='C2') cell_id2 = self.make_cell(project_id, cloud_id, region_id, 'cell_2', bar='C2') self.assertNotEqual(cell_id1, cell_id2) self.make_host( project_id, cloud_id, region_id, 'www.example.xyz', IPAddress(u'10.1.2.101'), 'server', cell_id=cell_id1, ) self.make_host( project_id, cloud_id, region_id, 'www.example.abc', IPAddress(u'10.1.2.102'), 'server', cell_id=cell_id2, ) all_res, _ = dbapi.hosts_get_all(self.context, {}, default_pagination) self.assertEqual(len(all_res), 2) self.assertEqual( len([host for host in all_res if host['cell_id'] == cell_id1]), 1) filters = { "cell_id": cell_id1, } res, _ = dbapi.hosts_get_all(self.context, filters, default_pagination) self.assertEqual(len(res), 1) self.assertEqual(res[0].name, 'www.example.xyz')
def test_hosts_get_all_with_resolved_var_filters(self): project_id = self.make_project('project_1', foo='P1', zoo='P2') cloud_id = self.make_cloud(project_id, 'cloud_1') region_id = self.make_region( project_id, cloud_id, 'region_1', foo='R1') switch_id = self.make_network_device( project_id, cloud_id, region_id, 'switch1.example.com', IPAddress('10.1.2.101'), 'switch', zoo='S1', bar='S2') self.make_host( project_id, cloud_id, region_id, 'www.example.xyz', IPAddress(u'10.1.2.101'), 'server', parent_id=switch_id, key1="value1", key2="value2") self.make_host( project_id, cloud_id, region_id, 'www2.example.xyz', IPAddress(u'10.1.2.102'), 'server', parent_id=switch_id, key1="value-will-not-match", key2="value2") filters = { "region_id": 1, "vars": "key1:value1,zoo:S1,foo:R1", "resolved-values": True, } res, _ = dbapi.hosts_get_all( self.context, filters, default_pagination) self.assertEqual(len(res), 1) self.assertEqual(res[0].name, 'www.example.xyz')
def test_hosts_get_all_with_label_filters(self): cloud_id = self.make_cloud(self.mock_project_id, 'cloud_1') region_id = self.make_region(self.mock_project_id, cloud_id, 'region_1') labels = {"labels": ["compute"]} host1 = self.make_host( self.mock_project_id, cloud_id, region_id, 'www1.example.com', IPAddress(u'10.1.2.101'), 'server', ) dbapi.hosts_labels_update(self.context, host1, labels) self.make_host( self.mock_project_id, cloud_id, region_id, 'www1.example2.com', IPAddress(u'10.1.2.102'), 'server', ) res, _ = dbapi.hosts_get_all(self.context, {"label": "compute"}, default_pagination) self.assertEqual(len(res), 1) self.assertEqual(res[0].name, 'www1.example.com')
def test_hosts_get_all_with_filters_noexist(self): project_id = self.make_project('project_1', foo='P1', zoo='P2') cloud_id = self.make_cloud(project_id, 'cloud_1') region_id = self.make_region(project_id, cloud_id, 'region_1', foo='R1') host_id = self.make_host(project_id, cloud_id, region_id, 'www.example.xyz', IPAddress(u'10.1.2.101'), 'server') variables = {"key1": "value1", "key2": "value2"} dbapi.variables_update_by_resource_id(self.context, "hosts", host_id, variables) filters = { "region_id": "region_1", "vars": "key1:value5", } res, _ = dbapi.hosts_get_all(self.context, filters, default_pagination) self.assertEqual(len(res), 0)