summaryrefslogtreecommitdiffstats
path: root/cli
diff options
context:
space:
mode:
authorAlejandro Sirgo Rica <asirgo@soleta.eu>2024-09-12 10:30:56 +0200
committerAlejandro Sirgo Rica <asirgo@soleta.eu>2024-09-12 10:30:56 +0200
commit7478b9ce159af5535be0d4f67dc362d65acf363b (patch)
treed779c3d785f5c826aa5dbe1dc33ab8d49c47af5a /cli
parent6a96a130b5637625917b469a2376ad022df26867 (diff)
repo: overload delete and add repo to modify the addresses
Overload the add repo and delete repo commands to have different functionality based on the flags. Add a repo: ogcli add repo --name test2 --address 192.168.9.140 Add addresses to repo with id 27: ogcli add repo --address 192.168.9.141 192.168.9.142 --id 27 Delete address from repo with id 27: ogcli delete repo --address 192.168.9.141 --id 27 Delete repo: ogcli delete repo --id 27
Diffstat (limited to 'cli')
-rw-r--r--cli/objects/repo.py114
1 files changed, 102 insertions, 12 deletions
diff --git a/cli/objects/repo.py b/cli/objects/repo.py
index 76760cb..e77f6fa 100644
--- a/cli/objects/repo.py
+++ b/cli/objects/repo.py
@@ -17,23 +17,66 @@ class OgRepo():
print_json(r.text)
@staticmethod
+ def _add_repo(rest, parsed_args):
+ payload = {
+ 'addr': parsed_args.address,
+ 'name': parsed_args.name,
+ }
+ rest.post('/repository/add', payload=payload)
+
+ @staticmethod
+ def _add_repo_ip(rest, parsed_args):
+ for address in parsed_args.address:
+ if not check_address(address):
+ print(f'Invalid IP address: {address}')
+ return
+
+ r = rest.get('/repositories')
+ target_repo = None
+ for repo in r.json()['repositories']:
+ if repo['id'] == parsed_args.id:
+ target_repo = repo
+ break
+
+ if not target_repo:
+ print('Invalid repository id specified')
+ return
+
+ for address in parsed_args.address:
+ if address in target_repo['addr']:
+ print(f'The repository already contains the address {address}')
+ return
+
+ payload = {
+ 'id': parsed_args.id,
+ 'addr': target_repo['addr'] + parsed_args.address,
+ 'name': target_repo['name'],
+ }
+ rest.post('/repository/update', payload=payload)
+
+ @staticmethod
def add_repo(rest, args):
parser = argparse.ArgumentParser(prog='ogcli add repo')
+
+ # Mutually exclusive group for adding new repo or adding address to existing repo
+ group = parser.add_mutually_exclusive_group(required=True)
+ group.add_argument('--name',
+ nargs='?',
+ help='name of the repository')
+ group.add_argument('--id',
+ type=int,
+ help='repo id (list repos using "ogcli list repos")')
parser.add_argument('--address',
nargs='+',
required=True,
help='IP list separated by spaces')
- parser.add_argument('--name',
- nargs='?',
- required=True,
- help='name of the repository')
+
parsed_args = parser.parse_args(args)
- payload = {
- 'addr': parsed_args.address,
- 'name': parsed_args.name,
- }
- rest.post('/repository/add', payload=payload)
+ if parsed_args.name:
+ OgRepo._add_repo(rest, parsed_args)
+ elif parsed_args.id:
+ OgRepo._add_repo_ip(rest, parsed_args)
@staticmethod
def update_repo(rest, args):
@@ -61,16 +104,63 @@ class OgRepo():
rest.post('/repository/update', payload=payload)
@staticmethod
+ def _delete_repo_ip(rest, parsed_args):
+ for address in parsed_args.address:
+ if not check_address(address):
+ print(f'Invalid IP address: {address}')
+ return
+
+ r = rest.get('/repositories')
+ target_repo = None
+ for repo in r.json()['repositories']:
+ if repo['id'] == parsed_args.id:
+ target_repo = repo
+ break
+
+ if not target_repo:
+ print('Invalid repository id specified')
+ return
+
+ for address in parsed_args.address:
+ if address not in target_repo['addr']:
+ print(f'Invalid address {address}: The repository has the following IPs: {target_repo["addr"]}')
+ return
+
+ target_repo['addr'].remove(address)
+
+ payload = {
+ 'id': parsed_args.id,
+ 'addr': target_repo['addr'],
+ 'name': target_repo['name'],
+ }
+ rest.post('/repository/update', payload=payload)
+
+ @staticmethod
+ def _delete_repo(rest, parsed_args):
+ payload = {'id': parsed_args.id}
+ rest.post('/repository/delete', payload=payload)
+
+ @staticmethod
def delete_repo(rest, args):
parser = argparse.ArgumentParser(prog='ogcli delete repo')
+
parser.add_argument('--id',
type=int,
- nargs='?',
required=True,
help='repo id (list repos using "ogcli list repos")')
+
+ # --address is optional; if provided, the IP will be removed, otherwise,
+ # the repo will be deleted
+ parser.add_argument('--address',
+ nargs='+',
+ help='IP address to remove from the repository')
+
parsed_args = parser.parse_args(args)
- payload = {'id': parsed_args.id}
- rest.post('/repository/delete', payload=payload)
+
+ if parsed_args.address:
+ OgRepo._delete_repo_ip(rest, parsed_args)
+ else:
+ OgRepo._delete_repo(rest, parsed_args)
@staticmethod
def set_repo(rest, args):