2 Copyright 2020 TATA ELXSI
4 Licensed under the Apache License, Version 2.0 (the 'License');
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
16 Author: KUMARAN M (kumaran.m@tataelxsi.co.in), RAJESH S (rajesh.s@tataelxsi.co.in), BARATH KUMAR R (barath.r@tataelxsi.co.in)
19 * @file K8sAddClusterComponent.ts.
21 import { HttpHeaders } from '@angular/common/http';
22 import { Component, ElementRef, Injector, OnInit, ViewChild } from '@angular/core';
23 import { FormBuilder, FormGroup, Validators } from '@angular/forms';
24 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
25 import { TranslateService } from '@ngx-translate/core';
26 import { NotifierService } from 'angular-notifier';
27 import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA } from 'CommonModel';
28 import { environment } from 'environment';
29 import * as jsyaml from 'js-yaml';
30 import { RestService } from 'RestService';
31 import { SharedService } from 'SharedService';
32 import { isNullOrUndefined } from 'util';
33 import { VimAccountDetails } from 'VimAccountModel';
36 * @Component takes K8sAddClusterComponent.html as template url
39 selector: 'app-k8s-add-cluster',
40 templateUrl: './K8sAddClusterComponent.html',
41 styleUrls: ['./K8sAddClusterComponent.scss']
43 /** Exporting a class @exports K8sAddClusterComponent */
44 export class K8sAddClusterComponent implements OnInit {
45 /** To inject services @public */
46 public injector: Injector;
48 /** FormGroup instance added to the form @ html @public */
49 public k8sclusterForm: FormGroup;
51 /** Contains all vim account collections */
52 public vimAccountSelect: VimAccountDetails;
54 /** Instance for active modal service @public */
55 public activeModal: NgbActiveModal;
57 /** Variable set for twoway bindng @public */
58 public vimAccountId: string;
60 /** Form submission Add */
61 public submitted: boolean = false;
63 /** Check the loading results @public */
64 public isLoadingResults: boolean = false;
66 /** Give the message for the loading @public */
67 public message: string = 'PLEASEWAIT';
69 /** Element ref for fileInputNets @public */
70 @ViewChild('fileInputNets', { static: true }) public fileInputNets: ElementRef;
72 /** Element ref for fileInputNetsLabel @public */
73 @ViewChild('fileInputNetsLabel', { static: true }) public fileInputNetsLabel: ElementRef;
75 /** Element ref for fileInputCredentials @public */
76 @ViewChild('fileInputCredentials', { static: true }) public fileInputCredentials: ElementRef;
78 /** Element ref for fileInputCredentialsLabel @public */
79 @ViewChild('fileInputCredentialsLabel', { static: true }) public fileInputCredentialsLabel: ElementRef;
81 /** FormBuilder instance added to the formBuilder @private */
82 private formBuilder: FormBuilder;
84 /** Utilizes rest service for any CRUD operations @private */
85 private restService: RestService;
87 /** Notifier service to popup notification @private */
88 private notifierService: NotifierService;
90 /** Contains tranlsate instance @private */
91 private translateService: TranslateService;
93 /** Controls the header form @private */
94 private headers: HttpHeaders;
96 /** Contains all methods related to shared @private */
97 private sharedService: SharedService;
99 constructor(injector: Injector) {
100 this.injector = injector;
101 this.restService = this.injector.get(RestService);
102 this.activeModal = this.injector.get(NgbActiveModal);
103 this.formBuilder = this.injector.get(FormBuilder);
104 this.notifierService = this.injector.get(NotifierService);
105 this.translateService = this.injector.get(TranslateService);
106 this.sharedService = this.injector.get(SharedService);
109 public ngOnInit(): void {
110 /** On Initializing call the methods */
111 this.k8sclusterFormAction();
112 this.getDetailsvimAccount();
113 this.headers = new HttpHeaders({
114 Accept: 'application/json',
115 'Content-Type': 'application/json',
116 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
120 /** On modal initializing forms @public */
121 public k8sclusterFormAction(): void {
122 this.k8sclusterForm = this.formBuilder.group({
123 name: ['', [Validators.required]],
124 k8s_version: ['', [Validators.required]],
125 vim_account: [null, [Validators.required]],
126 description: ['', [Validators.required]],
127 nets: ['', [Validators.required]],
128 credentials: ['', [Validators.required]]
132 /** convenience getter for easy access to form fields */
133 get f(): FormGroup['controls'] { return this.k8sclusterForm.controls; }
135 /** Call the vimAccount details in the selection options @public */
136 public getDetailsvimAccount(): void {
137 this.isLoadingResults = true;
138 this.restService.getResource(environment.VIMACCOUNTS_URL).subscribe((vimAccounts: VimAccountDetails) => {
139 this.vimAccountSelect = vimAccounts;
140 this.isLoadingResults = false;
141 }, (error: ERRORDATA) => {
142 this.restService.handleError(error, 'get');
143 this.isLoadingResults = false;
147 /** On modal submit k8sAddClusterSubmit will called @public */
148 public k8sAddClusterSubmit(): void {
149 this.submitted = true;
150 this.sharedService.cleanForm(this.k8sclusterForm);
151 if (this.k8sclusterForm.invalid) {
154 const modalData: MODALCLOSERESPONSEDATA = {
157 const apiURLHeader: APIURLHEADER = {
158 url: environment.K8SCLUSTER_URL,
159 httpOptions: { headers: this.headers }
161 const validJSONCredentails: boolean = this.sharedService.checkJson(this.k8sclusterForm.value.credentials);
162 if (validJSONCredentails) {
163 this.k8sclusterForm.value.credentials = jsyaml.load(this.k8sclusterForm.value.credentials.toString(), { json: true });
165 this.notifierService.notify('error', this.translateService.instant('INVALIDCONFIG'));
168 const validJSONNets: boolean = this.sharedService.checkJson(this.k8sclusterForm.value.nets);
170 this.k8sclusterForm.value.nets = jsyaml.load(this.k8sclusterForm.value.nets.toString(), { json: true });
172 this.notifierService.notify('error', this.translateService.instant('INVALIDCONFIG'));
175 this.isLoadingResults = true;
176 this.restService.postResource(apiURLHeader, this.k8sclusterForm.value).subscribe((result: {}) => {
177 this.activeModal.close(modalData);
178 this.isLoadingResults = false;
179 this.notifierService.notify('success', this.k8sclusterForm.value.name +
180 this.translateService.instant('PAGE.K8S.CREATEDSUCCESSFULLY'));
181 }, (error: ERRORDATA) => {
182 this.restService.handleError(error, 'post');
183 this.isLoadingResults = false;
187 /** Nets file process @private */
188 public netsFile(files: FileList): void {
189 if (files && files.length === 1) {
190 this.sharedService.getFileString(files, 'json').then((fileContent: string): void => {
191 const getNetsJson: string = jsyaml.load(fileContent, { json: true });
192 // tslint:disable-next-line: no-backbone-get-set-outside-model
193 this.k8sclusterForm.get('nets').setValue(JSON.stringify(getNetsJson));
194 }).catch((err: string): void => {
195 if (err === 'typeError') {
196 this.notifierService.notify('error', this.translateService.instant('JSONFILETYPEERRROR'));
198 this.notifierService.notify('error', this.translateService.instant('ERROR'));
200 this.fileInputNetsLabel.nativeElement.innerText = this.translateService.instant('CHOOSEFILE');
201 this.fileInputNets.nativeElement.value = null;
203 } else if (files && files.length > 1) {
204 this.notifierService.notify('error', this.translateService.instant('DROPFILESVALIDATION'));
206 this.fileInputNetsLabel.nativeElement.innerText = files[0].name;
207 this.fileInputNets.nativeElement.value = null;
210 /** credentials file process @private */
211 public credentialsFile(files: FileList): void {
212 if (files && files.length === 1) {
213 this.sharedService.getFileString(files, 'yaml').then((fileContent: string): void => {
214 const getCredentialsJson: string = jsyaml.load(fileContent, { json: true });
215 // tslint:disable-next-line: no-backbone-get-set-outside-model
216 this.k8sclusterForm.get('credentials').setValue(JSON.stringify(getCredentialsJson));
217 }).catch((err: string): void => {
218 if (err === 'typeError') {
219 this.notifierService.notify('error', this.translateService.instant('YAMLFILETYPEERRROR'));
221 this.notifierService.notify('error', this.translateService.instant('ERROR'));
223 this.fileInputCredentialsLabel.nativeElement.innerText = this.translateService.instant('CHOOSEFILE');
224 this.fileInputCredentials.nativeElement.value = null;
226 } else if (files && files.length > 1) {
227 this.notifierService.notify('error', this.translateService.instant('DROPFILESVALIDATION'));
229 this.fileInputCredentialsLabel.nativeElement.innerText = files[0].name;
230 this.fileInputCredentials.nativeElement.value = null;