WSO2 Governance Registry import/create assets from an Excel file

person kasunfolder_openGovernance Registry, python, WSO2local_offer, , , , access_time October 25, 2016

WSO2 Governance registry allows it users to manage their enterprise artifacts or asset in a centralized repository.It makes easy for SOA based enterprise or an organization to manage and keep track of all type of enterprise assets including but not limited to REST , SOAP, WSDL and WADL etc.In WSO2 Governance Registry there is a great web based application to manage and discover the available assets and resources with the registry. And it also allows adding custom type registry resources by using Configurable Governance Artifacts called RXTs. You can add your own desired RXT definition to WSO2 Greg and thereafter you can create assets from that particular artifact type using publisher application. UIs for the relevant artifact type is automatically rendered according to the field types and tables defined in the RXT file.

As it is the default way to use the Greg Publisher application to create and publish asset,In this post I will discuss how we can use WSO2 Greg Governance APIs to create asset through a client program.

WSO2 Greg provides a rich set of APIs where users or developers can interact with Greg server through HTTP protocol.Greg has exposed many of its capabilities as REST services as standard CRUD operations and mainly it is divided into two categories, WSO2 Governance API[3] and Registry API[2].

WSO2 Registry API allows its users to interact with repository aspect of the GREG and it allows a set of API endpoints to manage resources level actions.For example, you can put or create any type of resource into any registry location using the Registry API. But in contrast, Governance API allows users to interact with all the governance aspect of the GREG server, For example, users can create an asset for specific artifact types, they can manage the life-cycle state of an asset and etc.

Rather than explaining the concept lets see how we can create assets from the data in an Excel sheet.

I have created following sample artifact-type named car which represents the basic information about a car automobile.I will use the fields in the following RXT to create assets for car artifact-type. You can add a new artifact to WSO2 GREG through the admin console.

Let’s go directly to the source code, I have written a sample client application using Python since it requires a lesser number of lines of code and more readable to explain a concept.

In the main method, I have opened the target Excel file by getting the file location from configurations and get the first sheet from the excel file.In here if you know the name of the sheet which the data are located, you can get the sheet by specifying its name.I have used the openpyxl library to read the excel file from python, Here is a good article on how to get started with the openpyxl library.

After getting the relevant sheet, we iterate through the rows and extract the data in each column by providing the row number and column number like in a cartesian plane.In the range() function I have started the range from 2 instead of 1 because of the assumption, First row in the excel sheet contains the header information about each column , If it is not the case with your data , change the number accordingly.After extracting the data, I have used the get_json() method to put all the data into JSON format to send them through HTTP POST request.Since its just a matter of converting python Dict to JSON string, I haven’t extracted the body for get_json() method here, You can find it in the source code and it is self-explanatory.

so far we have extracted the data and put the data into a JSON string, ready to send in a POST request body.The create_asset() method is the one who invoke the create asset API through python requests HTTP library.It simply builds the required headers and URL for invoking the API and check for the response whether it has any errors and update the user. The only tricky part here is building the value for Authorization header, There I have used a base64 encoder name b64encode which is coming with the default python installation and generate the base64 encoded string of ‘<username>:<password>’. And the verify=false has been enforced since we haven’t use proper ssl certificate in GREG server.

That’s it ,Now you should be able to see all the newly created assets from the store or publisher applications.Cars WSO2 Governance Center Store

 

You can find the complete source code for the above sample application in this[1] git repository.

Happy importing data! 😀

 

[1] : https://github.com/tmkasun/me/tree/kasun/blog/greg_import_from_excel/sample_client

[2] : https://docs.wso2.com/display/Governance530/Registry+REST+API

[3] : https://docs.wso2.com/display/Governance530/Governance+REST+API

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">