blob: cafc7075d01f7a0819cc7756c860d0d0ad2ce478 [file] [log] [blame]
Michael Davis43366db2017-05-15 18:12:35 -05001/**
2 * Controller for firmware
3 *
4 * @module app/configuration
5 * @exports firmwareController
6 * @name firmwareController
7 * @version 0.1.0
8 */
9
10window.angular && (function (angular) {
11 'use strict';
12
13 angular
14 .module('app.configuration')
15 .controller('firmwareController', [
16 '$scope',
17 '$window',
18 'APIUtils',
19 'dataService',
20 '$location',
21 '$anchorScroll',
Iftekharul Islam1acb4122017-11-02 13:20:32 -050022 'Constants',
23 function ($scope, $window, APIUtils, dataService, $location, $anchorScroll, Constants) {
Michael Davis43366db2017-05-15 18:12:35 -050024 $scope.dataService = dataService;
25
Michael Davis43366db2017-05-15 18:12:35 -050026 //Scroll to target anchor
27 $scope.gotoAnchor = function () {
28 $location.hash('upload');
29 $anchorScroll();
30 };
Iftekharul Islamc0161392017-06-14 15:46:15 -050031
32 $scope.firmwares = [];
33 $scope.bmcActiveVersion = "";
34 $scope.hostActiveVersion = "";
35 $scope.display_error = false;
36 $scope.confirm_upload_image = false;
37 $scope.reboot_confirm = false;
38 $scope.preserve_settings_confirm = false;
39 $scope.delete_image_id = "";
40 $scope.activate_image_id = "";
Iftekharul Islam1acb4122017-11-02 13:20:32 -050041 $scope.priority_image_id = "";
42 $scope.priority_from = -1;
43 $scope.priority_to = -1;
44 $scope.confirm_priority = false;
Iftekharul Islamc0161392017-06-14 15:46:15 -050045 $scope.file_empty = true;
46 $scope.uploading = false;
47
48 $scope.error = {
49 modal_title: "",
50 title: "",
51 desc: "",
52 type: "warning"
53 };
54
55 $scope.activateImage = function(imageId){
56 $scope.activate_image_id = imageId;
57 $scope.preserve_settings_confirm = true;
58 }
59
60 $scope.displayError = function(data){
61 $scope.error = data;
62 $scope.display_error = true;
63 }
64
65 $scope.preserveSettingsConfirmed = function(){
Iftekharul Islam2a489552017-11-02 13:23:08 -050066 $scope.uploading = true;
67 APIUtils.activateImage($scope.activate_image_id).then(function(response){
68 $scope.uploading = false;
69 if(response.status == 'error'){
70 $scope.displayError({
71 modal_title: response.data.description,
72 title: response.data.description,
73 desc: response.data.exception,
74 type: 'Error'
75 });
76 }else{
77 $scope.loadFirmwares();
78 }
79 });
Iftekharul Islamc0161392017-06-14 15:46:15 -050080 $scope.preserve_settings_confirm = false;
81 }
82
83 $scope.confirmWarmReboot = function(){
84 $scope.reboot_confirm = false;
85 }
86
87 $scope.upload = function(){
88 if(!$scope.file_empty){
89 $scope.confirm_upload_image = true;
90 }
91 }
92 $scope.confirmUpload = function(){
93 $scope.uploading = true;
Iftekharul Islam1acb4122017-11-02 13:20:32 -050094 APIUtils.uploadImage($scope.file).then(function(response){
Iftekharul Islamc0161392017-06-14 15:46:15 -050095 $scope.uploading = false;
96 if(response.status == 'error'){
97 $scope.displayError({
98 modal_title: response.data.description,
99 title: response.data.description,
100 desc: response.data.exception,
101 type: 'Error'
102 });
103 }else{
104 $scope.loadFirmwares();
105 }
106 });
107 $scope.confirm_upload_image = false;
108 }
109
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500110 $scope.download = function(){
111 $scope.downloading = true;
112 APIUtils.downloadImage($scope.download_host, $scope.download_filename).then(function(response){
113 var data = response.data;
114 $scope.downloading = false;
115 var headers = response.headers();
116
117 var filename = headers['x-filename'];
118 var contentType = headers['content-type'];
119
120 if(!headers['x-filename']){
Iftekharul Islam2a489552017-11-02 13:23:08 -0500121 filename = $scope.download_filename;
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500122 }
123
124 var linkElement = document.createElement('a');
125 try {
126 var blob = new Blob([data], { type: contentType });
127 var url = window.URL.createObjectURL(blob);
128
129 linkElement.setAttribute('href', url);
130 linkElement.setAttribute("download", filename);
131
132 var clickEvent = new MouseEvent("click", {
133 "view": window,
134 "bubbles": true,
135 "cancelable": false
136 });
137 linkElement.dispatchEvent(clickEvent);
138 } catch (ex) {
139 console.log(ex);
140 }
141 });
142 }
143
144 $scope.changePriority = function(imageId, from, to){
145 $scope.priority_image_id = imageId;
146 $scope.priority_from = from;
147 $scope.priority_to = to;
148
149 if((from + to) == 1){
150 $scope.confirm_priority = true;
151 }else{
152 $scope.confirmChangePriority();
153 }
154 }
155
156 $scope.confirmChangePriority = function(){
157 $scope.loading = true;
158 APIUtils.changePriority($scope.priority_image_id, $scope.priority_to).then(function(response){
159 $scope.loading = false;
160 if(response.status == 'error'){
161 $scope.displayError({
162 modal_title: response.data.description,
163 title: response.data.description,
164 desc: response.data.exception,
165 type: 'Error'
166 });
167 }else{
168 $scope.loadFirmwares();
169 }
170 });
171 $scope.confirm_priority = false;
172 }
Iftekharul Islamc0161392017-06-14 15:46:15 -0500173 $scope.deleteImage = function(imageId){
174 $scope.delete_image_id = imageId;
175 $scope.confirm_delete = true;
176 }
Iftekharul Islam2a489552017-11-02 13:23:08 -0500177 $scope.confirmDeleteImage = function(){
178 $scope.loading = true;
179 APIUtils.deleteImage($scope.delete_image_id).then(function(response){
180 $scope.loading = false;
181 if(response.status == 'error'){
182 $scope.displayError({
183 modal_title: response.data.description,
184 title: response.data.description,
185 desc: response.data.exception,
186 type: 'Error'
187 });
188 }else{
189 $scope.loadFirmwares();
190 }
191 });
Iftekharul Islamc0161392017-06-14 15:46:15 -0500192 $scope.confirm_delete = false;
193 }
Iftekharul Islamc0161392017-06-14 15:46:15 -0500194 $scope.fileNameChanged = function(){
195 $scope.file_empty = false;
196 }
197
198 $scope.uploading = false;
199 $scope.filters = {
200 bmc: {
201 imageType: 'BMC'
202 },
203 host: {
204 imageType: 'Host'
205 }
206 };
207
208 $scope.loadFirmwares = function(){
Michael Davisdf3bd122017-08-10 11:03:42 -0500209 APIUtils.getFirmwares().then(function(result){
210 $scope.firmwares = result.data;
211 $scope.bmcActiveVersion = result.bmcActiveVersion;
212 $scope.hostActiveVersion = result.hostActiveVersion;
Iftekharul Islamc0161392017-06-14 15:46:15 -0500213 });
214 }
215
216 $scope.loadFirmwares();
Michael Davis43366db2017-05-15 18:12:35 -0500217 }
218 ]
219 );
220
221})(angular);