def create(self, request): """Create a dns zone""" if not validate_cloud_objects_limit(): raise APIBadRequest(_('Licence cloud objects limit reached. Please check your license.')) serializer = DnsCreateSerializer(data=request.data) serializer.is_valid(raise_exception=True) designate = designate_client(self.identity_admin_api.session, region_name=serializer.validated_data.pop('region_name', None), sudo_project_id=serializer.validated_data.pop('sudo_project_id', None), all_projects=serializer.validated_data.pop('all_projects', None)) serializer.validated_data.pop('client', None) try: zone = designate.zones.create(**serializer.validated_data) except designate_exceptions.Conflict as e: LOG.error('Unable to create zone, reason {0}'.format(e)) raise ValidationError( {'detail': _('Unable to create zone. A zone with the same domain name already exists')} ) except designate_exceptions.Base as e: LOG.error('Unable to create zone, reason {0}'.format(e)) raise ValidationError({'detail': _('Unable to create zone. Please see logs for more info')}) else: return Response(zone, status=HTTP_201_CREATED)
def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) if not validate_cloud_objects_limit(): raise APIBadRequest(_('Licence cloud objects limit reached. Please check your license.')) try: os_image_api = Images(api_session=self.os_admin_api.session) image = os_image_api.create(owner=self.os_admin_api.project_id, **serializer.validated_data) except Exception as e: LOG.error(e) handle(self.request, message='Unable to create the image') else: headers = self.get_success_headers(serializer.data) data = serializer.data data['id'] = image.id reseller_resources = user_reseller_resources(self.request.user) if reseller_resources: Image.objects.update_or_create( defaults={ 'reseller_resources': reseller_resources, 'min_disk': serializer.validated_data.get('min_disk', 0), 'min_ram': serializer.validated_data.get('min_ram', 0), 'region': serializer.validated_data.get('region') }, id=image.id, ) return Response(data, status=status.HTTP_201_CREATED, headers=headers)
def perform_create(self, serializer: StaffVolumeCreateSerializer): if not validate_cloud_objects_limit(): raise APIBadRequest( _('Licence cloud objects limit reached. Please check your license.' )) os_api = OSApi.from_project_id( project_id=serializer.validated_data['project'].project_id) volume_source = serializer.validated_data.pop('source', {}) source_type = volume_source.get('source_type') source = volume_source.get('source') source_id = source.pk if source else None try: os_api.volumes.create( name=serializer.validated_data['name'], size=serializer.validated_data['size'], region_name=serializer.validated_data['region'], type=serializer.validated_data.get('type', None), source_type=source_type, source_id=source_id) except EndpointNotFound as e: LOG.error(e) raise APIBadRequest( detail=_('Cinder endpoint not found for region {}').format( serializer.validated_data['region'], )) except Unauthorized as e: LOG.error(e) raise OpenstackAuthError(_('Operation not allowed')) except Exception as e: LOG.error(e) handle(self.request, message=e)
def perform_create(self, serializer): if not validate_cloud_objects_limit(): raise APIBadRequest( _('Licence cloud objects limit reached. Please check your license.' )) serializer.is_valid(raise_exception=True) os_api = OSApi.from_request(request=self.request) try: os_api.routers.create(kwargs=serializer.validated_data) except Exception as e: LOG.error(e) handle(self.request, message=_('Unable to create router'))
def perform_create(self, serializer): if not validate_cloud_objects_limit(): raise APIBadRequest( _('Licence cloud objects limit reached. Please check your license.' )) serializer.is_valid(raise_exception=True) router_admin_api = Routers(api_session=self.identity_admin_api.session) try: router_admin_api.create(kwargs=serializer.validated_data) except Exception as e: LOG.error(e) handle(self.request, message=_('Unable to create router'))
def perform_create(self, serializer: StaffVolumeBackupCreateSerializer): if not validate_cloud_objects_limit(): raise APIBadRequest( _('Licence cloud objects limit reached. Please check your license.' )) volume_id = serializer.validated_data.get('volume_id') volume = Volume.objects.filter(id=volume_id).first() if volume: if serializer.validated_data.pop('for_client'): try: volume_related_project = volume.project except Project.DoesNotExist: raise APIBadRequest( _('Cannot create volume backup for client because related volume has not ' 'client project associated')) else: project_id_used = volume_related_project.project_id os_api = OSApi.from_project_id( project_id=volume_related_project.project_id) else: project_id_used = plugin_settings.USER_PROJECT_ID os_api = OSAdminApi() if serializer.validated_data.get('incremental') is True: if not has_full_backup_for_incremental_backup( project_id=project_id_used, related_volume=volume): # for incremental backups check if a full backup related to the volume exists in the same project # - openstack doesn't validate this at specific project level raise APIBadRequest(detail=_( 'Cannot create incremental backup without having a full backup first ' 'in your os project')) try: volume_backup = os_api.volume_backups.create( region_id=volume.region, **serializer.validated_data, ) activity_helper.add_current_activity_params( object_id=volume_backup.id) except Exception as e: LOG.error(e) handle(self.request, message=_('Unable to create volume backup')) return Response( {'detail': _('Volume backup creation in progress')}) else: raise APIBadRequest( _('Cannot create volume backup as related volume does not exist' ))
def perform_create(self, serializer: VolumeBackupCreateSerializer): if not validate_cloud_objects_limit(): raise APIBadRequest( _('Licence cloud objects limit reached. Please check your license.' )) volume_id = serializer.validated_data.get('volume_id') volume = Volume.objects.filter(id=volume_id).first() if volume: if serializer.validated_data.get('incremental') is True: client = self.request.user.clients.first() try: project = client.first_project.project_id except (AttributeError, TypeError): raise APIBadRequest( detail=_('No client with an OpenStack project found')) if not has_full_backup_for_incremental_backup( project_id=project, related_volume=volume): # for incremental backups check if a full backup related to the volume exists in the same project # - openstack doesn't validate this at specific project level raise APIBadRequest(detail=_( 'Cannot create incremental backup without having a full backup first' )) os_api = OSApi.from_request(request=self.request) try: volume_backup = os_api.volume_backups.create( region_id=volume.region, **serializer.validated_data, ) activity_helper.add_current_activity_params( object_id=volume_backup.id) except Exception as e: del e # unused handle( self.request, message=self.format_exception( default_message=_('Unable to create volume backup'))) return Response( {'detail': _('Volume backup creation in progress')}) else: raise APIBadRequest( _('Cannot create volume backup as related volume does not exist' ))
def perform_create(self, serializer): if not validate_cloud_objects_limit(): raise APIBadRequest( _('Failed to create volume, please contact support')) os_api = OSApi.from_request(request=self.request) volume_source = serializer.validated_data.pop('source', {}) source_type = volume_source.get('source_type') source = volume_source.get('source') source_id = source.pk if source else None try: os_api.volumes.create( name=serializer.validated_data['name'], size=serializer.validated_data['size'], region_name=serializer.validated_data['region'], type=serializer.validated_data.get('type', None), source_type=source_type, source_id=source_id) except Exception as e: LOG.error(e) handle(self.request, message=e)
def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) if not validate_cloud_objects_limit(): raise APIBadRequest( _('Licence cloud objects limit reached. Please check your license.' )) if not active_features.is_enabled('openstack.images.updatecreate'): raise ForbiddenException(_('Image creation not allowed')) try: image = self.os_api.images.create(owner=self.os_api.project, **serializer.validated_data) except Exception as e: LOG.error(e) handle(self.request, message='Unable to create the image') else: headers = self.get_success_headers(serializer.data) data = serializer.data data['id'] = image.id return Response(data, status=status.HTTP_201_CREATED, headers=headers)
def create(self, request): """Create a dns zone""" if not validate_cloud_objects_limit(): raise APIBadRequest( _('Failed to create DNS zone. Please contact support.')) serializer = DnsCreateSerializer(data=request.data) serializer.is_valid(raise_exception=True) api_session = get_api_session(request) designate = designate_client(api_session, region_name=serializer.validated_data.pop( 'region_name', None)) try: zone = designate.zones.create(**serializer.validated_data) except (designate_exceptions.Base, ClientException) as e: LOG.error('Unable to create zone, reason {0}'.format(e)) raise ValidationError({'detail': e}) else: return Response(zone, status=HTTP_201_CREATED)
def perform_create(self, serializer: StaffVolumeSnapshotCreateSerializer): if not validate_cloud_objects_limit(): raise APIBadRequest( _('Licence cloud objects limit reached. Please check your license.' )) volume_id = serializer.validated_data.get('volume_id') for_client = serializer.validated_data.pop('for_client', False) volume = Volume.objects.filter(id=volume_id).first() if volume: try: if for_client: if not volume.project: raise APIBadRequest( _('Cannot create snapshot for client as volume does not have a related ' 'project in the database.')) if volume.project.project_id == plugin_settings.user_project_id: raise APIBadRequest( _('The volume is in the admin project. No client found.' )) os_api = OSApi.from_project_id( project_id=volume.project.project_id) else: os_api = OSAdminApi() volume_snapshot = os_api.volume_snapshots.create( region_name=volume.region, **serializer.validated_data, ) activity_helper.add_current_activity_params( object_id=volume_snapshot.id) except Exception as e: handle(self.request, message=str(e)) return Response( {'detail': _('Volume snapshot creation in progress')}) else: raise APIBadRequest( _('Cannot create volume snapshot as related volume does not exist' ))
def perform_create(self, serializer: VolumeSnapshotCreateSerializer): if not validate_cloud_objects_limit(): raise APIBadRequest( _('Licence cloud objects limit reached. Please check your license.' )) volume_id = serializer.validated_data.get('volume_id') volume = Volume.objects.filter(id=volume_id).first() if volume: os_api = OSApi.from_request(request=self.request) try: volume_snapshot = os_api.volume_snapshots.create( region_name=volume.region, **serializer.validated_data, ) activity_helper.add_current_activity_params( object_id=volume_snapshot.id) except Exception as e: handle(self.request, message=str(e)) return Response( {'detail': _('Volume snapshot creation in progress')}) else: raise APIBadRequest( _('Cannot create volume snapshot as related volume does not exist' ))
def perform_create(self, serializer): if not validate_cloud_objects_limit(): raise APIBadRequest(_('Licence cloud objects limit reached. Please check your license.')) project = serializer.validated_data['project'] region = serializer.validated_data.get('region') nics = serializer.validated_data['nics'] flavor = serializer.validated_data['flavor'] user_supplied_user_data = serializer.validated_data.get('user_data', None) user = serializer.validated_data.get('user', 'root') password = serializer.validated_data.get('root_password') user_data_passwd_template = None if password: password = crypt.crypt(password, crypt.mksalt( method=getattr(settings, 'INSTANCE_PASSWORD_HASH_METHOD', crypt.METHOD_SHA512) )) if user == 'root': user_data_passwd_template = getattr( settings, 'INSTANCE_CLOUD_INIT_ROOT_PASSWORD_SET', '' ).format(root_password=password) else: user_data_passwd_template = getattr( settings, 'INSTANCE_CLOUD_INIT_NEW_USER_AND_PASSWORD_SET', '' ).format(new_user_name=user, new_user_password=password) ssh_keys_set_template = getattr( settings, 'INSTANCE_CLOUD_INIT_SSH_KEYS_SET', '' ).format(user=user) ssh_keys = serializer.validated_data.get('ssh_keys', None) try: final_user_data = parse_user_data_mime( user_data_passwd_template=user_data_passwd_template, user_supplied_user_data=user_supplied_user_data, ssh_keys_set_template=ssh_keys_set_template, keys_content=ssh_keys, additional_userdata=getattr(settings, 'STAFF_INSTANCE_ADDITIONAL_CLOUD_INIT_USERDATA', None), ) except Exception as e: raise APIBadRequest(str(e)) dev_mapping_v1 = serializer.validated_data['boot_source'].get('dev_mapping_v1') dev_mapping_v2 = serializer.validated_data['boot_source'].get('dev_mapping_v2') volume_type = serializer.validated_data['boot_source'].get('volume_type') boot_image = serializer.validated_data['boot_source'].get('image') first_boot_device = serializer.validated_data['boot_source'].get('boot_device') boot_image_id = boot_image.id if boot_image else None create_args = dict(project_id=project.project_id, project_domain_id=project.project_domain_id, region_name=region.id) user_id = self.request.user.id if dev_mapping_v2 and first_boot_device and volume_type: # NOTE(tomo): Volume type was requested, we need to create the volume first # since nova does not support this directly chain = (create_instance_volume.s(source_type=first_boot_device['source_type'], source_id=first_boot_device['uuid'], volume_type=volume_type, volume_size=first_boot_device['volume_size'], **create_args) | wait_for_volume_status.s(status='available', **create_args) | create_instance_task.s(name=serializer.validated_data.get('name'), image=boot_image_id, flavor=flavor.id, nics=nics, user_data=final_user_data, block_device_mapping_v2=dev_mapping_v2, block_device_mapping=dev_mapping_v1, user_id=user_id, **create_args)) chain() else: # TODO(tomo): This create instance task is ran in sync so we can catch the exception below # however, we need to unify this with the above, mainly run the create async and deal with errors # in another way try: boot_from_iso = False if boot_image_id: # wait for image wait_for(lambda: Image.objects.filter(id=boot_image_id).count() > 0, 600) boot_image_model = Image.objects.get(id=boot_image_id) boot_from_iso = boot_image_model.disk_format == 'iso' if boot_from_iso: create_instance_from_iso_task.delay( volume_id=None, name=serializer.validated_data.get('name'), image=boot_image_id, flavor=flavor.id, nics=nics, user_data=final_user_data, block_device_mapping_v2=dev_mapping_v2, block_device_mapping=dev_mapping_v1, **create_args, ) else: instance_id = create_instance_task( volume_id=None, name=serializer.validated_data.get('name'), image=boot_image_id, flavor=flavor.id, nics=nics, user_data=final_user_data, block_device_mapping_v2=dev_mapping_v2, block_device_mapping=dev_mapping_v1, user_id=user_id, **create_args, ) activity_helper.add_current_activity_params(object_id=instance_id) except Unauthorized as e: LOG.error(e) raise exceptions.OpenstackAuthError( _('Project id for client does not exist, or does not have the permission for the operation')) except Exception as e: LOG.error(e) exceptions.handle(self.request, message=e)