]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
308ad15f36d6334f749ae44bab194d9bc0738506
[ceph-ci.git] /
1 import { Component, OnInit } from '@angular/core';
2 import { FormControl, Validators } from '@angular/forms';
3 import { ActivatedRoute, Router } from '@angular/router';
4 import { SmbService } from '~/app/shared/api/smb.service';
5 import { ActionLabelsI18n, URLVerbs } from '~/app/shared/constants/app.constants';
6 import { Icons } from '~/app/shared/enum/icons.enum';
7 import { CdForm } from '~/app/shared/forms/cd-form';
8 import { CdFormBuilder } from '~/app/shared/forms/cd-form-builder';
9 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
10 import { FinishedTask } from '~/app/shared/models/finished-task';
11 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
12 import { JOIN_AUTH_RESOURCE, SMBCluster, SMBJoinAuth } from '../smb.model';
13 import { Observable } from 'rxjs';
14 import { JOINAUTH_URL } from '../smb-join-auth-list/smb-join-auth-list.component';
15 import { Location } from '@angular/common';
16
17 @Component({
18   selector: 'cd-smb-join-auth-form',
19   templateUrl: './smb-join-auth-form.component.html',
20   styleUrls: ['./smb-join-auth-form.component.scss']
21 })
22 export class SmbJoinAuthFormComponent extends CdForm implements OnInit {
23   form: CdFormGroup;
24   action: string;
25   resource: string;
26   editing: boolean;
27   icons = Icons;
28
29   smbClusters$: Observable<SMBCluster[]>;
30
31   constructor(
32     private actionLabels: ActionLabelsI18n,
33     private taskWrapperService: TaskWrapperService,
34     private formBuilder: CdFormBuilder,
35     private smbService: SmbService,
36     private router: Router,
37     private route: ActivatedRoute,
38     private location: Location
39   ) {
40     super();
41     this.editing = this.router.url.startsWith(`${JOINAUTH_URL}/${URLVerbs.EDIT}`);
42     this.resource = $localize`Active directory (AD) access resource`;
43   }
44
45   ngOnInit() {
46     this.action = this.actionLabels.CREATE;
47     this.smbClusters$ = this.smbService.listClusters();
48     this.createForm();
49
50     if (this.editing) {
51       this.action = this.actionLabels.UPDATE;
52       let editingAuthId: string;
53       this.route.params.subscribe((params: { authId: string }) => {
54         editingAuthId = params.authId;
55       });
56
57       this.smbService.getJoinAuth(editingAuthId).subscribe((joinAuth: SMBJoinAuth) => {
58         this.form.get('authId').setValue(joinAuth.auth_id);
59         this.form.get('username').setValue(joinAuth.auth.username);
60         this.form.get('password').setValue(joinAuth.auth.password);
61         this.form.get('linkedToCluster').setValue(joinAuth.linked_to_cluster);
62       });
63     }
64   }
65
66   createForm() {
67     this.form = this.formBuilder.group({
68       authId: new FormControl('', {
69         validators: [Validators.required]
70       }),
71       username: new FormControl('', {
72         validators: [Validators.required]
73       }),
74       password: new FormControl('', {
75         validators: [Validators.required]
76       }),
77       linkedToCluster: new FormControl(null)
78     });
79   }
80
81   submit() {
82     const authId = this.form.getValue('authId');
83     const username = this.form.getValue('username');
84     const password = this.form.getValue('password');
85     const linkedToCluster = this.form.getValue('linkedToCluster');
86     const BASE_URL = 'smb/ad/';
87
88     const joinAuth: SMBJoinAuth = {
89       resource_type: JOIN_AUTH_RESOURCE,
90       auth_id: authId,
91       auth: { username: username, password: password },
92       linked_to_cluster: linkedToCluster
93     };
94
95     const self = this;
96     let taskUrl = `${BASE_URL}${this.editing ? URLVerbs.EDIT : URLVerbs.CREATE}`;
97     this.taskWrapperService
98       .wrapTaskAroundCall({
99         task: new FinishedTask(taskUrl, {
100           authId: authId
101         }),
102         call: this.smbService.createJoinAuth(joinAuth)
103       })
104       .subscribe({
105         error() {
106           self.form.setErrors({ cdSubmitButton: true });
107         },
108         complete: () => {
109           this.location.back();
110         }
111       });
112   }
113 }