-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an endpoint for creating pipeline tokens
Add a POST endpoint to be used with the admin token to create tokens for a pipeline. There is no limit on the number of tokens that can be created for a particular pipeline.
- Loading branch information
Showing
4 changed files
with
82 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Copyright (C) 2021, 2022 Genome Research Ltd. | ||
# Copyright (C) 2021, 2022, 2024 Genome Research Ltd. | ||
# | ||
# Author: Kieron Taylor [email protected] | ||
# Author: Marina Gourtovaia [email protected] | ||
|
@@ -27,6 +27,8 @@ | |
from npg_porch.db.models import Pipeline as DbPipeline, Task as DbTask, Event | ||
from npg_porch.models import Task, Pipeline, TaskStateEnum | ||
|
||
from npg_porch.db.models import Token | ||
|
||
|
||
class AsyncDbAccessor: | ||
''' | ||
|
@@ -92,6 +94,17 @@ async def create_pipeline(self, pipeline: Pipeline) -> Pipeline: | |
await session.commit() | ||
return pipe.convert_to_model() | ||
|
||
|
||
async def create_pipeline_token(self, name: str, desc: str) -> str: | ||
session = self.session | ||
db_pipeline = await self._get_pipeline_db_object(name) | ||
|
||
token = Token(pipeline=db_pipeline, description=desc) | ||
session.add(token) | ||
await session.commit() | ||
return token.token | ||
|
||
|
||
async def create_task(self, token_id: int, task: Task) -> Task: | ||
self.logger.debug('CREATE TASK: ' + str(task)) | ||
session = self.session | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Copyright (C) 2021, 2022 Genome Research Ltd. | ||
# Copyright (C) 2021, 2022, 2024 Genome Research Ltd. | ||
# | ||
# Author: Kieron Taylor [email protected] | ||
# Author: Marina Gourtovaia [email protected] | ||
|
@@ -18,7 +18,7 @@ | |
# You should have received a copy of the GNU General Public License along with | ||
# this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
from fastapi import APIRouter, HTTPException, Depends | ||
from fastapi import APIRouter, HTTPException, Depends, Response | ||
import logging | ||
import re | ||
from sqlalchemy.exc import IntegrityError | ||
|
@@ -84,6 +84,32 @@ async def get_pipeline( | |
return pipeline | ||
|
||
|
||
@router.post( | ||
"/{pipeline_name}/token/{token_desc}", | ||
response_model=str, | ||
responses={ | ||
status.HTTP_201_CREATED: {"description": "Token was created"}, | ||
status.HTTP_404_NOT_FOUND: {"description": "Pipeline not found"}, | ||
}, | ||
summary="Create a new token for a pipeline and return it.", | ||
description=""" | ||
Returns a token for the pipeline with the given name. | ||
A valid token issued for any pipeline is required for authorisation.""" | ||
) | ||
async def create_pipeline_token( | ||
pipeline_name: str, | ||
token_desc: str, | ||
db_accessor=Depends(get_DbAccessor), | ||
permissions=Depends(validate) | ||
) -> Response: | ||
try: | ||
token = await db_accessor.create_pipeline_token(name=pipeline_name, desc=token_desc) | ||
except NoResultFound: | ||
raise HTTPException(status_code=404, | ||
detail=f"Pipeline '{pipeline_name}' not found") | ||
return Response(status_code=status.HTTP_201_CREATED, content=token, media_type="text/plain") | ||
|
||
|
||
@router.post( | ||
"/", | ||
response_model=Pipeline, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters