| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 1 | /* |
| 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: SANDHYA JS (sandhya.j@tataelxsi.co.in) |
| 17 | */ |
| 18 | /** |
| 19 | * @file Attach Profile Component. |
| 20 | */ |
| 21 | import { HttpHeaders } from '@angular/common/http'; |
| 22 | import { ChangeDetectorRef, Component, Injector, Input, OnInit } from '@angular/core'; |
| 23 | import { AbstractControl, FormArray, 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 { K8SCreateCLUSTERDATA, K8SPayload, ProfileMap, ProfileMappings } from 'K8sModel'; |
| 30 | import { RestService } from 'RestService'; |
| 31 | /** |
| 32 | * Creating Component |
| 33 | * @Component takes K8sAttachProfileComponent.html as template url |
| 34 | */ |
| 35 | @Component({ |
| 36 | selector: 'app-k8s-attach-profile', |
| 37 | templateUrl: './K8sAttachProfileComponent.html', |
| 38 | styleUrls: ['./K8sAttachProfileComponent.scss'] |
| 39 | }) |
| 40 | /** Exporting a class @exports K8sAttachProfileComponent */ |
| 41 | export class K8sAttachProfileComponent implements OnInit { |
| 42 | /** To inject services @public */ |
| 43 | public injector: Injector; |
| 44 | |
| 45 | /** Instance for active modal service @public */ |
| 46 | public activeModal: NgbActiveModal; |
| 47 | |
| 48 | /** FormGroup user Edit Account added to the form @ html @public */ |
| 49 | public attachForm: FormGroup; |
| 50 | |
| 51 | /** Form submission Add */ |
| 52 | public submitted: boolean = false; |
| 53 | |
| 54 | /** Form submission Add */ |
| 55 | public selectedData: string; |
| 56 | |
| 57 | /** Input contains Modal dialog component Instance @private */ |
| 58 | @Input() public userTitle: string; |
| 59 | |
| 60 | /** Input contains Modal dialog component Instance @private */ |
| 61 | @Input() public userID: string; |
| 62 | |
| 63 | /** Contains user details information @public */ |
| 64 | public userDetails: K8SCreateCLUSTERDATA[]; |
| 65 | |
| 66 | /** Contains user details information @public */ |
| 67 | public filterDetails: K8SCreateCLUSTERDATA[]; |
| 68 | |
| 69 | /** Contains user details information @public */ |
| 70 | public checkDetails: K8SCreateCLUSTERDATA[]; |
| 71 | |
| 72 | /** Contains user details information @public */ |
| 73 | public profileDetails: K8SCreateCLUSTERDATA; |
| 74 | |
| 75 | /** Contains user details information @public */ |
| 76 | public count: number; |
| 77 | |
| 78 | /** Profile Mapping @public */ |
| 79 | public profileMap: ProfileMap = {}; |
| 80 | |
| 81 | /** Check the loading results @public */ |
| 82 | public isLoadingResults: boolean = false; |
| 83 | |
| 84 | /** Give the message for the loading @public */ |
| 85 | public message: string = 'PLEASEWAIT'; |
| 86 | |
| 87 | /** Input contains Modal dialog component Instance @public */ |
| 88 | @Input() public profileType: string; |
| 89 | |
| 90 | /** Input contains Modal dialog component Instance @public */ |
| 91 | @Input() public profileID: string; |
| 92 | |
| 93 | /** Give the message for the loading @public */ |
| 94 | public profileUrl: string; |
| 95 | |
| 96 | /** Give the message for the loading @public */ |
| 97 | public isAttach: boolean = false; |
| 98 | |
| 99 | /** Instance of the rest service @private */ |
| 100 | private restService: RestService; |
| 101 | |
| 102 | /** FormBuilder instance added to the formBuilder @private */ |
| 103 | private formBuilder: FormBuilder; |
| 104 | |
| 105 | /** Controls the header form @private */ |
| 106 | private headers: HttpHeaders; |
| 107 | |
| 108 | /** Notifier service to popup notification @private */ |
| 109 | private notifierService: NotifierService; |
| 110 | |
| 111 | /** Contains tranlsate instance @private */ |
| 112 | private translateService: TranslateService; |
| 113 | |
| 114 | /** Profile Form array @private */ |
| 115 | private attachFormArray: FormArray; |
| 116 | |
| 117 | /** Detect changes for the User Input */ |
| 118 | private cd: ChangeDetectorRef; |
| 119 | |
| 120 | constructor(injector: Injector) { |
| 121 | this.injector = injector; |
| 122 | this.formBuilder = this.injector.get(FormBuilder); |
| 123 | this.restService = this.injector.get(RestService); |
| 124 | this.activeModal = this.injector.get(NgbActiveModal); |
| 125 | this.notifierService = this.injector.get(NotifierService); |
| 126 | this.translateService = this.injector.get(TranslateService); |
| 127 | this.cd = this.injector.get(ChangeDetectorRef); |
| 128 | } |
| 129 | |
| 130 | /** Generate primitive params @public */ |
| 131 | get profileParamsBuilder(): FormGroup { |
| 132 | return this.formBuilder.group({ |
| 133 | profile_name: [null, [Validators.required]] |
| 134 | }); |
| 135 | } |
| 136 | |
| 137 | /** convenience getter for easy access to form fields */ |
| 138 | get f(): FormGroup['controls'] { return this.attachForm.controls; } |
| 139 | |
| 140 | /** Lifecyle Hooks the trigger before component is instantiate @public */ |
| 141 | public ngOnInit(): void { |
| 142 | this.headers = new HttpHeaders({ |
| 143 | 'Content-Type': 'application/json', |
| 144 | Accept: 'application/json', |
| 145 | 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0' |
| 146 | }); |
| 147 | this.initializeForm(); |
| 148 | this.generateData(); |
| 149 | this.generateProfile(); |
| 150 | } |
| 151 | |
| 152 | /** Initializing Form Action @public */ |
| 153 | public initializeForm(): void { |
| 154 | this.attachForm = this.formBuilder.group({ |
| 155 | infra_config_profiles: this.formBuilder.array([]) |
| 156 | }); |
| 157 | } |
| 158 | |
| 159 | /** Handle FormArray Controls @public */ |
| 160 | public getControls(): AbstractControl[] { |
| 161 | return (this.attachForm.get('infra_config_profiles') as FormArray).controls; |
| 162 | } |
| 163 | |
| 164 | /** Fetching the data from server to Load in the smarttable @public */ |
| 165 | public generateData(): void { |
| 166 | if (this.profileID !== '') { |
| 167 | this.isLoadingResults = true; |
| 168 | if (this.profileType === 'infra-config') { |
| 169 | this.profileUrl = environment.K8SCREATECLUSTER_URL + '/' + this.profileID + '/' + 'infra_config_profiles'; |
| 170 | } else if (this.profileType === 'infra-controller') { |
| 171 | this.profileUrl = environment.K8SCREATECLUSTER_URL + '/' + this.profileID + '/' + 'infra_controller_profiles'; |
| 172 | } else if (this.profileType === 'app-profile') { |
| 173 | this.profileUrl = environment.K8SCREATECLUSTER_URL + '/' + this.profileID + '/' + 'app_profiles'; |
| 174 | } else if (this.profileType === 'resource-profile') { |
| 175 | this.profileUrl = environment.K8SCREATECLUSTER_URL + '/' + this.profileID + '/' + 'resource_profiles'; |
| 176 | } |
| 177 | this.restService.getResource(this.profileUrl).subscribe((userDetail: K8SCreateCLUSTERDATA[]): void => { |
| 178 | this.checkDetails = userDetail; |
| 179 | this.filterDetails = this.checkDetails.filter((itemData: K8SCreateCLUSTERDATA): boolean => itemData.default === false); |
| 180 | if (this.filterDetails.length !== 0) { |
| 181 | this.filterDetails.forEach((datas: K8SCreateCLUSTERDATA): void => { |
| 182 | let profileData: ProfileMappings[] = []; |
| 183 | profileData = this.filterDetails.map((item) => ({ |
| 184 | name: item.name, |
| 185 | _id: item._id |
| 186 | })); |
| 187 | this.profileDetails = { infra_config_profiles: profileData }; |
| 188 | }); |
| 189 | this.count = this.profileDetails.infra_config_profiles.length; |
| 190 | this.loadMapping(); |
| 191 | } else { |
| 192 | this.profileDetails = { infra_config_profiles: [] }; |
| 193 | } |
| 194 | this.isLoadingResults = false; |
| 195 | }, (error: ERRORDATA): void => { |
| 196 | this.isLoadingResults = false; |
| 197 | this.restService.handleError(error, 'get'); |
| 198 | }); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /** Fetching the data from server to Load in the smarttable @public */ |
| 203 | public generateProfile(): void { |
| 204 | this.isLoadingResults = true; |
| 205 | if (this.profileType === 'infra-config') { |
| 206 | this.profileUrl = environment.K8SINFRACONFIGPROFILE_URL; |
| 207 | } else if (this.profileType === 'infra-controller') { |
| 208 | this.profileUrl = environment.K8SINFRACONTROLLERPROFILE_URL; |
| 209 | } else if (this.profileType === 'app-profile') { |
| 210 | this.profileUrl = environment.K8SAPPPROFILE_URL; |
| 211 | } else if (this.profileType === 'resource-profile') { |
| 212 | this.profileUrl = environment.K8SRESOURCEPROFILE_URL; |
| 213 | } |
| 214 | this.restService.getResource(this.profileUrl).subscribe((userDetail: K8SCreateCLUSTERDATA[]): void => { |
| 215 | this.userDetails = userDetail.filter((itemData: K8SCreateCLUSTERDATA): boolean => itemData.default === false); |
| 216 | this.isLoadingResults = false; |
| 217 | }, (error: ERRORDATA): void => { |
| 218 | this.isLoadingResults = false; |
| 219 | this.restService.handleError(error, 'get'); |
| 220 | }); |
| 221 | } |
| 222 | |
| 223 | /** Set all profile values to the form @public */ |
| 224 | public loadMapping(): void { |
| 225 | this.profileDetails.infra_config_profiles.forEach((datas: ProfileMappings): void => { |
| 226 | this.attachFormArray = this.attachForm.get('infra_config_profiles') as FormArray; |
| 227 | const control = this.formBuilder.group({ |
| 228 | profile_name: [datas._id] |
| 229 | }); |
| 230 | this.attachFormArray.push(control); |
| 231 | }); |
| 232 | } |
| 233 | |
| 234 | /** Remove profile from the list @public */ |
| 235 | public removeMapping(index: number): void { |
| 236 | this.attachFormArray.removeAt(index); |
| 237 | this.isAttach = false; |
| 238 | } |
| 239 | |
| 240 | /** Submit profile @public */ |
| 241 | public attachProfile(): void { |
| 242 | this.submitted = true; |
| 243 | const modalData: MODALCLOSERESPONSEDATA = { |
| 244 | message: 'Done' |
| 245 | }; |
| 246 | if (this.attachForm.invalid) { return; } |
| 247 | if (this.profileType === 'infra-config') { |
| 248 | this.profileUrl = environment.K8SCREATECLUSTER_URL + '/' + this.profileID + '/' + 'infra_config_profiles'; |
| 249 | } else if (this.profileType === 'infra-controller') { |
| 250 | this.profileUrl = environment.K8SCREATECLUSTER_URL + '/' + this.profileID + '/' + 'infra_controller_profiles'; |
| 251 | } else if (this.profileType === 'app-profile') { |
| 252 | this.profileUrl = environment.K8SCREATECLUSTER_URL + '/' + this.profileID + '/' + 'app_profiles'; |
| 253 | } else if (this.profileType === 'resource-profile') { |
| 254 | this.profileUrl = environment.K8SCREATECLUSTER_URL + '/' + this.profileID + '/' + 'resource_profiles'; |
| 255 | } |
| 256 | const apiURLHeader: APIURLHEADER = { |
| 257 | url: this.profileUrl |
| 258 | }; |
| 259 | this.profileMap.add_profile = []; |
| 260 | this.attachForm.value.infra_config_profiles.forEach((res: ProfileMappings): void => { |
| 261 | this.profileMap.add_profile.push({ id: res.profile_name }); |
| 262 | }); |
| 263 | this.profileMap.add_profile.splice(0, this.count); |
| 264 | if (this.profileMap.add_profile.length !== 0) { |
| 265 | if (!this.attachForm.dirty) { |
| 266 | this.notifierService.notify('warning', this.translateService.instant('PAGE.TOPOLOGY.DATAEMPTY')); |
| 267 | return; |
| 268 | } |
| 269 | this.isLoadingResults = true; |
| 270 | this.restService.patchResource(apiURLHeader, this.profileMap).subscribe((result: {}): void => { |
| 271 | this.isLoadingResults = false; |
| 272 | this.activeModal.close(modalData); |
| 273 | this.notifierService.notify('success', this.translateService.instant('PAGE.K8S.ATTACHEDSUCCESSFULLY')); |
| 274 | }, (error: ERRORDATA): void => { |
| 275 | this.isLoadingResults = false; |
| 276 | this.restService.handleError(error, 'patch'); |
| 277 | }); |
| 278 | } else { |
| 279 | this.notifierService.notify('error', this.translateService.instant('PAGE.K8S.WARNING')); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | /** Add extra mapping and set empty profile @public */ |
| 284 | public addMapping(): void { |
| 285 | this.attachFormArray = this.attachForm.get('infra_config_profiles') as FormArray; |
| 286 | this.attachFormArray.push(this.profileParamsBuilder); |
| 287 | if ((this.attachFormArray.value.length === this.count + 1) || (this.attachFormArray.value.length === 1)) { |
| 288 | this.isAttach = true; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | /** Remove profile @public */ |
| 293 | public deleteProfile(getProfile: ProfileMappings): void { |
| 294 | const modalData: MODALCLOSERESPONSEDATA = { |
| 295 | message: 'Done' |
| 296 | }; |
| 297 | const removeProfile: ProfileMap = { remove_profile: [] }; |
| 298 | removeProfile.remove_profile = [{ id: getProfile._id }]; |
| 299 | if (this.profileType === 'infra-config') { |
| 300 | this.profileUrl = environment.K8SCREATECLUSTER_URL + '/' + this.profileID + '/' + 'infra_config_profiles'; |
| 301 | } else if (this.profileType === 'infra-controller') { |
| 302 | this.profileUrl = environment.K8SCREATECLUSTER_URL + '/' + this.profileID + '/' + 'infra_controller_profiles'; |
| 303 | } else if (this.profileType === 'app-profile') { |
| 304 | this.profileUrl = environment.K8SCREATECLUSTER_URL + '/' + this.profileID + '/' + 'app_profiles'; |
| 305 | } else if (this.profileType === 'resource-profile') { |
| 306 | this.profileUrl = environment.K8SCREATECLUSTER_URL + '/' + this.profileID + '/' + 'resource_profiles'; |
| 307 | } |
| 308 | const apiURLHeader: APIURLHEADER = { |
| 309 | url: this.profileUrl |
| 310 | }; |
| 311 | this.isLoadingResults = true; |
| 312 | this.restService.patchResource(apiURLHeader, removeProfile).subscribe((result: {}): void => { |
| 313 | this.isLoadingResults = false; |
| 314 | this.activeModal.close(modalData); |
| 315 | this.notifierService.notify('success', this.translateService.instant('PAGE.K8S.DETATCHEDSUCCESSFULLY')); |
| 316 | }, (error: ERRORDATA): void => { |
| 317 | this.isLoadingResults = false; |
| 318 | this.restService.handleError(error, 'patch'); |
| 319 | }); |
| 320 | } |
| 321 | } |