dialog.ts 3.7 KB
Newer Older
H
haijiao.yin 已提交
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.

15 16
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {Component, Inject, OnInit} from '@angular/core';
S
Shu Muto 已提交
17
import {AbstractControl, FormBuilder, FormGroup, Validators} from '@angular/forms';
S
Sebastian Florek 已提交
18
import {MAT_DIALOG_DATA, MatDialog, MatDialogRef} from '@angular/material/dialog';
19
import {switchMap} from 'rxjs/operators';
S
Shu Muto 已提交
20
import {AlertDialog, AlertDialogConfig} from '../../../../common/dialogs/alert/dialog';
21 22
import {CsrfTokenService} from '../../../../common/services/global/csrftoken';
import {CONFIG} from '../../../../index.config';
H
haijiao.yin 已提交
23

24 25 26
export interface CreateSecretDialogMeta {
  namespace: string;
}
H
haijiao.yin 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

@Component({
  selector: 'kd-create-secret-dialog',
  templateUrl: 'template.html',
})
export class CreateSecretDialog implements OnInit {
  form: FormGroup;

  private readonly config_ = CONFIG;

  /**
   * Max-length validation rule for secretName.
   */
  secretNameMaxLength = 253;

  /**
   * Pattern validation rule for secretName.
   */
M
Marcin Maciaszczyk 已提交
45
  secretNamePattern = new RegExp('^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$');
H
haijiao.yin 已提交
46 47 48 49

  /**
   * Pattern validating if the secret data is Base64 encoded.
   */
50
  dataPattern = new RegExp('^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$');
H
haijiao.yin 已提交
51 52

  constructor(
S
Shu Muto 已提交
53 54 55 56 57
    public dialogRef: MatDialogRef<CreateSecretDialog>,
    @Inject(MAT_DIALOG_DATA) public data_: CreateSecretDialogMeta,
    private readonly http_: HttpClient,
    private readonly csrfToken_: CsrfTokenService,
    private readonly matDialog_: MatDialog,
58
    private readonly fb_: FormBuilder
S
Shu Muto 已提交
59
  ) {}
H
haijiao.yin 已提交
60 61 62 63

  ngOnInit(): void {
    this.form = this.fb_.group({
      secretName: [
S
Shu Muto 已提交
64 65 66 67 68
        '',
        Validators.compose([
          Validators.maxLength(this.secretNameMaxLength),
          Validators.pattern(this.secretNamePattern),
        ]),
H
haijiao.yin 已提交
69
      ],
S
Shu Muto 已提交
70
      data: ['', Validators.pattern(this.dataPattern)],
H
haijiao.yin 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    });
  }

  get secretName(): AbstractControl {
    return this.form.get('secretName');
  }

  get data(): AbstractControl {
    return this.form.get('data');
  }

  /**
   * Creates new secret based on the state of the controller.
   */
  createSecret(): void {
    if (!this.form.valid) return;

    const secretSpec = {
      name: this.secretName.value,
      namespace: this.data_.namespace,
      data: this.data.value,
    };

    const tokenPromise = this.csrfToken_.getTokenForAction('secret');
95 96 97 98 99 100 101 102
    tokenPromise
      .pipe(
        switchMap(csrfToken =>
          this.http_.post<{valid: boolean}>(
            'api/v1/secret/',
            {...secretSpec},
            {headers: new HttpHeaders().set(this.config_.csrfHeaderName, csrfToken.token)}
          )
S
Shu Muto 已提交
103
        )
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
      )
      .subscribe(
        () => {
          this.dialogRef.close(this.secretName.value);
        },
        error => {
          this.dialogRef.close();
          const configData: AlertDialogConfig = {
            title: 'Error creating secret',
            message: error.data,
            confirmLabel: 'OK',
          };
          this.matDialog_.open(AlertDialog, {data: configData});
        }
      );
H
haijiao.yin 已提交
119 120 121 122 123 124 125 126 127
  }

  /**
   * Cancels the create secret form.
   */
  cancel(): void {
    this.dialogRef.close();
  }
}