Example #1
0
    def post(self, request):
        serializer = serializers.AggregationProfileSerializer(
            data=request.data)

        if serializer.is_valid():
            serializer.save()

            groupaggr = poem_models.GroupOfAggregations.objects.get(
                name=request.data['groupname'])
            aggr = poem_models.Aggregation.objects.get(
                apiid=request.data['apiid'])
            groupaggr.aggregations.add(aggr)

            data = {
                'endpoint_group': request.data['endpoint_group'],
                'metric_operation': request.data['metric_operation'],
                'profile_operation': request.data['profile_operation'],
                'metric_profile': request.data['metric_profile'],
                'groups': json.loads(request.data['groups'])
            }

            create_profile_history(aggr, data, request.user)

            return Response(serializer.data, status=status.HTTP_201_CREATED)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Example #2
0
    def put(self, request):
        profile = poem_models.MetricProfiles.objects.get(
            apiid=request.data['apiid'])
        profile.name = request.data['name']
        profile.description = request.data['description']
        profile.groupname = request.data['groupname']
        profile.save()

        groupprofile = poem_models.GroupOfMetricProfiles.objects.get(
            name=request.data['groupname'])
        groupprofile.metricprofiles.add(profile)

        create_profile_history(profile,
                               dict(request.data)['services'], request.user,
                               request.data['description'])

        return Response(status=status.HTTP_201_CREATED)
Example #3
0
    def put(self, request):
        profile = ThresholdsProfiles.objects.get(
            apiid=request.data['apiid']
        )
        profile.name = request.data['name']
        profile.groupname = request.data['groupname']
        profile.save()

        group = GroupOfThresholdsProfiles.objects.get(
            name=request.data['groupname']
        )
        group.thresholdsprofiles.add(profile)

        data = {'rules': request.data['rules']}

        create_profile_history(profile, data, request.user)

        return Response(status=status.HTTP_201_CREATED)
Example #4
0
    def post(self, request):
        serializer = serializers.MetricProfileSerializer(data=request.data)

        if serializer.is_valid():
            serializer.save()

            groupprofile = poem_models.GroupOfMetricProfiles.objects.get(
                name=request.data['groupname'])
            profile = poem_models.MetricProfiles.objects.get(
                apiid=request.data['apiid'])
            groupprofile.metricprofiles.add(profile)

            create_profile_history(profile,
                                   dict(request.data)['services'],
                                   request.user, request.data['description'])

            return Response(serializer.data, status=status.HTTP_201_CREATED)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Example #5
0
    def post(self, request):
        serializer = serializers.ThresholdsProfileSerializer(data=request.data)

        if serializer.is_valid():
            tp = serializer.save()

            group = GroupOfThresholdsProfiles.objects.get(
                name=request.data['groupname']
            )
            group.thresholdsprofiles.add(tp)

            data = {'rules': request.data['rules']}

            create_profile_history(tp, data, request.user)

            return Response(serializer.data, status=status.HTTP_201_CREATED)

        else:
            return Response(serializer.data, status=status.HTTP_400_BAD_REQUEST)
Example #6
0
    def put(self, request):
        aggr = poem_models.Aggregation.objects.get(apiid=request.data['apiid'])
        aggr.groupname = request.data['groupname']
        aggr.name = request.data['name']
        aggr.save()

        groupaggr = poem_models.GroupOfAggregations.objects.get(
            name=request.data['groupname'])
        groupaggr.aggregations.add(aggr)

        data = {
            'endpoint_group': request.data['endpoint_group'],
            'metric_operation': request.data['metric_operation'],
            'profile_operation': request.data['profile_operation'],
            'metric_profile': request.data['metric_profile'],
            'groups': json.loads(request.data['groups'])
        }

        create_profile_history(aggr, data, request.user)

        return Response(status=status.HTTP_201_CREATED)
Example #7
0
def sync_webapi(api, model):
    token = MyAPIKey.objects.get(name="WEB-API")

    headers = {'Accept': 'application/json', 'x-api-key': token.token}
    response = requests.get(api, headers=headers, timeout=180)
    response.raise_for_status()
    data = response.json()['data']

    data_api = set([p['id'] for p in data])
    data_db = set(model.objects.all().values_list('apiid', flat=True))
    entries_not_indb = data_api.difference(data_db)
    entries_indb = data_api.intersection(data_db)

    new_entries = []
    for p in data:
        if p['id'] in entries_not_indb:
            if p.get('info', False):
                new_entries.append(
                    dict(name=p['info']['name'],
                         description=p['info'].get('description', ''),
                         apiid=p['id'],
                         groupname=''))
            else:
                new_entries.append(
                    dict(name=p['name'],
                         description=p.get('description', ''),
                         apiid=p['id'],
                         groupname=''))

    if new_entries:
        for entry in new_entries:
            instance = model.objects.create(**entry)

            if isinstance(instance, poem_models.MetricProfiles):
                services = []
                for item in data:
                    if item['id'] == instance.apiid:
                        for service in item['services']:
                            for metric in service['metrics']:
                                services.append(
                                    dict(service=service['service'],
                                         metric=metric))
                        description = item.get('description', '')
                create_profile_history(instance, services, 'poem', description)

            if isinstance(instance, poem_models.Aggregation):
                for item in data:
                    if item['id'] == instance.apiid:
                        aggr_data = {
                            'endpoint_group': item['endpoint_group'],
                            'metric_operation': item['metric_operation'],
                            'profile_operation': item['profile_operation'],
                            'metric_profile': item['metric_profile']['name'],
                            'groups': item['groups']
                        }
                create_profile_history(instance, aggr_data, 'poem')

            if isinstance(instance, poem_models.ThresholdsProfiles):
                for item in data:
                    if item['id'] == instance.apiid:
                        tp_data = {'rules': item['rules']}
                create_profile_history(instance, tp_data, 'poem')

    entries_deleted_onapi = data_db.difference(data_api)
    for p in entries_deleted_onapi:
        instance = model.objects.get(apiid=p)
        poem_models.TenantHistory.objects.filter(
            object_id=instance.id,
            content_type=ContentType.objects.get_for_model(model)).delete()
        instance.delete()

    for p in data:
        if p['id'] in entries_indb:
            instance = model.objects.get(apiid=p['id'])
            if p.get('info', False):
                instance.name = p['info']['name']
                instance.description = p['info'].get('description', '')
            else:
                instance.name = p['name']
                instance.description = p.get('description', '')
            instance.save()
Example #8
0
    def put(self, request):
        try:
            userprofile = poem_models.UserProfile.objects.get(
                user=request.user)
            userprofile_groups = userprofile.groupsofthresholdsprofiles.all()

            if userprofile_groups.count() == 0 and \
                    not request.user.is_superuser:
                return error_response(
                    status_code=status.HTTP_401_UNAUTHORIZED,
                    detail='You do not have permission to change thresholds '
                    'profiles.')

            else:
                if request.data['apiid']:
                    profile = poem_models.ThresholdsProfiles.objects.get(
                        apiid=request.data['apiid'])

                    group0 = None
                    if profile.groupname:
                        group0 = \
                            poem_models.GroupOfThresholdsProfiles.objects.get(
                                name=profile.groupname
                            )

                        if group0 not in userprofile_groups and \
                                not request.user.is_superuser:
                            return error_response(
                                status_code=status.HTTP_401_UNAUTHORIZED,
                                detail='You do not have permission to change '
                                'thresholds profiles assigned to this '
                                'group.')

                    else:
                        if not request.user.is_superuser:
                            return error_response(
                                status_code=status.HTTP_401_UNAUTHORIZED,
                                detail='You do not have permission to change '
                                'thresholds profiles without assigned '
                                'group.')

                    group = poem_models.GroupOfThresholdsProfiles.objects.get(
                        name=request.data['groupname'])

                    change_perm = request.user.is_superuser or \
                        group in userprofile_groups

                    if change_perm:
                        profile.groupname = request.data['groupname']
                        profile.save()

                        group.thresholdsprofiles.add(profile)
                        if group0:
                            group0.thresholdsprofiles.remove(profile)

                        data = {'rules': request.data['rules']}

                        create_profile_history(profile, data, request.user)

                        return Response(status=status.HTTP_201_CREATED)

                    else:
                        return error_response(
                            status_code=status.HTTP_401_UNAUTHORIZED,
                            detail='You do not have permission to assign '
                            'thresholds profiles to the given group.')

                else:
                    return error_response(
                        status_code=status.HTTP_400_BAD_REQUEST,
                        detail='Apiid field should be specified.')

        except poem_models.UserProfile.DoesNotExist:
            return error_response(
                status_code=status.HTTP_404_NOT_FOUND,
                detail='No user profile for authenticated user.')

        except poem_models.ThresholdsProfiles.DoesNotExist:
            return error_response(status_code=status.HTTP_404_NOT_FOUND,
                                  detail='Thresholds profile does not exist.')

        except poem_models.GroupOfThresholdsProfiles.DoesNotExist:
            return error_response(
                status_code=status.HTTP_404_NOT_FOUND,
                detail='Group of thresholds profiles does not exist.')

        except KeyError as e:
            return error_response(status_code=status.HTTP_400_BAD_REQUEST,
                                  detail='Missing data key: {}'.format(
                                      e.args[0]))
Example #9
0
    def post(self, request):
        serializer = serializers.ThresholdsProfileSerializer(data=request.data)

        try:
            userprofile = poem_models.UserProfile.objects.get(
                user=request.user)
            userprofile_groups = userprofile.groupsofthresholdsprofiles.all()

            if userprofile_groups.count() == 0 and \
                    not request.user.is_superuser:
                return error_response(
                    status_code=status.HTTP_401_UNAUTHORIZED,
                    detail='You do not have permission to add thresholds '
                    'profiles.')

            else:
                group = None
                if 'groupname' in request.data and request.data['groupname']:
                    group = poem_models.GroupOfThresholdsProfiles.objects.get(
                        name=request.data['groupname'])

                add_perm = request.user.is_superuser or \
                    group and group in userprofile_groups

                if add_perm:
                    if serializer.is_valid():
                        tp = serializer.save()

                        if group:
                            group.thresholdsprofiles.add(tp)

                        data = {'rules': request.data['rules']}

                        create_profile_history(tp, data, request.user)

                        return Response(serializer.data,
                                        status=status.HTTP_201_CREATED)

                    else:
                        details = []
                        for error in serializer.errors:
                            details.append('{}: {}'.format(
                                error, serializer.errors[error][0]))
                        return error_response(
                            status_code=status.HTTP_400_BAD_REQUEST,
                            detail=' '.join(details))

                else:
                    return error_response(
                        status_code=status.HTTP_401_UNAUTHORIZED,
                        detail='You do not have permission to assign '
                        'thresholds profiles to the given group.')

        except poem_models.UserProfile.DoesNotExist:
            return error_response(
                status_code=status.HTTP_404_NOT_FOUND,
                detail='No user profile for authenticated user.')

        except poem_models.GroupOfThresholdsProfiles.DoesNotExist:
            return error_response(
                status_code=status.HTTP_404_NOT_FOUND,
                detail='Group of thresholds profiles does not exist.')
Example #10
0
    def put(self, request):
        if request.data['apiid']:
            try:
                aggr = poem_models.Aggregation.objects.get(
                    apiid=request.data['apiid'])

                init_groupaggr = None
                if aggr.groupname and not request.user.is_superuser:
                    try:
                        init_groupaggr = \
                            poem_models.GroupOfAggregations.objects.get(
                                name=aggr.groupname
                            )

                    except poem_models.GroupOfAggregations.DoesNotExist:
                        return error_response(
                            detail="Initial profile's group of aggregations "
                            "does not exist.",
                            status_code=status.HTTP_404_NOT_FOUND)

                userprofile = poem_models.UserProfile.objects.get(
                    user=request.user)

            except poem_models.Aggregation.DoesNotExist:
                return error_response(
                    detail='Aggregation profile with given apiid does not '
                    'exist.',
                    status_code=status.HTTP_404_NOT_FOUND)

            except poem_models.UserProfile.DoesNotExist:
                return error_response(
                    detail='User profile for the given user does not exist.',
                    status_code=status.HTTP_404_NOT_FOUND)

            else:
                if init_groupaggr in userprofile.groupsofaggregations.all() or \
                        request.user.is_superuser:
                    try:
                        if request.data['groupname']:
                            groupaggr = \
                                poem_models.GroupOfAggregations.objects.get(
                                    name=request.data['groupname']
                                )

                        else:
                            return error_response(
                                detail='Please provide group of aggregations.',
                                status_code=status.HTTP_400_BAD_REQUEST)

                    except poem_models.GroupOfAggregations.DoesNotExist:
                        return error_response(
                            status_code=status.HTTP_404_NOT_FOUND,
                            detail='Given group of aggregations does not exist.'
                        )

                    else:
                        if groupaggr in userprofile.groupsofaggregations.all() \
                                or request.user.is_superuser:
                            aggr.groupname = request.data['groupname']
                            aggr.save()

                            groupaggr.aggregations.add(aggr)

                            data = {
                                'endpoint_group':
                                request.data['endpoint_group'],
                                'metric_operation':
                                request.data['metric_operation'],
                                'profile_operation':
                                request.data['profile_operation'],
                                'metric_profile':
                                request.data['metric_profile'],
                                'groups':
                                json.loads(request.data['groups'])
                            }

                            create_profile_history(aggr, data, request.user)

                            return Response(status=status.HTTP_201_CREATED)

                        else:
                            return error_response(
                                detail='You do not have permission to change '
                                'resources in the given group.',
                                status_code=status.HTTP_401_UNAUTHORIZED)

                else:
                    return error_response(
                        detail='You do not have permission to change resources '
                        'in the given group.',
                        status_code=status.HTTP_401_UNAUTHORIZED)

        else:
            return error_response(detail='Apiid field undefined!',
                                  status_code=status.HTTP_400_BAD_REQUEST)
Example #11
0
    def post(self, request):
        serializer = serializers.AggregationProfileSerializer(
            data=request.data)

        if serializer.is_valid():
            try:
                if request.data['groupname']:
                    groupaggr = poem_models.GroupOfAggregations.objects.get(
                        name=request.data['groupname'])

                else:
                    return error_response(
                        status_code=status.HTTP_400_BAD_REQUEST,
                        detail='You must provide a group of aggregations.')

                userprofile = poem_models.UserProfile.objects.get(
                    user=request.user)

            except poem_models.GroupOfAggregations.DoesNotExist:
                return error_response(
                    detail='Given group of aggregations does not exist.',
                    status_code=status.HTTP_404_NOT_FOUND)

            except poem_models.UserProfile.DoesNotExist:
                return error_response(
                    details='No user profile for authenticated user.',
                    status_code=status.HTTP_404_NOT_FOUND)

            else:
                if groupaggr in userprofile.groupsofaggregations.all() or \
                        request.user.is_superuser:
                    serializer.save()

                    aggr = poem_models.Aggregation.objects.get(
                        apiid=request.data['apiid'])
                    groupaggr.aggregations.add(aggr)

                    data = {
                        'endpoint_group': request.data['endpoint_group'],
                        'metric_operation': request.data['metric_operation'],
                        'profile_operation': request.data['profile_operation'],
                        'metric_profile': request.data['metric_profile'],
                        'groups': json.loads(request.data['groups'])
                    }

                    create_profile_history(aggr, data, request.user)

                    return Response(serializer.data,
                                    status=status.HTTP_201_CREATED)

                else:
                    return error_response(
                        detail='You do not have permission to add resources to '
                        'the given group.',
                        status_code=status.HTTP_401_UNAUTHORIZED)

        else:
            details = []
            for error in serializer.errors:
                details.append('{}: {}'.format(error,
                                               serializer.errors[error][0]))

            return error_response(detail=' '.join(details),
                                  status_code=status.HTTP_400_BAD_REQUEST)
Example #12
0
    def post(self, request):
        serializer = serializers.MetricProfileSerializer(data=request.data)

        try:
            userprofile = poem_models.UserProfile.objects.get(
                user=request.user)

            if len(userprofile.groupsofmetricprofiles.all()) == 0 and \
                    not request.user.is_superuser:
                return error_response(
                    status_code=status.HTTP_401_UNAUTHORIZED,
                    detail='You do not have permission to add metric '
                    'profiles.')

        except poem_models.UserProfile.DoesNotExist:
            return error_response(
                status_code=status.HTTP_404_NOT_FOUND,
                detail='No user profile for authenticated user.')

        else:
            if serializer.is_valid():
                try:
                    groupprofile = \
                        poem_models.GroupOfMetricProfiles.objects.get(
                            name=request.data['groupname']
                        )

                    add_permission = \
                        request.user.is_superuser or \
                        groupprofile in userprofile.groupsofmetricprofiles.all()

                    if add_permission:
                        serializer.save()

                        profile = poem_models.MetricProfiles.objects.get(
                            apiid=request.data['apiid'])
                        groupprofile.metricprofiles.add(profile)

                        create_profile_history(profile,
                                               dict(request.data)['services'],
                                               request.user,
                                               request.data['description'])

                        return Response(serializer.data,
                                        status=status.HTTP_201_CREATED)

                    else:
                        return error_response(
                            status_code=status.HTTP_401_UNAUTHORIZED,
                            detail='You do not have permission to assign '
                            'metric profiles to the given group.')

                except poem_models.GroupOfMetricProfiles.DoesNotExist:
                    return error_response(
                        status_code=status.HTTP_404_NOT_FOUND,
                        detail='Group of metric profiles not found.')

            else:
                details = []
                for error in serializer.errors:
                    details.append('{}: {}'.format(
                        error, serializer.errors[error][0]))
                return Response({'detail': ' '.join(details)},
                                status=status.HTTP_400_BAD_REQUEST)
Example #13
0
    def put(self, request):
        try:
            userprofile = poem_models.UserProfile.objects.get(
                user=request.user)

            if len(userprofile.groupsofmetricprofiles.all()) == 0 and \
                    not request.user.is_superuser:
                return error_response(
                    status_code=status.HTTP_401_UNAUTHORIZED,
                    detail='You do not have permission to change metric '
                    'profiles.')

        except poem_models.UserProfile.DoesNotExist:
            return error_response(
                status_code=status.HTTP_404_NOT_FOUND,
                detail='No user profile for authenticated user.')

        else:
            try:
                groupprofile = poem_models.GroupOfMetricProfiles.objects.get(
                    name=request.data['groupname'])

                change_permission = request.user.is_superuser or \
                    groupprofile in userprofile.groupsofmetricprofiles.all()

                if request.data['apiid']:
                    try:
                        profile = poem_models.MetricProfiles.objects.get(
                            apiid=request.data['apiid'])
                        init_group = \
                            poem_models.GroupOfMetricProfiles.objects.get(
                                name=profile.groupname
                            )

                    except poem_models.MetricProfiles.DoesNotExist:
                        return error_response(
                            status_code=status.HTTP_404_NOT_FOUND,
                            detail='Metric profile does not exist.')

                    except poem_models.GroupOfMetricProfiles.DoesNotExist:
                        return error_response(
                            status_code=status.HTTP_404_NOT_FOUND,
                            detail='Group of metric profiles does not exist.')

                    else:
                        if not request.user.is_superuser and \
                                init_group not in \
                                userprofile.groupsofmetricprofiles.all():
                            return error_response(
                                status_code=status.HTTP_401_UNAUTHORIZED,
                                detail='You do not have permission to change '
                                'metric profiles in the given group.')

                        else:
                            profile.description = request.data['description']
                            profile.groupname = request.data['groupname']

                            if change_permission:
                                profile.save()

                                groupprofile.metricprofiles.add(profile)

                                create_profile_history(
                                    profile,
                                    dict(request.data)['services'],
                                    request.user, request.data['description'])

                                return Response(status=status.HTTP_201_CREATED)

                            else:
                                return error_response(
                                    status_code=status.HTTP_401_UNAUTHORIZED,
                                    detail='You do not have permission to '
                                    'assign metric profiles to the given'
                                    ' group.')

                else:
                    if change_permission:
                        return error_response(
                            detail='Apiid field undefined!',
                            status_code=status.HTTP_400_BAD_REQUEST)

                    else:
                        return error_response(
                            status_code=status.HTTP_401_UNAUTHORIZED,
                            detail='You do not have permission to assign '
                            'metric profiles to the given group.')

            except poem_models.GroupOfMetricProfiles.DoesNotExist:
                return error_response(
                    status_code=status.HTTP_404_NOT_FOUND,
                    detail='Given group of metric profiles does not exist.')