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';
18 selector: 'cd-smb-join-auth-form',
19 templateUrl: './smb-join-auth-form.component.html',
20 styleUrls: ['./smb-join-auth-form.component.scss']
22 export class SmbJoinAuthFormComponent extends CdForm implements OnInit {
29 smbClusters$: Observable<SMBCluster[]>;
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
41 this.editing = this.router.url.startsWith(`${JOINAUTH_URL}/${URLVerbs.EDIT}`);
42 this.resource = $localize`Active directory (AD) access resource`;
46 this.action = this.actionLabels.CREATE;
47 this.smbClusters$ = this.smbService.listClusters();
51 this.action = this.actionLabels.UPDATE;
52 let editingAuthId: string;
53 this.route.params.subscribe((params: { authId: string }) => {
54 editingAuthId = params.authId;
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);
67 this.form = this.formBuilder.group({
68 authId: new FormControl('', {
69 validators: [Validators.required]
71 username: new FormControl('', {
72 validators: [Validators.required]
74 password: new FormControl('', {
75 validators: [Validators.required]
77 linkedToCluster: new FormControl(null)
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/';
88 const joinAuth: SMBJoinAuth = {
89 resource_type: JOIN_AUTH_RESOURCE,
91 auth: { username: username, password: password },
92 linked_to_cluster: linkedToCluster
96 let taskUrl = `${BASE_URL}${this.editing ? URLVerbs.EDIT : URLVerbs.CREATE}`;
97 this.taskWrapperService
99 task: new FinishedTask(taskUrl, {
102 call: this.smbService.createJoinAuth(joinAuth)
106 self.form.setErrors({ cdSubmitButton: true });
109 this.location.back();