Skip to content

Commit

Permalink
partial update for credentials api to match the new changes
Browse files Browse the repository at this point in the history
  • Loading branch information
arash77 committed Dec 16, 2024
1 parent b6ccdcb commit c58db16
Show file tree
Hide file tree
Showing 3 changed files with 420 additions and 289 deletions.
141 changes: 108 additions & 33 deletions lib/galaxy/schema/credentials.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
from enum import Enum
from typing import List
from typing import (
Dict,
List,
Optional,
)

from pydantic import (
BaseModel,
Field,
RootModel,
)
from typing_extensions import Literal

from galaxy.schema.fields import (
DecodedDatabaseIdField,
EncodedDatabaseIdField,
)
from galaxy.schema.schema import Model

SOURCE_TYPE = Literal["tool"]


class CredentialType(str, Enum):
secret = "secret"
Expand All @@ -30,33 +36,98 @@ class CredentialResponse(Model):
title="Credential Name",
description="Name of the credential",
)
type: CredentialType = Field(


class VariableResponse(CredentialResponse):
value: Optional[str] = Field(
None,
title="Value",
description="Value of the credential",
)


class SecretResponse(CredentialResponse):
already_set: bool = Field(
...,
title="Type",
description="Type of the credential",
title="Already Set",
description="Whether the secret is already set",
)


class CredentialsListResponse(Model):
service_reference: str = Field(
class CredentialGroupResponse(Model):
id: EncodedDatabaseIdField = Field(
...,
title="Service Reference",
description="Reference to the service",
title="Group ID",
description="ID of the group",
)
name: str = Field(
...,
title="Group Name",
description="Name of the group",
)
variables: List[VariableResponse] = Field(
...,
title="Variables",
description="List of variables",
)
user_credentials_id: EncodedDatabaseIdField = Field(
secrets: List[SecretResponse] = Field(
...,
title="Secrets",
description="List of secrets",
)


class UserCredentialBaseResponse(Model):
user_id: EncodedDatabaseIdField = Field(
...,
title="User ID",
description="ID of the user",
)
id: EncodedDatabaseIdField = Field(
...,
title="User Credentials ID",
description="ID of the user credentials",
)
credentials: List[CredentialResponse] = Field(
source_type: SOURCE_TYPE = Field(
...,
title="Credentials",
description="List of credentials",
title="Source Type",
description="Type of the source",
)
source_id: str = Field(
...,
title="Source ID",
description="ID of the source",
)
reference: str = Field(
...,
title="Service Reference",
description="Reference to the service",
)
current_group_name: str = Field(
...,
title="Current Group Name",
description="Name of the current group",
)


class UserCredentialsResponse(UserCredentialBaseResponse):
groups: Dict[str, CredentialGroupResponse] = Field(
...,
title="Groups",
description="Groups of credentials",
)


class UserCredentialCreateResponse(UserCredentialBaseResponse):
group: CredentialGroupResponse = Field(
...,
title="Group",
description="Group of credentials",
)


class UserCredentialsListResponse(RootModel):
root: List[CredentialsListResponse] = Field(
root: List[UserCredentialsResponse] = Field(
...,
title="User Credentials",
description="List of user credentials",
Expand All @@ -82,11 +153,26 @@ class CredentialPayload(Model):


class CredentialsPayload(Model):
service_reference: str = Field(
source_type: SOURCE_TYPE = Field(
...,
title="Source Type",
description="Type of the source",
)
source_id: str = Field(
...,
title="Source ID",
description="ID of the source",
)
reference: str = Field(
...,
title="Service Reference",
description="Reference to the service",
)
group_name: Optional[str] = Field(
"default",
title="Group Name",
description="Name of the group",
)
credentials: List[CredentialPayload] = Field(
...,
title="Credentials",
Expand All @@ -107,25 +193,14 @@ class UpdateCredentialPayload(Model):
)


class UpdateCredentialsPayload(BaseModel):
root: List[UpdateCredentialPayload] = Field(
...,
title="Update Credentials",
description="List of credentials to update",
)


class VerifyCredentialsResponse(Model):
exists: bool = Field(
class UpdateCredentialsPayload(Model):
group_id: DecodedDatabaseIdField = Field(
...,
title="Exists",
description="Indicates if the credentials exist",
title="Group ID",
description="ID of the group",
)


class DeleteCredentialsResponse(Model):
deleted: bool = Field(
credentials: List[UpdateCredentialPayload] = Field(
...,
title="Deleted",
description="Indicates if the credentials were deleted",
title="Update Credentials",
description="List of credentials to update",
)
118 changes: 51 additions & 67 deletions lib/galaxy/webapps/galaxy/api/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@
import logging
from typing import Optional

from fastapi import Query
from fastapi import (
Query,
Response,
status,
)

from galaxy.managers.context import ProvidesUserContext
from galaxy.schema.credentials import (
CredentialsListResponse,
CredentialsPayload,
DeleteCredentialsResponse,
SOURCE_TYPE,
UpdateCredentialsPayload,
UserCredentialCreateResponse,
UserCredentialsListResponse,
VerifyCredentialsResponse,
)
from galaxy.schema.fields import DecodedDatabaseIdField
from galaxy.webapps.galaxy.api import (
Expand Down Expand Up @@ -42,41 +45,20 @@ def list_user_credentials(
self,
user_id: UserIdPathParam,
trans: ProvidesUserContext = DependsOnTrans,
source_type: Optional[str] = Query(
source_type: Optional[SOURCE_TYPE] = Query(
None,
description="The type of source to filter by.",
),
source_id: Optional[str] = Query(
None,
description="The ID of the source to filter by.",
),
group_name: Optional[str] = Query(
None,
description="The name of the group to filter by.",
),
) -> UserCredentialsListResponse:
return self.service.list_user_credentials(trans, user_id, source_type, source_id)

@router.get(
"/api/users/{user_id}/credentials/{user_credentials_id}",
summary="Verifies if credentials have been provided for a specific service",
)
def verify_service_credentials(
self,
user_id: UserIdPathParam,
user_credentials_id: DecodedDatabaseIdField,
trans: ProvidesUserContext = DependsOnTrans,
) -> VerifyCredentialsResponse:
return self.service.verify_service_credentials(trans, user_id, user_credentials_id)

@router.get(
"/api/users/{user_id}/credentials/{user_credentials_id}/{credentials_id}",
summary="Verifies if a credential have been provided",
)
def verify_credentials(
self,
user_id: UserIdPathParam,
user_credentials_id: DecodedDatabaseIdField,
credentials_id: DecodedDatabaseIdField,
trans: ProvidesUserContext = DependsOnTrans,
) -> VerifyCredentialsResponse:
return self.service.verify_credentials(trans, user_credentials_id, credentials_id)
return self.service.list_user_credentials(trans, user_id, source_type, source_id, group_name)

@router.post(
"/api/users/{user_id}/credentials",
Expand All @@ -87,43 +69,45 @@ def provide_credential(
user_id: UserIdPathParam,
payload: CredentialsPayload,
trans: ProvidesUserContext = DependsOnTrans,
) -> CredentialsListResponse:
) -> UserCredentialCreateResponse:
return self.service.provide_credential(trans, user_id, payload)

@router.put(
"/api/users/{user_id}/credentials/{user_credentials_id}",
summary="Updates credentials for a specific secret/variable",
)
def update_credential(
self,
user_id: UserIdPathParam,
user_credentials_id: DecodedDatabaseIdField,
payload: UpdateCredentialsPayload,
trans: ProvidesUserContext = DependsOnTrans,
) -> CredentialsListResponse:
return self.service.update_credential(trans, user_id, user_credentials_id, payload)
# @router.put(
# "/api/users/{user_id}/credentials/{user_credentials_id}",
# summary="Updates credentials for a specific secret/variable",
# )
# def update_credential(
# self,
# user_id: UserIdPathParam,
# user_credentials_id: DecodedDatabaseIdField,
# payload: UpdateCredentialsPayload,
# trans: ProvidesUserContext = DependsOnTrans,
# ) -> CredentialsListResponse:
# return self.service.update_credential(trans, user_id, user_credentials_id, payload)

@router.delete(
"/api/users/{user_id}/credentials/{user_credentials_id}",
summary="Deletes all credentials for a specific service",
)
def delete_service_credentials(
self,
user_id: UserIdPathParam,
user_credentials_id: DecodedDatabaseIdField,
trans: ProvidesUserContext = DependsOnTrans,
) -> DeleteCredentialsResponse:
return self.service.delete_service_credentials(trans, user_id, user_credentials_id)
# @router.delete(
# "/api/users/{user_id}/credentials/{user_credentials_id}",
# summary="Deletes all credentials for a specific service",
# )
# def delete_service_credentials(
# self,
# user_id: UserIdPathParam,
# user_credentials_id: DecodedDatabaseIdField,
# trans: ProvidesUserContext = DependsOnTrans,
# ):
# self.service.delete_service_credentials(trans, user_id, user_credentials_id)
# return Response(status_code=status.HTTP_204_NO_CONTENT)

@router.delete(
"/api/users/{user_id}/credentials/{user_credentials_id}/{credentials_id}",
summary="Deletes a specific credential",
)
def delete_credentials(
self,
user_id: UserIdPathParam,
user_credentials_id: DecodedDatabaseIdField,
credentials_id: DecodedDatabaseIdField,
trans: ProvidesUserContext = DependsOnTrans,
) -> DeleteCredentialsResponse:
return self.service.delete_credentials(trans, user_id, user_credentials_id, credentials_id)
# @router.delete(
# "/api/users/{user_id}/credentials/{user_credentials_id}/{group_id}",
# summary="Deletes a specific credential",
# )
# def delete_credentials(
# self,
# user_id: UserIdPathParam,
# user_credentials_id: DecodedDatabaseIdField,
# group_id: DecodedDatabaseIdField,
# trans: ProvidesUserContext = DependsOnTrans,
# ):
# self.service.delete_credentials(trans, user_id, user_credentials_id, group_id)
# return Response(status_code=status.HTTP_204_NO_CONTENT)
Loading

0 comments on commit c58db16

Please sign in to comment.