forked from RackHD/on-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredfish-enable-alerts-job.js
122 lines (105 loc) · 4.06 KB
/
redfish-enable-alerts-job.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2017, EMC, Inc
'use strict';
var di = require('di');
module.exports = RestJobFactory;
di.annotate(RestJobFactory, new di.Provide('Job.Redfish.Alert.Enable'));
di.annotate(RestJobFactory, new di.Inject(
'Job.Base',
'Logger',
'_',
'Assert',
'Promise',
'Util',
'HttpTool',
'Services.Waterline',
'Services.Configuration'));
function RestJobFactory(BaseJob,
Logger,
_,
assert,
Promise,
util,
HttpTool,
waterline,
configuration){
var logger = Logger.initialize(RestJobFactory);
/**
* The interface that runs Rest Job from tasks
* @constructor
*/
function RestJob(options, context, taskId){
var self = this;
self.options = options;
self.context = context;
RestJob.super_.call(self, logger, options, context, taskId);
self.restClient = new HttpTool();
}
util.inherits(RestJob, BaseJob);
RestJob.prototype._run = function run(){
var self = this;
return new Promise.resolve()
.then(function(){
return updateOptions(self);
})
.then(function(setup){
logger.debug("setting up the redfish alert subcription");
return self.restClient.setupRequest(setup);
})
.then(function(){
logger.debug("Running The rest call for redfish subscription");
return self.restClient.runRequest();
})
.then(function(data){
self.context.restData = data;
self._done();
})
.catch(function(err){
logger.error("Found error during HTTP request.", {error: err});
self._done(err);
});
};
function updateOptions(obj){
logger.debug("Setting up the values in order to post RackHD subscribtion");
//user entered values always take precedence
var httpSetup = obj.options;
httpSetup.method = obj.options.data.method || "POST";
httpSetup.data.Protocol = obj.options.data.Protocol || "Redfish";
httpSetup.data.Context = obj.options.data.Context || "RackhHD Subscription";
httpSetup.data.EventTypes = obj.options.data.EventTypes ||
["ResourceAdded", "StatusChange", "Alert"];
if (obj.options.data.Destination){
httpSetup.data.Destination = obj.options.data.Destination;
}else{
var ip = configuration.get("rackhdPublicIp");
var httpsPort = configuration.get("httpEndpoints")[1].port;
httpSetup.data.Destination = "https://" + ip + ":"+
httpsPort + "/api/2.0/notification/alerts";
}
//Getting the http credentials
if (obj.options.credential &&
obj.options.credential.password &&
obj.options.credential.username){
httpSetup.credential.username = obj.options.credential.username;
httpSetup.credential.password = obj.options.credential.password;
httpSetup.url = obj.options.url;
return httpSetup;
}else{
return waterline.obms.findByNode(obj.context.target, 'redfish-obm-service', true)
.then(function(obm){
if (!obm){
throw new Error("Couldn't find redfish obm setting for node " +
obj.context.target);
}
else{
httpSetup.credential = {};
httpSetup.credential.username = obm.config.user;
httpSetup.credential.password = obm.config.password;
httpSetup.url = obj.options.url || "https://" + obm.config.host +
"/redfish/v1/EventService/Subscriptions";
return httpSetup;
}
});
}
}
return RestJob;
}