def test_is_valid_boolstr(self): self.assertTrue(utils.is_valid_boolstr('true')) self.assertTrue(utils.is_valid_boolstr('false')) self.assertTrue(utils.is_valid_boolstr('yes')) self.assertTrue(utils.is_valid_boolstr('no')) self.assertTrue(utils.is_valid_boolstr('y')) self.assertTrue(utils.is_valid_boolstr('n')) self.assertTrue(utils.is_valid_boolstr('1')) self.assertTrue(utils.is_valid_boolstr('0')) self.assertFalse(utils.is_valid_boolstr('maybe')) self.assertFalse(utils.is_valid_boolstr('only on tuesdays'))
def create(self, req, body): """Creates a new snapshot.""" context = req.environ['nova.context'] authorize(context) if not self.is_valid_body(body, 'snapshot'): raise exc.HTTPUnprocessableEntity() snapshot = body['snapshot'] volume_id = snapshot['volume_id'] volume = self.volume_api.get(context, volume_id) force = snapshot.get('force', False) LOG.audit(_("Create snapshot from volume %s"), volume_id, context=context) if not utils.is_valid_boolstr(force): msg = _("Invalid value '%s' for force. ") % force raise exception.InvalidParameterValue(err=msg) if utils.bool_from_str(force): new_snapshot = self.volume_api.create_snapshot_force(context, volume, snapshot.get('display_name'), snapshot.get('display_description')) else: new_snapshot = self.volume_api.create_snapshot(context, volume, snapshot.get('display_name'), snapshot.get('display_description')) retval = _translate_snapshot_detail_view(context, new_snapshot) return {'snapshot': retval}
def create(self, req, body): """Creates a new snapshot.""" context = req.environ['nova.context'] authorize(context) if not self.is_valid_body(body, 'snapshot'): raise exc.HTTPUnprocessableEntity() snapshot = body['snapshot'] volume_id = snapshot['volume_id'] vol = self.volume_api.get(context, volume_id) force = snapshot.get('force', False) LOG.audit(_("Create snapshot from volume %s"), volume_id, context=context) if not utils.is_valid_boolstr(force): msg = _("Invalid value '%s' for force. ") % force raise exception.InvalidParameterValue(err=msg) if utils.bool_from_str(force): new_snapshot = self.volume_api.create_snapshot_force(context, vol, snapshot.get('display_name'), snapshot.get('display_description')) else: new_snapshot = self.volume_api.create_snapshot(context, vol, snapshot.get('display_name'), snapshot.get('display_description')) retval = _translate_snapshot_detail_view(context, new_snapshot) return {'snapshot': retval}
def create(name, memory, vcpus, root_gb, ephemeral_gb=None, flavorid=None, swap=None, rxtx_factor=None, is_public=True): """Creates instance types.""" if flavorid is None: flavorid = uuid.uuid4() if swap is None: swap = 0 if rxtx_factor is None: rxtx_factor = 1.0 if ephemeral_gb is None: ephemeral_gb = 0 kwargs = { 'memory_mb': memory, 'vcpus': vcpus, 'root_gb': root_gb, 'ephemeral_gb': ephemeral_gb, 'swap': swap, 'rxtx_factor': rxtx_factor, } # ensure name do not exceed 255 characters utils.check_string_length(name, 'name', min_length=1, max_length=255) # 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 ['memory_mb', 'vcpus', 'root_gb', 'ephemeral_gb', 'swap']: try: kwargs[option] = int(kwargs[option]) assert kwargs[option] >= 0 except (ValueError, AssertionError): msg = _("'%s' argument must be a positive integer") % option raise exception.InvalidInput(reason=msg) # rxtx_factor should be a positive float try: kwargs['rxtx_factor'] = float(kwargs['rxtx_factor']) assert kwargs['rxtx_factor'] > 0 except (ValueError, AssertionError): msg = _("'rxtx_factor' argument must be a positive float") 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 = _("'%s' argument must be greater than 0") % option 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) # ensure is_public attribute is boolean if not utils.is_valid_boolstr(is_public): msg = _("is_public must be a boolean") raise exception.InvalidInput(reason=msg) kwargs['is_public'] = utils.bool_from_str(is_public) try: return db.instance_type_create(context.get_admin_context(), kwargs) except db_session.DBError, e: LOG.exception(_('DB error: %s') % e) raise exception.InstanceTypeCreateFailed()
def create(name, memory, vcpus, root_gb, ephemeral_gb=None, flavorid=None, swap=None, rxtx_factor=None, is_public=True): """Creates instance types.""" if flavorid is None: flavorid = uuid.uuid4() if swap is None: swap = 0 if rxtx_factor is None: rxtx_factor = 1.0 if ephemeral_gb is None: ephemeral_gb = 0 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 ['memory_mb', 'vcpus', 'root_gb', 'ephemeral_gb', 'swap']: try: kwargs[option] = int(kwargs[option]) assert kwargs[option] >= 0 except (ValueError, AssertionError): msg = _("'%s' argument must be a positive integer") % option raise exception.InvalidInput(reason=msg) # rxtx_factor should be a positive float try: kwargs['rxtx_factor'] = float(kwargs['rxtx_factor']) assert kwargs['rxtx_factor'] > 0 except (ValueError, AssertionError): msg = _("'rxtx_factor' argument must be a positive float") 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 = _("'%s' argument must be greater than 0") % option 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) # ensure is_public attribute is boolean if not utils.is_valid_boolstr(is_public): msg = _("is_public must be a boolean") raise exception.InvalidInput(reason=msg) kwargs['is_public'] = utils.bool_from_str(is_public) 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 apply(self, context, resource): if not resource.metadata: resource.metadata = {} if not resource.security_group: resource.security_group = 'default' if not resource.instance_type: resource.instance_type = instance_types.get_default_instance_type() if not resource.min_count: resource.min_count = 1 if not resource.max_count: resource.max_count = resource.min_count resource.block_device_mapping = resource.block_device_mapping or [] if resource.instance_type['disabled']: raise exception.InstanceTypeNotFound( instance_type_id=resource.instance_type['id']) if resource.user_data: l = len(resource.user_data) if l > MAX_USERDATA_SIZE: # NOTE(mikal): user_data is stored in a text column, and # the database might silently truncate if its over length. raise exception.InstanceUserDataTooLarge( length=l, maxsize=MAX_USERDATA_SIZE) try: base64.decodestring(resource.user_data) except base64.binascii.Error: raise exception.InstanceUserDataMalformed() # Reserve quotas resource.num_instances, resource.quota_reservations = \ self._check_num_instances_quota(context, resource.instance_type, resource.min_count, resource.max_count) self._check_metadata_properties_quota(context, resource.metadata) self._check_injected_file_quota(context, resource.injected_files) self._check_requested_networks(context, resource.requested_networks) # Handle config_drive resource.config_drive_id = None if resource.config_drive and not utils.is_valid_boolstr( resource.config_drive): # config_drive is volume id resource.config_drive_id = resource.config_drive resource.config_drive = None # Ensure config_drive image exists cd_image_service, config_drive_id = \ glance.get_remote_image_service(context, resource.config_drive_id) cd_image_service.show(context, resource.config_drive_id) if resource.key_data is None and resource.key_name: resource.key_pair = self.db.key_pair_get(context, context.user_id, resource.key_name) resource.key_data = resource.key_pair['public_key'] resource.root_device_name = block_device.properties_root_device_name( resource.image.get('properties', {})) resource.availability_zone, resource.forced_host = \ self._handle_availability_zone(resource.availability_zone) resource.system_metadata = instance_types.save_instance_type_info( dict(), resource.instance_type) return orc_utils.DictableObject(details='request_validated', resource=resource)