Files
python-magnumclient/magnumclient/api/containers.py
Davanum Srinivas 3b0518c800 Set HTTP PUT Content-Length for container actions
We are not passing any body for the container actions, so let's
default the content length to 0. Otherwise the pecan/wsgi waits
for a body and just hangs.

Change-Id: I9517f611d5a60791c6a03f37deeaab43d96bcc94
2014-12-25 20:39:51 -05:00

131 lines
4.2 KiB
Python

# Copyright 2014 NEC Corporation. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from magnumclient.common import base
from magnumclient.common import utils
from magnumclient import exceptions
CREATION_ATTRIBUTES = ['name', 'desc', 'image_id']
class Container(base.Resource):
def __repr__(self):
return "<Container %s>" % self._info
class ContainerManager(base.Manager):
resource_class = Container
@staticmethod
def _path(id=None):
return '/v1/containers/%s' % id if id else '/v1/containers'
def list(self, marker=None, limit=None, sort_key=None,
sort_dir=None, detail=False):
"""Retrieve a list of containers.
:param marker: Optional, the UUID of a containers, eg the last
containers from a previous result set. Return
the next result set.
:param limit: The maximum number of results to return per
request, if:
1) limit > 0, the maximum number of containers to return.
2) limit == 0, return the entire list of containers.
3) limit param is NOT specified (None), the number of items
returned respect the maximum imposed by the Magnum API
(see Magnum's api.max_limit option).
:param sort_key: Optional, field used for sorting.
:param sort_dir: Optional, direction of sorting, either 'asc' (the
default) or 'desc'.
:param detail: Optional, boolean whether to return detailed information
about containers.
:returns: A list of containers.
"""
if limit is not None:
limit = int(limit)
filters = utils.common_filters(marker, limit, sort_key, sort_dir)
path = ''
if detail:
path += 'detail'
if filters:
path += '?' + '&'.join(filters)
if limit is None:
# TODO(yuanying): if endpoint returns "map",
# change None to response_key
return self._list(self._path(path), None)
else:
# TODO(yuanying): if endpoint returns "map",
# change None to response_key
return self._list_pagination(self._path(path), None,
limit=limit)
def get(self, id):
try:
return self._list(self._path(id))[0]
except IndexError:
return None
def create(self, **kwargs):
new = {}
for (key, value) in kwargs.items():
if key in CREATION_ATTRIBUTES:
new[key] = value
else:
raise exceptions.InvalidAttribute()
return self._create(self._path(), new)
def delete(self, id):
return self._delete(self._path(id))
def update(self, id, patch):
return self._update(self._path(id), patch)
def _action(self, id, action, **kwargs):
kwargs.setdefault('headers', {})
kwargs['headers'].setdefault('Content-Length', '0')
resp, body = self.api.json_request('PUT',
self._path(id) + action,
**kwargs)
return resp
def start(self, id):
return self._action(id, '/start')
def stop(self, id):
return self._action(id, '/stop')
def reboot(self, id):
return self._action(id, '/reboot')
def pause(self, id):
return self._action(id, '/pause')
def unpause(self, id):
return self._action(id, '/unpause')
def logs(self, id):
return self._action(id, '/logs')
def execute(self, id, command):
return self._action(id, '/execute', command=command)