| 1 |
#------------------------------------------------------------------------- |
| 2 |
# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 |
# Licensed under the MIT License. See License.txt in the project root for |
| 4 |
# license information. |
| 5 |
#-------------------------------------------------------------------------- |
| 6 |
|
| 7 |
# Python imports |
| 8 |
import logging |
| 9 |
|
| 10 |
# C imports |
| 11 |
cimport c_amqp_management |
| 12 |
cimport c_message |
| 13 |
|
| 14 |
|
| 15 |
_logger = logging.getLogger(__name__) |
| 16 |
|
| 17 |
|
| 18 |
cpdef create_management_operation(cSession session, const char* management_node): |
| 19 |
mgr_op = cManagementOperation() |
| 20 |
mgr_op.create(session, management_node) |
| 21 |
return mgr_op |
| 22 |
|
| 23 |
|
| 24 |
cdef class cManagementOperation(StructBase): |
| 25 |
|
| 26 |
cdef c_amqp_management.AMQP_MANAGEMENT_HANDLE _c_value |
| 27 |
cdef _session |
| 28 |
|
| 29 |
def __cinit__(self): |
| 30 |
pass |
| 31 |
|
| 32 |
def __dealloc__(self): |
| 33 |
_logger.debug("Deallocating cManagementOperation") |
| 34 |
self.destroy() |
| 35 |
|
| 36 |
cdef _validate(self): |
| 37 |
if <void*>self._c_value is NULL: |
| 38 |
self._memory_error() |
| 39 |
|
| 40 |
cpdef destroy(self): |
| 41 |
if <void*>self._c_value is not NULL: |
| 42 |
_logger.debug("Destroying cManagementOperation") |
| 43 |
c_amqp_management.amqp_management_destroy(self._c_value) |
| 44 |
self._c_value = <c_amqp_management.AMQP_MANAGEMENT_HANDLE>NULL |
| 45 |
self._session = None |
| 46 |
|
| 47 |
cdef wrap(self, cManagementOperation value): |
| 48 |
self.destroy() |
| 49 |
self._session = value._session |
| 50 |
self._c_value = value._c_value |
| 51 |
self._validate() |
| 52 |
|
| 53 |
cdef create(self, cSession session, const char* management_node): |
| 54 |
self.destroy() |
| 55 |
self._session = session |
| 56 |
self._c_value = c_amqp_management.amqp_management_create(<c_session.SESSION_HANDLE>session._c_value, management_node) |
| 57 |
self._validate() |
| 58 |
|
| 59 |
cpdef set_trace(self, bint value): |
| 60 |
c_amqp_management.amqp_management_set_trace(self._c_value, value) |
| 61 |
|
| 62 |
cpdef set_response_field_names(self, const char* status_code, const char* status_description): |
| 63 |
if c_amqp_management.amqp_management_set_override_status_code_key_name(self._c_value, status_code) != 0: |
| 64 |
self._value_error("Failed to set status code field name.") |
| 65 |
if c_amqp_management.amqp_management_set_override_status_description_key_name(self._c_value, status_description) != 0: |
| 66 |
self._value_error("Failed to set status description field name.") |
| 67 |
|
| 68 |
cpdef open(self, callback_context): |
| 69 |
cdef void *context |
| 70 |
if callback_context is None: |
| 71 |
context = <void*>NULL |
| 72 |
else: |
| 73 |
context = <void*>callback_context |
| 74 |
if c_amqp_management.amqp_management_open_async(self._c_value, on_amqp_management_open_complete, context, on_amqp_management_error, context) != 0: |
| 75 |
self._value_error("Unable to open management link.") |
| 76 |
|
| 77 |
cpdef close(self): |
| 78 |
if c_amqp_management.amqp_management_close(self._c_value) != 0: |
| 79 |
self._value_error("Unable to close management link.") |
| 80 |
|
| 81 |
cpdef execute(self, const char* operation, const char* type, locales, cMessage message, callback_context): |
| 82 |
cdef const char* c_locales |
| 83 |
cdef void *context |
| 84 |
if locales is None: |
| 85 |
c_locales = <const char*>NULL |
| 86 |
else: |
| 87 |
c_locales = <const char*>locales |
| 88 |
if callback_context is None: |
| 89 |
context = <void*>NULL |
| 90 |
else: |
| 91 |
context = <void*>callback_context |
| 92 |
if <void*>c_amqp_management.amqp_management_execute_operation_async(self._c_value, operation, type, c_locales, message._c_value, on_execute_operation_complete, context) == NULL: |
| 93 |
self._value_error("Unable to execute management operation.") |
| 94 |
|
| 95 |
|
| 96 |
#### Management Link Callbacks |
| 97 |
|
| 98 |
cdef void on_amqp_management_open_complete(void* context, c_amqp_management.AMQP_MANAGEMENT_OPEN_RESULT_TAG open_result) noexcept: |
| 99 |
_logger.debug("Management link open: %r", open_result) |
| 100 |
if context != NULL: |
| 101 |
context_obj = <object>context |
| 102 |
context_obj._management_open_complete(open_result) |
| 103 |
|
| 104 |
cdef void on_amqp_management_error(void* context) noexcept: |
| 105 |
_logger.debug("Management link error") |
| 106 |
if context != NULL: |
| 107 |
context_obj = <object>context |
| 108 |
context_obj._management_operation_error() |
| 109 |
|
| 110 |
cdef void on_execute_operation_complete(void* context, c_amqp_management.AMQP_MANAGEMENT_EXECUTE_OPERATION_RESULT_TAG execute_operation_result, unsigned int status_code, const char* status_description, c_message.MESSAGE_HANDLE message) noexcept: |
| 111 |
cdef c_message.MESSAGE_HANDLE cloned |
| 112 |
description = "None" if <void*>status_description == NULL else status_description |
| 113 |
_logger.debug("Management op complete: %r, status code: %r, description: %r", execute_operation_result, status_code, description) |
| 114 |
if context != NULL: |
| 115 |
context_obj = <object>context |
| 116 |
if status_code == 0: |
| 117 |
context_obj(execute_operation_result, status_code, description, None) |
| 118 |
else: |
| 119 |
if <void*>message != NULL: |
| 120 |
cloned = c_message.message_clone(message) |
| 121 |
wrapped_message = message_factory(cloned) |
| 122 |
else: |
| 123 |
wrapped_message = None |
| 124 |
context_obj(execute_operation_result, status_code, description, wrapped_message) |