From c048a57ff193e9be6d6e0829a3697ad4c4da12d4 Mon Sep 17 00:00:00 2001 From: Fabian von Feilitzsch Date: Wed, 27 Sep 2017 18:30:58 -0400 Subject: [PATCH] fix serialization --- openshift/helper/ansible.py | 2 +- openshift/helper/base.py | 21 +++++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/openshift/helper/ansible.py b/openshift/helper/ansible.py index dd785fc9..4a1d2ec2 100644 --- a/openshift/helper/ansible.py +++ b/openshift/helper/ansible.py @@ -218,7 +218,7 @@ def object_from_params(self, module_params, obj=None): obj.string_data = None logger.debug("Object from params:") - logger.debug(json.dumps(obj.to_dict(), indent=4)) + logger.debug(obj.to_str()) return obj def request_body_from_params(self, module_params): diff --git a/openshift/helper/base.py b/openshift/helper/base.py index f5afe2cc..df7b2c3d 100644 --- a/openshift/helper/base.py +++ b/openshift/helper/base.py @@ -160,6 +160,15 @@ def has_method(self, method_action): return False return method is not None + def fix_serialization(self, obj): + if obj and obj.kind == "Service": + for port in obj.spec.ports: + try: + port.target_port = int(port.target_port) + except ValueError: + pass + return obj + def get_object(self, name=None, namespace=None): k8s_obj = None method_name = 'list' if self.kind.endswith('list') else 'read' @@ -198,7 +207,7 @@ def patch_object(self, name, namespace, k8s_obj): self.__remove_creation_timestamps(k8s_obj) w, stream = self._create_stream(namespace) return_obj = None - self.logger.debug("Patching object: {}".format(json.dumps(k8s_obj.to_dict()))) + self.logger.debug("Patching object: {}".format(k8s_obj.to_str())) try: patch_method = self.lookup_method('patch', namespace) if namespace: @@ -215,7 +224,7 @@ def patch_object(self, name, namespace, k8s_obj): if not return_obj or self.kind in ('project', 'namespace'): return_obj = self._wait_for_response(name, namespace, 'patch') - return return_obj + return self.fix_serialization(return_obj) def create_object(self, namespace=None, k8s_obj=None, body=None): """ @@ -257,7 +266,7 @@ def create_object(self, namespace=None, k8s_obj=None, body=None): if not return_obj or self.kind in ('project', 'namespace'): return_obj = self._wait_for_response(name, namespace, 'create') - return return_obj + return self.fix_serialization(return_obj) def delete_object(self, name, namespace): self.logger.debug('Starting delete object {0} {1} {2}'.format(self.kind, name, namespace)) @@ -342,7 +351,7 @@ def replace_object(self, name, namespace, k8s_obj=None, body=None): if not return_obj or self.kind in ('project', 'namespace'): return_obj = self._wait_for_response(name, namespace, 'replace') - return return_obj + return self.fix_serialization(return_obj) @staticmethod def objects_match(obj_a, obj_b): @@ -557,7 +566,7 @@ def _read_stream(self, watcher, stream, name): for event in stream: obj = None if event.get('object'): - obj_json = json.dumps(event['object'].to_dict()) + obj_json = event['object'].to_str() self.logger.debug( "EVENT type: {0} object: {1}".format(event['type'], obj_json) ) @@ -609,4 +618,4 @@ def _read_stream(self, watcher, stream, name): self.logger.debug('STREAM FAILED: {}'.format(exc)) pass - return return_obj + return self.fix_serialization(return_obj)