def retry_view(self, request, create_id): retry_from = get_object_or_404(DatabaseCreate, pk=create_id) success, redirect = self.check_status(request, create_id, 'retry') if not success: return redirect TaskRegister.database_create( name=retry_from.name, plan=retry_from.plan, environment=retry_from.environment, team=retry_from.team, project=retry_from.project, description=retry_from.description, backup_hour=retry_from.infra.backup_hour, maintenance_window=retry_from.infra.maintenance_window, maintenance_day=retry_from.infra.maintenance_day, subscribe_to_email_events=retry_from.subscribe_to_email_events, is_protected=retry_from.is_protected, user=request.user, retry_from=retry_from, **{'pool': retry_from.pool}) url = reverse('admin:notification_taskhistory_changelist') filter = "user={}".format(request.user.username) return HttpResponseRedirect('{}?{}'.format(url, filter))
def post(self, request, format=None): validations = (self._validate_required_params, self._validate_search_metadata_params, self._validate_env, self._validate_database, self._validate_user, self._validate_team, self._validate_database_alocation, self._validate_if_kubernetes_env, self._validate_plan) for validation in validations: err = validation() if err is not None: return err backup_hour, maintenance_hour, maintenance_day = ( DatabaseForm.randomize_backup_and_maintenance_hour()) TaskRegister.database_create(name=self.name_param, plan=self.dbaas_plan, environment=self.env, team=self.dbaas_team, project=None, description=self.description_param, user=self.dbaas_user, is_protected=True, backup_hour=backup_hour, maintenance_window=maintenance_hour, maintenance_day=maintenance_day, **self.extra_params) return Response(status=status.HTTP_201_CREATED)
def create(self, request): serializer = self.get_serializer( data=request.DATA, files=request.FILES) if serializer.is_valid(): self.pre_save(serializer.object) data = serializer.restore_fields(request.DATA, request.FILES) result = TaskRegister.database_create( name=data['name'], plan=data['plan'], environment=data['environment'], team=data['team'], project=data['project'], description=data['description'], subscribe_to_email_events=data['subscribe_to_email_events'], user=request.user, register_user=False ) headers = self.get_success_headers(data) task_url = Site.objects.get_current().domain + \ '/api/task?task_id=%s' % str(result.id) return Response( {"task": task_url}, status=status.HTTP_201_CREATED, headers=headers ) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def create(self, request): serializer = self.get_serializer(data=request.DATA, files=request.FILES) if serializer.is_valid(): self.pre_save(serializer.object) data = serializer.restore_fields(request.DATA, request.FILES) backup_hour, maintenance_hour, maintenance_day = ( DatabaseForm.randomize_backup_and_maintenance_hour()) LOG.error("{}".format(data)) result = TaskRegister.database_create( name=data['name'], plan=data['plan'], environment=data['environment'], team=data['team'], project=data['project'], description=data['description'], backup_hour=data.get('backup_hour', backup_hour), maintenance_window=data.get('maintenance_window', maintenance_hour), maintenance_day=data.get('maintenance_day', maintenance_day), subscribe_to_email_events=data['subscribe_to_email_events'], user=request.user, register_user=False) headers = self.get_success_headers(data) task_url = Site.objects.get_current().domain + \ '/api/task?task_id=%s' % str(result.id) return Response({"task": task_url}, status=status.HTTP_201_CREATED, headers=headers) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def create(self, request): serializer = self.get_serializer(data=request.DATA, files=request.FILES) if serializer.is_valid(): self.pre_save(serializer.object) data = serializer.restore_fields(request.DATA, request.FILES) result = TaskRegister.database_create( name=data['name'], plan=data['plan'], environment=data['environment'], team=data['team'], project=data['project'], description=data['description'], subscribe_to_email_events=data['subscribe_to_email_events'], user=request.user, register_user=False) headers = self.get_success_headers(data) task_url = Site.objects.get_current().domain + \ '/api/task?task_id=%s' % str(result.id) return Response({"task": task_url}, status=status.HTTP_201_CREATED, headers=headers) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def retry_view(self, request, create_id): retry_from = get_object_or_404(DatabaseCreate, pk=create_id) error = False if not retry_from.is_status_error: error = True messages.add_message( request, messages.ERROR, "You can not do retry because create status is '{}'".format( retry_from.get_status_display() ), ) if not retry_from.can_do_retry: error = True messages.add_message( request, messages.ERROR, "Create retry is disabled" ) if error: return HttpResponseRedirect( reverse( 'admin:maintenance_databasecreate_change', args=(create_id,) ) ) TaskRegister.database_create( name=retry_from.name, plan=retry_from.plan, environment=retry_from.environment, team=retry_from.team, project=retry_from.project, description=retry_from.description, subscribe_to_email_events=retry_from.subscribe_to_email_events, is_protected=retry_from.is_protected, user=request.user, retry_from=retry_from ) url = reverse('admin:notification_taskhistory_changelist') filter = "user={}".format(request.user.username) return HttpResponseRedirect('{}?{}'.format(url, filter))
def retry_view(self, request, create_id): retry_from = get_object_or_404(DatabaseCreate, pk=create_id) success, redirect = self.check_status(request, create_id, 'retry') if not success: return redirect TaskRegister.database_create( name=retry_from.name, plan=retry_from.plan, environment=retry_from.environment, team=retry_from.team, project=retry_from.project, description=retry_from.description, subscribe_to_email_events=retry_from.subscribe_to_email_events, is_protected=retry_from.is_protected, user=request.user, retry_from=retry_from ) url = reverse('admin:notification_taskhistory_changelist') filter = "user={}".format(request.user.username) return HttpResponseRedirect('{}?{}'.format(url, filter))
def add_view(self, request, form_url='', extra_context=None): self.form = DatabaseForm try: if request.method == 'POST': teams = Team.objects.filter(users=request.user) LOG.info("user %s teams: %s" % (request.user, teams)) if not teams: self.message_user( request, self.database_add_perm_message, level=messages.ERROR ) return HttpResponseRedirect( reverse('admin:logical_database_changelist') ) # if no team is specified and the user has only one team, then # set it to the database if teams.count() == 1 and request.method == 'POST' and not request.user.has_perm( self.perm_add_database_infra): post_data = request.POST.copy() if 'team' in post_data: post_data['team'] = u"%s" % teams[0].pk request.POST = post_data form = DatabaseForm(request.POST) if not form.is_valid(): return super(DatabaseAdmin, self).add_view(request, form_url, extra_context=extra_context) database_creation_message = "call create_database - name={}, plan={}, environment={}, team={}, project={}, description={}, backup_hour={}, maintenance_window={}, maintenance_day={}, user={}, subscribe_to_email_events {}".format( form.cleaned_data['name'], form.cleaned_data['plan'], form.cleaned_data['environment'], form.cleaned_data['team'], form.cleaned_data['project'], form.cleaned_data['description'], form.cleaned_data['backup_hour'], form.cleaned_data['maintenance_window'], form.cleaned_data['maintenance_day'], request.user, form.cleaned_data['subscribe_to_email_events'], ) LOG.debug(database_creation_message) TaskRegister.database_create( name=form.cleaned_data['name'], plan=form.cleaned_data['plan'], environment=form.cleaned_data['environment'], team=form.cleaned_data['team'], project=form.cleaned_data['project'], description=form.cleaned_data['description'], backup_hour=form.cleaned_data['backup_hour'], maintenance_window=form.cleaned_data['maintenance_window'], maintenance_day=form.cleaned_data['maintenance_day'], subscribe_to_email_events=form.cleaned_data['subscribe_to_email_events'], user=request.user, pool=form.cleaned_data['pool'], ) url = reverse('admin:notification_taskhistory_changelist') # Redirect after POST return HttpResponseRedirect(url + "?user=%s" % request.user.username) else: return super(DatabaseAdmin, self).add_view(request, form_url, extra_context=extra_context) except DatabaseAlreadyExists: self.message_user(request, _( 'An inconsistency was found: The database "%s" already exists in infra-structure but not in DBaaS.') % request.POST['name'], level=messages.ERROR) request.method = 'GET' return super(DatabaseAdmin, self).add_view(request, form_url, extra_context=extra_context)
def post(self, request, format=None): data = request.DATA name = data['name'] user = data['user'] team = data['team'] env = get_url_env(request) try: description = data['description'] if not description: raise Exception("A description must be provided") except Exception as e: msg = "A description must be provided." return log_and_response( msg=msg, http_status=status.HTTP_500_INTERNAL_SERVER_ERROR) name_regexp = re.compile('^[a-z][a-z0-9_]+$') if name_regexp.match(name) is None: msg = "Your database name must match /^[a-z][a-z0-9_]+$/ ." return log_and_response( msg=msg, http_status=status.HTTP_500_INTERNAL_SERVER_ERROR) try: Database.objects.get(name=name, environment__name=env) msg = "There is already a database called {} in {}.".format( name, env) return log_and_response( msg=msg, http_status=status.HTTP_500_INTERNAL_SERVER_ERROR) except ObjectDoesNotExist: pass if database_name_evironment_constraint(name, env): msg = "{} already exists in production!".format(name) return log_and_response( msg=msg, http_status=status.HTTP_500_INTERNAL_SERVER_ERROR) try: dbaas_user = AccountUser.objects.get(email=user) except ObjectDoesNotExist as e: msg = "User does not exist." return log_and_response( msg=msg, e=e, http_status=status.HTTP_500_INTERNAL_SERVER_ERROR) except MultipleObjectsReturned as e: msg = "There are multiple user for {} email.".format(user) return log_and_response( msg=msg, e=e, http_status=status.HTTP_500_INTERNAL_SERVER_ERROR) try: dbaas_team = Team.objects.get(name=team) except ObjectDoesNotExist as e: msg = "Team does not exist." return log_and_response( msg=msg, e=e, http_status=status.HTTP_500_INTERNAL_SERVER_ERROR) try: dbaas_user.team_set.get(name=dbaas_team.name) except ObjectDoesNotExist as e: msg = "The user is not on {} team.".format(dbaas_team.name) return log_and_response( msg=msg, e=e, http_status=status.HTTP_500_INTERNAL_SERVER_ERROR) try: dbaas_environment = Environment.objects.get(name=env) except (ObjectDoesNotExist) as e: msg = "Environment does not exist." return log_and_response( msg=msg, http_status=status.HTTP_500_INTERNAL_SERVER_ERROR) databases_used_by_team = dbaas_team.count_databases_in_use( environment=dbaas_environment) database_alocation_limit = dbaas_team.database_alocation_limit if databases_used_by_team >= database_alocation_limit: msg = "The database alocation limit of {} has been exceeded for the selected team: {}".format( database_alocation_limit, dbaas_team) return log_and_response( msg=msg, http_status=status.HTTP_500_INTERNAL_SERVER_ERROR) if 'plan' not in data: msg = "Plan was not found" return log_and_response( msg=msg, http_status=status.HTTP_500_INTERNAL_SERVER_ERROR) plan = data['plan'] hard_plans = Plan.objects.values( 'name', 'description', 'pk', 'environments__name' ).extra( where=['is_active=True', 'provider={}'.format(Plan.CLOUDSTACK)]) plans = get_plans_dict(hard_plans) plan = [splan for splan in plans if splan['name'] == plan] LOG.info("Plan: {}".format(plan)) if any(plan): dbaas_plan = Plan.objects.get(pk=plan[0]['pk']) else: msg = "Plan was not found" return log_and_response( msg=msg, http_status=status.HTTP_500_INTERNAL_SERVER_ERROR) if dbaas_environment not in dbaas_plan.environments.all(): msg = 'Plan "{}" is not available to "{}" environment'.format( dbaas_plan, dbaas_environment) return log_and_response(msg=msg, http_status=status.HTTP_400_BAD_REQUEST) TaskRegister.database_create(name=name, plan=dbaas_plan, environment=dbaas_environment, team=dbaas_team, project=None, description=description, user=dbaas_user, is_protected=True) return Response(status=status.HTTP_201_CREATED)