Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 1 | /** |
| 2 | * API utilities service |
| 3 | * |
| 4 | * @module app/common/services/api-utils |
| 5 | * @exports APIUtils |
| 6 | * @name APIUtils |
| 7 | * @version 0.0.1 |
| 8 | */ |
| 9 | |
| 10 | window.angular && (function (angular) { |
| 11 | 'use strict'; |
| 12 | angular |
| 13 | .module('app.common.services') |
| 14 | .factory('APIUtils', ['$http', 'Constants', function($http, Constants){ |
| 15 | var SERVICE = { |
| 16 | LOGIN_CREDENTIALS: Constants.LOGIN_CREDENTIALS, |
| 17 | API_CREDENTIALS: Constants.API_CREDENTIALS, |
| 18 | API_RESPONSE: Constants.API_RESPONSE, |
| 19 | CHASSIS_POWER_STATE: Constants.CHASSIS_POWER_STATE, |
| 20 | HOST_STATE_TEXT: Constants.HOST_STATE, |
| 21 | HOST_STATE: Constants.HOST_STATE, |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 22 | LED_STATE: Constants.LED_STATE, |
| 23 | LED_STATE_TEXT: Constants.LED_STATE_TEXT, |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 24 | getChassisState: function(callback){ |
| 25 | $http({ |
| 26 | method: 'GET', |
| 27 | url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/chassis0", |
| 28 | headers: { |
| 29 | 'Accept': 'application/json', |
| 30 | 'Content-Type': 'application/json' |
| 31 | }, |
| 32 | withCredentials: true |
| 33 | }).success(function(response){ |
| 34 | var json = JSON.stringify(response); |
| 35 | var content = JSON.parse(json); |
| 36 | callback(content.data.CurrentPowerState); |
| 37 | }).error(function(error){ |
| 38 | console.log(error); |
| 39 | }); |
| 40 | }, |
| 41 | getHostState: function(callback){ |
| 42 | $http({ |
| 43 | method: 'GET', |
| 44 | url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0", |
| 45 | headers: { |
| 46 | 'Accept': 'application/json', |
| 47 | 'Content-Type': 'application/json' |
| 48 | }, |
| 49 | withCredentials: true |
| 50 | }).success(function(response){ |
| 51 | var json = JSON.stringify(response); |
| 52 | var content = JSON.parse(json); |
| 53 | callback(content.data.CurrentHostState); |
| 54 | }).error(function(error){ |
| 55 | console.log(error); |
| 56 | }); |
| 57 | }, |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 58 | getLEDState: function(callback){ |
| 59 | $http({ |
| 60 | method: 'GET', |
| 61 | url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/led/groups/enclosure_identify", |
| 62 | headers: { |
| 63 | 'Accept': 'application/json', |
| 64 | 'Content-Type': 'application/json' |
| 65 | }, |
| 66 | withCredentials: true |
| 67 | }).success(function(response){ |
| 68 | var json = JSON.stringify(response); |
| 69 | var content = JSON.parse(json); |
| 70 | callback(content.data.Asserted); |
| 71 | }).error(function(error){ |
| 72 | console.log(error); |
| 73 | }); |
| 74 | }, |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 75 | login: function(username, password, callback){ |
| 76 | $http({ |
| 77 | method: 'POST', |
| 78 | url: SERVICE.API_CREDENTIALS.host + "/login", |
| 79 | headers: { |
| 80 | 'Accept': 'application/json', |
| 81 | 'Content-Type': 'application/json' |
| 82 | }, |
| 83 | withCredentials: true, |
| 84 | data: JSON.stringify({"data": [username, password]}) |
| 85 | }).success(function(response){ |
| 86 | if(callback){ |
| 87 | callback(response); |
| 88 | } |
| 89 | }).error(function(error){ |
| 90 | if(callback){ |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 91 | if(error && error.status && error.status == 'error'){ |
| 92 | callback(error); |
| 93 | }else{ |
| 94 | callback(error, true); |
| 95 | } |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 96 | } |
| 97 | console.log(error); |
| 98 | }); |
| 99 | }, |
| 100 | logout: function(callback){ |
| 101 | $http({ |
| 102 | method: 'POST', |
| 103 | url: SERVICE.API_CREDENTIALS.host + "/logout", |
| 104 | headers: { |
| 105 | 'Accept': 'application/json', |
| 106 | 'Content-Type': 'application/json' |
| 107 | }, |
| 108 | withCredentials: true, |
| 109 | data: JSON.stringify({"data": []}) |
| 110 | }).success(function(response){ |
| 111 | if(callback){ |
| 112 | callback(response); |
| 113 | } |
| 114 | }).error(function(error){ |
| 115 | if(callback){ |
| 116 | callback(null, error); |
| 117 | } |
| 118 | console.log(error); |
| 119 | }); |
| 120 | }, |
| 121 | chassisPowerOn: function(callback){ |
| 122 | $http({ |
| 123 | method: 'POST', |
| 124 | url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0", |
| 125 | headers: { |
| 126 | 'Accept': 'application/json', |
| 127 | 'Content-Type': 'application/json' |
| 128 | }, |
| 129 | withCredentials: true, |
| 130 | data: JSON.stringify({"data": []}) |
| 131 | }).success(function(response){ |
| 132 | var json = JSON.stringify(response); |
| 133 | var content = JSON.parse(json); |
| 134 | if(callback){ |
| 135 | return callback(content.data.CurrentPowerState); |
| 136 | } |
| 137 | }).error(function(error){ |
| 138 | if(callback){ |
| 139 | callback(error); |
| 140 | }else{ |
| 141 | console.log(error); |
| 142 | } |
| 143 | }); |
| 144 | }, |
| 145 | chassisPowerOff: function(callback){ |
| 146 | $http({ |
| 147 | method: 'POST', |
| 148 | url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0", |
| 149 | headers: { |
| 150 | 'Accept': 'application/json', |
| 151 | 'Content-Type': 'application/json' |
| 152 | }, |
| 153 | withCredentials: true, |
| 154 | data: JSON.stringify({"data": []}) |
| 155 | }).success(function(response){ |
| 156 | var json = JSON.stringify(response); |
| 157 | var content = JSON.parse(json); |
| 158 | if(callback){ |
| 159 | return callback(content.data.CurrentPowerState); |
| 160 | } |
| 161 | }).error(function(error){ |
| 162 | if(callback){ |
| 163 | callback(error); |
| 164 | }else{ |
| 165 | console.log(error); |
| 166 | } |
| 167 | }); |
| 168 | }, |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 169 | setLEDState: function(state, callback){ |
| 170 | $http({ |
| 171 | method: 'PUT', |
| 172 | url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/led/groups/enclosure_identify/attr/Asserted", |
| 173 | headers: { |
| 174 | 'Accept': 'application/json', |
| 175 | 'Content-Type': 'application/json' |
| 176 | }, |
| 177 | withCredentials: true, |
| 178 | data: JSON.stringify({"data": state}) |
| 179 | }).success(function(response){ |
| 180 | var json = JSON.stringify(response); |
| 181 | var content = JSON.parse(json); |
| 182 | if(callback){ |
| 183 | return callback(content.status); |
| 184 | } |
| 185 | }).error(function(error){ |
| 186 | if(callback){ |
| 187 | callback(error); |
| 188 | }else{ |
| 189 | console.log(error); |
| 190 | } |
| 191 | }); |
| 192 | }, |
Iftekharul Islam | 5536812 | 2017-03-27 09:46:50 -0500 | [diff] [blame] | 193 | bmcReboot: function(callback){ |
| 194 | $http({ |
| 195 | method: 'PUT', |
| 196 | url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/bmc0/attr/RequestedBmcTransition", |
| 197 | headers: { |
| 198 | 'Accept': 'application/json', |
| 199 | 'Content-Type': 'application/json' |
| 200 | }, |
| 201 | withCredentials: true, |
| 202 | data: JSON.stringify({"data": "xyz.openbmc_project.State.BMC.Transition.Reboot"}) |
| 203 | }).success(function(response){ |
| 204 | var json = JSON.stringify(response); |
| 205 | var content = JSON.parse(json); |
| 206 | if(callback){ |
| 207 | return callback(content.status); |
| 208 | } |
| 209 | }).error(function(error){ |
| 210 | if(callback){ |
| 211 | callback(error); |
| 212 | }else{ |
| 213 | console.log(error); |
| 214 | } |
| 215 | }); |
| 216 | }, |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 217 | hostPowerOn: function(callback){ |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 218 | $http({ |
| 219 | method: 'PUT', |
| 220 | url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0/attr/RequestedHostTransition", |
| 221 | headers: { |
| 222 | 'Accept': 'application/json', |
| 223 | 'Content-Type': 'application/json' |
| 224 | }, |
| 225 | withCredentials: true, |
| 226 | data: JSON.stringify({"data": "xyz.openbmc_project.State.Host.Transition.On"}) |
| 227 | }).success(function(response){ |
| 228 | var json = JSON.stringify(response); |
| 229 | var content = JSON.parse(json); |
| 230 | if(callback){ |
| 231 | return callback(content.status); |
| 232 | } |
| 233 | }).error(function(error){ |
| 234 | if(callback){ |
| 235 | callback(error); |
| 236 | }else{ |
| 237 | console.log(error); |
| 238 | } |
| 239 | }); |
| 240 | }, |
| 241 | hostPowerOff: function(callback){ |
| 242 | $http({ |
| 243 | method: 'PUT', |
| 244 | url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0/attr/RequestedHostTransition", |
| 245 | headers: { |
| 246 | 'Accept': 'application/json', |
| 247 | 'Content-Type': 'application/json' |
| 248 | }, |
| 249 | withCredentials: true, |
| 250 | data: JSON.stringify({"data": "xyz.openbmc_project.State.Host.Transition.Off"}) |
| 251 | }).success(function(response){ |
| 252 | var json = JSON.stringify(response); |
| 253 | var content = JSON.parse(json); |
| 254 | if(callback){ |
| 255 | return callback(content.status); |
| 256 | } |
| 257 | }).error(function(error){ |
| 258 | if(callback){ |
| 259 | callback(error); |
| 260 | }else{ |
| 261 | console.log(error); |
| 262 | } |
| 263 | }); |
| 264 | }, |
| 265 | hostReboot: function(callback){ |
| 266 | $http({ |
| 267 | method: 'POST', |
| 268 | url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0", |
| 269 | headers: { |
| 270 | 'Accept': 'application/json', |
| 271 | 'Content-Type': 'application/json' |
| 272 | }, |
| 273 | withCredentials: true, |
| 274 | data: JSON.stringify({"data": []}), |
| 275 | }).success(function(response){ |
| 276 | var json = JSON.stringify(response); |
| 277 | var content = JSON.parse(json); |
| 278 | if(callback){ |
| 279 | return callback(content); |
| 280 | } |
| 281 | }).error(function(error){ |
| 282 | if(callback){ |
| 283 | callback(error); |
| 284 | }else{ |
| 285 | console.log(error); |
| 286 | } |
| 287 | }); |
| 288 | }, |
| 289 | hostShutdown: function(callback){ |
| 290 | $http({ |
| 291 | method: 'POST', |
| 292 | url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0", |
| 293 | headers: { |
| 294 | 'Accept': 'application/json', |
| 295 | 'Content-Type': 'application/json' |
| 296 | }, |
| 297 | withCredentials: true, |
| 298 | data: JSON.stringify({"data": []}) |
| 299 | }).success(function(response){ |
| 300 | var json = JSON.stringify(response); |
| 301 | var content = JSON.parse(json); |
| 302 | if(callback){ |
| 303 | return callback(content); |
| 304 | } |
| 305 | }).error(function(error){ |
| 306 | if(callback){ |
| 307 | callback(error); |
| 308 | }else{ |
| 309 | console.log(error); |
| 310 | } |
| 311 | }); |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 312 | }, |
| 313 | getLogs: function(callback){ |
| 314 | $http({ |
| 315 | method: 'GET', |
| 316 | url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/logging/enumerate", |
| 317 | headers: { |
| 318 | 'Accept': 'application/json', |
| 319 | 'Content-Type': 'application/json' |
| 320 | }, |
| 321 | withCredentials: true |
| 322 | }).success(function(response){ |
| 323 | var json = JSON.stringify(response); |
| 324 | var content = JSON.parse(json); |
| 325 | var dataClone = JSON.parse(JSON.stringify(content.data)); |
| 326 | var data = []; |
| 327 | var severityCode = ''; |
| 328 | var priority = ''; |
| 329 | var resolved = false; |
| 330 | var relatedItems = []; |
| 331 | |
| 332 | for(var key in content.data){ |
| 333 | if(content.data.hasOwnProperty(key) && content.data[key].hasOwnProperty('Id')){ |
| 334 | var severityFlags = {low: false, medium: false, high: false}; |
| 335 | severityCode = content.data[key].Severity.split(".").pop(); |
| 336 | priority = Constants.SEVERITY_TO_PRIORITY_MAP[severityCode]; |
| 337 | severityFlags[priority.toLowerCase()] = true; |
| 338 | relatedItems = []; |
| 339 | content.data[key].associations.forEach(function(item){ |
Iftekharul Islam | 532763f | 2017-04-25 09:44:40 -0500 | [diff] [blame^] | 340 | relatedItems.push(item[2]); |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 341 | }); |
| 342 | |
| 343 | data.push(Object.assign({ |
| 344 | path: key, |
| 345 | copied: false, |
| 346 | priority: priority, |
| 347 | severity_code: severityCode, |
| 348 | severity_flags: severityFlags, |
| 349 | additional_data: content.data[key].AdditionalData.join("\n"), |
| 350 | selected: false, |
Iftekharul Islam | 8b4828a | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 351 | search_text: ("#" + content.data[key].Id + " " + severityCode + " " + content.data[key].Severity + " " + content.data[key].AdditionalData.join(" ")).toLowerCase(), |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 352 | meta: false, |
| 353 | confirm: false, |
| 354 | related_items: relatedItems, |
| 355 | data: {key: key, value: content.data[key]} |
| 356 | }, content.data[key])); |
| 357 | } |
| 358 | } |
| 359 | callback(data, dataClone); |
| 360 | }).error(function(error){ |
| 361 | console.log(error); |
| 362 | }); |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 363 | } |
| 364 | }; |
| 365 | return SERVICE; |
| 366 | }]); |
| 367 | |
| 368 | })(window.angular); |