K8sObject: handle API changes from Kubernetes 1.5
when fetching a list of items, an empty list is now returned as None from the API. return [] in this case.
handle the case where Kubernetes fails while returning a string only, instead of a data structure.
@@ -211,7 +211,8 @@ class K8sObject(object):
if int(status) == 422:
raise UnprocessableEntityException(message)
raise BadRequestException(message)
- return state.get('data', dict()).get('items', list())
+ items = state.get('data', dict()).get('items', list())
+ return items if items is not None else []
def get_model(self):
if self.name is None:
@@ -235,7 +236,10 @@ class K8sObject(object):
raise SyntaxError('K8sObject.get_with_params(): data: [ {0} ] is invalid.'.format(data))
url = '{base}'.format(base=self.base_url)
state = self.request(method='GET', url=url, data=data)
- return state.get('data', None).get('items', list())
+ items = state.get('data', None).get('items', list())
+ if items is None:
+ return []
+ return items
def create(self):
if self.name is None:
@@ -322,10 +326,19 @@ class K8sObject(object):
url = '/version'
state = self.request(method='GET', url=url)
+
if not state.get('success'):
status = state.get('status', '')
- reason = state.get('data', dict()).get('message', None)
+ data = state.get('data', dict())
+
+ if isinstance(data, dict) and 'message' in data:
+ reason = data.get('message', None)
+ else:
+ reason = data
message = 'K8sObject: GET failed: HTTP {0} : {1}'.format(status, reason)
+
+ if status == 401:
+ raise UnauthorizedException(message)
raise BadRequestException(message)
return state['data']
