-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
instance_types.py
149 lines (116 loc) · 4.66 KB
/
instance_types.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
# Copyright (c) 2010 Citrix Systems, Inc.
# Copyright 2011 Ken Pepple
#
# 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.
"""Built-in instance properties."""
import re
from nova import context
from nova import db
from nova import exception
from nova import flags
from nova import log as logging
FLAGS = flags.FLAGS
LOG = logging.getLogger(__name__)
INVALID_NAME_REGEX = re.compile("[^\w\.\- ]")
def create(name, memory, vcpus, root_gb, ephemeral_gb, flavorid, swap=None,
rxtx_factor=None):
"""Creates instance types."""
if swap is None:
swap = 0
if rxtx_factor is None:
rxtx_factor = 1
kwargs = {
'memory_mb': memory,
'vcpus': vcpus,
'root_gb': root_gb,
'ephemeral_gb': ephemeral_gb,
'swap': swap,
'rxtx_factor': rxtx_factor,
}
# ensure name does not contain any special characters
invalid_name = INVALID_NAME_REGEX.search(name)
if invalid_name:
msg = _("names can only contain [a-zA-Z0-9_.- ]")
raise exception.InvalidInput(reason=msg)
# ensure some attributes are integers and greater than or equal to 0
for option in kwargs:
try:
kwargs[option] = int(kwargs[option])
assert kwargs[option] >= 0
except (ValueError, AssertionError):
msg = _("create arguments must be positive integers")
raise exception.InvalidInput(reason=msg)
# some value are required to be nonzero, not just positive
for option in ['memory_mb', 'vcpus']:
try:
assert kwargs[option] > 0
except AssertionError:
msg = _("create arguments must be positive integers")
raise exception.InvalidInput(reason=msg)
kwargs['name'] = name
# NOTE(vish): Internally, flavorid is stored as a string but it comes
# in through json as an integer, so we convert it here.
kwargs['flavorid'] = unicode(flavorid)
try:
return db.instance_type_create(context.get_admin_context(), kwargs)
except exception.DBError, e:
LOG.exception(_('DB error: %s') % e)
raise exception.InstanceTypeCreateFailed()
def destroy(name):
"""Marks instance types as deleted."""
try:
assert name is not None
db.instance_type_destroy(context.get_admin_context(), name)
except (AssertionError, exception.NotFound):
LOG.exception(_('Instance type %s not found for deletion') % name)
raise exception.InstanceTypeNotFoundByName(instance_type_name=name)
def get_all_types(inactive=False, filters=None):
"""Get all non-deleted instance_types.
Pass true as argument if you want deleted instance types returned also.
"""
ctxt = context.get_admin_context()
inst_types = db.instance_type_get_all(
ctxt, inactive=inactive, filters=filters)
inst_type_dict = {}
for inst_type in inst_types:
inst_type_dict[inst_type['name']] = inst_type
return inst_type_dict
get_all_flavors = get_all_types
def get_default_instance_type():
"""Get the default instance type."""
name = FLAGS.default_instance_type
return get_instance_type_by_name(name)
def get_instance_type(instance_type_id):
"""Retrieves single instance type by id."""
if instance_type_id is None:
return get_default_instance_type()
ctxt = context.get_admin_context()
return db.instance_type_get(ctxt, instance_type_id)
def get_instance_type_by_name(name):
"""Retrieves single instance type by name."""
if name is None:
return get_default_instance_type()
ctxt = context.get_admin_context()
return db.instance_type_get_by_name(ctxt, name)
# TODO(termie): flavor-specific code should probably be in the API that uses
# flavors.
def get_instance_type_by_flavor_id(flavorid):
"""Retrieve instance type by flavorid.
:raises: FlavorNotFound
"""
ctxt = context.get_admin_context(read_deleted="yes")
return db.instance_type_get_by_flavor_id(ctxt, flavorid)