예제 #1
0
    def create(cls, issue_id, *, properties=None, auto_commit=False):
        """Creates a new Issue object with the properties and tags provided

        Attributes:
            issue_id (str): Unique identifier for the issue object
            account (:obj:`Account`): Account which owns the issue
            properties (dict): Dictionary of properties for the issue object.
        """
        if cls.get(issue_id):
            raise IssueException('Issue {} already exists'.format(issue_id))

        res = Issue()
        res.issue_id = issue_id
        res.issue_type_id = IssueType.get(cls.issue_type).issue_type_id

        if properties:
            for name, value in properties.items():
                prop = IssueProperty()
                prop.issue_id = res.issue_id
                prop.name = name
                prop.value = value.isoformat() if type(value) == datetime else value
                res.properties.append(prop)
                db.session.add(prop)

        db.session.add(res)
        if auto_commit:
            db.session.commit()

        return cls.get(res.issue_id)
예제 #2
0
    def get_all(cls):
        """Returns a list of all issues of a given type

        Returns:
            list of issue objects
        """
        issues = db.Issue.find(
            Issue.issue_type_id == IssueType.get(cls.issue_type).issue_type_id)

        return {res.issue_id: cls(res) for res in issues}
예제 #3
0
    def get_all(cls):
        """Returns a list of all issues of a given type

        Returns:
            list of issue objects
        """
        qry = Issue.query.filter(
            Issue.issue_type_id == IssueType.get(cls.issue_type).issue_type_id
        )

        return {res.issue_id: cls(res) for res in qry.all()}
예제 #4
0
    def get(cls, issue_id):
        """Returns the class object identified by `issue_id`

        Args:
            issue_id (str): Unique EC2 Instance ID to load from database

        Returns:
            EC2 Instance object if found, else None
        """
        res = Issue.get(issue_id, IssueType.get(cls.issue_type).issue_type_id)
        return cls(res) if res else None
예제 #5
0
    def search(cls, *, limit=100, page=1, properties=None, return_query=False):
        """Search for issues based on the provided filters

        Args:
            limit (`int`): Number of results to return. Default: 100
            page (`int`): Pagination offset for results. Default: 1
            properties (`dict`): A `dict` containing property name and value pairs. Values can be either a str or a list
            of strings, in which case a boolean OR search is performed on the values
            return_query (`bool`): Returns the query object prior to adding the limit and offset functions. Allows for
            sub-classes to amend the search feature with extra conditions. The calling function must handle pagination
            on its own

        Returns:
            `list` of `Issue`, `sqlalchemy.orm.Query`
        """
        qry = Issue.query.order_by(Issue.issue_id).filter(
            Issue.issue_type_id == IssueType.get(cls.issue_type).issue_type_id
        )

        if properties:
            for prop_name, value in properties.items():
                alias = aliased(IssueProperty)
                qry = qry.join(alias, Issue.issue_id == alias.issue_id)
                if type(value) == list:
                    where_clause = []
                    for item in value:
                        where_clause.append(alias.value == item)

                    qry = qry.filter(
                        and_(
                            alias.name == prop_name,
                            or_(*where_clause)
                         ).self_group()
                    )
                else:
                    qry = qry.filter(
                        and_(
                            alias.name == prop_name,
                            alias.value == value
                        ).self_group()
                    )

        if return_query:
            return qry

        total = qry.count()
        qry = qry.limit(limit)
        qry = qry.offset((page - 1) * limit if page > 1 else 0)

        return total, [cls(x) for x in qry.all()]
예제 #6
0
from sqlalchemy.orm import aliased
from sqlalchemy.sql import func, and_

from cloud_inquisitor.constants import ROLE_USER, AccountTypes
from cloud_inquisitor.database import db
from cloud_inquisitor.plugins import BaseView
from cloud_inquisitor.plugins.types.accounts import AWSAccount
from cloud_inquisitor.plugins.types.issues import RequiredTagsIssue
from cloud_inquisitor.plugins.types.resources import EC2Instance
from cloud_inquisitor.schema import (Account, Resource, ResourceType,
                                     ResourceProperty, IssueProperty, Issue,
                                     IssueType, AccountType)
from cloud_inquisitor.utils import MenuItem
from cloud_inquisitor.wrappers import check_auth, rollback

reqtag_type_id = IssueType.get(RequiredTagsIssue.issue_type).issue_type_id
ec2_type_id = ResourceType.get(EC2Instance.resource_type).resource_type_id
aws_account_type_id = AccountType.get(AWSAccount.account_type).account_type_id


class StatsGet(BaseView):
    URLS = ['/api/v1/stats']
    MENU_ITEMS = [
        MenuItem('default', 'Dashboard', 'dashboard', 'dashboard', order=1)
    ]

    @rollback
    @check_auth(ROLE_USER)
    def get(self):
        rfc26 = []
        accounts = list(AWSAccount.get_all(include_disabled=False).values())