|
| 1 | +from axonopscli.axonops import HTTPCodeError |
| 2 | + |
| 3 | + |
1 | 4 | class ScheduledRepair: |
2 | 5 | """ Class to manage the Scheduled Repair in AxonOps """ |
3 | 6 | schedule_repair_add_url = "/api/v1/addrepair" |
| 7 | + repair_url = "/api/v1/repair" |
| 8 | + cassandrascheduledrepair_url = "/api/v1/cassandrascheduledrepair" |
4 | 9 |
|
5 | 10 | def __init__(self, axonops, args): |
6 | 11 | self.axonops = axonops |
7 | 12 | self.args = args |
8 | 13 | self.repair_data = None |
9 | | - self.full_url = f"{self.schedule_repair_add_url}/{args.org}/cassandra/{args.cluster}" |
| 14 | + self.full_add_repair_url = f"{self.schedule_repair_add_url}/{args.org}/cassandra/{args.cluster}" |
| 15 | + self.full_repair_url = f"{self.repair_url}/{args.org}/cassandra/{args.cluster}" |
| 16 | + self.full_cassandrascheduledrepair_url = f"{self.cassandrascheduledrepair_url}/{args.org}/cassandra/{args.cluster}" |
| 17 | + |
| 18 | + def remove_old_repairs_from_axonops(self): |
| 19 | + """ Check if the scheduled repair already exists in AxonOps, if so, remove it. """ |
| 20 | + if self.args.v: |
| 21 | + print("Checking if scheduled repair already exists") |
| 22 | + |
| 23 | + if self.args.tags != "": |
| 24 | + if self.args.v: |
| 25 | + print("Getting scheduled repair with tag:", self.args.tags) |
| 26 | + response = self.axonops.do_request( |
| 27 | + url=self.full_repair_url, |
| 28 | + method='GET', |
| 29 | + ) |
| 30 | + if not response: |
| 31 | + if self.args.v: |
| 32 | + print("No response received when checking for existing scheduled repair") |
| 33 | + return |
| 34 | + elif 'ScheduledRepairs' in response and response['ScheduledRepairs']: |
| 35 | + for repair in response['ScheduledRepairs']: |
| 36 | + if self.args.v: |
| 37 | + print(f"Checking scheduled repair: {repair['ID']}") |
| 38 | + self.remove_repair(repair['ID']) |
| 39 | + if 'Params' in repair: |
| 40 | + print(repair['Params']) |
| 41 | + |
| 42 | + else: |
| 43 | + print("Repair tag not found, this will be threaded as a new scheduled repair") |
| 44 | + else: |
| 45 | + if self.args.v: |
| 46 | + print("No tag provided, this will be threaded as a new scheduled repair") |
10 | 47 |
|
11 | 48 | def set_options(self): |
12 | 49 | """Apply optional CLI parameters into the payload before sending it.""" |
@@ -59,13 +96,36 @@ def set_options(self): |
59 | 96 | self.repair_data['paxos'] = "Paxos Only" |
60 | 97 |
|
61 | 98 | def set_repair(self): |
62 | | - full_url = f"{self.axonops.dash_url()}{self.schedule_repair_add_url}" |
63 | 99 | print("Setting the scheduled repair") |
64 | 100 | if self.args.v: |
65 | | - print("POST", self.full_url, self.repair_data) |
| 101 | + print("POST", self.full_add_repair_url, self.repair_data) |
| 102 | + |
| 103 | + if self.args.delete: |
| 104 | + if self.args.v: |
| 105 | + print("This scheduled repair is delete, not sending to AxonOps") |
| 106 | + return |
66 | 107 |
|
67 | 108 | self.axonops.do_request( |
68 | | - url=self.full_url, |
| 109 | + url=self.full_add_repair_url, |
69 | 110 | method='POST', |
70 | 111 | json_data=self.repair_data, |
71 | 112 | ) |
| 113 | + |
| 114 | + def remove_repair(self, repair_id: str) -> bool: |
| 115 | + """ Remove a scheduled repair from AxonOps by its ID. """ |
| 116 | + if self.args.v: |
| 117 | + print(f"Removing scheduled repair with ID: {repair_id}") |
| 118 | + |
| 119 | + try: |
| 120 | + response = self.axonops.do_request( |
| 121 | + url=f"{self.full_cassandrascheduledrepair_url}?id={repair_id}", |
| 122 | + method='DELETE', |
| 123 | + ) |
| 124 | + except HTTPCodeError: |
| 125 | + print(f"Failed to remove scheduled repair with ID: {repair_id}") |
| 126 | + raise |
| 127 | + else: |
| 128 | + if self.args.v: |
| 129 | + print(f"Response received when removing scheduled repair with ID: {repair_id}: {response}") |
| 130 | + |
| 131 | + return True |
0 commit comments