Iftekharul Islam | f157d37 | 2017-03-08 11:11:27 -0600 | [diff] [blame] | 1 | /** |
| 2 | chassis |
| 3 | /org/openbmc/control/chassis0 —> PowerOn ..PowerOff |
| 4 | |
| 5 | host reboot |
| 6 | /org/openbmc/control/host0 —>reboot |
| 7 | |
| 8 | shutdown |
| 9 | /org/openbmc/control/host0 —> shutdown |
| 10 | **/ |
| 11 | angular |
| 12 | .module('app.services', []) |
Iftekharul Islam | 38f681f | 2017-03-21 13:22:59 -0500 | [diff] [blame^] | 13 | .service('apiInterceptor', ['$q', '$rootScope', 'dataService', function($q, $rootScope, dataService){ |
| 14 | return { |
| 15 | 'request': function(config){ |
| 16 | dataService.server_unreachable = false; |
| 17 | dataService.loading = true; |
| 18 | return config; |
| 19 | }, |
| 20 | 'response': function(response){ |
| 21 | dataService.loading = false; |
| 22 | dataService.last_updated = new Date(); |
Iftekharul Islam | f157d37 | 2017-03-08 11:11:27 -0600 | [diff] [blame] | 23 | |
Iftekharul Islam | 38f681f | 2017-03-21 13:22:59 -0500 | [diff] [blame^] | 24 | if(response == null){ |
| 25 | dataService.server_unreachable = true; |
| 26 | } |
Iftekharul Islam | ccfe245 | 2017-03-16 10:47:04 -0500 | [diff] [blame] | 27 | |
Iftekharul Islam | 38f681f | 2017-03-21 13:22:59 -0500 | [diff] [blame^] | 28 | if(response && response.status == 'error' && |
| 29 | dataService.path != '/login'){ |
| 30 | $rootScope.$emit('timedout-user', {}); |
| 31 | } |
Iftekharul Islam | ccfe245 | 2017-03-16 10:47:04 -0500 | [diff] [blame] | 32 | |
Iftekharul Islam | 38f681f | 2017-03-21 13:22:59 -0500 | [diff] [blame^] | 33 | return response; |
| 34 | }, |
| 35 | 'responseError': function(rejection){ |
| 36 | dataService.server_unreachable = true; |
| 37 | dataService.loading = false; |
| 38 | return $q.reject(rejection); |
| 39 | } |
| 40 | }; |
Iftekharul Islam | ccfe245 | 2017-03-16 10:47:04 -0500 | [diff] [blame] | 41 | }]) |
Iftekharul Islam | 38f681f | 2017-03-21 13:22:59 -0500 | [diff] [blame^] | 42 | .service('Constants', function(){ |
| 43 | return { |
Iftekharul Islam | f157d37 | 2017-03-08 11:11:27 -0600 | [diff] [blame] | 44 | LOGIN_CREDENTIALS: { |
Iftekharul Islam | 38f681f | 2017-03-21 13:22:59 -0500 | [diff] [blame^] | 45 | username: "test", |
| 46 | password: "testpass", |
Iftekharul Islam | f157d37 | 2017-03-08 11:11:27 -0600 | [diff] [blame] | 47 | }, |
| 48 | API_CREDENTIALS: { |
Iftekharul Islam | f157d37 | 2017-03-08 11:11:27 -0600 | [diff] [blame] | 49 | host: 'https://9.3.164.147' |
| 50 | }, |
Iftekharul Islam | 38f681f | 2017-03-21 13:22:59 -0500 | [diff] [blame^] | 51 | API_RESPONSE: { |
| 52 | ERROR_STATUS: 'error', |
| 53 | ERROR_MESSAGE: '401 Unauthorized', |
| 54 | SUCCESS_STATUS: 'ok', |
| 55 | SUCCESS_MESSAGE: '200 OK' |
| 56 | }, |
Iftekharul Islam | f157d37 | 2017-03-08 11:11:27 -0600 | [diff] [blame] | 57 | CHASSIS_POWER_STATE: { |
| 58 | on: 'On', |
| 59 | off: 'Off' |
| 60 | }, |
Iftekharul Islam | ccfe245 | 2017-03-16 10:47:04 -0500 | [diff] [blame] | 61 | HOST_STATE_TEXT: { |
Iftekharul Islam | f157d37 | 2017-03-08 11:11:27 -0600 | [diff] [blame] | 62 | on: 'Running', |
| 63 | off: 'Off', |
Iftekharul Islam | ccfe245 | 2017-03-16 10:47:04 -0500 | [diff] [blame] | 64 | booting: 'Quiesced', |
| 65 | unreachable: 'Unreachable' |
| 66 | }, |
| 67 | HOST_STATE: { |
| 68 | on: 1, |
| 69 | off: -1, |
| 70 | booting: 0, |
| 71 | unreachable: -2 |
Iftekharul Islam | 38f681f | 2017-03-21 13:22:59 -0500 | [diff] [blame^] | 72 | } |
| 73 | }; |
| 74 | }) |
| 75 | .service('dataService', ['Constants', function(Constants){ |
| 76 | this.app_version = "openBMC V.0.0.1"; |
| 77 | this.server_health = 'Error'; |
| 78 | this.server_state = 'Unreachable'; |
| 79 | this.server_status = -2; |
| 80 | this.chassis_state = 'On'; |
| 81 | this.server_id = "Server 9.3.164.147"; |
| 82 | this.last_updated = new Date(); |
| 83 | |
| 84 | this.loading = false; |
| 85 | this.server_unreachable = false; |
| 86 | this.loading_message = ""; |
| 87 | this.showNavigation = false; |
| 88 | this.bodyStyle = {}; |
| 89 | this.path = ''; |
| 90 | |
| 91 | this.setPowerOnState = function(){ |
| 92 | this.server_state = Constants.HOST_STATE_TEXT.on; |
| 93 | this.server_status = Constants.HOST_STATE.on; |
| 94 | }, |
| 95 | |
| 96 | this.setPowerOffState = function(){ |
| 97 | this.server_state = Constants.HOST_STATE_TEXT.off; |
| 98 | this.server_status = Constants.HOST_STATE.off; |
| 99 | }, |
| 100 | |
| 101 | this.setBootingState = function(){ |
| 102 | this.server_state = Constants.HOST_STATE_TEXT.booting; |
| 103 | this.server_status = Constants.HOST_STATE.booting; |
| 104 | }, |
| 105 | |
| 106 | this.setUnreachableState = function(){ |
| 107 | this.server_state = Constants.HOST_STATE_TEXT.unreachable; |
| 108 | this.server_status = Constants.HOST_STATE.unreachable; |
| 109 | } |
| 110 | }]) |
| 111 | .factory('APIUtils', ['$http', 'Constants', function($http, Constants){ |
| 112 | var SERVICE = { |
| 113 | LOGIN_CREDENTIALS: Constants.LOGIN_CREDENTIALS, |
| 114 | API_CREDENTIALS: Constants.API_CREDENTIALS, |
| 115 | API_RESPONSE: Constants.API_RESPONSE, |
| 116 | CHASSIS_POWER_STATE: Constants.CHASSIS_POWER_STATE, |
| 117 | HOST_STATE_TEXT: Constants.HOST_STATE, |
| 118 | HOST_STATE: Constants.HOST_STATE, |
Iftekharul Islam | f157d37 | 2017-03-08 11:11:27 -0600 | [diff] [blame] | 119 | getChassisState: function(callback){ |
| 120 | $http({ |
| 121 | method: 'GET', |
| 122 | url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/chassis0", |
| 123 | headers: { |
| 124 | 'Accept': 'application/json', |
| 125 | 'Content-Type': 'application/json' |
| 126 | }, |
| 127 | withCredentials: true |
| 128 | }).success(function(response){ |
| 129 | var json = JSON.stringify(response); |
| 130 | var content = JSON.parse(json); |
| 131 | callback(content.data.CurrentPowerState); |
| 132 | }).error(function(error){ |
| 133 | console.log(error); |
| 134 | }); |
| 135 | }, |
| 136 | getHostState: function(callback){ |
| 137 | $http({ |
| 138 | method: 'GET', |
| 139 | url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0", |
| 140 | headers: { |
| 141 | 'Accept': 'application/json', |
| 142 | 'Content-Type': 'application/json' |
| 143 | }, |
| 144 | withCredentials: true |
| 145 | }).success(function(response){ |
| 146 | var json = JSON.stringify(response); |
| 147 | var content = JSON.parse(json); |
| 148 | callback(content.data.CurrentHostState); |
| 149 | }).error(function(error){ |
| 150 | console.log(error); |
| 151 | }); |
| 152 | }, |
Iftekharul Islam | 38f681f | 2017-03-21 13:22:59 -0500 | [diff] [blame^] | 153 | login: function(username, password, callback){ |
Iftekharul Islam | f157d37 | 2017-03-08 11:11:27 -0600 | [diff] [blame] | 154 | $http({ |
| 155 | method: 'POST', |
| 156 | url: SERVICE.API_CREDENTIALS.host + "/login", |
| 157 | headers: { |
| 158 | 'Accept': 'application/json', |
| 159 | 'Content-Type': 'application/json' |
| 160 | }, |
| 161 | withCredentials: true, |
Iftekharul Islam | 38f681f | 2017-03-21 13:22:59 -0500 | [diff] [blame^] | 162 | data: JSON.stringify({"data": [username, password]}) |
Iftekharul Islam | f157d37 | 2017-03-08 11:11:27 -0600 | [diff] [blame] | 163 | }).success(function(response){ |
| 164 | if(callback){ |
| 165 | callback(response); |
| 166 | } |
| 167 | }).error(function(error){ |
Iftekharul Islam | 38f681f | 2017-03-21 13:22:59 -0500 | [diff] [blame^] | 168 | if(callback){ |
| 169 | callback(null, true); |
| 170 | } |
| 171 | console.log(error); |
| 172 | }); |
| 173 | }, |
| 174 | logout: function(callback){ |
| 175 | $http({ |
| 176 | method: 'POST', |
| 177 | url: SERVICE.API_CREDENTIALS.host + "/logout", |
| 178 | headers: { |
| 179 | 'Accept': 'application/json', |
| 180 | 'Content-Type': 'application/json' |
| 181 | }, |
| 182 | withCredentials: true, |
| 183 | data: JSON.stringify({"data": []}) |
| 184 | }).success(function(response){ |
| 185 | if(callback){ |
| 186 | callback(response); |
| 187 | } |
| 188 | }).error(function(error){ |
| 189 | if(callback){ |
| 190 | callback(null, error); |
| 191 | } |
Iftekharul Islam | f157d37 | 2017-03-08 11:11:27 -0600 | [diff] [blame] | 192 | console.log(error); |
| 193 | }); |
| 194 | }, |
| 195 | chassisPowerOn: function(callback){ |
| 196 | $http({ |
| 197 | method: 'POST', |
| 198 | url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0", |
| 199 | headers: { |
| 200 | 'Accept': 'application/json', |
| 201 | 'Content-Type': 'application/json' |
| 202 | }, |
| 203 | withCredentials: true, |
| 204 | data: JSON.stringify({"data": []}) |
| 205 | }).success(function(response){ |
| 206 | var json = JSON.stringify(response); |
| 207 | var content = JSON.parse(json); |
| 208 | if(callback){ |
| 209 | return callback(content.data.CurrentPowerState); |
| 210 | } |
| 211 | }).error(function(error){ |
| 212 | if(callback){ |
| 213 | callback(error); |
| 214 | }else{ |
| 215 | console.log(error); |
| 216 | } |
| 217 | }); |
| 218 | }, |
| 219 | chassisPowerOff: function(callback){ |
| 220 | $http({ |
| 221 | method: 'POST', |
| 222 | url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0", |
| 223 | headers: { |
| 224 | 'Accept': 'application/json', |
| 225 | 'Content-Type': 'application/json' |
| 226 | }, |
| 227 | withCredentials: true, |
| 228 | data: JSON.stringify({"data": []}) |
| 229 | }).success(function(response){ |
| 230 | var json = JSON.stringify(response); |
| 231 | var content = JSON.parse(json); |
| 232 | if(callback){ |
| 233 | return callback(content.data.CurrentPowerState); |
| 234 | } |
| 235 | }).error(function(error){ |
| 236 | if(callback){ |
| 237 | callback(error); |
| 238 | }else{ |
| 239 | console.log(error); |
| 240 | } |
| 241 | }); |
| 242 | }, |
| 243 | hostPowerOn: function(callback){ |
Iftekharul Islam | ccfe245 | 2017-03-16 10:47:04 -0500 | [diff] [blame] | 244 | /** |
| 245 | curl -c cjar -b cjar -k -H "Content-Type: application/json" -d |
| 246 | "{\"data\": \"xyz.openbmc_project.State.Host.Transition.Off\"}" |
| 247 | -X PUT |
| 248 | https://9.3.164.147/xyz/openbmc_project/state/host0/attr/RequestedHostTransition |
| 249 | **/ |
Iftekharul Islam | f157d37 | 2017-03-08 11:11:27 -0600 | [diff] [blame] | 250 | $http({ |
Iftekharul Islam | ccfe245 | 2017-03-16 10:47:04 -0500 | [diff] [blame] | 251 | method: 'PUT', |
| 252 | url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0/attr/RequestedHostTransition", |
Iftekharul Islam | f157d37 | 2017-03-08 11:11:27 -0600 | [diff] [blame] | 253 | headers: { |
| 254 | 'Accept': 'application/json', |
| 255 | 'Content-Type': 'application/json' |
| 256 | }, |
| 257 | withCredentials: true, |
Iftekharul Islam | ccfe245 | 2017-03-16 10:47:04 -0500 | [diff] [blame] | 258 | data: JSON.stringify({"data": "xyz.openbmc_project.State.Host.Transition.On"}) |
Iftekharul Islam | f157d37 | 2017-03-08 11:11:27 -0600 | [diff] [blame] | 259 | }).success(function(response){ |
| 260 | var json = JSON.stringify(response); |
| 261 | var content = JSON.parse(json); |
| 262 | if(callback){ |
Iftekharul Islam | ccfe245 | 2017-03-16 10:47:04 -0500 | [diff] [blame] | 263 | return callback(content.status); |
Iftekharul Islam | f157d37 | 2017-03-08 11:11:27 -0600 | [diff] [blame] | 264 | } |
| 265 | }).error(function(error){ |
| 266 | if(callback){ |
| 267 | callback(error); |
| 268 | }else{ |
| 269 | console.log(error); |
| 270 | } |
| 271 | }); |
| 272 | }, |
| 273 | hostPowerOff: function(callback){ |
| 274 | $http({ |
Iftekharul Islam | ccfe245 | 2017-03-16 10:47:04 -0500 | [diff] [blame] | 275 | method: 'PUT', |
| 276 | url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0/attr/RequestedHostTransition", |
Iftekharul Islam | f157d37 | 2017-03-08 11:11:27 -0600 | [diff] [blame] | 277 | headers: { |
| 278 | 'Accept': 'application/json', |
| 279 | 'Content-Type': 'application/json' |
| 280 | }, |
| 281 | withCredentials: true, |
Iftekharul Islam | ccfe245 | 2017-03-16 10:47:04 -0500 | [diff] [blame] | 282 | data: JSON.stringify({"data": "xyz.openbmc_project.State.Host.Transition.Off"}) |
Iftekharul Islam | f157d37 | 2017-03-08 11:11:27 -0600 | [diff] [blame] | 283 | }).success(function(response){ |
| 284 | var json = JSON.stringify(response); |
| 285 | var content = JSON.parse(json); |
| 286 | if(callback){ |
Iftekharul Islam | ccfe245 | 2017-03-16 10:47:04 -0500 | [diff] [blame] | 287 | return callback(content.status); |
Iftekharul Islam | f157d37 | 2017-03-08 11:11:27 -0600 | [diff] [blame] | 288 | } |
| 289 | }).error(function(error){ |
| 290 | if(callback){ |
| 291 | callback(error); |
| 292 | }else{ |
| 293 | console.log(error); |
| 294 | } |
| 295 | }); |
| 296 | }, |
| 297 | hostReboot: function(callback){ |
| 298 | $http({ |
| 299 | method: 'POST', |
| 300 | url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0", |
| 301 | headers: { |
| 302 | 'Accept': 'application/json', |
| 303 | 'Content-Type': 'application/json' |
| 304 | }, |
| 305 | withCredentials: true, |
| 306 | data: JSON.stringify({"data": []}), |
| 307 | }).success(function(response){ |
| 308 | var json = JSON.stringify(response); |
| 309 | var content = JSON.parse(json); |
| 310 | if(callback){ |
| 311 | return callback(content); |
| 312 | } |
| 313 | }).error(function(error){ |
| 314 | if(callback){ |
| 315 | callback(error); |
| 316 | }else{ |
| 317 | console.log(error); |
| 318 | } |
| 319 | }); |
| 320 | }, |
| 321 | hostShutdown: function(callback){ |
| 322 | $http({ |
| 323 | method: 'POST', |
| 324 | url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0", |
| 325 | headers: { |
| 326 | 'Accept': 'application/json', |
| 327 | 'Content-Type': 'application/json' |
| 328 | }, |
| 329 | withCredentials: true, |
| 330 | data: JSON.stringify({"data": []}) |
| 331 | }).success(function(response){ |
| 332 | var json = JSON.stringify(response); |
| 333 | var content = JSON.parse(json); |
| 334 | if(callback){ |
| 335 | return callback(content); |
| 336 | } |
| 337 | }).error(function(error){ |
| 338 | if(callback){ |
| 339 | callback(error); |
| 340 | }else{ |
| 341 | console.log(error); |
| 342 | } |
| 343 | }); |
| 344 | } |
| 345 | }; |
| 346 | return SERVICE; |
Iftekharul Islam | 115984a | 2017-03-09 09:47:35 -0600 | [diff] [blame] | 347 | }]) |
| 348 | .factory('userModel',['APIUtils',function(APIUtils){ |
| 349 | return { |
Iftekharul Islam | 38f681f | 2017-03-21 13:22:59 -0500 | [diff] [blame^] | 350 | login : function(username, password, callback){ |
| 351 | APIUtils.login(username, password, function(response, error){ |
| 352 | if(response && |
| 353 | response.status == APIUtils.API_RESPONSE.SUCCESS_STATUS){ |
| 354 | sessionStorage.setItem('LOGIN_ID', username); |
| 355 | callback(true); |
| 356 | }else{ |
| 357 | callback(false, error); |
| 358 | } |
| 359 | }); |
Iftekharul Islam | 115984a | 2017-03-09 09:47:35 -0600 | [diff] [blame] | 360 | }, |
| 361 | isLoggedIn : function(){ |
| 362 | if(sessionStorage.getItem('LOGIN_ID') === null){ |
| 363 | return false; |
| 364 | } |
Iftekharul Islam | 115984a | 2017-03-09 09:47:35 -0600 | [diff] [blame] | 365 | return true; |
| 366 | }, |
Iftekharul Islam | 38f681f | 2017-03-21 13:22:59 -0500 | [diff] [blame^] | 367 | logout : function(callback){ |
| 368 | APIUtils.logout(function(response, error){ |
| 369 | if(response && |
| 370 | response.status == APIUtils.API_RESPONSE.SUCCESS_STATUS){ |
| 371 | sessionStorage.removeItem('LOGIN_ID'); |
| 372 | callback(true); |
| 373 | }else{ |
| 374 | callback(false, error); |
| 375 | } |
| 376 | }); |
Iftekharul Islam | 115984a | 2017-03-09 09:47:35 -0600 | [diff] [blame] | 377 | } |
| 378 | }; |
Iftekharul Islam | f157d37 | 2017-03-08 11:11:27 -0600 | [diff] [blame] | 379 | }]); |