diff --git a/setup.py b/setup.py index fa6cfc28aca..b27f15fd746 100755 --- a/setup.py +++ b/setup.py @@ -9,10 +9,17 @@ packages=find_packages("src"), package_dir={"": "src"}, python_requires=">=3.7", + entry_points={ + 'console_scripts': [ + 'firebolt=firebolt.cli:run', + 'fb=firebolt.cli:run', + ], + }, install_requires=[ "httpx[http2]==0.18", "python-dotenv", "pydantic", + "typer", "toolz", ], extras_require={ diff --git a/src/firebolt/cli.py b/src/firebolt/cli.py new file mode 100644 index 00000000000..6bc94c775fe --- /dev/null +++ b/src/firebolt/cli.py @@ -0,0 +1,45 @@ +import json + +import typer + +from .firebolt_client import FireboltClient +from .model.engine import Engine + +app = typer.Typer() + +ENGINE_OPERATIONS = ["start", "stop", "restart"] +READ_COMMANDS = ["ls", "list", "read"] +DELETE_COMMANDS = ["delete", "destroy"] +CREATE_COMMANDS = ["create"] + + +def run(): + with FireboltClient.from_env() as _: + app() + + +@app.command() +def engine(action: str, name: str = None): + if action in READ_COMMANDS: + if name: + eng = Engine.get_by_name(name) + typer.echo(eng.json()) + else: + engines = Engine.list_engines() + typer.echo(json.dumps(engines)) + elif action in CREATE_COMMANDS: + pass + elif action in DELETE_COMMANDS: + eng = Engine.get_by_name(name) + eng.delete() + typer.secho(f"Engine: {name} was deleted", fg=typer.colors.RED) + elif action in ENGINE_OPERATIONS: + eng = Engine.get_by_name(name) + getattr(eng, action)() + else: + raise Exception(f"Engine command: {action} not supported") + + +# @app.command() +# def database(action: str, name: str = None): +# pass diff --git a/src/firebolt/model/engine.py b/src/firebolt/model/engine.py index ce310e4bf40..c6880f14c46 100644 --- a/src/firebolt/model/engine.py +++ b/src/firebolt/model/engine.py @@ -107,6 +107,20 @@ def get_by_name(cls, engine_name: str): engine_id = response.json()["engine_id"]["engine_id"] return cls.get_by_id(engine_id=engine_id) + @classmethod + def list_engines(cls): + fc = cls.get_firebolt_client() + response = fc.http_client.get( + url=f"/core/v1/accounts/{fc.account_id}/engines", + ) + return response.json() + + def delete(self): + response = self.firebolt_client.http_client.delete( + url=f"/core/v1/accounts/{self.firebolt_client.account_id}/engines/{self.engine_id}", + ) + return response.json() + @classmethod def create_analytics(cls): pass