Skip to content

Commit d6eed00

Browse files
committed
Minir changes
1 parent ccd61e2 commit d6eed00

3 files changed

Lines changed: 110 additions & 4 deletions

File tree

hetzner_api.routing.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
hetzner_api.settings:
2+
path: '/admin/config/services/hetzner-api'
3+
defaults:
4+
_form: '\Drupal\hetzner_api\Form\HetznerSettingsForm'
5+
_title: 'Hetzner API-indstillinger'
6+
requirements:
7+
_permission: 'administer site configuration'
8+
19
hetzner_api.servers:
210
path: '/hetzner/servers'
311
defaults:

src/Controller/HetznerController.php

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,66 @@
22

33
namespace Drupal\hetzner_api\Controller;
44

5-
class HetznerController {
6-
// Controller logic here
5+
use Drupal\Core\Controller\ControllerBase;
6+
use Symfony\Component\DependencyInjection\ContainerInterface;
7+
use Symfony\Component\HttpFoundation\Request;
8+
use Drupal\hetzner_api\Service\HetznerApiService;
9+
10+
class HetznerController extends ControllerBase {
11+
12+
protected $hetznerService;
13+
14+
public function __construct(HetznerApiService $hetznerService) {
15+
$this->hetznerService = $hetznerService;
16+
}
17+
18+
public static function create(ContainerInterface $container) {
19+
return new static(
20+
$container->get('hetzner_api.api')
21+
);
22+
}
23+
24+
/**
25+
* Route: /hetzner/servers
26+
*/
27+
public function getServers(Request $request = NULL) {
28+
$force = $request && $request->query->get('refresh') === '1';
29+
$servers = $this->hetznerService->getServers($force);
30+
31+
$header = ['Name', 'Status', 'IPv4', 'Created'];
32+
$rows = [];
33+
34+
foreach ($servers as $server) {
35+
$status = ucfirst($server['status']);
36+
$color = $server['status'] === 'running' ? 'green' : 'red';
37+
38+
$rows[] = [
39+
$server['name'],
40+
['data' => $status, 'style' => "color:$color;font-weight:bold"],
41+
$server['public_net']['ipv4']['ip'] ?? '',
42+
date('Y-m-d H:i', strtotime($server['created'])),
43+
];
44+
}
45+
46+
return [
47+
'#type' => 'table',
48+
'#header' => $header,
49+
'#rows' => $rows,
50+
'#attributes' => ['id' => 'hetzner-server-table'],
51+
'#attached' => [
52+
'library' => ['hetzner_api/refresh'],
53+
],
54+
];
55+
}
56+
57+
/**
58+
* Admin sync route: /admin/config/services/hetzner-api/sync
59+
*/
60+
public function manualSync() {
61+
$count = $this->hetznerService->syncServersToNodes(TRUE);
62+
$this->messenger()->addStatus($this->t('Synkroniseret @count server(e) fra Hetzner.', ['@count' => $count]));
63+
return $this->redirect('system.admin_config_services');
64+
}
65+
766
}
67+

src/Form/HetznerSettingsForm.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,44 @@
22

33
namespace Drupal\hetzner_api\Form;
44

5-
class HetznerSettingsForm {
6-
// Form logic here
5+
use Drupal\Core\Form\ConfigFormBase;
6+
use Drupal\Core\Form\FormStateInterface;
7+
use Drupal\Core\Url;
8+
9+
class HetznerSettingsForm extends ConfigFormBase {
10+
11+
protected function getEditableConfigNames() {
12+
return ['hetzner_api.settings'];
13+
}
14+
15+
public function getFormId() {
16+
return 'hetzner_api_settings_form';
17+
}
18+
19+
public function buildForm(array $form, FormStateInterface $form_state) {
20+
$config = $this->config('hetzner_api.settings');
21+
22+
$form['api_key'] = [
23+
'#type' => 'textfield',
24+
'#title' => $this->t('Hetzner API Key'),
25+
'#default_value' => $config->get('api_key'),
26+
'#required' => TRUE,
27+
];
28+
29+
$form['sync_link'] = [
30+
'#type' => 'link',
31+
'#title' => $this->t('Synkroniser nu'),
32+
'#url' => Url::fromRoute('hetzner_api.sync'),
33+
'#attributes' => ['class' => ['button']],
34+
];
35+
36+
return parent::buildForm($form, $form_state);
37+
}
38+
39+
public function submitForm(array &$form, FormStateInterface $form_state) {
40+
$this->config('hetzner_api.settings')
41+
->set('api_key', $form_state->getValue('api_key'))
42+
->save();
43+
parent::submitForm($form, $form_state);
44+
}
745
}

0 commit comments

Comments
 (0)