~rlane/nova/lp773690

« back to all changes in this revision

Viewing changes to nova/compute/api.py

  • Committer: rlane at wikimedia
  • Date: 2011-04-29 22:30:40 UTC
  • mfrom: (382.1.655 nova)
  • Revision ID: rlane@wikimedia.org-20110429223040-i0x3ds9eqwrabyru
MergeĀ fromĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
102
102
            if len(content) > content_limit:
103
103
                raise quota.QuotaError(code="OnsetFileContentLimitExceeded")
104
104
 
 
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()
 
113
            LOG.warn(msg)
 
114
            raise quota.QuotaError(msg, "MetadataLimitExceeded")
 
115
 
 
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()
 
124
                LOG.warn(msg)
 
125
                raise quota.QuotaError(msg, "MetadataLimitExceeded")
 
126
 
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.
113
135
 
114
136
        Verifies that quota and other arguments are valid.
115
137
 
116
138
        """
117
 
 
118
139
        if not instance_type:
119
140
            instance_type = instance_types.get_default_instance_type()
120
141
 
128
149
                                     "run %s more instances of this type.") %
129
150
                                   num_instances, "InstanceLimitExceeded")
130
151
 
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")
137
 
                   % locals())
138
 
            LOG.warn(msg)
139
 
            raise quota.QuotaError(msg, "MetadataLimitExceeded")
140
 
 
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")
151
 
                       % locals())
152
 
                LOG.warn(msg)
153
 
                raise quota.QuotaError(msg, "MetadataLimitExceeded")
154
 
 
 
152
        self._check_metadata_properties_quota(context, metadata)
155
153
        self._check_injected_file_quota(context, injected_files)
156
154
 
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
244
242
 
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 "
513
 
                    "instance"))
 
510
            raise exception.MigrationNotFoundByStatus(instance_id=instance_id,
 
511
                                                      status='finished')
514
512
 
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 "
528
 
                    "instance"))
 
525
            raise exception.MigrationNotFoundByStatus(instance_id=instance_id,
 
526
                                                      status='finished')
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,
723
721
                                            metadata):
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,
726
727
                                                    metadata)