102
102
if len(content) > content_limit:
103
103
raise quota.QuotaError(code="OnsetFileContentLimitExceeded")
105
def _check_metadata_properties_quota(self, context, metadata={}):
106
"""Enforce quota limits on metadata properties."""
107
num_metadata = len(metadata)
108
quota_metadata = quota.allowed_metadata_items(context, num_metadata)
109
if quota_metadata < num_metadata:
110
pid = context.project_id
111
msg = _("Quota exceeeded for %(pid)s, tried to set "
112
"%(num_metadata)s metadata properties") % locals()
114
raise quota.QuotaError(msg, "MetadataLimitExceeded")
116
# Because metadata is stored in the DB, we hard-code the size limits
117
# In future, we may support more variable length strings, so we act
118
# as if this is quota-controlled for forwards compatibility
119
for k, v in metadata.iteritems():
120
if len(k) > 255 or len(v) > 255:
121
pid = context.project_id
122
msg = _("Quota exceeeded for %(pid)s, metadata property "
123
"key or value too long") % locals()
125
raise quota.QuotaError(msg, "MetadataLimitExceeded")
105
127
def create(self, context, instance_type,
106
128
image_id, kernel_id=None, ramdisk_id=None,
107
129
min_count=1, max_count=1,
108
130
display_name='', display_description='',
109
131
key_name=None, key_data=None, security_group='default',
110
availability_zone=None, user_data=None, metadata=[],
132
availability_zone=None, user_data=None, metadata={},
111
133
injected_files=None):
112
134
"""Create the number and type of instances requested.
114
136
Verifies that quota and other arguments are valid.
118
139
if not instance_type:
119
140
instance_type = instance_types.get_default_instance_type()
128
149
"run %s more instances of this type.") %
129
150
num_instances, "InstanceLimitExceeded")
131
num_metadata = len(metadata)
132
quota_metadata = quota.allowed_metadata_items(context, num_metadata)
133
if quota_metadata < num_metadata:
134
pid = context.project_id
135
msg = (_("Quota exceeeded for %(pid)s,"
136
" tried to set %(num_metadata)s metadata properties")
139
raise quota.QuotaError(msg, "MetadataLimitExceeded")
141
# Because metadata is stored in the DB, we hard-code the size limits
142
# In future, we may support more variable length strings, so we act
143
# as if this is quota-controlled for forwards compatibility
144
for metadata_item in metadata:
145
k = metadata_item['key']
146
v = metadata_item['value']
147
if len(k) > 255 or len(v) > 255:
148
pid = context.project_id
149
msg = (_("Quota exceeeded for %(pid)s,"
150
" metadata property key or value too long")
153
raise quota.QuotaError(msg, "MetadataLimitExceeded")
152
self._check_metadata_properties_quota(context, metadata)
155
153
self._check_injected_file_quota(context, injected_files)
157
155
image = self.image_service.show(context, image_id)
239
237
# Set sane defaults if not specified
240
238
updates = dict(hostname=self.hostname_factory(instance_id))
241
239
if (not hasattr(instance, 'display_name') or
242
instance.display_name == None):
240
instance.display_name is None):
243
241
updates['display_name'] = "Server %s" % instance_id
245
243
instance = self.update(context, instance_id, **updates)
509
507
migration_ref = self.db.migration_get_by_instance_and_status(context,
510
508
instance_id, 'finished')
511
509
if not migration_ref:
512
raise exception.NotFound(_("No finished migrations found for "
510
raise exception.MigrationNotFoundByStatus(instance_id=instance_id,
515
513
params = {'migration_id': migration_ref['id']}
516
514
self._cast_compute_message('revert_resize', context, instance_id,
524
522
migration_ref = self.db.migration_get_by_instance_and_status(context,
525
523
instance_id, 'finished')
526
524
if not migration_ref:
527
raise exception.NotFound(_("No finished migrations found for "
525
raise exception.MigrationNotFoundByStatus(instance_id=instance_id,
529
527
instance_ref = self.db.instance_get(context, instance_id)
530
528
params = {'migration_id': migration_ref['id']}
531
529
self._cast_compute_message('confirm_resize', context, instance_id,
722
720
def update_or_create_instance_metadata(self, context, instance_id,
724
722
"""Updates or creates instance metadata."""
723
combined_metadata = self.get_instance_metadata(context, instance_id)
724
combined_metadata.update(metadata)
725
self._check_metadata_properties_quota(context, combined_metadata)
725
726
self.db.instance_metadata_update_or_create(context, instance_id,