# 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 = ['container_id', 'name', 'desc']


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, container_id):
        try:
            return self._list(self._path(container_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:
                pass
                raise exceptions.InvalidAttribute()
        return self._create(self._path(), new)

    def delete(self, container_id):
        return self._delete(self._path(container_id))

    def update(self, container_id, patch):
        return self._update(self._path(container_id), patch)

@@ -764,24 +764,8 @@
  = %5B
-'container_id', 
 'nam
@@ -2873,34 +2873,24 @@
 f get(self, 
-container_
 id):%0A       
@@ -2932,34 +2932,24 @@
 (self._path(
-container_
 id))%5B0%5D%0A    
@@ -3187,29 +3187,8 @@
 se:%0A
-                pass%0A
     
@@ -3304,26 +3304,16 @@
 e(self, 
-container_
 id):%0A   
@@ -3348,26 +3348,16 @@
 f._path(
-container_
 id))%0A%0A  
@@ -3375,26 +3375,16 @@
 e(self, 
-container_
 id, patc
@@ -3430,18 +3430,8 @@
 ath(
-container_
 id),

Make container in sync with bays and pods