env.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import logging
  2. from pathlib import Path
  3. from typing import Any, List, Union
  4. import typer
  5. from pydantic import BaseModel
  6. from typing_extensions import Annotated
  7. from fastapi_cloud_cli.utils.api import APIClient
  8. from fastapi_cloud_cli.utils.apps import get_app_config
  9. from fastapi_cloud_cli.utils.auth import is_logged_in
  10. from fastapi_cloud_cli.utils.cli import get_rich_toolkit, handle_http_errors
  11. from fastapi_cloud_cli.utils.env import validate_environment_variable_name
  12. logger = logging.getLogger(__name__)
  13. class EnvironmentVariable(BaseModel):
  14. name: str
  15. value: str
  16. class EnvironmentVariableResponse(BaseModel):
  17. data: List[EnvironmentVariable]
  18. def _get_environment_variables(app_id: str) -> EnvironmentVariableResponse:
  19. with APIClient() as client:
  20. response = client.get(f"/apps/{app_id}/environment-variables/")
  21. response.raise_for_status()
  22. return EnvironmentVariableResponse.model_validate(response.json())
  23. def _delete_environment_variable(app_id: str, name: str) -> bool:
  24. with APIClient() as client:
  25. response = client.delete(f"/apps/{app_id}/environment-variables/{name}")
  26. if response.status_code == 404:
  27. return False
  28. response.raise_for_status()
  29. return True
  30. def _set_environment_variable(
  31. app_id: str, name: str, value: str, is_secret: bool = False
  32. ) -> None:
  33. with APIClient() as client:
  34. response = client.post(
  35. f"/apps/{app_id}/environment-variables/",
  36. json={"name": name, "value": value, "is_secret": is_secret},
  37. )
  38. response.raise_for_status()
  39. env_app = typer.Typer()
  40. @env_app.command()
  41. def list(
  42. path: Annotated[
  43. Union[Path, None],
  44. typer.Argument(
  45. help="A path to the folder containing the app you want to deploy"
  46. ),
  47. ] = None,
  48. ) -> Any:
  49. """
  50. List the environment variables for the app.
  51. """
  52. with get_rich_toolkit(minimal=True) as toolkit:
  53. if not is_logged_in():
  54. toolkit.print(
  55. "No credentials found. Use [blue]`fastapi login`[/] to login.",
  56. tag="auth",
  57. )
  58. raise typer.Exit(1)
  59. app_path = path or Path.cwd()
  60. app_config = get_app_config(app_path)
  61. if not app_config:
  62. toolkit.print(
  63. f"No app found in the folder [bold]{app_path}[/].",
  64. )
  65. raise typer.Exit(1)
  66. with toolkit.progress(
  67. "Fetching environment variables...", transient=True
  68. ) as progress:
  69. with handle_http_errors(progress):
  70. environment_variables = _get_environment_variables(app_config.app_id)
  71. if not environment_variables.data:
  72. toolkit.print("No environment variables found.")
  73. return
  74. toolkit.print("Environment variables:")
  75. toolkit.print_line()
  76. for env_var in environment_variables.data:
  77. toolkit.print(f"[bold]{env_var.name}[/]")
  78. @env_app.command()
  79. def delete(
  80. name: Union[str, None] = typer.Argument(
  81. None,
  82. help="The name of the environment variable to delete",
  83. ),
  84. path: Annotated[
  85. Union[Path, None],
  86. typer.Argument(
  87. help="A path to the folder containing the app you want to deploy"
  88. ),
  89. ] = None,
  90. ) -> Any:
  91. """
  92. Delete an environment variable from the app.
  93. """
  94. with get_rich_toolkit(minimal=True) as toolkit:
  95. # TODO: maybe this logic can be extracted to a function
  96. if not is_logged_in():
  97. toolkit.print(
  98. "No credentials found. Use [blue]`fastapi login`[/] to login.",
  99. tag="auth",
  100. )
  101. raise typer.Exit(1)
  102. path_to_deploy = path or Path.cwd()
  103. app_config = get_app_config(path_to_deploy)
  104. if not app_config:
  105. toolkit.print(
  106. f"No app found in the folder [bold]{path_to_deploy}[/].",
  107. )
  108. raise typer.Exit(1)
  109. if not name:
  110. with toolkit.progress(
  111. "Fetching environment variables...", transient=True
  112. ) as progress:
  113. with handle_http_errors(progress):
  114. environment_variables = _get_environment_variables(
  115. app_config.app_id
  116. )
  117. if not environment_variables.data:
  118. toolkit.print("No environment variables found.")
  119. return
  120. name = toolkit.ask(
  121. "Select the environment variable to delete:",
  122. options=[
  123. {"name": env_var.name, "value": env_var.name}
  124. for env_var in environment_variables.data
  125. ],
  126. )
  127. assert name
  128. else:
  129. if not validate_environment_variable_name(name):
  130. toolkit.print(
  131. f"The environment variable name [bold]{name}[/] is invalid."
  132. )
  133. raise typer.Exit(1)
  134. toolkit.print_line()
  135. with toolkit.progress(
  136. "Deleting environment variable", transient=True
  137. ) as progress:
  138. with handle_http_errors(progress):
  139. deleted = _delete_environment_variable(app_config.app_id, name)
  140. if not deleted:
  141. toolkit.print("Environment variable not found.")
  142. raise typer.Exit(1)
  143. toolkit.print(f"Environment variable [bold]{name}[/] deleted.")
  144. @env_app.command()
  145. def set(
  146. name: Union[str, None] = typer.Argument(
  147. None,
  148. help="The name of the environment variable to set",
  149. ),
  150. value: Union[str, None] = typer.Argument(
  151. None,
  152. help="The value of the environment variable to set",
  153. ),
  154. path: Annotated[
  155. Union[Path, None],
  156. typer.Argument(
  157. help="A path to the folder containing the app you want to deploy"
  158. ),
  159. ] = None,
  160. ) -> Any:
  161. """
  162. Set an environment variable for the app.
  163. """
  164. with get_rich_toolkit(minimal=True) as toolkit:
  165. if not is_logged_in():
  166. toolkit.print(
  167. "No credentials found. Use [blue]`fastapi login`[/] to login.",
  168. tag="auth",
  169. )
  170. raise typer.Exit(1)
  171. path_to_deploy = path or Path.cwd()
  172. app_config = get_app_config(path_to_deploy)
  173. if not app_config:
  174. toolkit.print(
  175. f"No app found in the folder [bold]{path_to_deploy}[/].",
  176. )
  177. raise typer.Exit(1)
  178. if not name:
  179. name = toolkit.input("Enter the name of the environment variable to set:")
  180. if not value:
  181. value = toolkit.input(
  182. "Enter the value of the environment variable to set:", password=True
  183. )
  184. with toolkit.progress(
  185. "Setting environment variable", transient=True
  186. ) as progress:
  187. assert name is not None
  188. assert value is not None
  189. with handle_http_errors(progress):
  190. _set_environment_variable(app_config.app_id, name, value)
  191. toolkit.print(f"Environment variable [bold]{name}[/] set.")