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) task_history = TaskHistory() task_history.task_name = "create_database" task_history.task_status = task_history.STATUS_PENDING task_history.arguments = "Database name: {}".format(data['name']) task_history.save() result = create_database.delay( 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'], contacts=data['contacts'], task_history=task_history, user=request.user) 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) LOG.info("Plan %s" % data['plan']) result = create_database.delay(data['name'], data['plan'], data['environment'], data['team'], data['project'], data['description'], request.user) #data = serializer.to_native(self.object) #self.post_save(self.object, created=True) 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) task_history = TaskHistory() task_history.task_name="create_database" task_history.task_status= task_history.STATUS_PENDING task_history.arguments="Database name: {}".format(data['name']) task_history.save() result = create_database.delay(name=data['name'], plan=data['plan'], environment=data['environment'], team=data['team'], project=data['project'], description=data['description'], task_history=task_history, user=request.user) 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) LOG.info("Plan %s" % data['plan']) result = create_database.delay(data['name'], data['plan'], data['environment'], data['team'], data['project'], data['description'], request.user) #data = serializer.to_native(self.object) #self.post_save(self.object, created=True) 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 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) LOG.debug( "call create_database - name=%s, plan=%s, environment=%s, team=%s, project=%s, description=%s, user=%s" % ( form.cleaned_data['name'], form.cleaned_data['plan'], form.cleaned_data['environment'], form.cleaned_data['team'], form.cleaned_data['project'], form.cleaned_data['description'], request.user)) result = create_database.delay(form.cleaned_data['name'], form.cleaned_data['plan'], form.cleaned_data['environment'], form.cleaned_data['team'], form.cleaned_data['project'], form.cleaned_data['description'], request.user) url = reverse('admin:notification_taskhistory_changelist') return HttpResponseRedirect(url + "?user=%s" % request.user.username) # Redirect after POST 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 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) LOG.info("Plano %s" % data['plan']) plan = data['plan'] if plan.provider == plan.CLOUDSTACK: from notification.tasks import create_database result = create_database.delay(data['name'], data['plan'], data['environment'], data['team'], data['project'], data['description'], request.user) #data = serializer.to_native(self.object) #self.post_save(self.object, created=True) 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) else: self.pre_save(serializer.object) data = serializer.restore_fields(request.DATA, request.FILES) databaseinfra = DatabaseInfra.best_for(data['plan'], data['environment'], data['name']) self.object = models.Database.provision(data['name'], databaseinfra) self.object.team = data['team'] self.object.project = data['project'] self.object.description = data['description'] self.object.save() data = serializer.to_native(self.object) self.post_save(self.object, created=True) headers = self.get_success_headers(data) return Response(data, status=status.HTTP_201_CREATED, headers=headers) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
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) 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) task_history = TaskHistory() task_history.task_name = "create_database" task_history.arguments = "Database name: {}".format(name) task_history.save() create_database.delay(name=name, plan=dbaas_plan, environment=dbaas_environment, team=dbaas_team, project=None, description=description, task_history=task_history, user=dbaas_user, is_protected=True) return Response(status=status.HTTP_201_CREATED)
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) try: dbaas_environment = Environment.objects.get(name= env) except(ObjectDoesNotExist,IndexError), e: msg = "Environment does not exist." return log_and_response(msg=msg, http_status=status.HTTP_500_INTERNAL_SERVER_ERROR) create_database.delay(name, dbaas_plan, dbaas_environment,dbaas_team, None, 'Database from Tsuru', dbaas_user) return Response(status=status.HTTP_201_CREATED,) class ServiceRemove(APIView): renderer_classes = (JSONRenderer, JSONPRenderer) model = Database def delete(self, request, database_name, format=None): env = get_url_env(request) try: database = Database.objects.filter(name=database_name, environment__name=env).exclude(is_in_quarantine=True)[0] except IndexError, e: msg = "Database id provided does not exist {} in {}.".format(database_name, env) return log_and_response(msg=msg, e=e,http_status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def post(self, request, format=None): data = request.DATA name = data['name'] user = data['user'] team = data['team'] env = get_url_env(request) 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 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) 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) task_history = TaskHistory() task_history.task_name = "create_database" task_history.arguments = "Database name: {}".format(name) task_history.save() create_database.delay(name=name, plan=dbaas_plan, environment=dbaas_environment, team=dbaas_team, project=None, description='Database from Tsuru', task_history=task_history, user=dbaas_user) return Response(status=status.HTTP_201_CREATED,)
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) task_history = TaskHistory() task_history.task_name = "create_database" task_history.arguments = "Database name: {}".format(name) task_history.save() create_database.delay(name=name, plan=dbaas_plan, environment=dbaas_environment, team=dbaas_team, project=None, description='Database from Tsuru', task_history=task_history, user=dbaas_user) return Response(status=status.HTTP_201_CREATED, ) class ServiceRemove(APIView): renderer_classes = (JSONRenderer, JSONPRenderer) model = Database def delete(self, request, database_name, format=None): env = get_url_env(request) try: database = Database.objects.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={}, 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'], request.user, form.cleaned_data['subscribe_to_email_events']) LOG.debug(database_creation_message) task_history = TaskHistory() task_history.task_name = "create_database" task_history.task_status = task_history.STATUS_WAITING task_history.arguments = "Database name: {}".format( form.cleaned_data['name']) task_history.user = request.user task_history.save() create_database.delay( 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'], subscribe_to_email_events=form. cleaned_data['subscribe_to_email_events'], task_history=task_history, user=request.user) 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)
msg = "Plan was not found" return log_and_response(msg=msg, http_status=status.HTTP_500_INTERNAL_SERVER_ERROR) try: dbaas_environment = Environment.objects.get(name= env) except(ObjectDoesNotExist,IndexError), e: msg = "Environment does not exist." return log_and_response(msg=msg, http_status=status.HTTP_500_INTERNAL_SERVER_ERROR) task_history = TaskHistory() task_history.task_name="create_database" task_history.arguments="Database name: {}".format(name) task_history.save() create_database.delay(name=name, plan=dbaas_plan, environment=dbaas_environment, team=dbaas_team,project=None, description='Database from Tsuru', task_history=task_history, user=dbaas_user) return Response(status=status.HTTP_201_CREATED,) class ServiceRemove(APIView): renderer_classes = (JSONRenderer, JSONPRenderer) model = Database def delete(self, request, database_name, format=None): env = get_url_env(request) try: database = Database.objects.filter(name=database_name, environment__name=env).exclude(is_in_quarantine=True)[0] except IndexError, e: msg = "Database id provided does not exist {} in {}.".format(database_name, env)