1+ import logging
12from abc import abstractmethod
3+
4+ import dictdiffer
25from urllib3 .util .url import Url , parse_url
36from requests import session
47from kong .structures import ApiData , ServiceData , ConsumerData , \
58 PluginData , RouteData , TargetData , UpstreamData
6- from kong .exceptions import SchemaViolation
9+ from kong .exceptions import SchemaViolation , ObjectNotFound
10+
11+ logger = logging .getLogger (__name__ )
712
813
914class RestClient : # pylint:disable=too-few-public-methods
@@ -68,6 +73,17 @@ def _to_object_data(self, data_dict):
6873 def _to_list_object_data (self , list_data_dict ):
6974 return map (self ._to_object_data , list_data_dict )
7075
76+ def update_or_create (self , ** kwargs ):
77+ pk_or_id = kwargs .get (self ._object_data_class .pk_identifier )
78+ try :
79+ data_dict = self ._perform_retrieve (pk_or_id )
80+ except ObjectNotFound :
81+ return self .create (** kwargs )
82+
83+ self .update (pk_or_id , ** kwargs )
84+
85+ return self ._to_object_data (data_dict )
86+
7187 def create (self , ** kwargs ):
7288 data_dict = self ._perform_create (** kwargs )
7389 return self ._to_object_data (data_dict )
@@ -177,6 +193,9 @@ def _send_retrieve(self, name_or_id, endpoint=None):
177193 response = self .session .get (url )
178194
179195 if response .status_code == 404 :
196+ if response .json ().get ('message' ) == "Not found" :
197+ raise ObjectNotFound (response .content )
198+
180199 raise NameError (response .content )
181200
182201 if response .status_code != 200 :
@@ -188,7 +207,7 @@ def _validate_params(self, query_params, allowed_params):
188207 validated_params = {}
189208 for k , val in query_params .items ():
190209 if k in allowed_params :
191- validated_params [k ] = self ._stringify_if_list (val )
210+ validated_params [k ] = val # self._stringify_if_list(val)
192211 else :
193212 raise KeyError ('invalid query parameter: %s' % k )
194213 return validated_params
@@ -409,11 +428,11 @@ def _object_data_class(self):
409428 def _allowed_update_params (self ):
410429 return 'name' , 'protocol' , 'host' , 'port' , 'path' , \
411430 'retries' , 'connect_timeout' , 'send_timeout' , \
412- 'read_timeout' , 'url'
431+ 'read_timeout' , 'url' , "tags"
413432
414433 @property
415434 def _allowed_query_params (self ):
416- return []
435+ return ["tags" ]
417436
418437 @property
419438 def _path (self ):
@@ -432,9 +451,9 @@ def _object_data_class(self):
432451
433452 @property
434453 def _allowed_update_params (self ):
435- return 'protocols' , 'methods' , 'hosts' ,\
454+ return 'name' , ' protocols' , 'methods' , 'hosts' ,\
436455 'paths' , 'strip_path' , 'preserve_host' ,\
437- 'service' ,
456+ 'service' , 'tags'
438457
439458 @property
440459 def _allowed_query_params (self ):
@@ -444,6 +463,13 @@ def _allowed_query_params(self):
444463 def _path (self ):
445464 return 'routes/'
446465
466+ # pylint: disable=arguments-differ
467+ def _perform_update (self , pk_or_id , ** kwargs ):
468+ if 'service' in kwargs .keys ():
469+ kwargs ['service' ] = {"id" : self .get_service_id (kwargs ['service' ])}
470+
471+ return super (RouteAdminClient , self )._perform_update (pk_or_id , ** kwargs )
472+
447473 # pylint: disable=arguments-differ
448474 def _perform_create (self , service , ** kwargs ):
449475
0 commit comments