~ewanmellor/nova/lp835952

« back to all changes in this revision

Viewing changes to nova/volume/volume_types.py

  • Committer: Tarmac
  • Author(s): vladimir.p
  • Date: 2011-08-25 16:14:44 UTC
  • mfrom: (1453.3.16 nova)
  • Revision ID: tarmac-20110825161444-oj48iwhpq7c5d6j7
Added:
- volume metadata
- volume types
- volume types extra_specs

Volume create API receives volume types & metadata.

Work in progress on Openstack APIs for volume types & extra_specs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright (c) 2011 Zadara Storage Inc.
 
4
# Copyright (c) 2011 OpenStack LLC.
 
5
# Copyright 2010 United States Government as represented by the
 
6
# Administrator of the National Aeronautics and Space Administration.
 
7
# Copyright (c) 2010 Citrix Systems, Inc.
 
8
# Copyright 2011 Ken Pepple
 
9
#
 
10
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
11
#    not use this file except in compliance with the License. You may obtain
 
12
#    a copy of the License at
 
13
#
 
14
#         http://www.apache.org/licenses/LICENSE-2.0
 
15
#
 
16
#    Unless required by applicable law or agreed to in writing, software
 
17
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
18
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
19
#    License for the specific language governing permissions and limitations
 
20
#    under the License.
 
21
 
 
22
"""Built-in volume type properties."""
 
23
 
 
24
from nova import context
 
25
from nova import db
 
26
from nova import exception
 
27
from nova import flags
 
28
from nova import log as logging
 
29
 
 
30
FLAGS = flags.FLAGS
 
31
LOG = logging.getLogger('nova.volume.volume_types')
 
32
 
 
33
 
 
34
def create(context, name, extra_specs={}):
 
35
    """Creates volume types."""
 
36
    try:
 
37
        db.volume_type_create(context,
 
38
                              dict(name=name,
 
39
                                   extra_specs=extra_specs))
 
40
    except exception.DBError, e:
 
41
        LOG.exception(_('DB error: %s') % e)
 
42
        raise exception.ApiError(_("Cannot create volume_type with "
 
43
                                    "name %(name)s and specs %(extra_specs)s")
 
44
                                    % locals())
 
45
 
 
46
 
 
47
def destroy(context, name):
 
48
    """Marks volume types as deleted."""
 
49
    if name is None:
 
50
        raise exception.InvalidVolumeType(volume_type=name)
 
51
    else:
 
52
        try:
 
53
            db.volume_type_destroy(context, name)
 
54
        except exception.NotFound:
 
55
            LOG.exception(_('Volume type %s not found for deletion') % name)
 
56
            raise exception.ApiError(_("Unknown volume type: %s") % name)
 
57
 
 
58
 
 
59
def purge(context, name):
 
60
    """Removes volume types from database."""
 
61
    if name is None:
 
62
        raise exception.InvalidVolumeType(volume_type=name)
 
63
    else:
 
64
        try:
 
65
            db.volume_type_purge(context, name)
 
66
        except exception.NotFound:
 
67
            LOG.exception(_('Volume type %s not found for purge') % name)
 
68
            raise exception.ApiError(_("Unknown volume type: %s") % name)
 
69
 
 
70
 
 
71
def get_all_types(context, inactive=0, search_opts={}):
 
72
    """Get all non-deleted volume_types.
 
73
 
 
74
    Pass true as argument if you want deleted volume types returned also.
 
75
 
 
76
    """
 
77
    vol_types = db.volume_type_get_all(context, inactive)
 
78
 
 
79
    if search_opts:
 
80
        LOG.debug(_("Searching by: %s") % str(search_opts))
 
81
 
 
82
        def _check_extra_specs_match(vol_type, searchdict):
 
83
            for k, v in searchdict.iteritems():
 
84
                if k not in vol_type['extra_specs'].keys()\
 
85
                   or vol_type['extra_specs'][k] != v:
 
86
                    return False
 
87
            return True
 
88
 
 
89
        # search_option to filter_name mapping.
 
90
        filter_mapping = {'extra_specs': _check_extra_specs_match}
 
91
 
 
92
        result = {}
 
93
        for type_name, type_args in vol_types.iteritems():
 
94
            # go over all filters in the list
 
95
            for opt, values in search_opts.iteritems():
 
96
                try:
 
97
                    filter_func = filter_mapping[opt]
 
98
                except KeyError:
 
99
                    # no such filter - ignore it, go to next filter
 
100
                    continue
 
101
                else:
 
102
                    if filter_func(type_args, values):
 
103
                        # if one of conditions didn't match - remove
 
104
                        result[type_name] = type_args
 
105
                        break
 
106
        vol_types = result
 
107
    return vol_types
 
108
 
 
109
 
 
110
def get_volume_type(context, id):
 
111
    """Retrieves single volume type by id."""
 
112
    if id is None:
 
113
        raise exception.InvalidVolumeType(volume_type=id)
 
114
 
 
115
    try:
 
116
        return db.volume_type_get(context, id)
 
117
    except exception.DBError:
 
118
        raise exception.ApiError(_("Unknown volume type: %s") % id)
 
119
 
 
120
 
 
121
def get_volume_type_by_name(context, name):
 
122
    """Retrieves single volume type by name."""
 
123
    if name is None:
 
124
        raise exception.InvalidVolumeType(volume_type=name)
 
125
 
 
126
    try:
 
127
        return db.volume_type_get_by_name(context, name)
 
128
    except exception.DBError:
 
129
        raise exception.ApiError(_("Unknown volume type: %s") % name)