|
9 | 9 |
|
10 | 10 | from . import actions |
11 | 11 | from . import data |
| 12 | +from . import exceptions |
12 | 13 | from . import subscriptions |
13 | 14 | from .external import SessionLocal |
14 | 15 | from .models import ( |
|
19 | 20 | Subscription, |
20 | 21 | SubscriptionPlan, |
21 | 22 | KVBrood, |
| 23 | + Application, |
22 | 24 | ) |
23 | 25 |
|
24 | 26 |
|
@@ -622,6 +624,60 @@ def kv_delete_handler(args: argparse.Namespace) -> None: |
622 | 624 | session.close() |
623 | 625 |
|
624 | 626 |
|
| 627 | +def applications_list_handler(args: argparse.Namespace) -> None: |
| 628 | + """ |
| 629 | + Handler for "applications list" command. |
| 630 | + """ |
| 631 | + session = SessionLocal() |
| 632 | + try: |
| 633 | + query = session.query(Application) |
| 634 | + if args.group is not None: |
| 635 | + query = query.filter(Application.group_id == args.group) |
| 636 | + |
| 637 | + applications = query.all() |
| 638 | + |
| 639 | + print( |
| 640 | + json.dumps( |
| 641 | + { |
| 642 | + "applications": [ |
| 643 | + actions.application_as_json_dict(application) |
| 644 | + for application in applications |
| 645 | + ] |
| 646 | + } |
| 647 | + ) |
| 648 | + ) |
| 649 | + finally: |
| 650 | + session.close() |
| 651 | + |
| 652 | + |
| 653 | +def application_create_handler(args: argparse.Namespace) -> None: |
| 654 | + session = SessionLocal() |
| 655 | + try: |
| 656 | + application = Application( |
| 657 | + group_id=args.group, name=args.name, description=args.description, |
| 658 | + ) |
| 659 | + session.add(application) |
| 660 | + session.commit() |
| 661 | + print(json.dumps(actions.application_as_json_dict(application))) |
| 662 | + finally: |
| 663 | + session.close() |
| 664 | + |
| 665 | + |
| 666 | +def application_migrate_handler(args: argparse.Namespace) -> None: |
| 667 | + session = SessionLocal() |
| 668 | + try: |
| 669 | + query = session.query(Application).filter(Application.id == args.application) |
| 670 | + application = query.one_or_none() |
| 671 | + if application is None: |
| 672 | + raise exceptions.ApplicationsNotFound("Application not found") |
| 673 | + |
| 674 | + query.update({Application.group_id: args.group}) |
| 675 | + session.commit() |
| 676 | + print(json.dumps(actions.application_as_json_dict(application))) |
| 677 | + finally: |
| 678 | + session.close() |
| 679 | + |
| 680 | + |
625 | 681 | def main() -> None: |
626 | 682 | parser = argparse.ArgumentParser(description="Brood CLI") |
627 | 683 | parser.set_defaults(func=lambda _: parser.print_help()) |
@@ -1071,6 +1127,45 @@ def main() -> None: |
1071 | 1127 | ) |
1072 | 1128 | parser_kv_delete.set_defaults(func=kv_delete_handler) |
1073 | 1129 |
|
| 1130 | + # Applications command parser |
| 1131 | + parser_applications = subcommands.add_parser( |
| 1132 | + "applications", description="Brood applications" |
| 1133 | + ) |
| 1134 | + parser_applications.set_defaults(func=lambda _: parser_applications.print_help()) |
| 1135 | + subcommands_applications = parser_applications.add_subparsers( |
| 1136 | + description="Brood application commands" |
| 1137 | + ) |
| 1138 | + parser_applications_list = subcommands_applications.add_parser( |
| 1139 | + "list", description="List all applications" |
| 1140 | + ) |
| 1141 | + parser_applications_list.add_argument( |
| 1142 | + "-g", "--group", help="Filter applications by group ID" |
| 1143 | + ) |
| 1144 | + parser_applications_list.set_defaults(func=applications_list_handler) |
| 1145 | + parser_applications_create = subcommands_applications.add_parser( |
| 1146 | + "create", description="Create new application" |
| 1147 | + ) |
| 1148 | + parser_applications_create.add_argument( |
| 1149 | + "-g", "--group", required=True, help="Filter applications by group ID" |
| 1150 | + ) |
| 1151 | + parser_applications_create.add_argument( |
| 1152 | + "-n", "--name", required=True, help="Name of application" |
| 1153 | + ) |
| 1154 | + parser_applications_create.add_argument( |
| 1155 | + "-d", "--description", help="Description of application" |
| 1156 | + ) |
| 1157 | + parser_applications_create.set_defaults(func=application_create_handler) |
| 1158 | + parser_applications_migrate = subcommands_applications.add_parser( |
| 1159 | + "migrate", description="Migrate application to another group" |
| 1160 | + ) |
| 1161 | + parser_applications_migrate.add_argument( |
| 1162 | + "-a", "--application", required=True, help="Applications ID" |
| 1163 | + ) |
| 1164 | + parser_applications_migrate.add_argument( |
| 1165 | + "-g", "--group", required=True, help="Group ID migrate to" |
| 1166 | + ) |
| 1167 | + parser_applications_migrate.set_defaults(func=application_migrate_handler) |
| 1168 | + |
1074 | 1169 | args = parser.parse_args() |
1075 | 1170 | args.func(args) |
1076 | 1171 |
|
|
0 commit comments