cf create-service alert-notification standard my-alert-notification-instance
{"configuration":{<<>>}}
. For more information, see Exporting or Importing Configurations.
cf create-service alert-notification standard my-alert-notification-instance -c '{"configuration":{"conditions":[{"name":"demo_condition","propertyKey":"eventType","predicate":"EQUALS","propertyValue":"demo"},
{"name":"demo_condition_2","propertyKey":"eventType","predicate":"EQUALS","propertyValue":"demo_2"}],
"actions":[{"name":"demo_action","state":"ENABLED","destination":"https://ans-test-123.cfapps.eu00.hana.ondemand.com/web-hook/api/v1/events","type":"WEB_HOOK"},{"name":"demo_action_2","state":"ENABLED","type":"STORE"}],
"subscriptions":[{"name":"demo_subscription","conditions":["demo_condition"],"actions":["demo_action"],"state":"ENABLED"},{"name":"demo_subscription_2","conditions":["demo_condition_2"],"actions":["demo_action_2"],"state":"ENABLED"}]}}'
resources:
- name: example-alert-notification
type: org.cloudfoundry.managed-service
parameters:
service: alert-notification
service-plan: standard
config:
{
"configuration":{
"conditions":[
{
"name":"example_condition",
"propertyKey":"eventType",
"predicate":"EQUALS",
"propertyValue":"demo"
},
{
"name":"example_condition_2",
"propertyKey":"eventType",
"predicate":"EQUALS",
"propertyValue":"demo_2"
}
],
"actions":[
{
"name":"example_action",
"state":"ENABLED",
"destination":"https://ans-test-123.cfapps.eu00.hana.ondemand.com/web-hook/api/v1/events",
"type":"WEB_HOOK"
},
{
"name":"example_action_2",
"state":"ENABLED",
"type":"STORE"
}
],
"subscriptions":[
{
"name":"example_subscription",
"conditions":[
"example_condition"
],
"actions":[
"example_action"
],
"state":"ENABLED"
},
{
"name":"example_subscription_2",
"conditions":[
"example_condition_2"
],
"actions":[
"example_action_2"
],
"state":"ENABLED"
}
]
}
}
cf bind-service APP_NAME SERVICE_INSTANCE -c '{"name":"value","name":"value"}'
{
"url": "https://test-service-api.cfapps.sap.hana.ondemand.com",
"client_id": "b6a3d917-fk05-49cb-a122-3e34567a5b1",
"client_secret": "pEJBGTfegTY+DANjfm9cDrhyNNoQNwu"
}
{
"eventType": "po.create",
"resource": {
"resourceName": "purchase order create",
"resourceType": "app",
"tags": {
"env": "dev"
}
},
"severity": "FATAL",
"category": "ALERT",
"subject": "application has crashed",
"body": "Kindly check the app as something has crashed",
"tags": {
"ans:correlationId": "30118",
"ans:status": "CREATE_OR_UPDATE",
"customTag": "42"
}
}
The below code snippets demonstarte how the alert notification can be triggered with NodeJS and java applications
var data = JSON.stringify(json);
var auth = "xxxxxxxxxxxxxxxxxxxxxxx" + ":" + "xxxxxxxxxxxxxxxxxxxxxxx"
var buff = Buffer.from(auth);
var base64auth = "Basic " + buff.toString('base64');
const options = {
hostname: xxxxxxxxxxxxx.cfapps.sap.hana.ondemand.com',
port: 443,
path: '/cf/producer/v1/resource-events',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length,
'Authorization': base64auth
},
};
const req = https.request(options, (res) => {
console.log(`statusCode: ${res.statusCode}`)
res.on('data', (d) => {
process.stdout.write(d)
})
})
req.on('error', (error) => {
console.error(error)
})
req.write(data)
req.end()
res.writeHead(rc, {
'Content-Type': 'application/json'
});
res.end(JSON.stringify({
state: 1
}));
public void triggeralertnotification(HttpServletRequest request, HttpServletResponse response, PrintWriter writer) throws IOException, Exception{
String auth = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx" + ":" + "xxxxxxxxxxxxxxxxxxxxxxxxxx";
byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(StandardCharsets.UTF_8));
String authHeaderValue = "Basic " + new String(encodedAuth);
String path = "https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxcfapps.sap.hana.ondemand.com/cf/producer/v1/resource-events";
URL targetURL = new URL (path);
HttpURLConnection con = (HttpURLConnection) targetURL.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Authorization", authHeaderValue);
con.setRequestProperty( "charset", "utf-8");
con.setConnectTimeout(5000);
con.setDoOutput(true);
DataOutputStream postbody = new DataOutputStream(con.getOutputStream());
postbody.writeBytes(getAlertContent());
postbody.flush();
postbody.close();
LOGGER.info("Alert notification called with service url" + targetURL );
int sc = con.getResponseCode();
LOGGER.info("Returning service code " + sc );
con.disconnect();
response.setStatus(sc);
response.getWriter().println("<p>Alert notification triggered</p>");
}
private String getAlertContent() throws Exception{
InputStream is = getClass().getResourceAsStream("/json/alert.json");
String json = new BufferedReader(
new InputStreamReader(is, StandardCharsets.UTF_8))
.lines()
.collect(Collectors.joining("\n"));
LOGGER.info("Send output mail format" + json);
return json;
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
13 | |
10 | |
10 | |
9 | |
7 | |
6 | |
5 | |
5 | |
5 | |
4 |