741dcb5277
The container update function isn't called by any function. So we should delete it. Change-Id: I832b2082f8da441a7924623c29df2255fbd7ad28 Closes-Bug: #1562236
145 lines
4.8 KiB
Python
145 lines
4.8 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 six.moves.urllib import parse
|
|
|
|
from magnumclient.common import base
|
|
from magnumclient.common import utils
|
|
from magnumclient import exceptions
|
|
|
|
|
|
CREATION_ATTRIBUTES = ['name', 'image', 'command', 'bay_uuid', 'memory']
|
|
|
|
|
|
class Container(base.Resource):
|
|
def __repr__(self):
|
|
return "<Container %s>" % self._info
|
|
|
|
|
|
class ContainerManager(base.Manager):
|
|
resource_class = Container
|
|
|
|
@staticmethod
|
|
def _path(id=None, bay_ident=None):
|
|
|
|
if id:
|
|
return '/v1/containers/%s' % id
|
|
elif bay_ident:
|
|
return '/v1/containers/?bay_ident=%s' % (bay_ident)
|
|
else:
|
|
return '/v1/containers'
|
|
|
|
def list(self, marker=None, limit=None, sort_key=None,
|
|
sort_dir=None, detail=False, bay_ident=None):
|
|
"""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, bay_ident=bay_ident),
|
|
"containers")
|
|
else:
|
|
# TODO(yuanying): if endpoint returns "map",
|
|
# change None to response_key
|
|
return self._list_pagination(self._path(path,
|
|
bay_ident=bay_ident),
|
|
"containers",
|
|
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(
|
|
"Key must be in %s" % ','.join(CREATION_ATTRIBUTES))
|
|
return self._create(self._path(), new)
|
|
|
|
def delete(self, id):
|
|
return self._delete(self._path(id))
|
|
|
|
def _action(self, id, action, method='PUT', qparams=None, **kwargs):
|
|
if qparams:
|
|
action = "%s?%s" % (action,
|
|
parse.urlencode(qparams))
|
|
kwargs.setdefault('headers', {})
|
|
kwargs['headers'].setdefault('Content-Length', '0')
|
|
resp, body = self.api.json_request(method,
|
|
self._path(id) + action,
|
|
**kwargs)
|
|
return resp, body
|
|
|
|
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', method='GET')[1]['output']
|
|
|
|
def execute(self, id, command):
|
|
return self._action(id, '/execute',
|
|
qparams={'command': command})[1]['output']
|