本文整理汇总了Python中oslo_serialization.jsonutils.dump_as_bytes方法的典型用法代码示例。如果您正苦于以下问题:Python jsonutils.dump_as_bytes方法的具体用法?Python jsonutils.dump_as_bytes怎么用?Python jsonutils.dump_as_bytes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类oslo_serialization.jsonutils
的用法示例。
在下文中一共展示了jsonutils.dump_as_bytes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __call__
# 需要导入模块: from oslo_serialization import jsonutils [as 别名]
# 或者: from oslo_serialization.jsonutils import dump_as_bytes [as 别名]
def __call__(self, environ, start_response):
for err_str in self.app_iter:
err = {}
try:
err = json.loads(err_str.decode('utf-8'))
except ValueError:
pass
links = {'rel': 'help', 'href': 'https://docs.openstack.org'
'/api-guide/compute/microversions.html'}
err['max_version'] = self.max_version
err['min_version'] = self.min_version
err['code'] = "zun.microversion-unsupported"
err['links'] = [links]
err['title'] = "Requested microversion is unsupported"
self.app_iter = [json.dump_as_bytes(err).encode("latin-1")]
self.headers['Content-Length'] = str(len(self.app_iter[0]))
return super(HTTPNotAcceptableAPIVersion, self).__call__(
environ, start_response)
示例2: test_watch
# 需要导入模块: from oslo_serialization import jsonutils [as 别名]
# 或者: from oslo_serialization.jsonutils import dump_as_bytes [as 别名]
def test_watch(self, m_get):
path = '/test'
data = [{'obj': 'obj%s' % i} for i in range(3)]
lines = [jsonutils.dump_as_bytes(i) for i in data]
m_resp = mock.MagicMock()
m_resp.ok = True
m_resp.iter_lines.return_value = lines
m_get.return_value = m_resp
cycles = 3
self.assertEqual(
data * cycles,
list(itertools.islice(self.client.watch(path),
len(data) * cycles)))
self.assertEqual(cycles, m_get.call_count)
self.assertEqual(cycles, m_resp.close.call_count)
m_get.assert_called_with(self.base_url + path, headers={}, stream=True,
params={'watch': 'true'}, cert=(None, None),
verify=False, timeout=(30, 60))
示例3: test_watch_restart
# 需要导入模块: from oslo_serialization import jsonutils [as 别名]
# 或者: from oslo_serialization.jsonutils import dump_as_bytes [as 别名]
def test_watch_restart(self, m_get):
path = '/test'
data = [{'object': {'metadata': {'name': 'obj%s' % i,
'resourceVersion': i}}}
for i in range(3)]
lines = [jsonutils.dump_as_bytes(i) for i in data]
m_resp = mock.MagicMock()
m_resp.ok = True
m_resp.iter_lines.side_effect = [lines, requests.ReadTimeout, lines]
m_get.return_value = m_resp
self.assertEqual(data * 2,
list(itertools.islice(self.client.watch(path),
len(data) * 2)))
self.assertEqual(3, m_get.call_count)
self.assertEqual(3, m_resp.close.call_count)
m_get.assert_any_call(
self.base_url + path, headers={}, stream=True,
params={"watch": "true"}, cert=(None, None), verify=False,
timeout=(30, 60))
m_get.assert_any_call(
self.base_url + path, headers={}, stream=True,
params={"watch": "true", "resourceVersion": 2}, cert=(None, None),
verify=False, timeout=(30, 60))
示例4: test_StatsRequestHandler_handle
# 需要导入模块: from oslo_serialization import jsonutils [as 别名]
# 或者: from oslo_serialization.jsonutils import dump_as_bytes [as 别名]
def test_StatsRequestHandler_handle(self, mock_recv, mock_driverupdater):
TEST_OBJECT = {"test": "msg"}
mock_recv.return_value = 'bogus'
mock_updater = mock.MagicMock()
mock_update_listener_stats = mock.MagicMock()
mock_update_listener_stats.return_value = TEST_OBJECT
mock_updater.update_listener_statistics = (mock_update_listener_stats)
mock_driverupdater.return_value = mock_updater
mock_request = mock.MagicMock()
mock_send = mock.MagicMock()
mock_sendall = mock.MagicMock()
mock_request.send = mock_send
mock_request.sendall = mock_sendall
StatsRequestHandler = driver_listener.StatsRequestHandler(
mock_request, 'bogus', 'bogus')
StatsRequestHandler.handle()
mock_recv.assert_called_with(mock_request)
mock_update_listener_stats.assert_called_with('bogus')
mock_send.assert_called_with(b'15\n')
mock_sendall.assert_called_with(jsonutils.dump_as_bytes(TEST_OBJECT))
示例5: test_GetRequestHandler_handle
# 需要导入模块: from oslo_serialization import jsonutils [as 别名]
# 或者: from oslo_serialization.jsonutils import dump_as_bytes [as 别名]
def test_GetRequestHandler_handle(self, mock_recv, mock_process_get):
TEST_OBJECT = {"test": "msg"}
mock_recv.return_value = 'bogus'
mock_process_get.return_value = TEST_OBJECT
mock_request = mock.MagicMock()
mock_send = mock.MagicMock()
mock_sendall = mock.MagicMock()
mock_request.send = mock_send
mock_request.sendall = mock_sendall
GetRequestHandler = driver_listener.GetRequestHandler(
mock_request, 'bogus', 'bogus')
GetRequestHandler.handle()
mock_recv.assert_called_with(mock_request)
mock_process_get.assert_called_with('bogus')
mock_send.assert_called_with(b'15\n')
mock_sendall.assert_called_with(jsonutils.dump_as_bytes(TEST_OBJECT))
示例6: __call__
# 需要导入模块: from oslo_serialization import jsonutils [as 别名]
# 或者: from oslo_serialization.jsonutils import dump_as_bytes [as 别名]
def __call__(self, environ, start_response):
for err_str in self.app_iter:
err = {}
try:
err = jsonutils.loads(err_str.decode('utf-8'))
except ValueError:
pass
links = {'rel': 'help', 'href': 'http://docs.openstack.org'
'/api-guide/compute/microversions.html'}
err['max_version'] = self.max_version
err['min_version'] = self.min_version
err['code'] = "magnum.microversion-unsupported"
err['links'] = [links]
err['title'] = "Requested microversion is unsupported"
self.app_iter = [jsonutils.dump_as_bytes(err)]
self.headers['Content-Length'] = str(len(self.app_iter[0]))
return super(HTTPNotAcceptableAPIVersion, self).__call__(
environ, start_response)
示例7: test_create_success_with_201_response_code
# 需要导入模块: from oslo_serialization import jsonutils [as 别名]
# 或者: from oslo_serialization.jsonutils import dump_as_bytes [as 别名]
def test_create_success_with_201_response_code(
self, mock_client, mock_create):
body = {
"notification": {
"hostname": "fake_host",
"payload": {
"instance_uuid": uuidsentinel.instance_uuid,
"vir_domain_event": "STOPPED_FAILED",
"event": "LIFECYCLE"
},
"type": "VM",
"generated_time": NOW
}
}
fake_req = self.req
fake_req.headers['Content-Type'] = 'application/json'
fake_req.method = 'POST'
fake_req.body = jsonutils.dump_as_bytes(body)
resp = fake_req.get_response(self.app)
self.assertEqual(http.ACCEPTED, resp.status_code)
示例8: test_create_success_with_201_response_code
# 需要导入模块: from oslo_serialization import jsonutils [as 别名]
# 或者: from oslo_serialization.jsonutils import dump_as_bytes [as 别名]
def test_create_success_with_201_response_code(
self, mock_client, mock_create):
body = {
"host": {
"name": "host-1", "type": "fake",
"reserved": False,
"on_maintenance": False,
"control_attributes": "fake-control_attributes"
}
}
fake_req = self.req
fake_req.headers['Content-Type'] = 'application/json'
fake_req.method = 'POST'
fake_req.body = jsonutils.dump_as_bytes(body)
resp = fake_req.get_response(self.app)
self.assertEqual(http.CREATED, resp.status_code)
示例9: test_create_success_with_201_response_code
# 需要导入模块: from oslo_serialization import jsonutils [as 别名]
# 或者: from oslo_serialization.jsonutils import dump_as_bytes [as 别名]
def test_create_success_with_201_response_code(
self, mock_client, mock_create):
body = {
"segment": {
"name": "segment1",
"service_type": "COMPUTE",
"recovery_method": "auto",
"description": "failover_segment for compute"
}
}
fake_req = self.req
fake_req.headers['Content-Type'] = 'application/json'
fake_req.method = 'POST'
fake_req.body = jsonutils.dump_as_bytes(body)
resp = fake_req.get_response(self.app)
self.assertEqual(http.CREATED, resp.status_code)
示例10: get_token_from_zbxserver
# 需要导入模块: from oslo_serialization import jsonutils [as 别名]
# 或者: from oslo_serialization.jsonutils import dump_as_bytes [as 别名]
def get_token_from_zbxserver(self, node):
temp_auth_api = copy.deepcopy(zapi.dAUTH_API)
temp_auth_api['params']['user'] = \
self.hostinfo[node]['zbx_info']['zabbix_user']
temp_auth_api['params']['password'] = \
self.hostinfo[node]['zbx_info']['zabbix_pass']
zabbixip = \
self.hostinfo[node]['zbx_info']['zabbix_ip']
zabbixport = \
self.hostinfo[node]['zbx_info']['zabbix_port']
self.URL = "http://" + zabbixip + ":" + \
str(zabbixport) + zapi.URL
if netaddr.valid_ipv6(zabbixip):
self.URL = "http://[" + zabbixip + "]:" + \
str(zabbixport) + zapi.URL
response = requests.post(
self.URL,
headers=zapi.HEADERS,
data=jsonutils.dump_as_bytes(temp_auth_api)
)
response_dict = dict(response.json())
VNFMonitorZabbix.check_error(response_dict)
LOG.info('Success Connect Zabbix Server')
return response_dict['result']
示例11: generate_hot
# 需要导入模块: from oslo_serialization import jsonutils [as 别名]
# 或者: from oslo_serialization.jsonutils import dump_as_bytes [as 别名]
def generate_hot(self):
self._get_vnfd()
dev_attrs = self._update_fields()
vnfd_dict = yamlparser.simple_ordered_parse(self.vnfd_yaml)
LOG.debug('vnfd_dict %s', vnfd_dict)
self._get_unsupported_resource_props(self.heatclient)
self._generate_hot_from_tosca(vnfd_dict, dev_attrs,
self.inst_req_info, self.grant_info)
self.fields['template'] = self.heat_template_yaml
if not self.vnf['attributes'].get('heat_template'):
self.vnf['attributes']['heat_template'] = self.fields['template']
if self.monitoring_dict:
self.vnf['attributes'][
'monitoring_policy'] = jsonutils.dump_as_bytes(
self.monitoring_dict)
if self.appmonitoring_dict:
self.vnf['attributes']['app_monitoring_policy'] = \
jsonutils.dump_as_bytes(self.appmonitoring_dict)
示例12: create_wait
# 需要导入模块: from oslo_serialization import jsonutils [as 别名]
# 或者: from oslo_serialization.jsonutils import dump_as_bytes [as 别名]
def create_wait(self, plugin, context, vnf_dict, vnf_id, auth_attr):
region_name = vnf_dict.get('placement_attr', {}).get(
'region_name', None)
heatclient = hc.HeatClient(auth_attr, region_name)
stack = self._wait_until_stack_ready(
vnf_id, auth_attr, infra_cnst.STACK_CREATE_IN_PROGRESS,
infra_cnst.STACK_CREATE_COMPLETE,
vnfm.VNFCreateWaitFailed, region_name=region_name)
# scaling enabled
if vnf_dict['attributes'].get('scaling_group_names'):
group_names = jsonutils.loads(
vnf_dict['attributes'].get('scaling_group_names')).values()
mgmt_ips = self._find_mgmt_ips_from_groups(heatclient,
vnf_id,
group_names)
else:
mgmt_ips = self._find_mgmt_ips(stack.outputs)
if mgmt_ips:
vnf_dict['mgmt_ip_address'] = jsonutils.dump_as_bytes(mgmt_ips)
示例13: test_process_request_with_body
# 需要导入模块: from oslo_serialization import jsonutils [as 别名]
# 或者: from oslo_serialization.jsonutils import dump_as_bytes [as 别名]
def test_process_request_with_body(self, mock_create_token):
req = Request.blank(self.ordered_url)
req.method = 'POST'
old_body = {'fake_key': 'fake_value'}
req.body = jsonutils.dump_as_bytes(old_body)
self.alarmrc.process_request(req)
body_dict = jsonutils.loads(req.body)
self.assertDictEqual(old_body,
body_dict['trigger']['params']['data'])
self.assertEqual(self.alarm_url['05_key'],
body_dict['trigger']['params']['credential'])
self.assertEqual(self.alarm_url['03_monitoring_policy_name'],
body_dict['trigger']['policy_name'])
self.assertEqual(self.alarm_url['04_action_name'],
body_dict['trigger']['action_name'])
示例14: test_upload_vnf_package_content
# 需要导入模块: from oslo_serialization import jsonutils [as 别名]
# 或者: from oslo_serialization.jsonutils import dump_as_bytes [as 别名]
def test_upload_vnf_package_content(self, mock_vnf_pack_save,
mock_vnf_by_id,
mock_upload_vnf_package_content,
mock_glance_store):
updates = {'onboarding_state': 'CREATED',
'operational_state': 'DISABLED'}
vnf_package_dict = fakes.fake_vnf_package(updates)
vnf_package_obj = objects.VnfPackage(**vnf_package_dict)
mock_vnf_by_id.return_value = vnf_package_obj
mock_vnf_pack_save.return_value = vnf_package_obj
mock_glance_store.return_value = 'location', 0, 'checksum',\
'multihash', 'loc_meta'
req = fake_request.HTTPRequest.blank(
'/vnf_packages/%s/package_content'
% constants.UUID)
req.headers['Content-Type'] = 'application/zip'
req.method = 'PUT'
req.body = jsonutils.dump_as_bytes(mock.mock_open())
resp = req.get_response(self.app)
mock_glance_store.assert_called()
self.assertEqual(http_client.ACCEPTED, resp.status_code)
示例15: test_upload_vnf_package_from_uri
# 需要导入模块: from oslo_serialization import jsonutils [as 别名]
# 或者: from oslo_serialization.jsonutils import dump_as_bytes [as 别名]
def test_upload_vnf_package_from_uri(self, mock_vnf_pack_save,
mock_vnf_by_id,
mock_upload_vnf_package_from_uri,
mock_url_open):
body = {"addressInformation": "http://localhost/test_data.zip"}
updates = {'onboarding_state': 'CREATED',
'operational_state': 'DISABLED'}
vnf_package_dict = fakes.fake_vnf_package(updates)
vnf_package_obj = objects.VnfPackage(**vnf_package_dict)
mock_vnf_by_id.return_value = vnf_package_obj
mock_vnf_pack_save.return_value = vnf_package_obj
req = fake_request.HTTPRequest.blank(
'/vnf_packages/%s/package_content/upload_from_uri'
% constants.UUID)
req.headers['Content-Type'] = 'application/json'
req.method = 'POST'
req.body = jsonutils.dump_as_bytes(body)
resp = req.get_response(self.app)
self.assertEqual(http_client.ACCEPTED, resp.status_code)