<s> from __future__ import unicode_literals <EOL> import functools <EOL> import datetime <EOL> import logging <EOL> import time <EOL> import calendar <EOL> from django . db import models as django_models <EOL> from django . db import transaction , IntegrityError <EOL> from django . db . models import Q <EOL> from django . conf import settings as django_settings <EOL> from django . http import Http404 <EOL> from django . shortcuts import get_object_or_404 <EOL> from django . utils import timezone <EOL> from django_fsm import TransitionNotAllowed <EOL> import django_filters <EOL> from rest_framework import exceptions <EOL> from rest_framework import filters <EOL> from rest_framework import mixins <EOL> from rest_framework import permissions , status <EOL> from rest_framework import viewsets , views <EOL> from rest_framework . response import Response <EOL> from rest_framework . decorators import detail_route , list_route <EOL> from nodeconductor . core import mixins as core_mixins <EOL> from nodeconductor . core import models as core_models <EOL> from nodeconductor . core import exceptions as core_exceptions <EOL> from nodeconductor . core import serializers as core_serializers <EOL> from nodeconductor . core . filters import DjangoMappingFilterBackend , CategoryFilter , SynchronizationStateFilter , URLFilter <EOL> from nodeconductor . core . models import SynchronizationStates <EOL> from nodeconductor . core . utils import sort_dict , datetime_to_timestamp <EOL> from nodeconductor . iaas import models <EOL> from nodeconductor . iaas import serializers <EOL> from nodeconductor . iaas import tasks <EOL> from nodeconductor . iaas . serializers import ServiceSerializer <EOL> from nodeconductor . iaas . log import event_logger <EOL> from nodeconductor . quotas import filters as quota_filters <EOL> from nodeconductor . structure import filters as structure_filters , SupportedServices <EOL> from nodeconductor . structure . views import UpdateOnlyByPaidCustomerMixin <EOL> from nodeconductor . structure . managers import filter_queryset_for_user <EOL> from nodeconductor . structure . models import ProjectRole , Project , Customer , ProjectGroup , CustomerRole <EOL> logger = logging . getLogger ( __name__ ) <EOL> ZABBIX_ENABLED = getattr ( django_settings , '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' ) <EOL> def schedule_transition ( ) : <EOL> def decorator ( view_fn ) : <EOL> @ functools . wraps ( view_fn ) <EOL> def wrapped ( self , request , * args , ** kwargs ) : <EOL> supported_operations = { <EOL> '<STR_LIT:start>' : ( '<STR_LIT>' , tasks . schedule_starting ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , tasks . schedule_stopping ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , tasks . schedule_restarting ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , tasks . schedule_deleting ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , tasks . resize_flavor ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , tasks . extend_disk ) , <EOL> } <EOL> response , processing_task , logger_info = None , None , None <EOL> try : <EOL> with transaction . atomic ( ) : <EOL> instance = self . get_object ( ) <EOL> membership = instance . cloud_project_membership <EOL> is_admin = membership . project . has_user ( request . user , ProjectRole . ADMINISTRATOR ) <EOL> if not is_admin and not request . user . is_staff : <EOL> raise exceptions . PermissionDenied ( ) <EOL> options = view_fn ( self , request , instance , * args , ** kwargs ) <EOL> if isinstance ( options , tuple ) : <EOL> operation , logger_info = options [ : <NUM_LIT:2> ] <EOL> celery_kwargs = options [ <NUM_LIT:2> ] if len ( options ) >= <NUM_LIT:3> else { } <EOL> change_instance_state , processing_task = supported_operations [ operation ] <EOL> transition = getattr ( instance , change_instance_state ) <EOL> transition ( ) <EOL> instance . save ( update_fields = [ '<STR_LIT:state>' ] ) <EOL> else : <EOL> response = options <EOL> raise RuntimeError <EOL> except TransitionNotAllowed : <EOL> message = "<STR_LIT>" <EOL> return Response ( { '<STR_LIT:status>' : message % ( operation , instance . get_state_display ( ) ) } , <EOL> status = status . HTTP_409_CONFLICT ) <EOL> except IntegrityError : <EOL> return Response ( { '<STR_LIT:status>' : '<STR_LIT>' % operation } , <EOL> status = status . HTTP_400_BAD_REQUEST ) <EOL> except RuntimeError : <EOL> assert isinstance ( response , Response ) <EOL> return response <EOL> else : <EOL> processing_task . delay ( instance . uuid . hex , ** celery_kwargs ) <EOL> if logger_info is not None : <EOL> event_logger . instance . info ( <EOL> logger_info [ '<STR_LIT:message>' ] , <EOL> event_type = logger_info [ '<STR_LIT>' ] , <EOL> event_context = logger_info [ '<STR_LIT>' ] ) <EOL> return Response ( { '<STR_LIT:status>' : '<STR_LIT>' % operation } , <EOL> status = status . HTTP_202_ACCEPTED ) <EOL> return wrapped <EOL> return decorator <EOL> class InstanceFilter ( django_filters . FilterSet ) : <EOL> project_group_name = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> distinct = True , <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> project_name = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> distinct = True , <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> project_group = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> distinct = True , <EOL> ) <EOL> project = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> distinct = True , <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> customer = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> distinct = True , <EOL> ) <EOL> customer_name = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> distinct = True , <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> customer_native_name = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> distinct = True , <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> customer_abbreviation = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> distinct = True , <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> template_name = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> name = django_filters . CharFilter ( lookup_type = '<STR_LIT>' ) <EOL> state = django_filters . NumberFilter ( ) <EOL> description = django_filters . CharFilter ( <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> uuid = django_filters . CharFilter ( lookup_type = '<STR_LIT>' ) <EOL> strict = False <EOL> class Meta ( object ) : <EOL> model = models . Instance <EOL> fields = [ <EOL> '<STR_LIT:name>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:state>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:description>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:type>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> order_by = [ <EOL> '<STR_LIT:name>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:state>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:type>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> order_by_mapping = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> class InstanceViewSet ( UpdateOnlyByPaidCustomerMixin , <EOL> mixins . CreateModelMixin , <EOL> mixins . RetrieveModelMixin , <EOL> mixins . UpdateModelMixin , <EOL> mixins . ListModelMixin , <EOL> viewsets . GenericViewSet ) : <EOL> """<STR_LIT>""" <EOL> class PaidControl : <EOL> customer_path = '<STR_LIT>' <EOL> queryset = models . Instance . objects . all ( ) <EOL> serializer_class = serializers . InstanceSerializer <EOL> lookup_field = '<STR_LIT>' <EOL> filter_backends = ( structure_filters . GenericRoleFilter , DjangoMappingFilterBackend ) <EOL> permission_classes = ( permissions . IsAuthenticated , permissions . DjangoObjectPermissions ) <EOL> filter_class = InstanceFilter <EOL> def get_queryset ( self ) : <EOL> queryset = super ( InstanceViewSet , self ) . get_queryset ( ) <EOL> order = self . request . query_params . get ( '<STR_LIT:o>' , None ) <EOL> if order == '<STR_LIT>' : <EOL> queryset = queryset . extra ( select = { <EOL> '<STR_LIT>' : '<STR_LIT>' } ) . order_by ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> elif order == '<STR_LIT>' : <EOL> queryset = queryset . extra ( select = { <EOL> '<STR_LIT>' : '<STR_LIT>' } ) . order_by ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> installation_states = self . request . query_params . getlist ( '<STR_LIT>' ) <EOL> if installation_states : <EOL> query = Q ( ) <EOL> for installation_state in installation_states : <EOL> if installation_state == '<STR_LIT>' : <EOL> query |= ~ Q ( state = models . Instance . States . ONLINE ) | Q ( installation_state = installation_state ) <EOL> else : <EOL> query |= Q ( state = models . Instance . States . ONLINE , installation_state = installation_state ) <EOL> queryset = queryset . filter ( query ) <EOL> return queryset <EOL> def get_serializer_class ( self ) : <EOL> if self . request . method == '<STR_LIT:POST>' : <EOL> return serializers . InstanceCreateSerializer <EOL> elif self . request . method in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return serializers . InstanceUpdateSerializer <EOL> return super ( InstanceViewSet , self ) . get_serializer_class ( ) <EOL> def get_serializer_context ( self ) : <EOL> """<STR_LIT>""" <EOL> context = super ( InstanceViewSet , self ) . get_serializer_context ( ) <EOL> context [ '<STR_LIT:user>' ] = self . request . user <EOL> return context <EOL> def initial ( self , request , * args , ** kwargs ) : <EOL> if self . action in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> instance = self . get_object ( ) <EOL> if instance and instance . state not in instance . States . STABLE_STATES : <EOL> raise core_exceptions . IncorrectStateException ( <EOL> '<STR_LIT>' ) <EOL> elif self . action in ( '<STR_LIT>' , '<STR_LIT:start>' , '<STR_LIT>' ) : <EOL> instance = self . get_object ( ) <EOL> if instance and instance . state == instance . States . PROVISIONING_SCHEDULED : <EOL> raise core_exceptions . IncorrectStateException ( <EOL> '<STR_LIT>' ) <EOL> return super ( InstanceViewSet , self ) . initial ( request , * args , ** kwargs ) <EOL> def perform_create ( self , serializer ) : <EOL> serializer . validated_data [ '<STR_LIT>' ] = serializer . validated_data [ '<STR_LIT>' ] . sla_level <EOL> membership = serializer . validated_data [ '<STR_LIT>' ] <EOL> if membership . state == core_models . SynchronizationStates . ERRED : <EOL> raise core_exceptions . IncorrectStateException ( <EOL> detail = '<STR_LIT>' <EOL> ) <EOL> membership . project . customer . validate_quota_change ( { '<STR_LIT>' : <NUM_LIT:1> } , raise_exception = True ) <EOL> instance = serializer . save ( ) <EOL> event_logger . instance . info ( <EOL> '<STR_LIT>' , <EOL> event_type = '<STR_LIT>' , <EOL> event_context = { '<STR_LIT>' : instance } ) <EOL> tasks . provision_instance . delay ( instance . uuid . hex , backend_flavor_id = instance . flavor . backend_id ) <EOL> def perform_update ( self , serializer ) : <EOL> membership = self . get_object ( ) . cloud_project_membership <EOL> if membership . state == core_models . SynchronizationStates . ERRED : <EOL> raise core_exceptions . IncorrectStateException ( <EOL> detail = '<STR_LIT>' <EOL> ) <EOL> instance = serializer . save ( ) <EOL> event_logger . instance . info ( <EOL> '<STR_LIT>' , <EOL> event_type = '<STR_LIT>' , <EOL> event_context = { '<STR_LIT>' : instance } ) <EOL> from nodeconductor . iaas . tasks import push_instance_security_groups <EOL> push_instance_security_groups . delay ( instance . uuid . hex ) <EOL> @ detail_route ( methods = [ '<STR_LIT>' ] ) <EOL> @ schedule_transition ( ) <EOL> def stop ( self , request , instance , uuid = None ) : <EOL> logger_info = dict ( <EOL> message = '<STR_LIT>' , <EOL> event_type = '<STR_LIT>' , <EOL> event_context = { '<STR_LIT>' : instance } <EOL> ) <EOL> return '<STR_LIT>' , logger_info <EOL> @ detail_route ( methods = [ '<STR_LIT>' ] ) <EOL> @ schedule_transition ( ) <EOL> def start ( self , request , instance , uuid = None ) : <EOL> logger_info = dict ( <EOL> message = '<STR_LIT>' , <EOL> event_type = '<STR_LIT>' , <EOL> event_context = { '<STR_LIT>' : instance } <EOL> ) <EOL> return '<STR_LIT:start>' , logger_info <EOL> @ detail_route ( methods = [ '<STR_LIT>' ] ) <EOL> @ schedule_transition ( ) <EOL> def restart ( self , request , instance , uuid = None ) : <EOL> logger_info = dict ( <EOL> message = '<STR_LIT>' , <EOL> event_type = '<STR_LIT>' , <EOL> event_context = { '<STR_LIT>' : instance } <EOL> ) <EOL> return '<STR_LIT>' , logger_info <EOL> @ schedule_transition ( ) <EOL> def destroy ( self , request , instance , uuid ) : <EOL> try : <EOL> from nodeconductor . iaas . handlers import prevent_deletion_of_instances_with_connected_backups <EOL> prevent_deletion_of_instances_with_connected_backups ( None , instance ) <EOL> except django_models . ProtectedError as e : <EOL> return Response ( { '<STR_LIT>' : e . args [ <NUM_LIT:0> ] } , status = status . HTTP_409_CONFLICT ) <EOL> logger_info = dict ( <EOL> message = '<STR_LIT>' , <EOL> event_type = '<STR_LIT>' , <EOL> event_context = { '<STR_LIT>' : instance } <EOL> ) <EOL> return '<STR_LIT>' , logger_info <EOL> @ detail_route ( methods = [ '<STR_LIT>' ] ) <EOL> @ schedule_transition ( ) <EOL> def resize ( self , request , instance , uuid = None ) : <EOL> if instance . state != models . Instance . States . OFFLINE : <EOL> return Response ( { '<STR_LIT>' : '<STR_LIT>' } , <EOL> status = status . HTTP_409_CONFLICT ) <EOL> serializer = serializers . InstanceResizeSerializer ( instance , data = request . data ) <EOL> serializer . is_valid ( raise_exception = True ) <EOL> flavor = serializer . validated_data . get ( '<STR_LIT>' ) <EOL> if flavor is not None : <EOL> instance_cloud = instance . cloud_project_membership . cloud <EOL> if flavor . cloud != instance_cloud : <EOL> return Response ( { '<STR_LIT>' : "<STR_LIT>" } , <EOL> status = status . HTTP_400_BAD_REQUEST ) <EOL> instance . ram = flavor . ram <EOL> instance . cores = flavor . cores <EOL> instance . flavor_name = flavor . name <EOL> instance . save ( update_fields = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> event_logger . instance_flavor . info ( <EOL> '<STR_LIT>' , <EOL> event_type = '<STR_LIT>' , <EOL> event_context = { '<STR_LIT>' : instance , '<STR_LIT>' : flavor } <EOL> ) <EOL> return '<STR_LIT>' , None , dict ( flavor_uuid = flavor . uuid . hex ) <EOL> else : <EOL> new_size = serializer . validated_data [ '<STR_LIT>' ] <EOL> if new_size <= instance . data_volume_size : <EOL> return Response ( { '<STR_LIT>' : "<STR_LIT>" } , <EOL> status = status . HTTP_400_BAD_REQUEST ) <EOL> instance . data_volume_size = new_size <EOL> instance . save ( update_fields = [ '<STR_LIT>' ] ) <EOL> event_logger . instance_volume . info ( <EOL> '<STR_LIT>' , <EOL> event_type = '<STR_LIT>' , <EOL> event_context = { '<STR_LIT>' : instance , '<STR_LIT>' : new_size } <EOL> ) <EOL> return '<STR_LIT>' , None <EOL> @ detail_route ( ) <EOL> def usage ( self , request , uuid ) : <EOL> if not ZABBIX_ENABLED : <EOL> raise Http404 ( ) <EOL> instance = self . get_object ( ) <EOL> if not instance . backend_id or instance . state in ( models . Instance . States . PROVISIONING_SCHEDULED , <EOL> models . Instance . States . PROVISIONING ) : <EOL> raise Http404 ( ) <EOL> hour = <NUM_LIT> * <NUM_LIT> <EOL> now = time . time ( ) <EOL> data = { <EOL> '<STR_LIT>' : request . query_params . get ( '<STR_LIT>' , int ( now - hour ) ) , <EOL> '<STR_LIT>' : request . query_params . get ( '<STR_LIT:to>' , int ( now ) ) , <EOL> '<STR_LIT>' : request . query_params . get ( '<STR_LIT>' , <NUM_LIT:6> ) , <EOL> '<STR_LIT>' : request . query_params . get ( '<STR_LIT>' ) , <EOL> } <EOL> serializer = serializers . UsageStatsSerializer ( data = data ) <EOL> serializer . is_valid ( raise_exception = True ) <EOL> stats = serializer . get_stats ( [ instance ] , is_paas = instance . type == models . Instance . Services . PAAS ) <EOL> created_ts = datetime_to_timestamp ( instance . created ) <EOL> for stat in stats : <EOL> if stat [ '<STR_LIT>' ] >= created_ts and stat [ '<STR_LIT:to>' ] - created_ts < hour / <NUM_LIT:2> and '<STR_LIT:value>' not in stat : <EOL> stat [ '<STR_LIT:value>' ] = <NUM_LIT:0> <EOL> return Response ( stats , status = status . HTTP_200_OK ) <EOL> @ detail_route ( ) <EOL> def calculated_usage ( self , request , uuid ) : <EOL> """<STR_LIT>""" <EOL> if not ZABBIX_ENABLED : <EOL> raise Http404 ( ) <EOL> instance = self . get_object ( ) <EOL> if not instance . backend_id : <EOL> return Response ( { '<STR_LIT>' : '<STR_LIT>' } , <EOL> status = status . HTTP_409_CONFLICT ) <EOL> default_start = timezone . now ( ) - datetime . timedelta ( hours = <NUM_LIT:1> ) <EOL> timestamp_interval_serializer = core_serializers . TimestampIntervalSerializer ( data = { <EOL> '<STR_LIT:start>' : request . query_params . get ( '<STR_LIT>' , datetime_to_timestamp ( default_start ) ) , <EOL> '<STR_LIT:end>' : request . query_params . get ( '<STR_LIT:to>' , datetime_to_timestamp ( timezone . now ( ) ) ) <EOL> } ) <EOL> timestamp_interval_serializer . is_valid ( raise_exception = True ) <EOL> filter_data = timestamp_interval_serializer . get_filter_data ( ) <EOL> start = datetime_to_timestamp ( filter_data [ '<STR_LIT:start>' ] ) <EOL> end = datetime_to_timestamp ( filter_data [ '<STR_LIT:end>' ] ) <EOL> mapped = { <EOL> '<STR_LIT>' : request . query_params . getlist ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : request . query_params . get ( '<STR_LIT>' ) , <EOL> } <EOL> serializer = serializers . CalculatedUsageSerializer ( data = { k : v for k , v in mapped . items ( ) if v } ) <EOL> serializer . is_valid ( raise_exception = True ) <EOL> results = serializer . get_stats ( instance , start , end ) <EOL> return Response ( results , status = status . HTTP_200_OK ) <EOL> @ detail_route ( methods = [ '<STR_LIT>' ] ) <EOL> def assign_floating_ip ( self , request , uuid ) : <EOL> """<STR_LIT>""" <EOL> instance = self . get_object ( ) <EOL> serializer = serializers . AssignFloatingIpSerializer ( instance , data = request . data ) <EOL> serializer . is_valid ( raise_exception = True ) <EOL> if not instance . cloud_project_membership . external_network_id : <EOL> return Response ( { '<STR_LIT>' : '<STR_LIT>' } , <EOL> status = status . HTTP_409_CONFLICT ) <EOL> elif instance . cloud_project_membership . state in SynchronizationStates . UNSTABLE_STATES : <EOL> return Response ( { '<STR_LIT>' : '<STR_LIT>' } , <EOL> status = status . HTTP_409_CONFLICT ) <EOL> elif instance . state in models . Instance . States . UNSTABLE_STATES : <EOL> raise core_exceptions . IncorrectStateException ( <EOL> detail = '<STR_LIT>' ) <EOL> tasks . assign_floating_ip . delay ( serializer . validated_data [ '<STR_LIT>' ] , uuid ) <EOL> return Response ( { '<STR_LIT>' : '<STR_LIT>' } , <EOL> status = status . HTTP_202_ACCEPTED ) <EOL> class TemplateFilter ( django_filters . FilterSet ) : <EOL> name = django_filters . CharFilter ( <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> os_type = CategoryFilter ( <EOL> categories = models . Template . OsTypes . CATEGORIES <EOL> ) <EOL> application_type = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> ) <EOL> class Meta ( object ) : <EOL> model = models . Template <EOL> fields = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:name>' , <EOL> '<STR_LIT:type>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> class TemplateViewSet ( viewsets . ModelViewSet ) : <EOL> """<STR_LIT>""" <EOL> queryset = models . Template . objects . all ( ) <EOL> serializer_class = serializers . TemplateSerializer <EOL> permission_classes = ( permissions . IsAuthenticated , permissions . DjangoObjectPermissions ) <EOL> lookup_field = '<STR_LIT>' <EOL> filter_backends = ( DjangoMappingFilterBackend , ) <EOL> filter_class = TemplateFilter <EOL> def get_serializer_class ( self ) : <EOL> if self . request . method in ( '<STR_LIT:POST>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return serializers . TemplateCreateSerializer <EOL> return super ( TemplateViewSet , self ) . get_serializer_class ( ) <EOL> def get_queryset ( self ) : <EOL> queryset = super ( TemplateViewSet , self ) . get_queryset ( ) <EOL> user = self . request . user <EOL> if self . request . method == '<STR_LIT:GET>' : <EOL> cloud_uuid = self . request . query_params . get ( '<STR_LIT>' ) <EOL> if cloud_uuid is not None : <EOL> cloud_queryset = filter_queryset_for_user ( models . Cloud . objects . all ( ) , user ) <EOL> try : <EOL> cloud = cloud_queryset . get ( uuid = cloud_uuid ) <EOL> except models . Cloud . DoesNotExist : <EOL> return queryset . none ( ) <EOL> queryset = queryset . filter ( images__cloud = cloud ) <EOL> return queryset <EOL> class TemplateLicenseViewSet ( viewsets . ModelViewSet ) : <EOL> """<STR_LIT>""" <EOL> queryset = models . TemplateLicense . objects . all ( ) <EOL> serializer_class = serializers . TemplateLicenseSerializer <EOL> permission_classes = ( permissions . IsAuthenticated , permissions . DjangoObjectPermissions ) <EOL> lookup_field = '<STR_LIT>' <EOL> def initial ( self , request , * args , ** kwargs ) : <EOL> super ( TemplateLicenseViewSet , self ) . initial ( request , * args , ** kwargs ) <EOL> if self . action != '<STR_LIT>' and not self . request . user . is_staff : <EOL> raise Http404 <EOL> def get_queryset ( self ) : <EOL> queryset = super ( TemplateLicenseViewSet , self ) . get_queryset ( ) <EOL> if '<STR_LIT>' in self . request . query_params : <EOL> customer_uuid = self . request . query_params [ '<STR_LIT>' ] <EOL> queryset = queryset . filter ( templates__images__cloud__customer__uuid = customer_uuid ) <EOL> return queryset <EOL> def _filter_queryset ( self , queryset ) : <EOL> if '<STR_LIT>' in self . request . query_params : <EOL> customer_uuid = self . request . query_params [ '<STR_LIT>' ] <EOL> queryset = queryset . filter ( instance__cloud_project_membership__project__customer__uuid = customer_uuid ) <EOL> if '<STR_LIT:name>' in self . request . query_params : <EOL> queryset = queryset . filter ( template_license__name = self . request . query_params [ '<STR_LIT:name>' ] ) <EOL> if '<STR_LIT:type>' in self . request . query_params : <EOL> queryset = queryset . filter ( template_license__license_type = self . request . query_params [ '<STR_LIT:type>' ] ) <EOL> return queryset <EOL> @ list_route ( ) <EOL> def stats ( self , request ) : <EOL> queryset = filter_queryset_for_user ( models . InstanceLicense . objects . all ( ) , request . user ) <EOL> queryset = self . _filter_queryset ( queryset ) <EOL> aggregate_parameters = self . request . query_params . getlist ( '<STR_LIT>' , [ ] ) <EOL> aggregate_parameter_to_field_map = { <EOL> '<STR_LIT>' : [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] , <EOL> '<STR_LIT>' : [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] , <EOL> '<STR_LIT>' : [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] , <EOL> '<STR_LIT:type>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT:name>' : [ '<STR_LIT>' ] , <EOL> } <EOL> aggregate_fields = [ ] <EOL> for aggregate_parameter in aggregate_parameters : <EOL> if aggregate_parameter not in aggregate_parameter_to_field_map : <EOL> return Response ( '<STR_LIT>' % aggregate_parameter , <EOL> status = status . HTTP_400_BAD_REQUEST ) <EOL> aggregate_fields += aggregate_parameter_to_field_map [ aggregate_parameter ] <EOL> queryset = queryset . values ( * aggregate_fields ) . annotate ( count = django_models . Count ( '<STR_LIT:id>' , distinct = True ) ) <EOL> name_replace_map = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:type>' , <EOL> '<STR_LIT>' : '<STR_LIT:name>' , <EOL> } <EOL> for d in queryset : <EOL> for db_name , output_name in name_replace_map . iteritems ( ) : <EOL> if db_name in d : <EOL> d [ output_name ] = d [ db_name ] <EOL> del d [ db_name ] <EOL> for a in d : <EOL> if a . endswith ( '<STR_LIT>' ) : <EOL> d [ a ] = d [ a ] . hex <EOL> if '<STR_LIT>' in aggregate_parameters and '<STR_LIT>' not in aggregate_parameters : <EOL> for item in queryset : <EOL> project = Project . objects . get ( uuid = item [ '<STR_LIT>' ] ) <EOL> if project . project_group is not None : <EOL> item [ '<STR_LIT>' ] = project . project_group . uuid . hex <EOL> item [ '<STR_LIT>' ] = project . project_group . name <EOL> return Response ( queryset ) <EOL> class ResourceFilter ( django_filters . FilterSet ) : <EOL> project_group_name = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> distinct = True , <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> project_name = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> distinct = True , <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> project_uuid = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' <EOL> ) <EOL> project_groups = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> distinct = True , <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> name = django_filters . CharFilter ( lookup_type = '<STR_LIT>' ) <EOL> customer = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' <EOL> ) <EOL> customer_name = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> customer_abbreviation = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> customer_native_name = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> template_name = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> agreed_sla = django_filters . NumberFilter ( ) <EOL> actual_sla = django_filters . NumberFilter ( <EOL> name = '<STR_LIT>' , <EOL> distinct = True , <EOL> ) <EOL> class Meta ( object ) : <EOL> model = models . Instance <EOL> fields = [ <EOL> '<STR_LIT:name>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> order_by = [ <EOL> '<STR_LIT:name>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> order_by_mapping = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> class ResourceViewSet ( viewsets . ReadOnlyModelViewSet ) : <EOL> queryset = models . Instance . objects . exclude ( <EOL> state = models . Instance . States . DELETING , <EOL> ) <EOL> serializer_class = ServiceSerializer <EOL> lookup_field = '<STR_LIT>' <EOL> filter_backends = ( structure_filters . GenericRoleFilter , DjangoMappingFilterBackend ) <EOL> filter_class = ResourceFilter <EOL> def get_queryset ( self ) : <EOL> period = self . _get_period ( ) <EOL> if '<STR_LIT:->' in period : <EOL> year , month = map ( int , period . split ( '<STR_LIT:->' ) ) <EOL> else : <EOL> year = int ( period ) <EOL> month = <NUM_LIT:12> <EOL> last_day = calendar . monthrange ( year , month ) [ <NUM_LIT:1> ] <EOL> return super ( ResourceViewSet , self ) . get_queryset ( ) . filter ( created__lte = datetime . date ( year , month , last_day ) ) <EOL> def _get_period ( self ) : <EOL> period = self . request . query_params . get ( '<STR_LIT>' ) <EOL> if period is None : <EOL> today = datetime . date . today ( ) <EOL> period = '<STR_LIT>' % ( today . year , today . month ) <EOL> return period <EOL> def get_serializer_context ( self ) : <EOL> """<STR_LIT>""" <EOL> context = super ( ResourceViewSet , self ) . get_serializer_context ( ) <EOL> context [ '<STR_LIT>' ] = self . _get_period ( ) <EOL> return context <EOL> @ detail_route ( ) <EOL> def events ( self , request , uuid ) : <EOL> service = self . get_object ( ) <EOL> period = self . _get_period ( ) <EOL> history = get_object_or_404 ( models . InstanceSlaHistory , instance__uuid = service . uuid , period = period ) <EOL> history_events = list ( history . events . all ( ) . order_by ( '<STR_LIT>' ) . values ( '<STR_LIT>' , '<STR_LIT:state>' ) ) <EOL> serializer = serializers . SlaHistoryEventSerializer ( data = history_events , <EOL> many = True ) <EOL> serializer . is_valid ( raise_exception = True ) <EOL> return Response ( serializer . data , status = status . HTTP_200_OK ) <EOL> class ResourceStatsView ( views . APIView ) : <EOL> def _check_user ( self , request ) : <EOL> if not request . user . is_staff : <EOL> raise exceptions . PermissionDenied ( ) <EOL> def get ( self , request , format = None ) : <EOL> self . _check_user ( request ) <EOL> auth_url = request . query_params . get ( '<STR_LIT>' ) <EOL> if auth_url is None : <EOL> return Response ( <EOL> { '<STR_LIT>' : '<STR_LIT>' } , <EOL> status = status . HTTP_400_BAD_REQUEST , <EOL> ) <EOL> cloud = models . Cloud . objects . filter ( auth_url = auth_url ) . first ( ) <EOL> if cloud is None : <EOL> return Response ( <EOL> { '<STR_LIT>' : '<STR_LIT>' % auth_url } , <EOL> status = status . HTTP_400_BAD_REQUEST , <EOL> ) <EOL> memberships = models . CloudProjectMembership . objects . filter ( cloud__auth_url = auth_url ) <EOL> quota_values = models . CloudProjectMembership . get_sum_of_quotas_as_dict ( <EOL> memberships , ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , fields = [ '<STR_LIT>' ] ) <EOL> quota_stats = { <EOL> '<STR_LIT>' : quota_values [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : quota_values [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : quota_values [ '<STR_LIT>' ] , <EOL> } <EOL> stats = cloud . get_statistics ( ) <EOL> stats . update ( quota_stats ) <EOL> return Response ( sort_dict ( stats ) , status = status . HTTP_200_OK ) <EOL> class CustomerStatsView ( views . APIView ) : <EOL> def get ( self , request , format = None ) : <EOL> customer_statistics = [ ] <EOL> customer_queryset = filter_queryset_for_user ( Customer . objects . all ( ) , request . user ) <EOL> for customer in customer_queryset : <EOL> projects_count = filter_queryset_for_user ( <EOL> Project . objects . filter ( customer = customer ) , request . user ) . count ( ) <EOL> project_groups_count = filter_queryset_for_user ( <EOL> ProjectGroup . objects . filter ( customer = customer ) , request . user ) . count ( ) <EOL> instances_count = filter_queryset_for_user ( <EOL> models . Instance . objects . filter ( cloud_project_membership__project__customer = customer ) , <EOL> request . user ) . count ( ) <EOL> customer_statistics . append ( { <EOL> '<STR_LIT:name>' : customer . name , <EOL> '<STR_LIT>' : customer . abbreviation , <EOL> '<STR_LIT>' : projects_count , <EOL> '<STR_LIT>' : project_groups_count , <EOL> '<STR_LIT>' : instances_count , <EOL> } ) <EOL> return Response ( customer_statistics , status = status . HTTP_200_OK ) <EOL> class UsageStatsView ( views . APIView ) : <EOL> aggregate_models = { <EOL> '<STR_LIT>' : { '<STR_LIT>' : Customer , '<STR_LIT:path>' : models . Instance . Permissions . customer_path } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : ProjectGroup , '<STR_LIT:path>' : models . Instance . Permissions . project_group_path } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : Project , '<STR_LIT:path>' : models . Instance . Permissions . project_path } , <EOL> } <EOL> def _get_aggregate_queryset ( self , request , aggregate_model_name ) : <EOL> model = self . aggregate_models [ aggregate_model_name ] [ '<STR_LIT>' ] <EOL> return filter_queryset_for_user ( model . objects . all ( ) , request . user ) <EOL> def _get_aggregate_filter ( self , aggregate_model_name , obj ) : <EOL> path = self . aggregate_models [ aggregate_model_name ] [ '<STR_LIT:path>' ] <EOL> return { path : obj } <EOL> def get ( self , request , format = None ) : <EOL> if not ZABBIX_ENABLED : <EOL> raise Http404 ( ) <EOL> usage_stats = [ ] <EOL> aggregate_model_name = request . query_params . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if aggregate_model_name not in self . aggregate_models . keys ( ) : <EOL> return Response ( <EOL> '<STR_LIT>' % '<STR_LIT:U+002CU+0020>' . join ( self . aggregate_models . keys ( ) ) , <EOL> status = status . HTTP_400_BAD_REQUEST ) <EOL> aggregate_queryset = self . _get_aggregate_queryset ( request , aggregate_model_name ) <EOL> if '<STR_LIT>' in request . query_params : <EOL> aggregate_queryset = aggregate_queryset . filter ( uuid__in = request . query_params . getlist ( '<STR_LIT>' ) ) <EOL> visible_instances = filter_queryset_for_user ( models . Instance . objects . all ( ) , request . user ) <EOL> for aggregate_object in aggregate_queryset : <EOL> instances = visible_instances . filter ( <EOL> ** self . _get_aggregate_filter ( aggregate_model_name , aggregate_object ) ) <EOL> if instances : <EOL> hour = <NUM_LIT> * <NUM_LIT> <EOL> data = { <EOL> '<STR_LIT>' : request . query_params . get ( '<STR_LIT>' , int ( time . time ( ) - hour ) ) , <EOL> '<STR_LIT>' : request . query_params . get ( '<STR_LIT:to>' , int ( time . time ( ) ) ) , <EOL> '<STR_LIT>' : request . query_params . get ( '<STR_LIT>' , <NUM_LIT:6> ) , <EOL> '<STR_LIT>' : request . query_params . get ( '<STR_LIT>' ) , <EOL> } <EOL> serializer = serializers . UsageStatsSerializer ( data = data ) <EOL> serializer . is_valid ( raise_exception = True ) <EOL> stats = serializer . get_stats ( instances ) <EOL> usage_stats . append ( { '<STR_LIT:name>' : aggregate_object . name , '<STR_LIT>' : stats } ) <EOL> else : <EOL> usage_stats . append ( { '<STR_LIT:name>' : aggregate_object . name , '<STR_LIT>' : [ ] } ) <EOL> return Response ( usage_stats , status = status . HTTP_200_OK ) <EOL> class FlavorViewSet ( viewsets . ReadOnlyModelViewSet ) : <EOL> """<STR_LIT>""" <EOL> queryset = models . Flavor . objects . all ( ) <EOL> serializer_class = serializers . FlavorSerializer <EOL> lookup_field = '<STR_LIT>' <EOL> filter_backends = ( structure_filters . GenericRoleFilter , ) <EOL> class CloudFilter ( django_filters . FilterSet ) : <EOL> name = django_filters . CharFilter ( lookup_type = '<STR_LIT>' ) <EOL> customer = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> ) <EOL> customer_name = django_filters . CharFilter ( <EOL> lookup_type = '<STR_LIT>' , <EOL> name = '<STR_LIT>' , <EOL> ) <EOL> customer_native_name = django_filters . CharFilter ( <EOL> lookup_type = '<STR_LIT>' , <EOL> name = '<STR_LIT>' , <EOL> ) <EOL> project = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> distinct = True , <EOL> ) <EOL> project_uuid = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> distinct = True , <EOL> ) <EOL> project_name = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> lookup_type = '<STR_LIT>' , <EOL> distinct = True , <EOL> ) <EOL> settings = URLFilter ( view_name = '<STR_LIT>' , name = '<STR_LIT>' , distinct = True ) <EOL> class Meta ( object ) : <EOL> model = models . Cloud <EOL> fields = [ <EOL> '<STR_LIT:name>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> class CloudViewSet ( UpdateOnlyByPaidCustomerMixin , <EOL> core_mixins . UpdateOnlyStableMixin , <EOL> viewsets . ModelViewSet ) : <EOL> """<STR_LIT>""" <EOL> class PaidControl : <EOL> customer_path = '<STR_LIT>' <EOL> queryset = models . Cloud . objects . all ( ) . prefetch_related ( '<STR_LIT>' ) <EOL> serializer_class = serializers . CloudSerializer <EOL> lookup_field = '<STR_LIT>' <EOL> permission_classes = ( <EOL> permissions . IsAuthenticated , <EOL> permissions . DjangoObjectPermissions , <EOL> ) <EOL> filter_backends = ( structure_filters . GenericRoleFilter , filters . DjangoFilterBackend ) <EOL> filter_class = CloudFilter <EOL> def _can_create_or_update_cloud ( self , serializer ) : <EOL> if self . request . user . is_staff : <EOL> return True <EOL> if serializer . validated_data [ '<STR_LIT>' ] . has_user ( self . request . user , CustomerRole . OWNER ) : <EOL> return True <EOL> def perform_create ( self , serializer ) : <EOL> if not self . _can_create_or_update_cloud ( serializer ) : <EOL> raise exceptions . PermissionDenied ( ) <EOL> cloud = serializer . save ( state = SynchronizationStates . IN_SYNC ) <EOL> tasks . sync_services . delay ( [ cloud . uuid . hex ] ) <EOL> def perform_update ( self , serializer ) : <EOL> if not self . _can_create_or_update_cloud ( serializer ) : <EOL> raise exceptions . PermissionDenied ( ) <EOL> super ( CloudViewSet , self ) . perform_update ( serializer ) <EOL> class CloudProjectMembershipFilter ( quota_filters . QuotaFilterSetMixin , django_filters . FilterSet ) : <EOL> cloud = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> ) <EOL> project = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> ) <EOL> project_name = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> distinct = True , <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> project_group = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> ) <EOL> project_group_name = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> distinct = True , <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> ram = quota_filters . QuotaFilter ( <EOL> quota_name = '<STR_LIT>' , <EOL> quota_field = '<STR_LIT>' , <EOL> ) <EOL> vcpu = quota_filters . QuotaFilter ( <EOL> quota_name = '<STR_LIT>' , <EOL> quota_field = '<STR_LIT>' , <EOL> ) <EOL> storage = quota_filters . QuotaFilter ( <EOL> quota_name = '<STR_LIT>' , <EOL> quota_field = '<STR_LIT>' , <EOL> ) <EOL> max_instances = quota_filters . QuotaFilter ( <EOL> quota_name = '<STR_LIT>' , <EOL> quota_field = '<STR_LIT>' , <EOL> ) <EOL> class Meta ( object ) : <EOL> model = models . CloudProjectMembership <EOL> fields = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> order_by = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> order_by_mapping = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> class CloudProjectMembershipViewSet ( UpdateOnlyByPaidCustomerMixin , <EOL> mixins . CreateModelMixin , <EOL> mixins . RetrieveModelMixin , <EOL> mixins . DestroyModelMixin , <EOL> mixins . ListModelMixin , <EOL> core_mixins . UpdateOnlyStableMixin , <EOL> viewsets . GenericViewSet ) : <EOL> """<STR_LIT>""" <EOL> class PaidControl : <EOL> customer_path = '<STR_LIT>' <EOL> queryset = models . CloudProjectMembership . objects . all ( ) <EOL> serializer_class = serializers . CloudProjectMembershipSerializer <EOL> filter_backends = ( structure_filters . GenericRoleFilter , DjangoMappingFilterBackend ) <EOL> permission_classes = ( permissions . IsAuthenticated , permissions . DjangoObjectPermissions ) <EOL> filter_class = CloudProjectMembershipFilter <EOL> def perform_create ( self , serializer ) : <EOL> membership = serializer . save ( ) <EOL> tasks . sync_cloud_membership . delay ( membership . pk , is_membership_creation = True ) <EOL> @ detail_route ( methods = [ '<STR_LIT>' ] ) <EOL> def set_quotas ( self , request , ** kwargs ) : <EOL> if not request . user . is_staff : <EOL> raise exceptions . PermissionDenied ( ) <EOL> instance = self . get_object ( ) <EOL> if instance . state != core_models . SynchronizationStates . IN_SYNC : <EOL> return Response ( { '<STR_LIT>' : '<STR_LIT>' } , <EOL> status = status . HTTP_409_CONFLICT ) <EOL> serializer = serializers . CloudProjectMembershipQuotaSerializer ( data = request . data ) <EOL> serializer . is_valid ( raise_exception = True ) <EOL> data = dict ( serializer . validated_data ) <EOL> if data . get ( '<STR_LIT>' ) is not None : <EOL> quotas = django_settings . NODECONDUCTOR . get ( '<STR_LIT>' , { } ) <EOL> volume_ratio = quotas . get ( '<STR_LIT>' , <NUM_LIT:4> ) <EOL> snapshots_ratio = quotas . get ( '<STR_LIT>' , <NUM_LIT:20> ) <EOL> data [ '<STR_LIT>' ] = volume_ratio * data [ '<STR_LIT>' ] <EOL> data [ '<STR_LIT>' ] = snapshots_ratio * data [ '<STR_LIT>' ] <EOL> instance . schedule_syncing ( ) <EOL> instance . save ( ) <EOL> tasks . push_cloud_membership_quotas . delay ( instance . pk , quotas = data ) <EOL> return Response ( { '<STR_LIT:status>' : '<STR_LIT>' } , <EOL> status = status . HTTP_202_ACCEPTED ) <EOL> @ detail_route ( methods = [ '<STR_LIT>' ] ) <EOL> def import_instance ( self , request , ** kwargs ) : <EOL> membership = self . get_object ( ) <EOL> is_admin = membership . project . has_user ( request . user , ProjectRole . ADMINISTRATOR ) <EOL> if not is_admin and not request . user . is_staff : <EOL> raise exceptions . PermissionDenied ( ) <EOL> if membership . state == core_models . SynchronizationStates . ERRED : <EOL> return Response ( { '<STR_LIT>' : '<STR_LIT>' } , <EOL> status = status . HTTP_409_CONFLICT ) <EOL> serializer = serializers . CloudProjectMembershipLinkSerializer ( data = request . data , <EOL> context = { '<STR_LIT>' : membership } ) <EOL> serializer . is_valid ( raise_exception = True ) <EOL> instance_id = serializer . validated_data [ '<STR_LIT:id>' ] <EOL> template = serializer . validated_data . get ( '<STR_LIT>' ) <EOL> template_id = template . uuid . hex if template else None <EOL> tasks . import_instance . delay ( membership . pk , instance_id = instance_id , template_id = template_id ) <EOL> event_logger . instance_import . info ( <EOL> '<STR_LIT>' , <EOL> event_type = '<STR_LIT>' , <EOL> event_context = { '<STR_LIT>' : instance_id } ) <EOL> return Response ( { '<STR_LIT:status>' : '<STR_LIT>' } , <EOL> status = status . HTTP_202_ACCEPTED ) <EOL> @ detail_route ( methods = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def external_network ( self , request , pk = None ) : <EOL> if request . method == '<STR_LIT>' : <EOL> membership = self . get_object ( ) <EOL> if membership . external_network_id : <EOL> tasks . delete_external_network . delay ( pk ) <EOL> return Response ( { '<STR_LIT:status>' : '<STR_LIT>' } , <EOL> status = status . HTTP_202_ACCEPTED ) <EOL> else : <EOL> return Response ( { '<STR_LIT:status>' : '<STR_LIT>' } , <EOL> status = status . HTTP_204_NO_CONTENT ) <EOL> serializer = serializers . ExternalNetworkSerializer ( data = request . data ) <EOL> serializer . is_valid ( raise_exception = True ) <EOL> tasks . create_external_network . delay ( pk , serializer . data ) <EOL> return Response ( { '<STR_LIT:status>' : '<STR_LIT>' } , <EOL> status = status . HTTP_202_ACCEPTED ) <EOL> @ detail_route ( methods = [ '<STR_LIT>' ] ) <EOL> def allocate_floating_ip ( self , request , pk = None ) : <EOL> """<STR_LIT>""" <EOL> membership = self . get_object ( ) <EOL> if not membership . external_network_id : <EOL> return Response ( { '<STR_LIT>' : '<STR_LIT>' } , <EOL> status = status . HTTP_409_CONFLICT ) <EOL> elif membership . state in core_models . SynchronizationStates . UNSTABLE_STATES : <EOL> raise core_exceptions . IncorrectStateException ( <EOL> detail = '<STR_LIT>' ) <EOL> tasks . allocate_floating_ip . delay ( pk ) <EOL> return Response ( { '<STR_LIT>' : '<STR_LIT>' } , <EOL> status = status . HTTP_202_ACCEPTED ) <EOL> class SecurityGroupFilter ( django_filters . FilterSet ) : <EOL> name = django_filters . CharFilter ( <EOL> name = '<STR_LIT:name>' , <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> description = django_filters . CharFilter ( <EOL> name = '<STR_LIT:description>' , <EOL> lookup_type = '<STR_LIT>' , <EOL> ) <EOL> cloud = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> ) <EOL> project = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> ) <EOL> state = SynchronizationStateFilter ( ) <EOL> class Meta ( object ) : <EOL> model = models . SecurityGroup <EOL> fields = [ <EOL> '<STR_LIT:name>' , <EOL> '<STR_LIT:description>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:state>' , <EOL> ] <EOL> class SecurityGroupViewSet ( core_mixins . UpdateOnlyStableMixin , viewsets . ModelViewSet ) : <EOL> """<STR_LIT>""" <EOL> queryset = models . SecurityGroup . objects . all ( ) <EOL> serializer_class = serializers . SecurityGroupSerializer <EOL> lookup_field = '<STR_LIT>' <EOL> permission_classes = ( permissions . IsAuthenticated , <EOL> permissions . DjangoObjectPermissions ) <EOL> filter_class = SecurityGroupFilter <EOL> filter_backends = ( structure_filters . GenericRoleFilter , filters . DjangoFilterBackend , ) <EOL> def perform_create ( self , serializer ) : <EOL> security_group = serializer . save ( ) <EOL> tasks . create_security_group . delay ( security_group . uuid . hex ) <EOL> def perform_update ( self , serializer ) : <EOL> super ( SecurityGroupViewSet , self ) . perform_update ( serializer ) <EOL> security_group = self . get_object ( ) <EOL> security_group . schedule_syncing ( ) <EOL> security_group . save ( ) <EOL> tasks . update_security_group . delay ( serializer . instance . uuid . hex ) <EOL> def destroy ( self , request , * args , ** kwargs ) : <EOL> security_group = self . get_object ( ) <EOL> security_group . schedule_syncing ( ) <EOL> security_group . save ( ) <EOL> tasks . delete_security_group . delay ( security_group . uuid . hex ) <EOL> return Response ( { '<STR_LIT:status>' : '<STR_LIT>' } , status = status . HTTP_202_ACCEPTED ) <EOL> class IpMappingFilter ( django_filters . FilterSet ) : <EOL> project = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> ) <EOL> private_ip = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> ) <EOL> public_ip = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> ) <EOL> class Meta ( object ) : <EOL> model = models . IpMapping <EOL> fields = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> class IpMappingViewSet ( viewsets . ModelViewSet ) : <EOL> """<STR_LIT>""" <EOL> queryset = models . IpMapping . objects . all ( ) <EOL> serializer_class = serializers . IpMappingSerializer <EOL> lookup_field = '<STR_LIT>' <EOL> filter_backends = ( structure_filters . GenericRoleFilter , filters . DjangoFilterBackend , ) <EOL> permission_classes = ( permissions . IsAuthenticated , <EOL> permissions . DjangoObjectPermissions ) <EOL> filter_class = IpMappingFilter <EOL> class FloatingIPFilter ( django_filters . FilterSet ) : <EOL> project = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> ) <EOL> cloud = django_filters . CharFilter ( <EOL> name = '<STR_LIT>' , <EOL> ) <EOL> class Meta ( object ) : <EOL> model = models . FloatingIP <EOL> fields = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:status>' , <EOL> ] <EOL> class FloatingIPViewSet ( viewsets . ReadOnlyModelViewSet ) : <EOL> """<STR_LIT>""" <EOL> queryset = models . FloatingIP . objects . all ( ) <EOL> serializer_class = serializers . FloatingIPSerializer <EOL> lookup_field = '<STR_LIT>' <EOL> permission_classes = ( permissions . IsAuthenticated , permissions . DjangoObjectPermissions ) <EOL> filter_backends = ( structure_filters . GenericRoleFilter , filters . DjangoFilterBackend ) <EOL> filter_class = FloatingIPFilter <EOL> SupportedServices . register_resource_filter ( models . Instance , InstanceFilter ) </s>
