]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
e9e0b876f3bd896914d96c6bd5254233f838a756
[ceph-ci.git] /
1 import { Component, OnInit } from '@angular/core';
2 import { FormGroup } from '@angular/forms';
3
4 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
5 import _ from 'lodash';
6
7 import { OsdService } from '~/app/shared/api/osd.service';
8 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
9 import { NotificationType } from '~/app/shared/enum/notification-type.enum';
10 import { Flag } from '~/app/shared/models/flag';
11 import { Permissions } from '~/app/shared/models/permissions';
12 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
13 import { NotificationService } from '~/app/shared/services/notification.service';
14
15 @Component({
16   selector: 'cd-osd-flags-indiv-modal',
17   templateUrl: './osd-flags-indiv-modal.component.html',
18   styleUrls: ['./osd-flags-indiv-modal.component.scss']
19 })
20 export class OsdFlagsIndivModalComponent implements OnInit {
21   permissions: Permissions;
22   selected: object[];
23   initialSelection: Flag[] = [];
24   osdFlagsForm = new FormGroup({});
25   flags: Flag[] = [
26     {
27       code: 'noup',
28       name: $localize`No Up`,
29       description: $localize`OSDs are not allowed to start`,
30       value: false,
31       clusterWide: false,
32       indeterminate: false
33     },
34     {
35       code: 'nodown',
36       name: $localize`No Down`,
37       description: $localize`OSD failure reports are being ignored, such that the monitors will not mark OSDs down`,
38       value: false,
39       clusterWide: false,
40       indeterminate: false
41     },
42     {
43       code: 'noin',
44       name: $localize`No In`,
45       description: $localize`OSDs that were previously marked out will not be marked back in when they start`,
46       value: false,
47       clusterWide: false,
48       indeterminate: false
49     },
50     {
51       code: 'noout',
52       name: $localize`No Out`,
53       description: $localize`OSDs will not automatically be marked out after the configured interval`,
54       value: false,
55       clusterWide: false,
56       indeterminate: false
57     }
58   ];
59   clusterWideTooltip: string = $localize`The flag has been enabled for the entire cluster.`;
60
61   constructor(
62     public activeModal: NgbActiveModal,
63     public actionLabels: ActionLabelsI18n,
64     private authStorageService: AuthStorageService,
65     private osdService: OsdService,
66     private notificationService: NotificationService
67   ) {
68     this.permissions = this.authStorageService.getPermissions();
69   }
70
71   ngOnInit() {
72     const osdCount = this.selected.length;
73     this.osdService.getFlags().subscribe((clusterWideFlags: string[]) => {
74       const activatedIndivFlags = this.getActivatedIndivFlags();
75       this.flags.forEach((flag) => {
76         const flagCount = activatedIndivFlags[flag.code];
77         if (clusterWideFlags.includes(flag.code)) {
78           flag.clusterWide = true;
79         }
80
81         if (flagCount === osdCount) {
82           flag.value = true;
83         } else if (flagCount > 0) {
84           flag.indeterminate = true;
85         }
86       });
87       this.initialSelection = _.cloneDeep(this.flags);
88     });
89   }
90
91   getActivatedIndivFlags(): { [flag: string]: number } {
92     const flagsCount = {};
93     this.flags.forEach((flag) => {
94       flagsCount[flag.code] = 0;
95     });
96
97     [].concat(...this.selected.map((osd) => osd['state'])).map((activatedFlag) => {
98       if (Object.keys(flagsCount).includes(activatedFlag)) {
99         flagsCount[activatedFlag] = flagsCount[activatedFlag] + 1;
100       }
101     });
102     return flagsCount;
103   }
104
105   changeValue(flag: Flag) {
106     flag.value = !flag.value;
107     flag.indeterminate = false;
108   }
109
110   resetSelection() {
111     this.flags = _.cloneDeep(this.initialSelection);
112   }
113
114   submitAction() {
115     const activeFlags = {};
116     this.flags.forEach((flag) => {
117       if (flag.indeterminate) {
118         activeFlags[flag.code] = null;
119       } else {
120         activeFlags[flag.code] = flag.value;
121       }
122     });
123     const selectedIds = this.selected.map((selection) => selection['osd']);
124     this.osdService.updateIndividualFlags(activeFlags, selectedIds).subscribe(
125       () => {
126         this.notificationService.show(NotificationType.success, $localize`Updated OSD Flags`);
127         this.activeModal.close();
128       },
129       () => {
130         this.activeModal.close();
131       }
132     );
133   }
134 }