Integration with Ballerina
No this is not about ballet dancing! It is about computer coding! Integration!
Ballerina is a general purpose, concurrent, and strongly typed programming language with both textual and graphical representations. It is designed to make it easier to write programs that integrate with data sources, services, and network-connected APIs of all kinds.
It is optimized primarily for such programs — while it can be use to program anything, it is not recommended to use Ballerina if a significant portion of the program is not related to integrating with data sources, services, or network-connected APIs. Ballerina has been inspired by Java, Go, and other languages, but it has a concurrency model built around a sequence diagram metaphor.
Ballerina’s formats of the visual composer or the code are completely interchangeable. So you can write the code and first then switch over to the Composer to draw a diagram and vice versa.
How to get Ballerina
You can download Ballerina by visiting Ballerina website . If you only want Ballerina run time simply download “Ballerina runtime” package. This package contains only the runtime environment needed to execute a Ballerina program.
If you want all tooling, get Ballerina Tools package. This package includes:
- The Ballerina Composer for visual editing
- Docerina for generating API documentation for Ballerina programs
- Testerina for testing Ballerina code
writing an etcd connector using Ballerina
Ballerina comes with out-of-the-box native connectors like HTTP/S, WebSocket, JMS, File, HTTP/2 etc.
In this exercise, I wanted to see how easy to write an integration code using Ballerina. I choose writing a client connector for etcd using HTTP Ballerina native connector. “etcd” is a distributed, consistent key-value store for shared configuration and service discovery.
Since I am coming from devOps background I love to do coding using vim. Specially when it’s come to scripting language. So I wanted to use Ballerina vim plugin and see how easy to write the code.
First of all, to test the connector functionality I wanted to setup etcd in my machine. I have used docker. I have installed docker by using docker-for-mac.
Following single command helped to configure etcd without any pain.
My etcd connector expect 4 parameters etcdURL, username, password and apiVersion to initialize the connection. Following code will do the connector initialization.
Since this is acting as a client (connecting to external server) I have defined it as ClientConnector.
etcd expose its functionality via HTTP endpoint therefor I have initialized connector by using native function call http:ClientConnector.
In Ballerina, we can define set of actions inside the connectors. Is this connector I have defined following actions.
* setKeyValue : Store key/value pair
* getValue : Get the value for given key
* updateValue : Update the value for given key
* deleteKey : Delete the key
* createDir : Create a directory
* listDir : List directory (recursive listing also supported)
* deleteDir : Delete directory (recursive delete also supported)
Actions can be invoked from external program. As a sample invocation, I have added the main method in sample/etcd/samples.bal
E.g Adding a key/value pair can be called using following command with the arguments
bin$ ./ballerina run main samples.bal setKeyValue <key> <value>
It will trigger following code line in the main method.
Here is the code section for the setKeyValue action
If authentication enable in etcd you must pass username and password to the connector or else you can make them empty. Within the connector, I have set the basic auth authentication header when username and password are not empty. Create proper request body and sending it to correct api endpoint will do the required functionality.
Here how it will display my etcd connector in graphical programming mode.
You may also have noticed @doc annotation. It will help to auto generate api docs. See below sample api doc.
If you want to create a docker image and run anywhere with all dependencies … its also support out-of-the-box by Ballerina container support.
If you like to know more about etcd connector, please see following GitHub link which has all the files related to etcd connector.
In summary Ballerina will make life easy for developers to writing an integration code. I have enjoyed very much!
Tryout Ballerina. Happy coding! Happy dancing :)