|
4 | 4 |
|
5 | 5 | use LKDev\HetznerCloud\APIResponse; |
6 | 6 | use LKDev\HetznerCloud\HetznerAPIClient; |
| 7 | +use LKDev\HetznerCloud\Models\Actions\Action; |
7 | 8 | use LKDev\HetznerCloud\Models\Contracts\Resources; |
8 | 9 | use LKDev\HetznerCloud\Models\Meta; |
9 | 10 | use LKDev\HetznerCloud\Models\Model; |
@@ -66,6 +67,69 @@ public function list(?RequestOpts $requestOpts = null): ?APIResponse |
66 | 67 | return null; |
67 | 68 | } |
68 | 69 |
|
| 70 | + /** |
| 71 | + * Creates a Load Balancer. |
| 72 | + * |
| 73 | + * @see https://docs.hetzner.cloud/#load-balancers-create-a-load-balancer |
| 74 | + * |
| 75 | + * @param string $name |
| 76 | + * @param string $loadBalancerType ID or name of the Load Balancer type |
| 77 | + * @param string|null $location ID or name of location (mutually exclusive with $networkZone) |
| 78 | + * @param string|null $networkZone Name of network zone (mutually exclusive with $location) |
| 79 | + * @param array|null $algorithm |
| 80 | + * @param array $labels |
| 81 | + * @param int|null $network |
| 82 | + * @param bool $publicInterface |
| 83 | + * @param array $services |
| 84 | + * @param array $targets |
| 85 | + * @return APIResponse|null |
| 86 | + * |
| 87 | + * @throws \LKDev\HetznerCloud\APIException |
| 88 | + */ |
| 89 | + public function create(string $name, string $loadBalancerType, ?string $location = null, ?string $networkZone = null, ?array $algorithm = null, array $labels = [], ?int $network = null, bool $publicInterface = true, array $services = [], array $targets = []): ?APIResponse |
| 90 | + { |
| 91 | + $payload = [ |
| 92 | + 'name' => $name, |
| 93 | + 'load_balancer_type' => $loadBalancerType, |
| 94 | + ]; |
| 95 | + if ($location !== null) { |
| 96 | + $payload['location'] = $location; |
| 97 | + } |
| 98 | + if ($networkZone !== null) { |
| 99 | + $payload['network_zone'] = $networkZone; |
| 100 | + } |
| 101 | + if ($algorithm !== null) { |
| 102 | + $payload['algorithm'] = $algorithm; |
| 103 | + } |
| 104 | + if (! empty($labels)) { |
| 105 | + $payload['labels'] = $labels; |
| 106 | + } |
| 107 | + if ($network !== null) { |
| 108 | + $payload['network'] = $network; |
| 109 | + } |
| 110 | + if (! $publicInterface) { |
| 111 | + $payload['public_interface'] = false; |
| 112 | + } |
| 113 | + if (! empty($services)) { |
| 114 | + $payload['services'] = $services; |
| 115 | + } |
| 116 | + if (! empty($targets)) { |
| 117 | + $payload['targets'] = $targets; |
| 118 | + } |
| 119 | + |
| 120 | + $response = $this->httpClient->post('load_balancers', ['json' => $payload]); |
| 121 | + if (! HetznerAPIClient::hasError($response)) { |
| 122 | + $body = json_decode((string) $response->getBody()); |
| 123 | + |
| 124 | + return APIResponse::create([ |
| 125 | + 'load_balancer' => LoadBalancer::parse($body->load_balancer), |
| 126 | + 'action' => Action::parse($body->action), |
| 127 | + ], $response->getHeaders()); |
| 128 | + } |
| 129 | + |
| 130 | + return null; |
| 131 | + } |
| 132 | + |
69 | 133 | /** |
70 | 134 | * Gets a specific Load Balancer object. |
71 | 135 | * |
|
0 commit comments