blob: 0295b352dd0024b7c4448cc2297e0ac43c8adaac [file] [log] [blame]
kumaran.m3b4814a2020-05-01 19:48:54 +05301/*
2 Copyright 2020 TATA ELXSI
3
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
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
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.
15
16 Author: KUMARAN M (kumaran.m@tataelxsi.co.in), RAJESH S (rajesh.s@tataelxsi.co.in), BARATH KUMAR R (barath.r@tataelxsi.co.in)
17*/
18/**
19 * @file K8sAddClusterComponent.ts.
20 */
21import { HttpHeaders } from '@angular/common/http';
22import { Component, ElementRef, Injector, OnInit, ViewChild } from '@angular/core';
23import { FormBuilder, FormGroup, Validators } from '@angular/forms';
24import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
25import { TranslateService } from '@ngx-translate/core';
26import { NotifierService } from 'angular-notifier';
27import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA } from 'CommonModel';
28import { environment } from 'environment';
29import * as jsyaml from 'js-yaml';
30import { RestService } from 'RestService';
31import { SharedService } from 'SharedService';
32import { isNullOrUndefined } from 'util';
33import { VimAccountDetails } from 'VimAccountModel';
34/**
35 * Creating Component
36 * @Component takes K8sAddClusterComponent.html as template url
37 */
38@Component({
39 selector: 'app-k8s-add-cluster',
40 templateUrl: './K8sAddClusterComponent.html',
41 styleUrls: ['./K8sAddClusterComponent.scss']
42})
43/** Exporting a class @exports K8sAddClusterComponent */
44export class K8sAddClusterComponent implements OnInit {
45 /** To inject services @public */
46 public injector: Injector;
47
48 /** FormGroup instance added to the form @ html @public */
49 public k8sclusterForm: FormGroup;
50
51 /** Contains all vim account collections */
52 public vimAccountSelect: VimAccountDetails;
53
54 /** Instance for active modal service @public */
55 public activeModal: NgbActiveModal;
56
57 /** Variable set for twoway bindng @public */
58 public vimAccountId: string;
59
60 /** Form submission Add */
61 public submitted: boolean = false;
62
63 /** Check the loading results @public */
64 public isLoadingResults: boolean = false;
65
66 /** Give the message for the loading @public */
67 public message: string = 'PLEASEWAIT';
68
69 /** Element ref for fileInputNets @public */
70 @ViewChild('fileInputNets', { static: true }) public fileInputNets: ElementRef;
71
72 /** Element ref for fileInputNetsLabel @public */
73 @ViewChild('fileInputNetsLabel', { static: true }) public fileInputNetsLabel: ElementRef;
74
75 /** Element ref for fileInputCredentials @public */
76 @ViewChild('fileInputCredentials', { static: true }) public fileInputCredentials: ElementRef;
77
78 /** Element ref for fileInputCredentialsLabel @public */
79 @ViewChild('fileInputCredentialsLabel', { static: true }) public fileInputCredentialsLabel: ElementRef;
80
81 /** FormBuilder instance added to the formBuilder @private */
82 private formBuilder: FormBuilder;
83
84 /** Utilizes rest service for any CRUD operations @private */
85 private restService: RestService;
86
87 /** Notifier service to popup notification @private */
88 private notifierService: NotifierService;
89
90 /** Contains tranlsate instance @private */
91 private translateService: TranslateService;
92
93 /** Controls the header form @private */
94 private headers: HttpHeaders;
95
96 /** Contains all methods related to shared @private */
97 private sharedService: SharedService;
98
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);
107 }
108
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'
117 });
118 }
119
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]]
129 });
130 }
131
132 /** convenience getter for easy access to form fields */
133 get f(): FormGroup['controls'] { return this.k8sclusterForm.controls; }
134
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;
144 });
145 }
146
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) {
152 return;
153 }
154 const modalData: MODALCLOSERESPONSEDATA = {
155 message: 'Done'
156 };
157 const apiURLHeader: APIURLHEADER = {
158 url: environment.K8SCLUSTER_URL,
159 httpOptions: { headers: this.headers }
160 };
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 });
164 } else {
165 this.notifierService.notify('error', this.translateService.instant('INVALIDCONFIG'));
166 return;
167 }
168 const validJSONNets: boolean = this.sharedService.checkJson(this.k8sclusterForm.value.nets);
169 if (validJSONNets) {
170 this.k8sclusterForm.value.nets = jsyaml.load(this.k8sclusterForm.value.nets.toString(), { json: true });
171 } else {
172 this.notifierService.notify('error', this.translateService.instant('INVALIDCONFIG'));
173 return;
174 }
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;
184 });
185 }
186
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'));
197 } else {
198 this.notifierService.notify('error', this.translateService.instant('ERROR'));
199 }
200 this.fileInputNetsLabel.nativeElement.innerText = this.translateService.instant('CHOOSEFILE');
201 this.fileInputNets.nativeElement.value = null;
202 });
203 } else if (files && files.length > 1) {
204 this.notifierService.notify('error', this.translateService.instant('DROPFILESVALIDATION'));
205 }
206 this.fileInputNetsLabel.nativeElement.innerText = files[0].name;
207 this.fileInputNets.nativeElement.value = null;
208 }
209
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'));
220 } else {
221 this.notifierService.notify('error', this.translateService.instant('ERROR'));
222 }
223 this.fileInputCredentialsLabel.nativeElement.innerText = this.translateService.instant('CHOOSEFILE');
224 this.fileInputCredentials.nativeElement.value = null;
225 });
226 } else if (files && files.length > 1) {
227 this.notifierService.notify('error', this.translateService.instant('DROPFILESVALIDATION'));
228 }
229 this.fileInputCredentialsLabel.nativeElement.innerText = files[0].name;
230 this.fileInputCredentials.nativeElement.value = null;
231 }
232
233}