summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/osd/osd-flags-indiv-modal/osd-flags-indiv-modal.component.ts
blob: e9e0b876f3bd896914d96c6bd5254233f838a756 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { Component, OnInit } from '@angular/core';
import { FormGroup } from '@angular/forms';

import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
import _ from 'lodash';

import { OsdService } from '~/app/shared/api/osd.service';
import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
import { NotificationType } from '~/app/shared/enum/notification-type.enum';
import { Flag } from '~/app/shared/models/flag';
import { Permissions } from '~/app/shared/models/permissions';
import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
import { NotificationService } from '~/app/shared/services/notification.service';

@Component({
  selector: 'cd-osd-flags-indiv-modal',
  templateUrl: './osd-flags-indiv-modal.component.html',
  styleUrls: ['./osd-flags-indiv-modal.component.scss']
})
export class OsdFlagsIndivModalComponent implements OnInit {
  permissions: Permissions;
  selected: object[];
  initialSelection: Flag[] = [];
  osdFlagsForm = new FormGroup({});
  flags: Flag[] = [
    {
      code: 'noup',
      name: $localize`No Up`,
      description: $localize`OSDs are not allowed to start`,
      value: false,
      clusterWide: false,
      indeterminate: false
    },
    {
      code: 'nodown',
      name: $localize`No Down`,
      description: $localize`OSD failure reports are being ignored, such that the monitors will not mark OSDs down`,
      value: false,
      clusterWide: false,
      indeterminate: false
    },
    {
      code: 'noin',
      name: $localize`No In`,
      description: $localize`OSDs that were previously marked out will not be marked back in when they start`,
      value: false,
      clusterWide: false,
      indeterminate: false
    },
    {
      code: 'noout',
      name: $localize`No Out`,
      description: $localize`OSDs will not automatically be marked out after the configured interval`,
      value: false,
      clusterWide: false,
      indeterminate: false
    }
  ];
  clusterWideTooltip: string = $localize`The flag has been enabled for the entire cluster.`;

  constructor(
    public activeModal: NgbActiveModal,
    public actionLabels: ActionLabelsI18n,
    private authStorageService: AuthStorageService,
    private osdService: OsdService,
    private notificationService: NotificationService
  ) {
    this.permissions = this.authStorageService.getPermissions();
  }

  ngOnInit() {
    const osdCount = this.selected.length;
    this.osdService.getFlags().subscribe((clusterWideFlags: string[]) => {
      const activatedIndivFlags = this.getActivatedIndivFlags();
      this.flags.forEach((flag) => {
        const flagCount = activatedIndivFlags[flag.code];
        if (clusterWideFlags.includes(flag.code)) {
          flag.clusterWide = true;
        }

        if (flagCount === osdCount) {
          flag.value = true;
        } else if (flagCount > 0) {
          flag.indeterminate = true;
        }
      });
      this.initialSelection = _.cloneDeep(this.flags);
    });
  }

  getActivatedIndivFlags(): { [flag: string]: number } {
    const flagsCount = {};
    this.flags.forEach((flag) => {
      flagsCount[flag.code] = 0;
    });

    [].concat(...this.selected.map((osd) => osd['state'])).map((activatedFlag) => {
      if (Object.keys(flagsCount).includes(activatedFlag)) {
        flagsCount[activatedFlag] = flagsCount[activatedFlag] + 1;
      }
    });
    return flagsCount;
  }

  changeValue(flag: Flag) {
    flag.value = !flag.value;
    flag.indeterminate = false;
  }

  resetSelection() {
    this.flags = _.cloneDeep(this.initialSelection);
  }

  submitAction() {
    const activeFlags = {};
    this.flags.forEach((flag) => {
      if (flag.indeterminate) {
        activeFlags[flag.code] = null;
      } else {
        activeFlags[flag.code] = flag.value;
      }
    });
    const selectedIds = this.selected.map((selection) => selection['osd']);
    this.osdService.updateIndividualFlags(activeFlags, selectedIds).subscribe(
      () => {
        this.notificationService.show(NotificationType.success, $localize`Updated OSD Flags`);
        this.activeModal.close();
      },
      () => {
        this.activeModal.close();
      }
    );
  }
}