import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
+import { Router } from '@angular/router';
import { I18n } from '@ngx-translate/i18n-polyfill';
+import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';
import { HostService } from '../../../shared/api/host.service';
+import { ListWithDetails } from '../../../shared/classes/list-with-details.class';
+import { CriticalConfirmationModalComponent } from '../../../shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
+import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
+import { Icons } from '../../../shared/enum/icons.enum';
+import { CdTableAction } from '../../../shared/models/cd-table-action';
import { CdTableColumn } from '../../../shared/models/cd-table-column';
import { CdTableFetchDataContext } from '../../../shared/models/cd-table-fetch-data-context';
import { CdTableSelection } from '../../../shared/models/cd-table-selection';
+import { FinishedTask } from '../../../shared/models/finished-task';
import { Permissions } from '../../../shared/models/permissions';
import { CephShortVersionPipe } from '../../../shared/pipes/ceph-short-version.pipe';
+import { JoinPipe } from '../../../shared/pipes/join.pipe';
import { AuthStorageService } from '../../../shared/services/auth-storage.service';
+import { DepCheckerService } from '../../../shared/services/dep-checker.service';
+import { TaskWrapperService } from '../../../shared/services/task-wrapper.service';
+import { URLBuilderService } from '../../../shared/services/url-builder.service';
+
+const BASE_URL = 'hosts';
@Component({
selector: 'cd-hosts',
templateUrl: './hosts.component.html',
- styleUrls: ['./hosts.component.scss']
+ styleUrls: ['./hosts.component.scss'],
+ providers: [{ provide: URLBuilderService, useValue: new URLBuilderService(BASE_URL) }]
})
-export class HostsComponent implements OnInit {
+export class HostsComponent extends ListWithDetails implements OnInit {
permissions: Permissions;
columns: Array<CdTableColumn> = [];
hosts: Array<object> = [];
isLoadingHosts = false;
cdParams = { fromLink: '/hosts' };
+ tableActions: CdTableAction[];
selection = new CdTableSelection();
+ modalRef: BsModalRef;
- @ViewChild('servicesTpl')
+ @ViewChild('servicesTpl', { static: true })
public servicesTpl: TemplateRef<any>;
constructor(
private authStorageService: AuthStorageService,
private hostService: HostService,
private cephShortVersionPipe: CephShortVersionPipe,
- private i18n: I18n
+ private joinPipe: JoinPipe,
+ private i18n: I18n,
+ private urlBuilder: URLBuilderService,
+ private actionLabels: ActionLabelsI18n,
+ private modalService: BsModalService,
+ private taskWrapper: TaskWrapperService,
+ private router: Router,
+ private depCheckerService: DepCheckerService
) {
+ super();
this.permissions = this.authStorageService.getPermissions();
+ this.tableActions = [
+ {
+ name: this.actionLabels.CREATE,
+ permission: 'create',
+ icon: Icons.add,
+ click: () => {
+ this.depCheckerService.checkOrchestratorOrModal(
+ this.actionLabels.CREATE,
+ this.i18n('Host'),
+ () => {
+ this.router.navigate([this.urlBuilder.getCreate()]);
+ }
+ );
+ }
+ },
+ {
+ name: this.actionLabels.DELETE,
+ permission: 'delete',
+ icon: Icons.destroy,
+ click: () => {
+ this.depCheckerService.checkOrchestratorOrModal(
+ this.actionLabels.DELETE,
+ this.i18n('Host'),
+ () => this.deleteHostModal()
+ );
+ },
+ disable: () => !this.selection.hasSelection
+ }
+ ];
}
ngOnInit() {
flexGrow: 3,
cellTemplate: this.servicesTpl
},
+ {
+ name: this.i18n('Labels'),
+ prop: 'labels',
+ flexGrow: 1,
+ pipe: this.joinPipe
+ },
{
name: this.i18n('Version'),
prop: 'ceph_version',
this.selection = selection;
}
+ deleteHostModal() {
+ const hostname = this.selection.first().hostname;
+ this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
+ initialState: {
+ itemDescription: 'Host',
+ itemNames: [hostname],
+ actionDescription: 'delete',
+ submitActionObservable: () =>
+ this.taskWrapper.wrapTaskAroundCall({
+ task: new FinishedTask('host/delete', { hostname: hostname }),
+ call: this.hostService.delete(hostname)
+ })
+ }
+ });
+ }
+
getHosts(context: CdTableFetchDataContext) {
if (this.isLoadingHosts) {
return;
'tcmu-runner': 'iscsi'
};
this.isLoadingHosts = true;
- this.hostService
- .list()
- .then((resp) => {
+ this.hostService.list().subscribe(
+ (resp: any[]) => {
resp.map((host) => {
- host.services.map((service) => {
+ host.services.map((service: any) => {
service.cdLink = `/perf_counters/${service.type}/${encodeURIComponent(service.id)}`;
const permission = this.permissions[typeToPermissionKey[service.type]];
service.canRead = permission ? permission.read : false;
});
this.hosts = resp;
this.isLoadingHosts = false;
- })
- .catch(() => {
+ },
+ () => {
this.isLoadingHosts = false;
context.error();
- });
+ }
+ );
}
}