1# Copyright (C) 2015 Hewlett-Packard Development Company, L.P.
2# All Rights Reserved.
3#
4#    Licensed under the Apache License, Version 2.0 (the "License"); you may
5#    not use this file except in compliance with the License. You may obtain
6#    a copy of the License at
7#
8#         http://www.apache.org/licenses/LICENSE-2.0
9#
10#    Unless required by applicable law or agreed to in writing, software
11#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13#    License for the specific language governing permissions and limitations
14#    under the License.
15
16"""Pools interface (v2 extension)"""
17
18from cinderclient import base
19
20
21class Pool(base.Resource):
22    NAME_ATTR = 'name'
23
24    def __repr__(self):
25        return "<Pool: %s>" % self.name
26
27
28class PoolManager(base.Manager):
29    """Manage :class:`Pool` resources."""
30    resource_class = Pool
31
32    def list(self, detailed=False):
33        """Lists all
34
35        :rtype: list of :class:`Pool`
36        """
37        if detailed is True:
38            pools = self._list("/scheduler-stats/get_pools?detail=True",
39                               "pools")
40            # Other than the name, all of the pool data is buried below in
41            # a 'capabilities' dictionary. In order to be consistent with the
42            # get-pools command line, these elements are moved up a level to
43            # be attributes of the pool itself.
44            for pool in pools:
45                if hasattr(pool, 'capabilities'):
46                    for k, v in pool.capabilities.items():
47                        setattr(pool, k, v)
48
49                    # Remove the capabilities dictionary since all of its
50                    # elements have been copied up to the containing pool
51                    del pool.capabilities
52            return pools
53        else:
54            pools = self._list("/scheduler-stats/get_pools", "pools")
55
56            # avoid cluttering the basic pool list with capabilities dict
57            for pool in pools:
58                if hasattr(pool, 'capabilities'):
59                    del pool.capabilities
60            return pools
61