Wednesday, July 6, 2016

Supporting WSO2 Data Services in AppCloud


Data services allows you to expose your relational databases, CSV files, Microsoft Excel sheets, Google spread sheets as SOAP and REST services. This helps you to expose your data through API Management solution such as API Cloud where your partners can consume your data in cloud. To read more about data services, refer to online documentation.

To start deploying dataservices in AppCloud, you need to have a car file which contains data service artifacts(dbs). In this post, I will explain how to
1. generate CRUD operations for a database table and get a data service.
2. import generated data service into to Developer studio and update with datasource configurations.
3. create a car file including above data service.
4. deploy it into AppCloud.

Lets get a database created in AppCloud so you can use it for the sample.
        Login to App Cloud using https://apps.cloud.wso2.com/appmgt/site/pages/index.jag
Create Database

provide database name, user and password
Find jdbc url etc
Connect to database and execute below queries
mysql -hmysql.storage.cloud.wso2.com -uemusr_vEqJD7xK -p
show databases;
use employeedb_manjulaorg;
create table employee(id VARCHAR(10) NOT NULL PRIMARY KEY, name VARCHAR(100), address VARCHAR(100));
insert into employee values('01','john','Boston');
insert into employee values('02','Micheal','Dallas');
insert into employee values('03','richard','Chicago');
SELECT * FROM employee;


1. Generate CRUD operations for a database table and get a data service.
Local setup details
Download wso2dss-3.5.0.zip file from wso2 web site and unzip.
Copy mysql-connector-java-5.* jar to wso2dss-3.5.0/repository/components/lib/
Start the server(wso2dss-3.5.0/bin/wso2server.sh start)
Login using admin/admin user.

Create a data source
In configure tab, create a new data source as below. You can get the database connection url, username from AppCloud UI as above.

Generate data service

In main tab, click on Add/Data Service/Generate button. Select data source you created in above step and provide the database name you created in AppCloud.
Next select table you need to expose with CRUD operations.
Provide a name to data service with namespace etc.
Data service will be deployed successfully.
Check the services deployed in services dashboard.
Try out your generated service with tryit tool.
Above are very simple steps to get CRUD operations exposed as a data service on a given table. You might need to change the queries, come up with better operation names, result elements etc. You can get this done easily by editing the data service through wizard or xml editor.
You can access the deployable file created in above steps in below location.
wso2dss-3.5.0/repository/deployment/server/dataservices/EmployeeService.dbs

2. Import generated data service into to Developer studio and update with data source configurations.
Open developer studio and create a data service project.
Next create a data service by importing the locally generated EmployeeService.dbs file.


Delete default(config) datasource and add a new one(right click on EmployeeService(data) -> Add Data Source -> RDBMS -> MySQL).
Make sure to keep the data source id as 'default'.


3. Create a car file including above data service.
Create composite application project.


Right click on composite project and export car file.



4. Deploy it into AppCloud.
Login to AppCloud start creating a application.
 Upload the created car file.
Application is created. Check the logs to see any errors in your data service.


If you get the service deployed correctly, check the car deployment logs as below.


To invoke the data service, click on the launch link. You will have to change the browser url to append /service/EmployeeService?wsdl as below.
https://manjulaorg-employeeservice-1-0-0.wso2apps.com/services/EmployeeService?wsdl 
Open the WSDL using soap ui tool and invoke the operations.
To summarize,
Most part of this post is about coming up with your data service and exporting it as a car file using developer studio.
If you have your own data service that is working locally, you can bring it to cloud easily as explained in this post using App Cloud.
To deploy multiple services in a single container, you can package multiple data services into car file and upload.



I will explain steps for securing this data service with username token ws security policy in next post.