1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
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
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
14
# http://www.apache.org/licenses/LICENSE-2.0
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
22
"""Built-in volume type properties."""
24
from nova import context
26
from nova import exception
27
from nova import flags
28
from nova import log as logging
31
LOG = logging.getLogger('nova.volume.volume_types')
34
def create(context, name, extra_specs={}):
35
"""Creates volume types."""
37
db.volume_type_create(context,
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")
47
def destroy(context, name):
48
"""Marks volume types as deleted."""
50
raise exception.InvalidVolumeType(volume_type=name)
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)
59
def purge(context, name):
60
"""Removes volume types from database."""
62
raise exception.InvalidVolumeType(volume_type=name)
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)
71
def get_all_types(context, inactive=0, search_opts={}):
72
"""Get all non-deleted volume_types.
74
Pass true as argument if you want deleted volume types returned also.
77
vol_types = db.volume_type_get_all(context, inactive)
80
LOG.debug(_("Searching by: %s") % str(search_opts))
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:
89
# search_option to filter_name mapping.
90
filter_mapping = {'extra_specs': _check_extra_specs_match}
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():
97
filter_func = filter_mapping[opt]
99
# no such filter - ignore it, go to next filter
102
if filter_func(type_args, values):
103
# if one of conditions didn't match - remove
104
result[type_name] = type_args
110
def get_volume_type(context, id):
111
"""Retrieves single volume type by id."""
113
raise exception.InvalidVolumeType(volume_type=id)
116
return db.volume_type_get(context, id)
117
except exception.DBError:
118
raise exception.ApiError(_("Unknown volume type: %s") % id)
121
def get_volume_type_by_name(context, name):
122
"""Retrieves single volume type by name."""
124
raise exception.InvalidVolumeType(volume_type=name)
127
return db.volume_type_get_by_name(context, name)
128
except exception.DBError:
129
raise exception.ApiError(_("Unknown volume type: %s") % name)