def _invoke(self, service_id, operation_id, input_value, ctx, rest_metadata, # pylint: disable=W0613 is_vapi_rest): """ Invokes the specified method using the input value, execution context and rest metadata provided :type service_id: :class:`str` :param service_id: Service identifier :type operation_id: :class:`str` :param operation_id: Operation identifier :type input_value: :class:`vmware.vapi.data.value.DataValue` :param input_value: method input parameters :type ctx: :class:`vmware.vapi.core.ExecutionContext` :param ctx: execution context object :type rest_metadata: :class:`vmware.vapi.lib.rest.OperationRestMetadata` :param rest_metadata: Rest metadata for the operation :type is_vapi_rest: :class:`bool` :param is_vapi_rest: Whether the Rest json message format is VAPI Rest or not :rtype: :class:`vmware.vapi.core.MethodResult` :return: method result object """ http_method = rest_metadata.http_method (url_path, auth_headers, request_body_str, cookies) = \ RestSerializer.serialize_request(input_value, ctx, rest_metadata, is_vapi_rest) # Add headers # Accept header is needed because any operation can report an error and # hence could have a body. headers = { 'Accept': 'application/json', } if http_method not in [HTTPMethod.GET, HTTPMethod.HEAD]: # TODO Maybe add this as part of REST metadata headers['Content-Type'] = 'application/json' if auth_headers is not None: headers.update(auth_headers) request_logger.debug('_invoke: request url: %s', url_path) request_logger.debug('_invoke: request http method: %s', http_method) request_logger.debug('_invoke: request headers: %s', headers) request_logger.debug('_invoke: request body: %s', request_body_str) http_request = HTTPRequest(method=http_method, url_path=url_path, headers=headers, body=request_body_str, cookies=cookies) # TODO Add post processors #for processor in self.post_processors: # request_msg = processor.process(request_msg) http_response = self._http_provider.do_request(http_request) request_logger.debug( '_invoke: response status: %s', http_response.status) request_logger.debug('_invoke: response body: %s', http_response.body) method_result = RestSerializer.deserialize_response( http_response.status, http_response.body, is_vapi_rest) return method_result
def test_string_value(self): input_status = 200 input_response = '"vm-100"' expected_result = MethodResult(output=StringValue('vm-100')) actual_result = RestSerializer.deserialize_response( status=input_status, response_str=input_response, is_vapi_rest=False) self.assertEqual(expected_result.output, actual_result.output) self.assertEqual(expected_result.error, actual_result.error)
def test_successful_status_202(self): input_status = 202 input_response = '{"value":"vm-100"}' expected_result = MethodResult(output=StringValue('vm-100')) actual_result = RestSerializer.deserialize_response( status=input_status, response_str=input_response, is_vapi_rest=True) self.assertEqual(expected_result.output, actual_result.output) self.assertEqual(expected_result.error, actual_result.error)
def test_void_value(self): input_status = 200 input_response = '' expected_result = MethodResult(output=VoidValue()) actual_result = RestSerializer.deserialize_response( status=input_status, response_str=input_response, is_vapi_rest=True) self.assertEqual(expected_result.output, actual_result.output) self.assertEqual(expected_result.error, actual_result.error)
def test_struct_value(self): input_status = 200 input_response = '{"id":"vm-100", "name":"Linux VM"}' expected_result = MethodResult(output=StructValue( values={ 'id': StringValue('vm-100'), 'name': StringValue('Linux VM') })) actual_result = RestSerializer.deserialize_response( status=input_status, response_str=input_response, is_vapi_rest=False) self.assertEqual(expected_result.output, actual_result.output) self.assertEqual(expected_result.error, actual_result.error)
def test_not_found_error_value(self): input_status = 404 input_response = '{"type":"com.vmware.vapi.std.errors.not_found","value":{"messages":[{"args":["datacenter-21"],"default_message":"Datacenter with identifier \'datacenter-21\' does not exist.","id":"com.vmware.api.vcenter.datacenter.not_found"}]}}' msg_val = StructValue( values={ 'id': StringValue('com.vmware.api.vcenter.datacenter.not_found'), 'default_message': StringValue( 'Datacenter with identifier \'datacenter-21\' does not exist.' ), 'args': ListValue([StringValue('datacenter-21')]) }) error_val = ErrorValue('com.vmware.vapi.std.errors.not_found', {'messages': ListValue([msg_val])}) expected_result = MethodResult(error=error_val) logging.debug(expected_result) actual_result = RestSerializer.deserialize_response( status=input_status, response_str=input_response, is_vapi_rest=True) self.assertEqual(expected_result.output, actual_result.output) self.assertEqual(expected_result.error, actual_result.error)
def _invoke( self, service_id, operation_id, input_value, ctx, rest_metadata, # pylint: disable=W0613 is_vapi_rest): """ Invokes the specified method using the input value, execution context and rest metadata provided :type service_id: :class:`str` :param service_id: Service identifier :type operation_id: :class:`str` :param operation_id: Operation identifier :type input_value: :class:`vmware.vapi.data.value.DataValue` :param input_value: method input parameters :type ctx: :class:`vmware.vapi.core.ExecutionContext` :param ctx: execution context object :type rest_metadata: :class:`vmware.vapi.lib.rest.OperationRestMetadata` :param rest_metadata: Rest metadata for the operation :type is_vapi_rest: :class:`bool` :param is_vapi_rest: Whether the Rest json message format is VAPI Rest or not :rtype: :class:`vmware.vapi.core.MethodResult` :return: method result object """ http_method = rest_metadata.http_method (url_path, input_headers, request_body_str, cookies) = \ RestSerializer.serialize_request(input_value, ctx, rest_metadata, is_vapi_rest) # Add headers # Accept header is needed because any operation can report an error and # hence could have a body. headers = { HTTP_ACCEPT_HEADER: JSON_CONTENT_TYPE, HTTP_USER_AGENT_HEADER: get_user_agent(), } if input_headers is not None: headers.update(input_headers) if rest_metadata.content_type is not None: headers[HTTP_CONTENT_TYPE_HEADER] = rest_metadata.content_type elif http_method not in [HTTPMethod.GET, HTTPMethod.HEAD]: # TODO Maybe add this as part of REST metadata headers[HTTP_CONTENT_TYPE_HEADER] = JSON_CONTENT_TYPE request_logger.debug('_invoke: request url: %s', url_path) request_logger.debug('_invoke: request http method: %s', http_method) request_logger.debug('_invoke: request headers: %s', headers) request_logger.debug('_invoke: request body: %s', request_body_str) http_request = HTTPRequest(method=http_method, url_path=url_path, headers=headers, body=request_body_str, cookies=cookies) # TODO Add post processors #for processor in self.post_processors: # request_msg = processor.process(request_msg) http_response = self._http_provider.do_request(http_request) request_logger.debug('_invoke: response status: %s', http_response.status) request_logger.debug('_invoke: response body: %s', http_response.body) if ctx.runtime_data is not None and 'response_extractor' in ctx.runtime_data: # pylint: disable=line-too-long ctx.runtime_data.get('response_extractor').set_http_status(http_response.status) # pylint: disable=line-too-long ctx.runtime_data.get('response_extractor').set_http_headers(http_response.headers) # pylint: disable=line-too-long ctx.runtime_data.get('response_extractor').set_http_body(http_response.body) # pylint: disable=line-too-long ctx.runtime_data.get('response_extractor').set_http_method(http_method) # pylint: disable=line-too-long url = self._http_provider._base_url + url_path # pylint: disable=protected-access ctx.runtime_data.get('response_extractor').set_http_url(url) # pylint: disable=line-too-long method_result = RestSerializer.deserialize_response( http_response.status, http_response.body, is_vapi_rest) return method_result