Skip to content

Commit

Permalink
HWKALERTS-132 Hello World example for REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasponce committed Feb 12, 2016
1 parent a71c453 commit 86b370a
Show file tree
Hide file tree
Showing 5 changed files with 308 additions and 0 deletions.
51 changes: 51 additions & 0 deletions examples/hello-world/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
= hello world

This example shows how to use Hawkular Alerts REST API from a script.

The scripts are written in groovy to make them work from maven in any platorm but those are pretty simple and can be
translated to bash or a different environment easily.

== Running the example

Build a Hawkular Alerts standalone distribution

[source,shell,subs="+attributes"]
----
cd hawkular-alerts
mvn clean install -Pstandalone
----

Start the standalone server

[source,shell,subs="+attributes"]
----
cd hawkular-alerts/hawkular-alerts-rest-tests/target/wildfly-10.0.0.Final/
bin/standalone.sh
----

Open a new shell to run the hello-world example

[source,shell,subs="+attributes"]
----
cd hawkular-alerts/examples/hello-world
mvn validate
----

TIP: By default, Hawkular Alerts will send email notifications using a SMTP server on localhost:25, for demo purposes
a test smtp server can be used to validate the reception of the emails. Hawkular Alerts has been tested using
https://nilhcem.github.io/FakeSMTP/[FakeSMTP].

TIP: To run the example against the Hawkular distribution, use the command `mvn validate -Dhawkular`.

== create.definitions.groovy

Create a hello world trigger to two conditions to fire an alert everytime that:

. numeric data with id "data-x" is less than 5 and
. numeric data with id "data-y" is greater than 5

Create an action definition to notify by email to admins group with cc to developers team.

== send_data.groovy

Send random data for "data-x" and "data-y" over the REST API.
92 changes: 92 additions & 0 deletions examples/hello-world/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2015-2016 Red Hat, Inc. and/or its affiliates
and other contributors as indicated by the @author tags.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.hawkular.alerts</groupId>
<artifactId>hello-world</artifactId>
<version>0.9.0.Final-SNAPSHOT</version>

<name>Hawkular Alerts Examples: Hello World</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<version.net.sf.json-lib>2.3</version.net.sf.json-lib>
<version.org.codehaus.groovy>2.4.5</version.org.codehaus.groovy>
<version.org.codehaus.groovy.maven>1.0</version.org.codehaus.groovy.maven>
<version.org.codehaus.groovy.modules.http-builder>0.7</version.org.codehaus.groovy.modules.http-builder>
</properties>

<dependencies>

<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>${version.org.codehaus.groovy}</version>
</dependency>

<dependency>
<groupId>org.codehaus.groovy.modules.http-builder</groupId>
<artifactId>http-builder</artifactId>
<version>${version.org.codehaus.groovy.modules.http-builder}</version>
</dependency>

<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>${version.net.sf.json-lib}</version>
<classifier>jdk15</classifier>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>${version.org.codehaus.groovy.maven}</version>
<executions>
<execution>
<id>create-definitions-in-hawkular-alerts</id>
<phase>validate</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>${basedir}/src/test/groovy/create_definitions.groovy</source>
</configuration>
</execution>
<execution>
<id>send-demo-data-to-hawkular-alerts</id>
<phase>validate</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>${basedir}/src/test/groovy/send_data.groovy</source>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
51 changes: 51 additions & 0 deletions examples/hello-world/src/test/groovy/create_definitions.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2015-2016 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import groovyx.net.http.ContentType
import groovyx.net.http.RESTClient
import net.sf.json.groovy.JsonSlurper

def url = "http://localhost:8080/hawkular/alerts/"
def tenant = "my-organization"

def definitions = new File("src/test/resources/hello-world-definitions.json").text

RESTClient client = new RESTClient(url, ContentType.JSON)

client.handler.failure = { resp ->
def mapper = new JsonSlurper()
def error = mapper.parseText(resp.entity.content.text)
println "Status error: ${resp.status} \nerrorMsg: [${error.errorMsg}]"
return resp
}

if (System.getProperties().containsKey("hawkular")) {
/*
User: jdoe
Password: password
String encodedCredentials = Base64.getMimeEncoder()
.encodeToString("$testUser:$testPasword".getBytes("utf-8"))
*/
client.defaultRequestHeaders.Authorization = "Basic amRvZTpwYXNzd29yZA=="
} else {
client.defaultRequestHeaders.put("Hawkular-Tenant", tenant)
}

def resp = client.post(path: "import/all", body: definitions)

if (resp.status == 200) {
println "Imported definitions from json: \n${definitions}"
}
68 changes: 68 additions & 0 deletions examples/hello-world/src/test/groovy/send_data.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2015-2016 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import groovyx.net.http.ContentType
import groovyx.net.http.RESTClient
import net.sf.json.groovy.JsonSlurper

def url = "http://localhost:8080/hawkular/alerts/"
def tenant = "my-organization"

RESTClient client = new RESTClient(url, ContentType.JSON)

client.handler.failure = { resp ->
def mapper = new JsonSlurper()
def error = mapper.parseText(resp.entity.content.text)
println "Status error: ${resp.status} \nerrorMsg: [${error.errorMsg}]"
return resp
}

if (System.getProperties().containsKey("hawkular")) {
/*
User: jdoe
Password: password
String encodedCredentials = Base64.getMimeEncoder()
.encodeToString("$testUser:$testPasword".getBytes("utf-8"))
*/
client.defaultRequestHeaders.Authorization = "Basic amRvZTpwYXNzd29yZA=="
} else {
client.defaultRequestHeaders.put("Hawkular-Tenant", tenant)
}

while (true) {

def x = [:]
x.id = "data-x"
x.timestamp = System.currentTimeMillis()
x.value = new Random().nextInt(10)

def y = [:]
y.id = "data-y"
y.timestamp = System.currentTimeMillis()
y.value = new Random().nextInt(10)

def data = [x, y]

println "Sending data ${data}"

def resp = client.post(path: "data", body: data)

if (resp.status != 200) {
break
}

sleep(500)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"triggers":[
{
"trigger":{
"id": "hello-world-trigger",
"name": "Hello World Trigger",
"description": "A mandatory Hello World Trigger",
"severity": "HIGH",
"enabled": true,
"actions":[
{
"actionPlugin": "email",
"actionId": "notify-to-admins"
}
]
},
"conditions":[
{
"triggerMode": "FIRING",
"type": "threshold",
"dataId": "data-x",
"operator": "LT",
"threshold": 5
},
{
"triggerMode": "FIRING",
"type": "threshold",
"dataId": "data-y",
"operator": "GT",
"threshold": 5
}
]
}
],
"actions":[
{
"actionPlugin": "email",
"actionId": "notify-to-admins",
"properties": {
"to": "admins@hawkular.org",
"cc": "developers@hawkular.org",
"description": "Notify by email to Admins with cc to Developers"
}
}
]
}

0 comments on commit 86b370a

Please sign in to comment.