| /* |
| Copyright 2020 TATA ELXSI |
| |
| 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. |
| |
| Author: SANDHYA JS (sandhya.j@tataelxsi.co.in) |
| */ |
| /** |
| * @file K8sAddClusterComponent.ts. |
| */ |
| import { HttpHeaders } from '@angular/common/http'; |
| import { Component, Injector, Input, OnInit } from '@angular/core'; |
| import { AbstractControl, FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms'; |
| import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'; |
| import { TranslateService } from '@ngx-translate/core'; |
| import { NotifierService } from 'angular-notifier'; |
| import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA, TYPESECTION } from 'CommonModel'; |
| import { environment } from 'environment'; |
| import { K8SCLUSTERDATA, K8SPayload } from 'K8sModel'; |
| import { RestService } from 'RestService'; |
| import { isNullOrUndefined, SharedService } from 'SharedService'; |
| /** |
| * Creating Component |
| * @Component takes NodeAddComponent.html as template url |
| */ |
| @Component({ |
| selector: 'app-node-add', |
| templateUrl: './NodeAddComponent.html', |
| styleUrls: ['./NodeAddComponent.scss'] |
| }) |
| /** Exporting a class @exports NodeAddComponent */ |
| export class NodeAddComponent implements OnInit { |
| /** To inject services @public */ |
| public injector: Injector; |
| |
| /** FormGroup instance added to the form @ html @public */ |
| public nodeForm: FormGroup; |
| |
| /** Contains all vim account collections */ |
| public clusterItems: K8SCLUSTERDATA; |
| |
| /** Input contains Modal dialog component Instance @public */ |
| @Input() public profileType: string; |
| |
| /** Input contains Modal dialog component Instance @public */ |
| @Input() public profileID: string; |
| |
| /** Input contains Modal dialog component Instance @public */ |
| @Input() public clusterId: string; |
| |
| /** Instance for active modal service @public */ |
| public activeModal: NgbActiveModal; |
| |
| /** Variable set for twoway bindng @public */ |
| public vimAccountId: string; |
| |
| /** contains url @public */ |
| public clusterUrl: string; |
| |
| /** contains privatesubnet items @public */ |
| privateItems: [] = []; |
| |
| /** contains publicsubnet items @public */ |
| publicItems: [] = []; |
| |
| /** contains privatesubnet items from api @public */ |
| privatesubnetItems: {}; |
| |
| /** contains publicsubnet itams from api @public */ |
| publicsubnetItems: {}; |
| |
| /** contains cluster ID @public */ |
| public cluster_name: string; |
| |
| /** contains scaling action paylaod@public */ |
| public scalingPayload = {}; |
| |
| /** Form submission Add */ |
| public submitted: boolean = false; |
| |
| /** contains payload */ |
| public payload: K8SPayload; |
| |
| /** Check the loading results @public */ |
| public isLoadingResults: boolean = false; |
| |
| /** Give the message for the loading @public */ |
| public message: string = 'PLEASEWAIT'; |
| |
| /** FormBuilder instance added to the formBuilder @private */ |
| private formBuilder: FormBuilder; |
| |
| /** Utilizes rest service for any CRUD operations @private */ |
| private restService: RestService; |
| |
| /** Notifier service to popup notification @private */ |
| private notifierService: NotifierService; |
| |
| /** Contains tranlsate instance @private */ |
| private translateService: TranslateService; |
| |
| /** Controls the header form @private */ |
| private headers: HttpHeaders; |
| |
| /** Contains all methods related to shared @private */ |
| private sharedService: SharedService; |
| |
| constructor(injector: Injector) { |
| this.injector = injector; |
| this.restService = this.injector.get(RestService); |
| this.activeModal = this.injector.get(NgbActiveModal); |
| this.formBuilder = this.injector.get(FormBuilder); |
| this.notifierService = this.injector.get(NotifierService); |
| this.translateService = this.injector.get(TranslateService); |
| this.sharedService = this.injector.get(SharedService); |
| } |
| |
| public ngOnInit(): void { |
| /** On Initializing call the methods */ |
| this.nodeFormAction(); |
| this.getDetailsvimAccount(); |
| this.headers = new HttpHeaders({ |
| Accept: 'application/json', |
| 'Content-Type': 'application/json', |
| 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0' |
| }); |
| if (this.profileType === 'card_node') { |
| this.cluster_name = this.clusterId; |
| this.clusterChange(); |
| } else if (this.profileType === 'k8s-scale' || this.profileType === 'edit-node') { |
| this.cluster_name = this.clusterId; |
| this.getDetails(); |
| } |
| else { |
| this.cluster_name = null; |
| } |
| } |
| |
| /** On modal initializing forms @public */ |
| public nodeFormAction(): void { |
| this.nodeForm = this.formBuilder.group({ |
| name: ['', [Validators.required]], |
| description: [''], |
| node_count: ['', [Validators.required]], |
| node_size: ['', [Validators.required]], |
| nodeCount: [''], |
| role: [''], |
| public_subnet: [[]], |
| private_subnet: [[]], |
| cluster_id: ['', [Validators.required]], |
| edit_name: [''] |
| }); |
| } |
| |
| /** convenience getter for easy access to form fields */ |
| get f(): FormGroup['controls'] { return this.nodeForm.controls; } |
| |
| |
| /** Call the vimAccount details in the selection options @public */ |
| public getDetailsvimAccount(): void { |
| this.isLoadingResults = true; |
| this.restService.getResource(environment.K8SCREATECLUSTER_URL).subscribe((vimAccounts: K8SCLUSTERDATA) => { |
| this.clusterItems = vimAccounts; |
| this.isLoadingResults = false; |
| }, (error: ERRORDATA) => { |
| this.restService.handleError(error, 'get'); |
| this.isLoadingResults = false; |
| }); |
| } |
| |
| /** To patch value for edit @public */ |
| public getDetails(): void { |
| this.isLoadingResults = true; |
| this.restService.getResource(environment.K8SCREATECLUSTER_URL + '/' + this.cluster_name + '/nodegroup' + '/' + this.profileID).subscribe((vimAccounts: K8SCLUSTERDATA) => { |
| if (this.profileType === 'edit-node') { |
| this.nodeForm.patchValue({ edit_name: vimAccounts.name, description: vimAccounts.description }); |
| } else if (this.profileType === 'k8s-scale') { |
| this.nodeForm.patchValue({ nodeCount: vimAccounts.node_count }); |
| } |
| this.isLoadingResults = false; |
| }, (error: ERRORDATA) => { |
| this.restService.handleError(error, 'get'); |
| this.isLoadingResults = false; |
| }); |
| } |
| |
| /** To get privatesubnet and publicsubnet details @public */ |
| public clusterChange(): void { |
| this.isLoadingResults = true; |
| this.restService.getResource(environment.K8SCREATECLUSTER_URL + '/' + this.cluster_name).subscribe((vimAccounts: K8SCLUSTERDATA) => { |
| if (!isNullOrUndefined(vimAccounts)) { |
| this.privatesubnetItems = vimAccounts.private_subnet; |
| this.publicsubnetItems = vimAccounts.public_subnet; |
| } |
| this.isLoadingResults = false; |
| }, (error: ERRORDATA) => { |
| this.restService.handleError(error, 'get'); |
| this.isLoadingResults = false; |
| }); |
| } |
| |
| /** On modal submit nodeSubmit will called @public */ |
| public nodeSubmit(): void { |
| if (this.profileType === 'card_node') { |
| this.getFormControl('nodeCount').disable(); |
| this.getFormControl('cluster_id').disable(); |
| this.getFormControl('edit_name').disable(); |
| this.clusterUrl = environment.K8SCREATECLUSTER_URL + '/' + this.cluster_name + '/nodegroup'; |
| this.createNode(); |
| } else if (this.profileType === 'k8s-scale') { |
| this.getFormControl('cluster_id').disable(); |
| this.getFormControl('name').disable(); |
| this.getFormControl('description').disable(); |
| this.getFormControl('private_subnet').disable(); |
| this.getFormControl('public_subnet').disable(); |
| this.getFormControl('role').disable(); |
| this.getFormControl('node_count').disable(); |
| this.getFormControl('node_size').disable(); |
| this.getFormControl('edit_name').disable(); |
| this.clusterUrl = environment.K8SCREATECLUSTER_URL + '/' + this.cluster_name + '/nodegroup' + '/' + this.profileID + '/scale'; |
| this.scaling(); |
| } else if (this.profileType === 'edit-node') { |
| this.getFormControl('cluster_id').disable(); |
| this.getFormControl('nodeCount').disable(); |
| this.getFormControl('private_subnet').disable(); |
| this.getFormControl('public_subnet').disable(); |
| this.getFormControl('role').disable(); |
| this.getFormControl('node_count').disable(); |
| this.getFormControl('node_size').disable(); |
| this.getFormControl('name').disable(); |
| this.clusterUrl = environment.K8SCREATECLUSTER_URL + '/' + this.cluster_name + '/nodegroup' + '/' + this.profileID; |
| this.update(); |
| } |
| } |
| |
| /** Create Node @public */ |
| public createNode(): void { |
| this.submitted = true; |
| this.sharedService.cleanForm(this.nodeForm); |
| if (this.nodeForm.invalid) { |
| return; |
| } |
| const modalData: MODALCLOSERESPONSEDATA = { |
| message: 'Done' |
| }; |
| const apiURLHeader: APIURLHEADER = { |
| url: this.clusterUrl, |
| httpOptions: { headers: this.headers } |
| }; |
| this.isLoadingResults = true; |
| const formData = this.nodeForm.value; |
| const payload = Object.keys(formData).reduce((acc, key) => { |
| const value = formData[key]; |
| if (key === 'node_count' && value !== null && value !== undefined && value !== '') { |
| acc[key] = Number(value); |
| } else if (value !== null && value !== undefined && value !== '' && value.length !== 0) { |
| acc[key] = value; |
| } |
| return acc; |
| }, {}); |
| this.restService.postResource(apiURLHeader, payload).subscribe((result: {}) => { |
| this.activeModal.close(modalData); |
| this.isLoadingResults = false; |
| this.notifierService.notify('success', this.nodeForm.value.name + |
| this.translateService.instant('PAGE.K8S.NODECREATEDSUCCESSFULLY')); |
| }, (error: ERRORDATA) => { |
| this.restService.handleError(error, 'post'); |
| this.isLoadingResults = false; |
| }); |
| } |
| /** Node Scaling @public */ |
| public scaling(): void { |
| this.submitted = true; |
| this.sharedService.cleanForm(this.nodeForm); |
| if (this.nodeForm.invalid) { |
| return; |
| } |
| const modalData: MODALCLOSERESPONSEDATA = { |
| message: 'Done' |
| }; |
| const apiURLHeader: APIURLHEADER = { |
| url: this.clusterUrl, |
| httpOptions: { headers: this.headers } |
| }; |
| this.isLoadingResults = true; |
| if (this.profileType === 'k8s-scale') { |
| this.scalingPayload = { |
| node_count: Number(this.nodeForm.value.nodeCount) |
| }; |
| } |
| this.restService.postResource(apiURLHeader, this.scalingPayload).subscribe((result: {}) => { |
| this.activeModal.close(modalData); |
| this.isLoadingResults = false; |
| this.notifierService.notify('success', |
| this.translateService.instant('PAGE.K8S.NODEUPDATEDSUCCESSFULLY')); |
| }, (error: ERRORDATA) => { |
| this.restService.handleError(error, 'post'); |
| this.isLoadingResults = false; |
| }); |
| } |
| /** Node update @public */ |
| public update(): void { |
| this.submitted = true; |
| this.sharedService.cleanForm(this.nodeForm); |
| if (this.nodeForm.invalid) { |
| return; |
| } |
| const modalData: MODALCLOSERESPONSEDATA = { |
| message: 'Done' |
| }; |
| const apiURLHeader: APIURLHEADER = { |
| url: this.clusterUrl, |
| httpOptions: { headers: this.headers } |
| }; |
| this.isLoadingResults = true; |
| if (this.profileType === 'edit-node') { |
| if (this.nodeForm.value.description === '') { |
| this.scalingPayload = { |
| name: this.nodeForm.value.edit_name |
| }; |
| delete this.nodeForm.value.description; |
| } else if (this.nodeForm.value.name === '') { |
| this.scalingPayload = { |
| description: this.nodeForm.value.description |
| }; |
| delete this.nodeForm.value.name; |
| } else { |
| this.scalingPayload = { |
| name: this.nodeForm.value.edit_name, |
| description: this.nodeForm.value.description |
| }; |
| } |
| } |
| this.restService.patchResource(apiURLHeader, this.scalingPayload).subscribe((result: {}) => { |
| this.activeModal.close(modalData); |
| this.isLoadingResults = false; |
| this.notifierService.notify('success', |
| this.translateService.instant('PAGE.K8S.NODEUPDATEDSUCCESSFULLY')); |
| }, (error: ERRORDATA) => { |
| this.restService.handleError(error, 'post'); |
| this.isLoadingResults = false; |
| }); |
| } |
| /** Used to get the AbstractControl of controlName passed @private */ |
| private getFormControl(controlName: string): AbstractControl { |
| // eslint-disable-next-line security/detect-object-injection |
| return this.nodeForm.controls[controlName]; |
| } |
| } |