1 import { Component, Input, OnChanges, OnInit, TemplateRef, ViewChild } from '@angular/core';
2 import { Router } from '@angular/router';
4 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
5 import { delay } from 'rxjs/operators';
7 import { CephServiceService } from '~/app/shared/api/ceph-service.service';
8 import { OrchestratorService } from '~/app/shared/api/orchestrator.service';
9 import { ListWithDetails } from '~/app/shared/classes/list-with-details.class';
10 import { CriticalConfirmationModalComponent } from '~/app/shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
11 import { ActionLabelsI18n, URLVerbs } from '~/app/shared/constants/app.constants';
12 import { TableComponent } from '~/app/shared/datatable/table/table.component';
13 import { Icons } from '~/app/shared/enum/icons.enum';
14 import { CdTableAction } from '~/app/shared/models/cd-table-action';
15 import { CdTableColumn } from '~/app/shared/models/cd-table-column';
16 import { CdTableFetchDataContext } from '~/app/shared/models/cd-table-fetch-data-context';
17 import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
18 import { FinishedTask } from '~/app/shared/models/finished-task';
19 import { OrchestratorFeature } from '~/app/shared/models/orchestrator.enum';
20 import { OrchestratorStatus } from '~/app/shared/models/orchestrator.interface';
21 import { Permissions } from '~/app/shared/models/permissions';
22 import { CephServiceSpec } from '~/app/shared/models/service.interface';
23 import { RelativeDatePipe } from '~/app/shared/pipes/relative-date.pipe';
24 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
25 import { ModalService } from '~/app/shared/services/modal.service';
26 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
27 import { URLBuilderService } from '~/app/shared/services/url-builder.service';
28 import { PlacementPipe } from './placement.pipe';
29 import { ServiceFormComponent } from './service-form/service-form.component';
31 const BASE_URL = 'services';
34 selector: 'cd-services',
35 templateUrl: './services.component.html',
36 styleUrls: ['./services.component.scss'],
37 providers: [{ provide: URLBuilderService, useValue: new URLBuilderService(BASE_URL) }]
39 export class ServicesComponent extends ListWithDetails implements OnChanges, OnInit {
40 @ViewChild(TableComponent, { static: true })
41 table: TableComponent;
42 @ViewChild('runningTpl', { static: true })
43 public runningTpl: TemplateRef<any>;
45 @Input() hostname: string;
47 // Do not display these columns
48 @Input() hiddenColumns: string[] = [];
50 @Input() hiddenServices: string[] = [];
52 @Input() hasDetails = true;
54 @Input() routedModal = true;
56 permissions: Permissions;
57 tableActions: CdTableAction[];
60 bsModalRef: NgbModalRef;
62 orchStatus: OrchestratorStatus;
63 actionOrchFeatures = {
64 create: [OrchestratorFeature.SERVICE_CREATE],
65 update: [OrchestratorFeature.SERVICE_EDIT],
66 delete: [OrchestratorFeature.SERVICE_DELETE]
69 columns: Array<CdTableColumn> = [];
70 services: Array<CephServiceSpec> = [];
71 isLoadingServices = false;
72 selection: CdTableSelection = new CdTableSelection();
76 private actionLabels: ActionLabelsI18n,
77 private authStorageService: AuthStorageService,
78 private modalService: ModalService,
79 private orchService: OrchestratorService,
80 private cephServiceService: CephServiceService,
81 private relativeDatePipe: RelativeDatePipe,
82 private taskWrapperService: TaskWrapperService,
83 private router: Router
86 this.permissions = this.authStorageService.getPermissions();
91 click: () => this.openModal(),
92 name: this.actionLabels.CREATE,
93 canBePrimary: (selection: CdTableSelection) => !selection.hasSelection
94 // disable: (selection: CdTableSelection) => this.getDisable('create', selection)
99 click: () => this.openModal(true),
100 name: this.actionLabels.EDIT,
101 disable: (selection: CdTableSelection) => this.getDisable('update', selection)
104 permission: 'delete',
106 click: () => this.deleteAction(),
107 name: this.actionLabels.DELETE,
108 disable: (selection: CdTableSelection) => this.getDisable('delete', selection)
113 openModal(edit = false) {
114 if (this.routedModal) {
116 ? this.router.navigate([
122 this.selection.first().service_type,
123 this.selection.first().service_name
128 : this.router.navigate([BASE_URL, { outlets: { modal: [URLVerbs.CREATE] } }]);
130 let initialState = {};
133 serviceName: this.selection.first()?.service_name,
134 serviceType: this.selection?.first()?.service_type,
135 hiddenServices: this.hiddenServices,
139 hiddenServices: this.hiddenServices,
142 this.bsModalRef = this.modalService.show(ServiceFormComponent, initialState, { size: 'lg' });
149 name: $localize`Service`,
150 prop: 'service_name',
154 name: $localize`Placement`,
156 pipe: new PlacementPipe(),
160 name: $localize`Running`,
163 cellTemplate: this.runningTpl
166 name: $localize`Last Refreshed`,
167 prop: 'status.last_refresh',
168 pipe: this.relativeDatePipe,
173 this.columns = columns.filter((col: any) => {
174 return !this.hiddenColumns.includes(col.prop);
177 this.orchService.status().subscribe((status: OrchestratorStatus) => {
178 this.orchStatus = status;
179 this.showDocPanel = !status.available;
184 if (this.orchStatus?.available) {
186 this.table.reloadData();
191 action: 'create' | 'update' | 'delete',
192 selection: CdTableSelection
193 ): boolean | string {
194 if (action === 'delete') {
195 if (!selection?.hasSingleSelection) {
199 if (action === 'update') {
200 const disableEditServices = ['osd', 'container'];
201 if (disableEditServices.indexOf(this.selection.first()?.service_type) >= 0) {
205 return this.orchService.getTableActionDisableDesc(
207 this.actionOrchFeatures[action]
211 getServices(context: CdTableFetchDataContext) {
212 if (this.isLoadingServices) {
215 this.isLoadingServices = true;
216 const pagination_obs = this.cephServiceService.list(context.toParams());
217 pagination_obs.observable.subscribe(
218 (services: CephServiceSpec[]) => {
219 this.services = services;
220 this.count = pagination_obs.count;
221 this.services = this.services.filter((col: any) => {
222 return !this.hiddenServices.includes(col.service_name);
224 this.isLoadingServices = false;
227 this.isLoadingServices = false;
234 updateSelection(selection: CdTableSelection) {
235 this.selection = selection;
239 const service = this.selection.first();
240 this.modalService.show(CriticalConfirmationModalComponent, {
241 itemDescription: $localize`Service`,
242 itemNames: [service.service_name],
243 actionDescription: 'delete',
244 submitActionObservable: () =>
245 this.taskWrapperService
246 .wrapTaskAroundCall({
247 task: new FinishedTask(`service/${URLVerbs.DELETE}`, {
248 service_name: service.service_name
250 call: this.cephServiceService.delete(service.service_name)
253 // Delay closing the dialog, otherwise the datatable still
254 // shows the deleted service after an auto-reload.
255 // Showing the dialog while delaying is done to increase
256 // the user experience.