[UI] Support data registration
[validation.git] / ui / src / main / webapp / app / BluvalUI / UnRegisterBlueprintInstance / UnRegisterBlueprintInstanceController.js
1 /*
2  * Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
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
17 var app = angular.module('UnRegisterBlueprintInstance');
18 app
19         .controller(
20                 'UnRegisterBlueprintInstanceController',
21                 function($scope, restAPISvc) {
22
23                     initialize();
24
25                     function initialize() {
26                         $scope.loadingBlueprintInstances = true;
27                         $scope.blueprintInstanceInfos = [];
28                         $scope.declerativeInsts = [];
29                         $scope.selectedBlueprintInstance = '';
30                         restAPISvc
31                                 .getRestAPI(
32                                         "/api/v1/blueprintinstance/",
33                                         function(data) {
34                                             if (data) {
35                                                 $scope.blueprintInstanceInfos = data;
36                                                 angular
37                                                         .forEach(
38                                                                 data,
39                                                                 function(
40                                                                         blueprintInstance) {
41                                                                     var temp = "id: "
42                                                                             + blueprintInstance.blueprintInstanceId
43                                                                             + " name: "
44                                                                             + blueprintInstance["blueprint"]["blueprintName"]
45                                                                             + " version: "
46                                                                             + blueprintInstance["version"];
47                                                                     $scope.declerativeInsts
48                                                                             .push(temp);
49                                                                 });
50                                             } else {
51                                                 confirm("No blueprint instances found");
52                                             }
53                                             $scope.loadingBlueprintInstances = false;
54                                         });
55                     }
56
57                     $scope.unRegister = function() {
58                         if (!$scope.selectedBlueprintInstance) {
59                             confirm("You must select a blueprint instance");
60                             return;
61                         }
62                         var finalBlueprintInstanceInfo = '';
63                         var id = $scope.selectedBlueprintInstance
64                                 .substring($scope.selectedBlueprintInstance
65                                         .indexOf("id:") + 4,
66                                         $scope.selectedBlueprintInstance
67                                                 .indexOf("name") - 1);
68                         angular
69                                 .forEach(
70                                         $scope.blueprintInstanceInfos,
71                                         function(blueprintInstanceInfo) {
72                                             if (blueprintInstanceInfo.blueprintInstanceId
73                                                     .toString().trim() === id
74                                                     .toString().trim()) {
75                                                 finalBlueprintInstanceInfo = blueprintInstanceInfo;
76                                             }
77                                         });
78                         if (!finalBlueprintInstanceInfo) {
79                             confirm("Error in blueprint instance data");
80                             return;
81                         }
82                         restAPISvc
83                                 .deleteRestAPI(
84                                         "/api/v1/blueprintinstance/",
85                                         finalBlueprintInstanceInfo,
86                                         function(data) {
87                                             if (data) {
88                                                 var confirmText = "The blueprint instance has been unregistered successfully."
89                                                 confirm(confirmText);
90                                             } else {
91                                                 confirm("Error when unregistering the blueprint instance");
92                                             }
93                                             initialize();
94                                         });
95                     }
96                 });