示例#1
0
 def index(self):
     with Select(User, "is_root != TRUE") as select:
         select.execute()
         users = select.fetchall()
     ret = []
     for user in users:
         ret.append({
             'id': user.id,
             'fullname': user.fullname,
             'email': user.email
         })
     return jsonify(sorted(ret, key=lambda i: i['id'], reverse=True))
示例#2
0
 def search(self):
     search_term = request.get_json()['searchTerm']
     search_term = '%' + search_term + '%'
     with Select(
             User,
             "is_root != TRUE AND (email ILIKE %s OR fullname ILIKE %s)"
     ) as select:
         select.execute((search_term, search_term))
         ret = select.fetchall()
     return jsonify([{
         'fullname': user.fullname,
         'id': user.id,
         'email': user.email
     } for user in ret])
示例#3
0
 def index(self):
     """ List ONLY slices that a user has been added to"""
     with Select(Slice, 'user_id = %s') as select:
         select.execute((g.user['id'], ))
         slices = select.fetchall()
     ret = []
     for sl in slices:
         lab = Lab.fetchone(id=sl.lab_id)
         ret.append({
             'id': sl.id,
             'status': sl.status,
             'lab': {
                 'name': lab.name,
                 'description': lab.description,
             }
         })
     return jsonify(sorted(ret, key=lambda i: i['id'], reverse=True))
示例#4
0
    def get(self, id):
        sl = Slice.fetchone(id=id)

        # only user or the lab creator can access
        if sl.user_id != g.user['id']:
            lab = Lab.fetchone(id=sl.lab_id)
            if lab.owner_id != g.user['id']:
                return jsonify(
                    message="Access denied to this slice resource"), 403

        # get networks
        with Select(NetworkNode, 'slice_id=%s and status!=%s') as select:
            select.execute((sl.id, 'inactive'))
            networks = [{
                'id': n.id,
                'name': n.name,
                'cidr': n.cidr,
                'status': n.status,
                'x': n.x,
                'y': n.y,
                'type': 'NetworkNode'
            } for n in select.fetchall()]

        instances = []
        links = []

        # get instances
        with Select(
                Instance,
                'instances.slice_id=%s and instances.status!=%s') as select:
            select.execute((sl.id, 'inactive'))
            results = select.fetchall()
            for res in results:
                instance = res
                instances.append({
                    'id': instance.id,
                    'name': instance.name,
                    'public_ip': instance.public_ip,
                    'status': instance.status,
                    'password': instance.password,
                    'x': instance.x,
                    'y': instance.y,
                    'configurations': instance.configurations.
                    value,  # TODO: remove .value here after implement it in postgrespy
                    'type': 'Instance'
                })

                for link in instance.links:
                    links.append({
                        'gid': link['gid'],
                        'ip': link['ip'],
                        'network': link['network'],
                        'target': link['target'],
                        'type': 'NetworkLink'
                    })

        # get routers
        routers = []
        with Select(Router,
                    'routers.slice_id=%s and routers.status!=%s') as select:
            select.execute((sl.id, 'inactive'))
            results = select.fetchall()
            for res in results:
                router = res
                routers.append({
                    'id': router.id,
                    'name': router.name,
                    'public_ip': router.public_ip,
                    'status': router.status,
                    'password': router.password,
                    'x': router.x,
                    'y': router.y,
                    'configurations': router.configurations.
                    value,  # TODO: remove .value here after implement it in postgrespy
                    'type': 'Router'
                })

                for link in router.links:
                    links.append({
                        'gid': link['gid'],
                        'ip': link['ip'],
                        'network': link['network'],
                        'target': link['target'],
                        'type': 'NetworkLink'
                    })
        user = User.fetchone(id=sl.user_id)
        return jsonify({
            'status': sl.status,
            'name': sl.name,
            'username': user.fullname,
            'networks': networks,
            'instances': instances,
            'routers': routers,
            'links': links
        })