MonitoringResourceController.php 25.9 KB
Newer Older
1
2
3
<?php

/*
4
 * Copyright 2005 - 2021 Centreon (https://www.centreon.com/)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * For more information : contact@centreon.com
 *
 */
declare(strict_types=1);

namespace Centreon\Application\Controller;

use FOS\RestBundle\View\View;
26
27
28
29
30
31
use FOS\RestBundle\Context\Context;
use Centreon\Domain\Contact\Contact;
use Centreon\Domain\Monitoring\Icon;
use JMS\Serializer\SerializerInterface;
use Centreon\Domain\Entity\EntityValidator;
use Symfony\Component\HttpFoundation\Request;
32
use Centreon\Domain\Monitoring\ResourceFilter;
33
use Centreon\Domain\Monitoring\ResourceStatus;
34
35
use Centreon\Application\Normalizer\IconUrlNormalizer;
use JMS\Serializer\Exception\ValidationFailedException;
36
use Centreon\Domain\RequestParameters\RequestParameters;
37
38
39
40
41
42
use Centreon\Domain\Monitoring\Resource as ResourceEntity;
use Centreon\Domain\Monitoring\Exception\ResourceException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Centreon\Domain\Monitoring\Interfaces\ResourceServiceInterface;
use Centreon\Domain\Monitoring\Serializer\ResourceExclusionStrategy;
use Centreon\Domain\RequestParameters\Interfaces\RequestParametersInterface;
43
44
45
46
47
48
49
50
51
52
53

/**
 * Resource APIs for the Unified View page
 *
 * @package Centreon\Application\Controller
 */
class MonitoringResourceController extends AbstractController
{
    /**
     * List of external parameters for list action
     *
54
     * @var array<string>
55
56
57
58
59
     */
    public const EXTRA_PARAMETERS_LIST = [
        'types',
        'states',
        'statuses',
60
61
62
        'hostgroup_names',
        'servicegroup_names',
        'monitoring_server_names',
63
        'status_types',
64
65
    ];

66
67
    public const FILTER_RESOURCES_ON_PERFORMANCE_DATA_AVAILABILITY = 'only_with_performance_data';

68
69
70
71
72
73
74
75
76
77
78

    private const META_SERVICE_CONFIGURATION_URI = '/main.php?p=60204&o=c&meta_id={resource_id}';
    private const META_SERVICE_DETAILS_ROUTE = 'centreon_application_monitoring_resource_details_meta_service';
    private const META_SERVICE_TIMELINE_ROUTE = 'centreon_application_monitoring_gettimelinebymetaservices';
    private const META_SERVICE_DOWNTIME_ROUTE = 'monitoring.downtime.addMetaServiceDowntime';
    private const META_SERVICE_ACKNOWLEDGEMENT_ROUTE =
        'centreon_application_acknowledgement_addmetaserviceacknowledgement';
    private const META_SERVICE_STATUS_GRAPH_ROUTE = 'monitoring.metric.getMetaServiceStatusMetrics';
    private const META_SERVICE_METRIC_LIST_ROUTE = 'centreon_application_find_meta_service_metrics';
    private const META_SERVICE_PERFORMANCE_GRAPH_ROUTE = 'monitoring.metric.getMetaServicePerformanceMetrics';

79
80
81
82
    private const HOST_CONFIGURATION_URI = '/main.php?p=60101&o=c&host_id={resource_id}';
    private const SERVICE_CONFIGURATION_URI = '/main.php?p=60201&o=c&service_id={resource_id}';
    private const HOST_LOGS_URI = '/main.php?p=20301&h={resource_id}';
    private const SERVICE_LOGS_URI = '/main.php?p=20301&svc={parent_resource_id}_{resource_id}';
83
    private const META_SERVICE_LOGS_URI = '/main.php?p=20301&svc={host_id}_{service_id}';
84
85
86
87
    private const HOST_REPORTING_URI = '/main.php?p=307&host={resource_id}';
    private const SERVICE_REPORTING_URI =
        '/main.php?p=30702&period=yesterday&start=&end=&host_id={parent_resource_id}&item={resource_id}';

88
89
    private const RESOURCE_LISTING_URI = '/monitoring/resources';

90
91
92
93
94
95
96
97
98
99
100
101
102
    public const TAB_DETAILS_NAME = 'details';
    public const TAB_GRAPH_NAME = 'graph';
    public const TAB_SERVICES_NAME = 'services';
    public const TAB_TIMELINE_NAME = 'timeline';
    public const TAB_SHORTCUTS_NAME = 'shortcuts';

    private const ALLOWED_TABS = [
        self::TAB_DETAILS_NAME,
        self::TAB_GRAPH_NAME,
        self::TAB_SERVICES_NAME,
        self::TAB_TIMELINE_NAME,
        self::TAB_SHORTCUTS_NAME,
    ];
103

104
105
106
107
108
109
110
111
112
113
    private const HOST_ACKNOWLEDGEMENT_ROUTE = 'centreon_application_acknowledgement_addhostacknowledgement';
    private const SERVICE_ACKNOWLEDGEMENT_ROUTE = 'centreon_application_acknowledgement_addserviceacknowledgement';
    private const HOST_DOWNTIME_ROUTE = 'monitoring.downtime.addHostDowntime';
    private const SERVICE_DOWNTIME_ROUTE = 'monitoring.downtime.addServiceDowntime';
    private const HOST_DETAILS_ROUTE = 'centreon_application_monitoring_resource_details_host';
    private const SERVICE_DETAILS_ROUTE = 'centreon_application_monitoring_resource_details_service';
    private const HOST_TIMELINE_ROUTE = 'centreon_application_monitoring_gettimelinebyhost';
    private const SERVICE_TIMELINE_ROUTE = 'centreon_application_monitoring_gettimelinebyhostandservice';
    private const SERVICE_STATUS_GRAPH_ROUTE = 'monitoring.metric.getServiceStatusMetrics';
    private const SERVICE_PERFORMANCE_GRAPH_ROUTE = 'monitoring.metric.getServicePerformanceMetrics';
114
115
116
    private const HOST_NOTIFICATION_POLICY_ROUTE = 'configuration.host.notification-policy';
    private const SERVICE_NOTIFICATION_POLICY_ROUTE = 'configuration.service.notification-policy';
    private const META_SERVICE_NOTIFICATION_POLICY_ROUTE = 'configuration.metaservice.notification-policy';
117

118
    // Groups for serialization
119
120
121
122
123
124
    public const SERIALIZER_GROUPS_LISTING = [
        ResourceEntity::SERIALIZER_GROUP_MAIN,
        ResourceEntity::SERIALIZER_GROUP_PARENT,
        Icon::SERIALIZER_GROUP_MAIN,
        ResourceStatus::SERIALIZER_GROUP_MAIN,
    ];
125
126
127
128
129
130
131
132
133

    // Groups for validation
    public const VALIDATION_GROUP_MAIN = 'resource_id_main';

    /**
     * @var ResourceServiceInterface
     */
    protected $resource;

134
135
136
137
138
    /**
     * @var UrlGeneratorInterface
     */
    protected $router;

139
140
141
142
143
    /**
     * @var IconUrlNormalizer
     */
    protected $iconUrlNormalizer;

144
145
    /**
     * @param ResourceServiceInterface $resource
146
     * @param UrlGeneratorInterface $router
147
     * @param IconUrlNormalizer $iconUrlNormalizer
148
     */
149
150
    public function __construct(
        ResourceServiceInterface $resource,
151
152
        UrlGeneratorInterface $router,
        IconUrlNormalizer $iconUrlNormalizer
153
    ) {
154
        $this->resource = $resource;
155
        $this->router = $router;
156
        $this->iconUrlNormalizer = $iconUrlNormalizer;
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
    }

    /**
     * List all the resources in real-time monitoring : hosts and services.
     *
     * @param RequestParametersInterface $requestParameters
     * @param Request $request
     * @param SerializerInterface $serializer
     * @param EntityValidator $entityValidator
     * @return View
     */
    public function list(
        RequestParametersInterface $requestParameters,
        Request $request,
        SerializerInterface $serializer,
        EntityValidator $entityValidator
    ): View {
        // ACL check
        $this->denyAccessUnlessGrantedForApiRealtime();

177
178
179
180
181
        /**
         * @var Contact $contact
         */
        $contact = $this->getUser();

182
183
        // set default values of filter data
        $filterData = [];
184
        foreach (self::EXTRA_PARAMETERS_LIST as $param) {
185
186
187
            $filterData[$param] = [];
        }

188
        $filterData[self::FILTER_RESOURCES_ON_PERFORMANCE_DATA_AVAILABILITY] = false;
189

190
        // load filter data with the query parameters
191
        foreach ($request->query->all() as $param => $data) {
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
            // skip pagination parameters
            if (in_array($param, ['search', 'limit', 'page', 'sort_by'])) {
                continue;
            }

            $filterData[$param] = json_decode($data, true) ?: $data;
        }

        // validate the filter data
        $errors = $entityValidator->validateEntity(
            ResourceFilter::class,
            $filterData,
            ['Default'],
            false // We don't allow extra fields
        );

        if ($errors->count() > 0) {
            throw new ValidationFailedException($errors);
        }

        // Parse the filter data into filter object
        $filter = $serializer->deserialize(
            json_encode($filterData),
            ResourceFilter::class,
            'json'
        );

        $context = (new Context())
220
            ->setGroups(self::SERIALIZER_GROUPS_LISTING)
221
222
223
224
            ->enableMaxDepth();

        $context->addExclusionStrategy(new ResourceExclusionStrategy());

225
226
        $resources = $this->resource
            ->filterByContact($contact)
227
228
            ->findResources($filter);

229
        $this->providePerformanceGraphEndpoint($resources);
230

231
        foreach ($resources as $resource) {
232
233
234
235
236
237
238
            if ($resource->getIcon() instanceof Icon) {
                $this->iconUrlNormalizer->normalize($resource->getIcon());
            }

            if ($resource->getParent() !== null && $resource->getParent()->getIcon() instanceof Icon) {
                $this->iconUrlNormalizer->normalize($resource->getParent()->getIcon());
            }
239

240
            // add shortcuts
241
            $this->provideLinks($resource, $contact);
242
243
        }

244
        return $this->view([
245
            'result' => $resources,
246
247
248
            'meta' => $requestParameters->toArray(),
        ])->setContext($context);
    }
249

250
251
252
253
254
255
256
257
258
259
260
    /**
     * Add performance graph endpoint on resources which have performance data
     *
     * @param ResourceEntity[] $resources
     * @return void
     */
    private function providePerformanceGraphEndpoint(array $resources)
    {
        $resourcesWithGraphData = $this->resource->extractResourcesWithGraphData($resources);

        foreach ($resources as $resource) {
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
            foreach ($resourcesWithGraphData as $resourceWithGraphData) {
                if (
                    $resource->getType() === ResourceEntity::TYPE_SERVICE
                    && $resourceWithGraphData->getType() === ResourceEntity::TYPE_SERVICE
                    && $resource->getParent()->getId() === $resourceWithGraphData->getParent()->getId()
                    && $resource->getId() === $resourceWithGraphData->getId()
                ) {
                    // set service performance graph endpoint from metrics controller
                    $resource->getLinks()->getEndpoints()->setPerformanceGraph(
                        $this->router->generate(
                            self::SERVICE_PERFORMANCE_GRAPH_ROUTE,
                            [
                                'hostId' => $resource->getParent()->getId(),
                                'serviceId' => $resource->getId(),
                            ]
                        )
                    );
                } elseif (
                    $resource->getType() === ResourceEntity::TYPE_META
                    && $resource->getId() === $resourceWithGraphData->getId()
                ) {
                    // set service performance graph endpoint from metrics controller
                    $resource->getLinks()->getEndpoints()->setPerformanceGraph(
                        $this->router->generate(
                            self::META_SERVICE_PERFORMANCE_GRAPH_ROUTE,
                            ['metaId' => $resource->getId()]
                        )
                    );
289
290
291
292
293
294
                }
            }
        }
    }

    /**
295
     * Add internal, external uris and endpoints to the given resource
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
     *
     * @param ResourceEntity $resource
     * @param Contact $contact
     * @return void
     */
    private function provideLinks(ResourceEntity $resource, Contact $contact): void
    {
        $this->provideEndpoints($resource);
        $this->provideInternalUris($resource, $contact);
    }

    /**
     * Add endpoints to the given resource
     *
     * @param ResourceEntity $resource
     * @return void
     */
    private function provideEndpoints(ResourceEntity $resource): void
    {
        $acknowledgementFilter = ['limit' => 1];
        $downtimeFilter = [
            'search' => json_encode([
                RequestParameters::AGGREGATE_OPERATOR_AND => [
                    [
                        'start_time' => [
                            RequestParameters::OPERATOR_LESS_THAN => time(),
                        ],
                        'end_time' => [
                            RequestParameters::OPERATOR_GREATER_THAN => time(),
                        ],
                        [
                            RequestParameters::AGGREGATE_OPERATOR_OR => [
                                'is_cancelled' => [
                                    RequestParameters::OPERATOR_NOT_EQUAL => 1,
                                ],
                                'deletion_time' => [
                                    RequestParameters::OPERATOR_GREATER_THAN => time(),
                                ],
                            ],
                        ]
                    ]
                ]
            ])
        ];

        $hostResource = null;

        if ($resource->getType() === ResourceEntity::TYPE_HOST) {
            $hostResource = $resource;
        } elseif ($resource->getType() === ResourceEntity::TYPE_SERVICE && $resource->getParent()) {
            $hostResource = $resource->getParent();

            $parameters = [
                'hostId' => $resource->getParent()->getId(),
                'serviceId' => $resource->getId(),
            ];

            $resource->getLinks()->getEndpoints()->setDetails(
                $this->router->generate(
355
                    self::SERVICE_DETAILS_ROUTE,
356
357
358
359
360
361
                    $parameters
                )
            );

            $resource->getLinks()->getEndpoints()->setTimeline(
                $this->router->generate(
362
                    self::SERVICE_TIMELINE_ROUTE,
363
364
365
366
367
368
                    $parameters
                )
            );

            $resource->getLinks()->getEndpoints()->setAcknowledgement(
                $this->router->generate(
369
                    self::SERVICE_ACKNOWLEDGEMENT_ROUTE,
370
371
372
373
374
375
                    array_merge($parameters, $acknowledgementFilter)
                )
            );

            $resource->getLinks()->getEndpoints()->setDowntime(
                $this->router->generate(
376
                    self::SERVICE_DOWNTIME_ROUTE,
377
378
379
380
381
382
                    array_merge($parameters, $downtimeFilter)
                )
            );

            $resource->getLinks()->getEndpoints()->setStatusGraph(
                $this->router->generate(
383
                    self::SERVICE_STATUS_GRAPH_ROUTE,
384
385
386
                    $parameters
                )
            );
387
388
389
390
391
392
393

            $resource->getLinks()->getEndpoints()->setNotificationPolicy(
                $this->router->generate(
                    self::SERVICE_NOTIFICATION_POLICY_ROUTE,
                    $parameters
                )
            );
394
395
396
397
398
399
400
        } elseif ($resource->getType() === ResourceEntity::TYPE_META) {
            $parameters = [
                'metaId' => $resource->getId(),
            ];

            $resource->getLinks()->getEndpoints()->setDetails(
                $this->router->generate(
401
                    self::META_SERVICE_DETAILS_ROUTE,
402
403
404
405
406
407
                    $parameters
                )
            );

            $resource->getLinks()->getEndpoints()->setTimeline(
                $this->router->generate(
408
                    self::META_SERVICE_TIMELINE_ROUTE,
409
410
411
412
413
414
                    $parameters
                )
            );

            $resource->getLinks()->getEndpoints()->setAcknowledgement(
                $this->router->generate(
415
                    self::META_SERVICE_ACKNOWLEDGEMENT_ROUTE,
416
417
418
419
420
421
                    array_merge($parameters, $acknowledgementFilter)
                )
            );

            $resource->getLinks()->getEndpoints()->setDowntime(
                $this->router->generate(
422
                    self::META_SERVICE_DOWNTIME_ROUTE,
423
424
425
426
427
428
                    array_merge($parameters, $downtimeFilter)
                )
            );

            $resource->getLinks()->getEndpoints()->setStatusGraph(
                $this->router->generate(
429
                    self::META_SERVICE_STATUS_GRAPH_ROUTE,
430
431
432
433
434
435
                    $parameters
                )
            );

            $resource->getLinks()->getEndpoints()->setMetrics(
                $this->router->generate(
436
                    self::META_SERVICE_METRIC_LIST_ROUTE,
437
438
439
                    $parameters
                )
            );
440
441
442
443
444
445
446

            $resource->getLinks()->getEndpoints()->setNotificationPolicy(
                $this->router->generate(
                    self::META_SERVICE_NOTIFICATION_POLICY_ROUTE,
                    ['metaServiceId' => $resource->getId()]
                )
            );
447
448
449
450
451
452
453
454
455
        }

        if ($hostResource !== null) {
            $parameters = [
                'hostId' => $hostResource->getId(),
            ];

            $hostResource->getLinks()->getEndpoints()->setDetails(
                $this->router->generate(
456
                    self::HOST_DETAILS_ROUTE,
457
458
459
460
461
462
                    $parameters
                )
            );

            $hostResource->getLinks()->getEndpoints()->setTimeline(
                $this->router->generate(
463
                    self::HOST_TIMELINE_ROUTE,
464
465
466
467
468
469
                    $parameters
                )
            );

            $hostResource->getLinks()->getEndpoints()->setAcknowledgement(
                $this->router->generate(
470
                    self::HOST_ACKNOWLEDGEMENT_ROUTE,
471
472
473
474
475
476
                    array_merge($parameters, $acknowledgementFilter)
                )
            );

            $hostResource->getLinks()->getEndpoints()->setDowntime(
                $this->router->generate(
477
                    self::HOST_DOWNTIME_ROUTE,
478
479
480
                    array_merge($parameters, $downtimeFilter)
                )
            );
481
482
483
484
485
486
487

            $hostResource->getLinks()->getEndpoints()->setNotificationPolicy(
                $this->router->generate(
                    self::HOST_NOTIFICATION_POLICY_ROUTE,
                    $parameters
                )
            );
488
489
490
        }
    }

491
492
493
    /**
     * Add internal uris (configuration, logs, reporting) to the given resource
     *
494
     * @param ResourceEntity $resource
495
496
497
     * @param Contact $contact
     * @return void
     */
498
    private function provideInternalUris(ResourceEntity $resource, Contact $contact): void
499
    {
500
        if ($resource->getType() === ResourceEntity::TYPE_SERVICE && $resource->getParent()) {
501
502
            $this->provideHostInternalUris($resource->getParent(), $contact);
            $this->provideServiceInternalUris($resource, $contact);
503
504
        } elseif ($resource->getType() === ResourceEntity::TYPE_META) {
            $this->provideMetaServiceInternalUris($resource, $contact);
505
506
507
508
509
510
511
512
        } else {
            $this->provideHostInternalUris($resource, $contact);
        }
    }

    /**
     * Add host internal uris (configuration, logs, reporting) to the given resource
     *
513
     * @param ResourceEntity $resource
514
515
516
     * @param Contact $contact
     * @return void
     */
517
    private function provideHostInternalUris(ResourceEntity $resource, Contact $contact): void
518
    {
519
520
521
522
        if (
            $contact->hasTopologyRole(Contact::ROLE_CONFIGURATION_HOSTS_WRITE)
            || $contact->hasTopologyRole(Contact::ROLE_CONFIGURATION_HOSTS_READ)
        ) {
523
            $resource->getLinks()->getUris()->setConfiguration(
524
                $this->generateResourceUri($resource, self::HOST_CONFIGURATION_URI)
525
526
527
528
            );
        }

        if ($contact->hasTopologyRole(Contact::ROLE_MONITORING_EVENT_LOGS)) {
529
            $resource->getLinks()->getUris()->setLogs(
530
                $this->generateResourceUri($resource, self::HOST_LOGS_URI)
531
532
533
534
            );
        }

        if ($contact->hasTopologyRole(Contact::ROLE_REPORTING_DASHBOARD_HOSTS)) {
535
            $resource->getLinks()->getUris()->setReporting(
536
                $this->generateResourceUri($resource, self::HOST_REPORTING_URI)
537
538
539
540
541
542
543
            );
        }
    }

    /**
     * Add service internal uris (configuration, logs, reporting) to the given resource
     *
544
     * @param ResourceEntity $resource
545
546
547
     * @param Contact $contact
     * @return void
     */
548
    private function provideServiceInternalUris(ResourceEntity $resource, Contact $contact): void
549
    {
550
551
552
553
        if (
            $contact->hasTopologyRole(Contact::ROLE_CONFIGURATION_SERVICES_WRITE)
            || $contact->hasTopologyRole(Contact::ROLE_CONFIGURATION_SERVICES_READ)
        ) {
554
            $resource->getLinks()->getUris()->setConfiguration(
555
                $this->generateResourceUri($resource, self::SERVICE_CONFIGURATION_URI)
556
557
558
559
            );
        }

        if ($contact->hasTopologyRole(Contact::ROLE_MONITORING_EVENT_LOGS)) {
560
            $resource->getLinks()->getUris()->setLogs(
561
                $this->generateResourceUri($resource, self::SERVICE_LOGS_URI)
562
563
564
565
            );
        }

        if ($contact->hasTopologyRole(Contact::ROLE_REPORTING_DASHBOARD_SERVICES)) {
566
            $resource->getLinks()->getUris()->setReporting(
567
                $this->generateResourceUri($resource, self::SERVICE_REPORTING_URI)
568
569
570
571
            );
        }
    }

572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
    /**
     * Add service internal uris (configuration, logs, reporting) to the given resource
     *
     * @param ResourceEntity $resource
     * @param Contact $contact
     * @return void
     */
    private function provideMetaServiceInternalUris(ResourceEntity $resource, Contact $contact): void
    {
        if (
            $contact->hasTopologyRole(Contact::ROLE_CONFIGURATION_META_SERVICES_WRITE)
            || $contact->hasTopologyRole(Contact::ROLE_CONFIGURATION_META_SERVICES_READ)
            || $contact->isAdmin()
        ) {
            $resource->getLinks()->getUris()->setConfiguration(
587
                $this->generateResourceUri($resource, self::META_SERVICE_CONFIGURATION_URI)
588
589
590
591
592
            );
        }

        if ($contact->hasTopologyRole(Contact::ROLE_MONITORING_EVENT_LOGS)) {
            $resource->getLinks()->getUris()->setLogs(
593
                $this->generateResourceUri($resource, self::META_SERVICE_LOGS_URI)
594
595
596
597
            );
        }
    }

598
599
600
    /**
     * Generate full uri from relative path
     *
601
     * @param ResourceEntity $resource
602
603
604
     * @param string $relativeUri
     * @return string
     */
605
    private function generateResourceUri(ResourceEntity $resource, string $relativeUri): string
606
    {
607
608
609
        $relativeUri = str_replace('{resource_id}', (string) $resource->getId(), $relativeUri);
        $relativeUri = str_replace('{host_id}', (string) $resource->getHostId(), $relativeUri);
        $relativeUri = str_replace('{service_id}', (string) $resource->getServiceId(), $relativeUri);
610
611

        if ($resource->getParent() !== null) {
612
            $relativeUri = str_replace('{parent_resource_id}', (string) $resource->getParent()->getId(), $relativeUri);
613
614
        }

615
        return $this->getBaseUri() . $relativeUri;
616
    }
617
618

    /**
619
     * Build uri to access host panel with details tab
620
621
     *
     * @param integer $hostId
622
     * @return string
623
     */
624
    public function buildHostDetailsUri(int $hostId): string
625
    {
626
627
628
629
630
631
632
633
634
635
636
637
        return $this->buildHostUri($hostId, self::TAB_DETAILS_NAME);
    }

    /**
     * Build uri to access host panel
     *
     * @param integer $hostId
     * @param string $tab tab name
     * @return string
     */
    public function buildHostUri(int $hostId, string $tab = self::TAB_DETAILS_NAME): string
    {
638
        if (!in_array($tab, self::ALLOWED_TABS)) {
639
640
641
            throw new ResourceException(sprintf(_('Cannot build uri to unknown tab : %s'), $tab));
        }

642
643
644
645
        return $this->buildListingUri([
            'details' => json_encode([
                'type' => ResourceEntity::TYPE_HOST,
                'id' => $hostId,
646
                'tab' => $tab,
647
                'uuid' => 'h' . $hostId
648
649
650
651
652
            ]),
        ]);
    }

    /**
653
     * Build uri to access service service panel with details tab
654
655
656
     *
     * @param integer $hostId
     * @param integer $serviceId
657
     * @return string
658
     */
659
    public function buildServiceDetailsUri(int $hostId, int $serviceId): string
660
    {
661
662
663
664
665
666
667
668
669
670
671
672
673
        return $this->buildServiceUri($hostId, $serviceId, self::TAB_DETAILS_NAME);
    }

    /**
     * Build uri to access service panel
     *
     * @param integer $hostId
     * @param integer $serviceId
     * @param string $tab tab name
     * @return string
     */
    public function buildServiceUri(int $hostId, int $serviceId, string $tab = self::TAB_DETAILS_NAME): string
    {
674
        if (!in_array($tab, self::ALLOWED_TABS)) {
675
676
677
            throw new ResourceException(sprintf(_('Cannot build uri to unknown tab : %s'), $tab));
        }

678
679
680
681
682
683
        return $this->buildListingUri([
            'details' => json_encode([
                'parentType' => ResourceEntity::TYPE_HOST,
                'parentId' => $hostId,
                'type' => ResourceEntity::TYPE_SERVICE,
                'id' => $serviceId,
684
                'tab' => $tab,
685
                'uuid' => 's' . $serviceId
686
687
688
689
            ]),
        ]);
    }

690
691
692
693
694
695
696
697
698
    /**
     * Build uri to access meta service panel
     *
     * @param integer $metaId
     * @param string $tab tab name
     * @return string
     */
    public function buildMetaServiceDetailsUri(int $metaId, string $tab = self::TAB_DETAILS_NAME): string
    {
699
        if (!in_array($tab, self::ALLOWED_TABS)) {
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
            throw new ResourceException(sprintf(_('Cannot build uri to unknown tab : %s'), $tab));
        }

        return $this->buildListingUri([
            'details' => json_encode([
                'parentType' => null,
                'parentId' => null,
                'type' => ResourceEntity::TYPE_META,
                'id' => $metaId,
                'tab' => $tab,
                'uuid' => 'm' . $metaId
            ]),
        ]);
    }

715
716
717
    /**
     * Build uri to access listing page of resources with specific parameters
     *
718
     * @param string[] $parameters
719
     * @return string
720
     */
721
    public function buildListingUri(array $parameters): string
722
    {
723
        $baseListingUri = $this->getBaseUri() . self::RESOURCE_LISTING_URI;
724
725
726
727
728
729
730

        if (!empty($parameters)) {
            $baseListingUri .= '?' . http_build_query($parameters);
        }

        return $baseListingUri;
    }
731
}