|
| 1 | +from upcloud_api.api import API |
| 2 | +from upcloud_api.load_balancer import LoadBalancer, LoadBalancerBackend |
| 3 | + |
| 4 | + |
| 5 | +class LoadBalancerManager: |
| 6 | + """ |
| 7 | + Functions for managing UpCloud loadbalancer instances and their properties with basic dictionary objects |
| 8 | + """ |
| 9 | + |
| 10 | + api: API |
| 11 | + |
| 12 | + def get_loadbalancers(self): |
| 13 | + """ |
| 14 | + Returns a list of loadbalancer dictionary objects |
| 15 | + """ |
| 16 | + |
| 17 | + url = '/load-balancer' |
| 18 | + return self.api.get_request(url) |
| 19 | + |
| 20 | + def get_loadbalancer(self, lb_uuid: str): |
| 21 | + """ |
| 22 | + Returns details for a single loadbalancer as a dictionary |
| 23 | +
|
| 24 | + :param lb_uuid: |
| 25 | + :return: LB details |
| 26 | + """ |
| 27 | + url = f'/load-balancer/{lb_uuid}' |
| 28 | + return self.api.get_request(url) |
| 29 | + |
| 30 | + def create_loadbalancer(self, body: LoadBalancer): |
| 31 | + """ |
| 32 | + Creates a loadbalancer service specified in body and returns its details |
| 33 | +
|
| 34 | + :param body: |
| 35 | + :return: LB details |
| 36 | + """ |
| 37 | + |
| 38 | + url = '/load-balancer' |
| 39 | + return self.api.post_request(url, body.to_dict()) |
| 40 | + |
| 41 | + def delete_loadbalancer(self, lb_uuid: str): |
| 42 | + """ |
| 43 | + Deletes a loadbalancer service |
| 44 | +
|
| 45 | + :param lb_uuid: |
| 46 | + """ |
| 47 | + |
| 48 | + url = f'/load-balancer/{lb_uuid}' |
| 49 | + return self.api.delete_request(url) |
| 50 | + |
| 51 | + def get_loadbalancer_backends(self, lb_uuid: str): |
| 52 | + """ |
| 53 | + Returns a list of backends for a loadbalancer service |
| 54 | +
|
| 55 | + :param lb_uuid: |
| 56 | + :return: List of LB backends |
| 57 | + """ |
| 58 | + |
| 59 | + url = f'/load-balancer/{lb_uuid}/backends' |
| 60 | + return self.api.get_request(url) |
| 61 | + |
| 62 | + def get_loadbalancer_backend(self, lb_uuid: str, backend: LoadBalancerBackend): |
| 63 | + """ |
| 64 | + Returns details for a single loadbalancer backend |
| 65 | +
|
| 66 | + :param lb_uuid: |
| 67 | + :param backend: |
| 68 | + :return: LB backend details |
| 69 | + """ |
| 70 | + |
| 71 | + url = f'/load-balancer/{lb_uuid}/backends/{backend.name}' |
| 72 | + return self.api.get_request(url) |
| 73 | + |
| 74 | + def create_loadbalancer_backend(self, lb_uuid: str, body: LoadBalancerBackend): |
| 75 | + """ |
| 76 | + Creates a new backend for a loadbalancer and returns its details |
| 77 | +
|
| 78 | + :param lb_uuid: |
| 79 | + :param body: |
| 80 | + :return: LB backend details |
| 81 | + """ |
| 82 | + |
| 83 | + url = f'/load-balancer/{lb_uuid}/backends' |
| 84 | + return self.api.post_request(url, body.to_dict()) |
| 85 | + |
| 86 | + def modify_loadbalancer_backend(self, lb_uuid: str, backend: str, body: LoadBalancerBackend): |
| 87 | + """ |
| 88 | + Modifies an existing loadbalancer backend and returns its details |
| 89 | +
|
| 90 | + :param lb_uuid: |
| 91 | + :param backend: |
| 92 | + :param body: |
| 93 | + :return: LB backend details |
| 94 | + """ |
| 95 | + |
| 96 | + url = f'/load-balancer/{lb_uuid}/backends/{backend}' |
| 97 | + return self.api.patch_request(url, body.to_dict()) |
| 98 | + |
| 99 | + def delete_loadbalancer_backend(self, lb_uuid: str, backend: str): |
| 100 | + """ |
| 101 | + Deletes a loadbalancer backend |
| 102 | +
|
| 103 | + :param lb_uuid: |
| 104 | + :param backend: |
| 105 | + :return: |
| 106 | + """ |
| 107 | + |
| 108 | + url = f'/load-balancer/{lb_uuid}/backends/{backend}' |
| 109 | + return self.api.delete_request(url) |
0 commit comments