verber.ts 4.9 KB
Newer Older
M
Marcin Maciaszczyk 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright 2017 The Kubernetes Authors.
//
// 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.

S
Shu Muto 已提交
15
import {HttpClient, HttpErrorResponse, HttpHeaders} from '@angular/common/http';
16
import {EventEmitter, Injectable} from '@angular/core';
S
Sebastian Florek 已提交
17
import {MatDialog, MatDialogConfig} from '@angular/material/dialog';
18
import {ObjectMeta, TypeMeta} from '@api/backendapi';
19
import {filter, switchMap} from 'rxjs/operators';
M
Marcin Maciaszczyk 已提交
20

21 22 23 24 25 26
import {AlertDialog, AlertDialogConfig} from '../../dialogs/alert/dialog';
import {DeleteResourceDialog} from '../../dialogs/deleteresource/dialog';
import {EditResourceDialog} from '../../dialogs/editresource/dialog';
import {ScaleResourceDialog} from '../../dialogs/scaleresource/dialog';
import {TriggerResourceDialog} from '../../dialogs/triggerresource/dialog';
import {RawResource} from '../../resources/rawresource';
27

28
import {ResourceMeta} from './actionbar';
M
Marcin Maciaszczyk 已提交
29 30 31 32

@Injectable()
export class VerberService {
  onDelete = new EventEmitter<boolean>();
M
Marcin Maciaszczyk 已提交
33
  onEdit = new EventEmitter<boolean>();
M
Marcin Maciaszczyk 已提交
34
  onScale = new EventEmitter<boolean>();
35
  onTrigger = new EventEmitter<boolean>();
M
Marcin Maciaszczyk 已提交
36

37
  constructor(private readonly dialog_: MatDialog, private readonly http_: HttpClient) {}
M
Marcin Maciaszczyk 已提交
38

39 40
  showDeleteDialog(displayName: string, typeMeta: TypeMeta, objectMeta: ObjectMeta): void {
    const dialogConfig = this.getDialogConfig_(displayName, typeMeta, objectMeta);
S
Shu Muto 已提交
41 42 43
    this.dialog_
      .open(DeleteResourceDialog, dialogConfig)
      .afterClosed()
44 45 46
      .pipe(filter(doDelete => doDelete))
      .pipe(
        switchMap(_ => {
S
Shu Muto 已提交
47
          const url = RawResource.getUrl(typeMeta, objectMeta);
48 49 50 51
          return this.http_.delete(url, {responseType: 'text'});
        })
      )
      .subscribe(_ => this.onDelete.emit(true), this.handleErrorResponse_.bind(this));
M
Marcin Maciaszczyk 已提交
52 53
  }

54 55
  showEditDialog(displayName: string, typeMeta: TypeMeta, objectMeta: ObjectMeta): void {
    const dialogConfig = this.getDialogConfig_(displayName, typeMeta, objectMeta);
S
Shu Muto 已提交
56 57 58
    this.dialog_
      .open(EditResourceDialog, dialogConfig)
      .afterClosed()
59 60 61
      .pipe(filter(result => result))
      .pipe(
        switchMap(result => {
S
Shu Muto 已提交
62
          const url = RawResource.getUrl(typeMeta, objectMeta);
63 64 65 66
          return this.http_.put(url, JSON.parse(result), {headers: this.getHttpHeaders_(), responseType: 'text'});
        })
      )
      .subscribe(_ => this.onEdit.emit(true), this.handleErrorResponse_.bind(this));
M
Marcin Maciaszczyk 已提交
67 68
  }

69 70
  showScaleDialog(displayName: string, typeMeta: TypeMeta, objectMeta: ObjectMeta): void {
    const dialogConfig = this.getDialogConfig_(displayName, typeMeta, objectMeta);
S
Shu Muto 已提交
71 72 73
    this.dialog_
      .open(ScaleResourceDialog, dialogConfig)
      .afterClosed()
74 75 76 77 78 79
      .pipe(filter(result => Number.isInteger(result)))
      .pipe(
        switchMap(result => {
          const url = `api/v1/scale/${typeMeta.kind}${objectMeta.namespace ? `/${objectMeta.namespace}` : ''}/${
            objectMeta.name
          }/`;
80

81 82 83 84
          return this.http_.put(url, result, {params: {scaleBy: result}});
        })
      )
      .subscribe(_ => this.onScale.emit(true), this.handleErrorResponse_.bind(this));
85 86
  }

87 88
  showTriggerDialog(displayName: string, typeMeta: TypeMeta, objectMeta: ObjectMeta): void {
    const dialogConfig = this.getDialogConfig_(displayName, typeMeta, objectMeta);
S
Shu Muto 已提交
89 90 91
    this.dialog_
      .open(TriggerResourceDialog, dialogConfig)
      .afterClosed()
92 93 94
      .pipe(filter(result => result))
      .pipe(
        switchMap(_ => {
S
Shu Muto 已提交
95
          const url = `api/v1/cronjob/${objectMeta.namespace}/${objectMeta.name}/trigger`;
96 97 98 99
          return this.http_.put(url, {}, {responseType: 'text'});
        })
      )
      .subscribe(_ => this.onTrigger.emit(true), this.handleErrorResponse_.bind(this));
100 101
  }

M
Marcin Maciaszczyk 已提交
102
  getDialogConfig_(displayName: string, typeMeta: TypeMeta, objectMeta: ObjectMeta): MatDialogConfig<ResourceMeta> {
103
    return {width: '900px', data: {displayName, typeMeta, objectMeta}};
104 105
  }

106
  handleErrorResponse_(err: HttpErrorResponse): void {
107 108 109 110
    if (err) {
      const alertDialogConfig: MatDialogConfig<AlertDialogConfig> = {
        width: '630px',
        data: {
111
          title: err.statusText === 'OK' ? 'Internal server error' : err.statusText,
112
          message: err.error || 'Could not perform the operation.',
113
          confirmLabel: 'OK',
S
Shu Muto 已提交
114
        },
115 116 117 118 119 120 121 122 123 124
      };
      this.dialog_.open(AlertDialog, alertDialogConfig);
    }
  }

  getHttpHeaders_(): HttpHeaders {
    const headers = new HttpHeaders();
    headers.set('Content-Type', 'application/json');
    headers.set('Accept', 'application/json');
    return headers;
M
Marcin Maciaszczyk 已提交
125 126
  }
}