Python oslo_serialization.jsonutils.dumps() Examples
The following are 30
code examples of oslo_serialization.jsonutils.dumps().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
oslo_serialization.jsonutils
, or try the search function
.
Example #1
Source File: fakes.py From ec2-api with Apache License 2.0 | 6 votes |
def __init__(self, image_dict, from_get=False): if from_get: attrs = [k for k in image_dict.keys() if image_dict[k] is not None] else: attrs = list(image_dict) attrs.extend( ['owner', 'created_at', 'visibility', 'status', 'container_format', 'name']) self._image_dict = {'id': image_dict['id']} self._image_dict.update({k: image_dict.get(k) for k in attrs}) for complex_attr in ('mappings', 'block_device_mapping'): if complex_attr in self._image_dict: self._image_dict[complex_attr] = jsonutils.dumps( self._image_dict[complex_attr]) for k in self._image_dict: setattr(self, k, self._image_dict[k])
Example #2
Source File: test_pool.py From kuryr-kubernetes with Apache License 2.0 | 6 votes |
def test_do_GET_show_empty(self): method = 'show' method_resp = "Empty pool" path = "http://localhost/showPool" pool_key = ["10.0.0.6", "9d2b45c4efaa478481c30340b49fd4d2", ["00efc78c-f11c-414a-bfcd-a82e16dc07d1", "fd6b13dc-7230-4cbe-9237-36b4614bc6b5"]] body = jsonutils.dumps({"pool_key": pool_key}) headers = {'Content-Type': 'application/json', 'Connection': 'close'} headers['Content-Length'] = len(body) trigger_exception = False formated_key = (pool_key[0], pool_key[1], tuple(sorted(pool_key[2]))) expected_resp = ('Pool {0} ports are:\n{1}' .format(formated_key, method_resp)).encode() self._do_GET_helper(method, method_resp, path, headers, body, expected_resp, trigger_exception, pool_key)
Example #3
Source File: test_report.py From zun with Apache License 2.0 | 6 votes |
def test_set_aggregates_for_provider_no_short_circuit(self): """Don't short-circuit if generation doesn't match, even if aggs have not changed. """ # Prime the provider tree cache self.client._provider_tree.new_root('rp', uuids.rp, generation=2) self.ks_adap_mock.put.return_value = fake_requests.FakeResponse( 200, content=jsonutils.dumps({ 'aggregates': [], 'resource_provider_generation': 5})) self.client.set_aggregates_for_provider(self.context, uuids.rp, [], generation=4) exp_payload = {'aggregates': [], 'resource_provider_generation': 4} self.ks_adap_mock.put.assert_called_once_with( '/resource_providers/%s/aggregates' % uuids.rp, json=exp_payload, microversion='1.19', endpoint_filter=mock.ANY, logger=mock.ANY, headers={'X-Openstack-Request-Id': self.context.global_id}) # Cache was updated ptree_data = self.client._provider_tree.data(uuids.rp) self.assertEqual(set(), ptree_data.aggregates) self.assertEqual(5, ptree_data.generation)
Example #4
Source File: test_pool.py From kuryr-kubernetes with Apache License 2.0 | 6 votes |
def test_do_GET_show_exception(self): method = 'show' method_resp = "251f748d-2a0d-4143-bce8-2e616f7a6a4a" path = "http://localhost/showPool" pool_key = ["10.0.0.6", "9d2b45c4efaa478481c30340b49fd4d2", ["00efc78c-f11c-414a-bfcd-a82e16dc07d1", "fd6b13dc-7230-4cbe-9237-36b4614bc6b5"]] body = jsonutils.dumps({"pool_key": pool_key}) headers = {'Content-Type': 'application/json', 'Connection': 'close'} headers['Content-Length'] = len(body) trigger_exception = True formated_key = (pool_key[0], pool_key[1], tuple(sorted(pool_key[2]))) expected_resp = ('Error showing pool: {0}.' .format(formated_key)).encode() self._do_GET_helper(method, method_resp, path, headers, body, expected_resp, trigger_exception, pool_key)
Example #5
Source File: test_report.py From zun with Apache License 2.0 | 6 votes |
def test_set_aggregates_for_provider(self): aggs = [uuids.agg1, uuids.agg2] self.ks_adap_mock.put.return_value = fake_requests.FakeResponse( 200, content=jsonutils.dumps({ 'aggregates': aggs, 'resource_provider_generation': 1})) # Prime the provider tree cache self.client._provider_tree.new_root('rp', uuids.rp, generation=0) self.assertEqual(set(), self.client._provider_tree.data(uuids.rp).aggregates) self.client.set_aggregates_for_provider(self.context, uuids.rp, aggs) exp_payload = {'aggregates': aggs, 'resource_provider_generation': 0} self.ks_adap_mock.put.assert_called_once_with( '/resource_providers/%s/aggregates' % uuids.rp, json=exp_payload, microversion='1.19', endpoint_filter=mock.ANY, logger=mock.ANY, headers={'X-Openstack-Request-Id': self.context.global_id}) # Cache was updated ptree_data = self.client._provider_tree.data(uuids.rp) self.assertEqual(set(aggs), ptree_data.aggregates) self.assertEqual(1, ptree_data.generation)
Example #6
Source File: test_pool.py From kuryr-kubernetes with Apache License 2.0 | 6 votes |
def test_do_GET_show(self): method = 'show' method_resp = "251f748d-2a0d-4143-bce8-2e616f7a6a4a" path = "http://localhost/showPool" pool_key = ["10.0.0.6", "9d2b45c4efaa478481c30340b49fd4d2", ["00efc78c-f11c-414a-bfcd-a82e16dc07d1", "fd6b13dc-7230-4cbe-9237-36b4614bc6b5"]] body = jsonutils.dumps({"pool_key": pool_key}) headers = {'Content-Type': 'application/json', 'Connection': 'close'} headers['Content-Length'] = len(body) trigger_exception = False formated_key = (pool_key[0], pool_key[1], tuple(sorted(pool_key[2]))) expected_resp = ('Pool {0} ports are:\n{1}' .format(formated_key, method_resp)).encode() self._do_GET_helper(method, method_resp, path, headers, body, expected_resp, trigger_exception, pool_key)
Example #7
Source File: test_report.py From zun with Apache License 2.0 | 6 votes |
def _test_remove_res_from_alloc( self, current_allocations, resources_to_remove, updated_allocations): with zun_utils.nested_contexts( mock.patch( "zun.scheduler.client.report.SchedulerReportClient.get"), mock.patch( "zun.scheduler.client.report.SchedulerReportClient.put") ) as (mock_get, mock_put): mock_get.return_value = fake_requests.FakeResponse( 200, content=jsonutils.dumps(current_allocations)) self.client.remove_resources_from_container_allocation( self.context, uuids.consumer_uuid, resources_to_remove) mock_get.assert_called_once_with( '/allocations/%s' % uuids.consumer_uuid, version='1.28', global_request_id=self.context.global_id) mock_put.assert_called_once_with( '/allocations/%s' % uuids.consumer_uuid, updated_allocations, version='1.28', global_request_id=self.context.global_id)
Example #8
Source File: test_pool.py From kuryr-kubernetes with Apache License 2.0 | 6 votes |
def test_do_GET_list_exception(self): method = 'list' method_resp = ('["10.0.0.6", "9d2b45c4efaa478481c30340b49fd4d2", ' '["00efc78c-f11c-414a-bfcd-a82e16dc07d1", ' '"fd6b13dc-7230-4cbe-9237-36b4614bc6b5"]] ' 'has 5 ports') path = "http://localhost/listPools" body = jsonutils.dumps({}) headers = {'Content-Type': 'application/json', 'Connection': 'close'} headers['Content-Length'] = len(body) trigger_exception = True expected_resp = ('Error listing the pools.').encode() self._do_GET_helper(method, method_resp, path, headers, body, expected_resp, trigger_exception)
Example #9
Source File: test_driver.py From zun with Apache License 2.0 | 6 votes |
def setUp(self): super(CinderVolumeDriverTestCase, self).setUp() self.fake_uuid = uuidutils.generate_uuid() self.fake_volume_id = 'fake-volume-id' self.fake_devpath = '/fake-path' self.fake_mountpoint = '/fake-mountpoint' self.fake_container_path = '/fake-container-path' self.fake_conn_info = { 'data': {'device_path': self.fake_devpath}, } self.volmap = mock.MagicMock() self.volmap.volume.uuid = self.fake_uuid self.volmap.volume_provider = 'cinder' self.volmap.volume_id = self.fake_volume_id self.volmap.container_path = self.fake_container_path self.volmap.connection_info = jsonutils.dumps(self.fake_conn_info)
Example #10
Source File: test_vif_pool.py From kuryr-kubernetes with Apache License 2.0 | 6 votes |
def test__get_in_use_ports(self): cls = vif_pool.BaseVIFPool m_driver = mock.MagicMock(spec=cls) kubernetes = self.useFixture(k_fix.MockK8sClient()).client pod = get_pod_obj() port_id = str(uuid.uuid4()) pod_vif = osv_vif.VIFBase(id=port_id) pod_state = vif.PodState(default_vif=pod_vif) pod['metadata']['annotations'][constants.K8S_ANNOTATION_VIF] = ( jsonutils.dumps(pod_state.obj_to_primitive())) items = [pod] kubernetes.get.return_value = {'items': items} resp = cls._get_in_use_ports(m_driver) self.assertEqual(resp, [port_id])
Example #11
Source File: host_capability.py From zun with Apache License 2.0 | 6 votes |
def get_pci_resources(self): addresses = [] try: output, status = utils.execute('lspci', '-D', '-nnmm') lines = output.split('\n') for line in lines: if not line: continue columns = line.split() address = columns[0] addresses.append(address) except processutils.ProcessExecutionError as e: raise exception.CommandError(cmd='lspci', error=str(e)) pci_info = [] for addr in addresses: pci_info.append(self._get_pci_dev_info(addr)) return jsonutils.dumps(pci_info)
Example #12
Source File: service.py From zun with Apache License 2.0 | 6 votes |
def add(self): try: params = self._prepare_request() except Exception: LOG.exception('Exception when reading CNI params.') return '', httplib.BAD_REQUEST, self.headers try: vif = self.plugin.add(params) data = jsonutils.dumps(vif.obj_to_primitive()) except exception.ResourceNotReady: LOG.error('Error when processing addNetwork request') return '', httplib.GATEWAY_TIMEOUT, self.headers except Exception: LOG.exception('Error when processing addNetwork request. CNI ' 'Params: %s', params) return '', httplib.INTERNAL_SERVER_ERROR, self.headers return data, httplib.ACCEPTED, self.headers
Example #13
Source File: test_k8s_client.py From kuryr-kubernetes with Apache License 2.0 | 6 votes |
def test_annotate_resource_not_found(self, m_patch, m_count): m_count.return_value = list(range(1, 5)) path = '/test' annotations = {'a1': 'v1', 'a2': 'v2'} resource_version = "123" annotate_obj = {'metadata': { 'annotations': annotations, 'resourceVersion': resource_version}} annotate_data = jsonutils.dumps(annotate_obj, sort_keys=True) m_resp_not_found = mock.MagicMock() m_resp_not_found.ok = False m_resp_not_found.status_code = requests.codes.not_found m_patch.return_value = m_resp_not_found self.assertRaises(exc.K8sResourceNotFound, self.client.annotate, path, annotations, resource_version=resource_version) m_patch.assert_called_once_with(self.base_url + path, data=annotate_data, headers=mock.ANY, cert=(None, None), verify=False)
Example #14
Source File: test_pool.py From kuryr-kubernetes with Apache License 2.0 | 6 votes |
def test_do_POST_populate(self): method = 'create' path = "http://localhost/populatePool" trunk_ips = ["10.0.0.6"] num_ports = 3 body = jsonutils.dumps({"trunks": trunk_ips, "num_ports": num_ports}) headers = {'Content-Type': 'application/json', 'Connection': 'close'} headers['Content-Length'] = len(body) trigger_exception = False expected_resp = ('Ports pool at {} was populated with 3 ports.' .format(trunk_ips)).encode() self._do_POST_helper(method, path, headers, body, expected_resp, trigger_exception, trunk_ips, num_ports)
Example #15
Source File: test_k8s_client.py From kuryr-kubernetes with Apache License 2.0 | 6 votes |
def test_annotate(self, m_patch, m_count): m_count.return_value = list(range(1, 5)) path = '/test' annotations = {'a1': 'v1', 'a2': 'v2'} resource_version = "123" ret = {'metadata': {'annotations': annotations, "resourceVersion": resource_version}} data = jsonutils.dumps(ret, sort_keys=True) m_resp = mock.MagicMock() m_resp.ok = True m_resp.json.return_value = ret m_patch.return_value = m_resp self.assertEqual(annotations, self.client.annotate( path, annotations, resource_version=resource_version)) m_patch.assert_called_once_with(self.base_url + path, data=data, headers=mock.ANY, cert=(None, None), verify=False)
Example #16
Source File: utils.py From ara-archive with GNU General Public License v3.0 | 6 votes |
def playbook_treeview(playbook): """ Creates a fake filesystem with playbook files and uses generate_tree() to recurse and return a JSON structure suitable for bootstrap-treeview. """ fs = fake_filesystem.FakeFilesystem() mock_os = fake_filesystem.FakeOsModule(fs) files = models.File.query.filter(models.File.playbook_id.in_([playbook])) paths = {} for file in files: fs.CreateFile(file.path) paths[file.path] = file.id return jsonutils.dumps(generate_tree('/', paths, mock_os), sort_keys=True, indent=2)
Example #17
Source File: pool.py From kuryr-kubernetes with Apache License 2.0 | 6 votes |
def _list_pools(self): try: drv_vif = drivers.PodVIFDriver.get_instance() drv_vif_pool = drivers.VIFPoolDriver.get_instance() drv_vif_pool.set_vif_driver(drv_vif) available_pools = drv_vif_pool.list_pools() except TypeError: LOG.error("Invalid driver type") raise pools_info = "" for pool_key, pool_items in available_pools.items(): pools_info += (jsonutils.dumps(pool_key) + " has " + str(len(pool_items)) + " ports\n") if pools_info: return pools_info return "There are no pools"
Example #18
Source File: k8s_client.py From kuryr-kubernetes with Apache License 2.0 | 6 votes |
def patch_crd(self, field, path, data, action='replace'): content_type = 'application/json-patch+json' url, header = self._get_url_and_header(path, content_type) if action == 'remove': data = [{'op': action, 'path': f'/{field}/{data}'}] else: data = [{'op': action, 'path': f'/{field}/{crd_field}', 'value': value} for crd_field, value in data.items()] LOG.debug("Patch %(path)s: %(data)s", { 'path': path, 'data': data}) response = self.session.patch(url, data=jsonutils.dumps(data), headers=header, cert=self.cert, verify=self.verify_server) self._raise_from_response(response) return response.json().get('status')
Example #19
Source File: subports.py From kuryr-kubernetes with Apache License 2.0 | 5 votes |
def show_pool(trunk_ip, project_id, sg, timeout=180): method = 'GET' body = jsonutils.dumps({"pool_key": [trunk_ip, project_id, sg]}) headers = {'Context-Type': 'application/json', 'Connection': 'close'} headers['Context-Length'] = len(body) path = 'http://localhost{0}'.format(constants.VIF_POOL_SHOW) socket_path = constants.MANAGER_SOCKET_FILE conn = UnixDomainHttpConnection(socket_path, timeout) conn.request(method, path, body=body, headers=headers) resp = conn.getresponse() print(resp.read())
Example #20
Source File: fake.py From kuryr-kubernetes with Apache License 2.0 | 5 votes |
def _fake_vifs_string(dictionary=None): if dictionary: return jsonutils.dumps(dictionary) else: return jsonutils.dumps(_fake_vifs_dict())
Example #21
Source File: subports.py From kuryr-kubernetes with Apache License 2.0 | 5 votes |
def delete_subports(trunk_ips, timeout=180): method = 'POST' body = jsonutils.dumps({"trunks": trunk_ips}) headers = {'Content-Type': 'application/json', 'Connection': 'close'} headers['Content-Length'] = len(body) path = 'http://localhost{0}'.format(constants.VIF_POOL_FREE) socket_path = constants.MANAGER_SOCKET_FILE conn = UnixDomainHttpConnection(socket_path, timeout) conn.request(method, path, body=body, headers=headers) resp = conn.getresponse() print(resp.read())
Example #22
Source File: subports.py From kuryr-kubernetes with Apache License 2.0 | 5 votes |
def create_subports(num_ports, trunk_ips, timeout=180): method = 'POST' body = jsonutils.dumps({"trunks": trunk_ips, "num_ports": num_ports}) headers = {'Content-Type': 'application/json', 'Connection': 'close'} headers['Content-Length'] = len(body) path = 'http://localhost{0}'.format(constants.VIF_POOL_POPULATE) socket_path = constants.MANAGER_SOCKET_FILE conn = UnixDomainHttpConnection(socket_path, timeout) conn.request(method, path, body=body, headers=headers) resp = conn.getresponse() print(resp.read())
Example #23
Source File: subports.py From kuryr-kubernetes with Apache License 2.0 | 5 votes |
def list_pools(timeout=180): method = 'GET' body = jsonutils.dumps({}) headers = {'Context-Type': 'application/json', 'Connection': 'close'} headers['Context-Length'] = len(body) path = 'http://localhost{0}'.format(constants.VIF_POOL_LIST) socket_path = constants.MANAGER_SOCKET_FILE conn = UnixDomainHttpConnection(socket_path, timeout) conn.request(method, path, body=body, headers=headers) resp = conn.getresponse() print(resp.read())
Example #24
Source File: pod_label.py From kuryr-kubernetes with Apache License 2.0 | 5 votes |
def _set_pod_labels(self, pod, labels): if not labels: LOG.debug("Removing Label annotation: %r", labels) annotation = None else: annotation = jsonutils.dumps(labels, sort_keys=True) LOG.debug("Setting Labels annotation: %r", annotation) k8s = clients.get_kubernetes_client() k8s.annotate(pod['metadata']['selfLink'], {constants.K8S_ANNOTATION_LABEL: annotation}, resource_version=pod['metadata']['resourceVersion'])
Example #25
Source File: fake.py From kuryr-kubernetes with Apache License 2.0 | 5 votes |
def _fake_vif_string(dictionary=None): if dictionary: return jsonutils.dumps(dictionary) else: return jsonutils.dumps(_fake_vif_dict())
Example #26
Source File: vif.py From kuryr-kubernetes with Apache License 2.0 | 5 votes |
def _set_pod_state(self, pod, state): # TODO(ivc): extract annotation interactions if not state: old_annotation = pod['metadata'].get('annotations', {}) LOG.debug("Removing VIFs annotation: %r for pod %s/%s (uid: %s)", old_annotation.get(constants.K8S_ANNOTATION_VIF), pod['metadata']['namespace'], pod['metadata']['name'], pod['metadata']['uid']) annotation = None else: state_dict = state.obj_to_primitive() annotation = jsonutils.dumps(state_dict, sort_keys=True) LOG.debug("Setting VIFs annotation: %r for pod %s/%s (uid: %s)", annotation, pod['metadata']['namespace'], pod['metadata']['name'], pod['metadata']['uid']) labels = pod['metadata'].get('labels') if not labels: LOG.debug("Removing Label annotation: %r", labels) labels_annotation = None else: labels_annotation = jsonutils.dumps(labels, sort_keys=True) LOG.debug("Setting Labels annotation: %r", labels_annotation) # NOTE(dulek): We don't care about compatibility with Queens format # here, as eventually all Kuryr services will be upgraded # and cluster will start working normally. Meanwhile # we just ignore issue of old services being unable to # read new annotations. k8s = clients.get_kubernetes_client() k8s.annotate(pod['metadata']['selfLink'], {constants.K8S_ANNOTATION_VIF: annotation, constants.K8S_ANNOTATION_LABEL: labels_annotation}, resource_version=pod['metadata']['resourceVersion'])
Example #27
Source File: test_status.py From kuryr-kubernetes with Apache License 2.0 | 5 votes |
def _test__convert_annotations(self, method, calls): self.cmd.k8s.annotate = mock.Mock() ann_objs = [ ('foo', vif.PodState(default_vif=vif.VIFMacvlanNested(vif_name='foo'))), ('bar', vif.VIFMacvlanNested(vif_name='bar')), ] ann_objs = [(name, jsonutils.dumps(ann.obj_to_primitive())) for name, ann in ann_objs] pods = { 'items': [ { 'metadata': { 'annotations': { constants.K8S_ANNOTATION_VIF: ann }, 'selfLink': name, 'resourceVersion': 1, } } for name, ann in ann_objs ] } self.cmd.k8s = mock.Mock(get=mock.Mock(return_value=pods)) method() for args in calls: self.cmd.k8s.annotate.assert_any_call(*args)
Example #28
Source File: test_k8s_client.py From kuryr-kubernetes with Apache License 2.0 | 5 votes |
def test_annotate_diff_resource_vers_no_conflict(self, m_patch, m_count): m_count.return_value = list(range(1, 5)) path = '/test' annotations = {'a1': 'v1', 'a2': 'v2'} resource_version = "123" new_resource_version = "456" conflicting_obj = {'metadata': { 'annotations': annotations, 'resourceVersion': resource_version}} good_obj = {'metadata': { 'annotations': annotations, 'resourceVersion': new_resource_version}} conflicting_data = jsonutils.dumps(conflicting_obj, sort_keys=True) m_resp_conflict = mock.MagicMock() m_resp_conflict.ok = False m_resp_conflict.status_code = requests.codes.conflict m_resp_good = mock.MagicMock() m_resp_good.ok = True m_resp_good.json.return_value = conflicting_obj m_patch.side_effect = [m_resp_conflict, m_resp_good] with mock.patch.object(self.client, 'get') as m_get: m_get.return_value = good_obj self.assertEqual(annotations, self.client.annotate( path, annotations, resource_version=resource_version)) m_patch.assert_has_calls([ mock.call(self.base_url + path, data=conflicting_data, headers=mock.ANY, cert=(None, None), verify=False)])
Example #29
Source File: log_ara.py From ara-archive with GNU General Public License v3.0 | 5 votes |
def v2_playbook_on_task_start(self, task, is_conditional, is_handler=False): self.close_task() LOG.debug('starting task %s (action %s)', task.name, task.action) pathspec = task.get_path() if pathspec: path, lineno = pathspec.split(':', 1) lineno = int(lineno) file_ = self.get_or_create_file(path) else: path = self.playbook.path lineno = 1 file_ = self.get_or_create_file(self.playbook.path) self.task = models.Task( name=task.get_name(), sortkey=next(self.task_counter), action=task.action, play=self.play, playbook=self.playbook, tags=jsonutils.dumps(task._attributes['tags']), file=file_, lineno=lineno, is_handler=is_handler) self.task.start() db.session.add(self.task) db.session.commit()
Example #30
Source File: test_k8s_client.py From kuryr-kubernetes with Apache License 2.0 | 5 votes |
def test_annotate_diff_resource_vers_conflict(self, m_patch, m_count): m_count.return_value = list(range(1, 5)) path = '/test' annotations = {'a1': 'v1', 'a2': 'v2'} resource_version = "123" new_resource_version = "456" conflicting_obj = {'metadata': { 'annotations': annotations, 'resourceVersion': resource_version}} actual_obj = {'metadata': { 'annotations': {'a1': 'v2'}, 'resourceVersion': new_resource_version}} good_obj = {'metadata': { 'annotations': annotations, 'resourceVersion': new_resource_version}} conflicting_data = jsonutils.dumps(conflicting_obj, sort_keys=True) good_data = jsonutils.dumps(good_obj, sort_keys=True) m_resp_conflict = mock.MagicMock() m_resp_conflict.ok = False m_resp_conflict.status_code = requests.codes.conflict m_patch.return_value = m_resp_conflict m_resp_good = mock.MagicMock() m_resp_good.ok = True m_resp_good.json.return_value = conflicting_obj m_patch.side_effect = [m_resp_conflict, m_resp_good] with mock.patch.object(self.client, 'get') as m_get: m_get.return_value = actual_obj self.assertEqual(annotations, self.client.annotate( path, annotations, resource_version=resource_version)) m_patch.assert_has_calls([ mock.call(self.base_url + path, data=conflicting_data, headers=mock.ANY, cert=(None, None), verify=False), mock.call(self.base_url + path, data=good_data, headers=mock.ANY, cert=(None, None), verify=False)])