Beispiel #1
0
    async def _clusters_from_cluster_ids(
        conn: sa.engine.Connection,
        cluster_ids: Set[PositiveInt],
        offset: int = 0,
        limit: Optional[int] = None,
    ) -> List[Cluster]:
        cluster_id_to_cluster: Dict[PositiveInt, Cluster] = {}
        async for row in conn.execute(
                sa.select([
                    clusters,
                    cluster_to_groups.c.gid,
                    cluster_to_groups.c.read,
                    cluster_to_groups.c.write,
                    cluster_to_groups.c.delete,
                ]).select_from(
                    clusters.join(
                        cluster_to_groups,
                        clusters.c.id == cluster_to_groups.c.cluster_id,
                    )).where(clusters.c.id.in_(cluster_ids)).offset(
                        offset).limit(limit)):
            cluster_access_rights = {
                row[cluster_to_groups.c.gid]:
                ClusterAccessRights(
                    **{
                        "read": row[cluster_to_groups.c.read],
                        "write": row[cluster_to_groups.c.write],
                        "delete": row[cluster_to_groups.c.delete],
                    })
            }

            cluster_id = row[clusters.c.id]
            if cluster_id not in cluster_id_to_cluster:
                cluster_id_to_cluster[cluster_id] = Cluster.construct(
                    id=cluster_id,
                    name=row[clusters.c.name],
                    description=row[clusters.c.description],
                    type=row[clusters.c.type],
                    owner=row[clusters.c.owner],
                    endpoint=row[clusters.c.endpoint],
                    authentication=row[clusters.c.authentication],
                    thumbnail=row[clusters.c.thumbnail],
                    access_rights=cluster_access_rights,
                )
            else:
                cluster_id_to_cluster[cluster_id].access_rights.update(
                    cluster_access_rights)

        return list(cluster_id_to_cluster.values())
Beispiel #2
0
    def creator(user: Dict[str, Any], **cluster_kwargs) -> Cluster:
        cluster_config = Cluster.Config.schema_extra["examples"][0]
        cluster_config["owner"] = user["primary_gid"]
        cluster_config.update(**cluster_kwargs)
        new_cluster = Cluster.parse_obj(cluster_config)
        assert new_cluster

        with postgres_db.connect() as conn:
            # insert basic cluster
            created_cluster = conn.execute(
                sa.insert(clusters).values(
                    new_cluster.to_clusters_db(only_update=False)).returning(
                        sa.literal_column("*"))).one()
            created_cluster_ids.append(created_cluster.id)
            if "access_rights" in cluster_kwargs:
                for gid, rights in cluster_kwargs["access_rights"].items():
                    conn.execute(
                        pg_insert(cluster_to_groups).values(
                            cluster_id=created_cluster.id,
                            gid=gid,
                            **rights.dict()).on_conflict_do_update(
                                index_elements=["gid", "cluster_id"],
                                set_=rights.dict()))
            access_rights_in_db = {}
            for row in conn.execute(
                    sa.select([
                        cluster_to_groups.c.gid,
                        cluster_to_groups.c.read,
                        cluster_to_groups.c.write,
                        cluster_to_groups.c.delete,
                    ]).select_from(clusters.join(cluster_to_groups)).where(
                        clusters.c.id == created_cluster.id)):
                access_rights_in_db[row.gid] = {
                    "read": row[cluster_to_groups.c.read],
                    "write": row[cluster_to_groups.c.write],
                    "delete": row[cluster_to_groups.c.delete],
                }

            return Cluster.construct(
                id=created_cluster.id,
                name=created_cluster.name,
                description=created_cluster.description,
                type=created_cluster.type,
                owner=created_cluster.owner,
                endpoint=created_cluster.endpoint,
                authentication=created_cluster.authentication,
                access_rights=access_rights_in_db,
            )
Beispiel #3
0
    def creator(**overrides) -> Cluster:
        cluster_config = Cluster.Config.schema_extra["examples"][0]
        cluster_config["owner"] = user_db["primary_gid"]
        cluster_config.update(**overrides)
        new_cluster = Cluster.parse_obj(cluster_config)
        assert new_cluster

        with postgres_db.connect() as conn:
            created_cluser_id = conn.scalar(
                # pylint: disable=no-value-for-parameter
                clusters.insert().values(
                    new_cluster.to_clusters_db(only_update=False)
                ).returning(clusters.c.id))
            created_cluster_ids.append(created_cluser_id)
            result = conn.execute(
                sa.select([
                    clusters,
                    cluster_to_groups.c.gid,
                    cluster_to_groups.c.read,
                    cluster_to_groups.c.write,
                    cluster_to_groups.c.delete,
                ]).select_from(
                    clusters.join(
                        cluster_to_groups,
                        clusters.c.id == cluster_to_groups.c.cluster_id,
                    )).where(clusters.c.id == created_cluser_id))

            row = result.fetchone()
            assert row
            return Cluster.construct(
                id=row[clusters.c.id],
                name=row[clusters.c.name],
                description=row[clusters.c.description],
                type=row[clusters.c.type],
                owner=row[clusters.c.owner],
                endpoint=row[clusters.c.endpoint],
                authentication=row[clusters.c.authentication],
                access_rights={
                    row[clusters.c.owner]: {
                        "read": row[cluster_to_groups.c.read],
                        "write": row[cluster_to_groups.c.write],
                        "delete": row[cluster_to_groups.c.delete],
                    }
                },
            )
Beispiel #4
0
    async def create_cluster(self, new_cluster: ClusterCreate) -> Cluster:
        async with self.engine.acquire() as conn:
            created_cluser_id: int = await conn.scalar(
                # pylint: disable=no-value-for-parameter
                clusters.insert().values(
                    new_cluster.to_clusters_db(only_update=False)
                ).returning(clusters.c.id))

            result = await conn.execute(
                sa.select([
                    clusters,
                    cluster_to_groups.c.gid,
                    cluster_to_groups.c.read,
                    cluster_to_groups.c.write,
                    cluster_to_groups.c.delete,
                ]).select_from(
                    clusters.join(
                        cluster_to_groups,
                        clusters.c.id == cluster_to_groups.c.cluster_id,
                    )).where(clusters.c.id == created_cluser_id))

            row = await result.fetchone()

            assert row  # nosec

            return Cluster.construct(
                id=row[clusters.c.id],
                name=row[clusters.c.name],
                description=row[clusters.c.description],
                type=row[clusters.c.type],
                owner=row[clusters.c.owner],
                endpoint=row[clusters.c.endpoint],
                authentication=row[clusters.c.authentication],
                access_rights={
                    row[clusters.c.owner]: {
                        "read": row[cluster_to_groups.c.read],
                        "write": row[cluster_to_groups.c.write],
                        "delete": row[cluster_to_groups.c.delete],
                    }
                },
            )