Quantcast
Channel: Java mon amour
Viewing all articles
Browse latest Browse all 1121

Nexus automation API

$
0
0
The Nexus documentation and examples are horribly fragmentary and scattered - I have rarely seen a such popular product being under-documented in this chaotic way.

Since 3.3 there is a Swagger-UI interface http://blog.sonatype.com/nexus-repository-new-beta-rest-api-for-content not available in 2.14 . Try for instance http://nexus-nodejs/swagger-ui/#!/assets/getAssets (replace nexus-nodejs with your own URL)

A quite explanatory article on REST API (including the /nexus/service/local/ vs /nexus/service/siesta story) is http://www.sonatype.org/nexus/2015/01/26/learn-the-nexus-rest-api-automating-sonatype-nexus/

One can always generate a Java Client from Swagger http://www.baeldung.com/spring-boot-rest-client-swagger-codegen or also https://github.com/swagger-api/swagger-codegen

Apparently Nexus chose Groovy as a language for automation https://help.sonatype.com/display/NXRM3/Writing+Scripts

REST syntax


To discover the syntax of the REST calls (with JSON payload) Nexus recommends simply ("request espionage") to use Developer Tools (F12 in IE, CTRL-SHIFT-I in Chrome and Firefox), go to Network tab, start capturing and execute manually some commands in the Administration UI.

This for instance is how to "whitelist" (pre-approve) use of a xml-apis:xml-apis:1.3.04 (GAV) component in the provisioned repository "approved_from_central" :

Request POST /service/local/procurement/resolutions/approved_from_central HTTP/1.1
X-Nexus-UI true
Accept application/json,application/vnd.siesta-error-v1+json,application/vnd.siesta-validation-errors-v1+json
Content-Type application/json
X-Requested-With XMLHttpRequest
Referer http://nexus-java/
Accept-Language de-CH
Accept-Encoding gzip, deflate
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; Ypkch32; rv:11.0) like Gecko
Host nexus-java
Content-Length 170
Connection Keep-Alive
Cache-Control no-cache
Cookie NXSESSIONID=e2265c85-f82b-45ce-a889-f6964cdaa214

Request Body
{"data":{"artifactCoordinate":{"groupId":"xml-apis","artifactId":"xml-apis","version":"1.3.04"},"properties":[{"key":"isApproved","value":"true"}],"ruleTypeId":"simple"}}




Return code is HTTP 201 (created)


In 2.14 you can get the whole list of APIs by logging in as admin, on the left menu click on Administration, Plugin Console, click on "Nexus Core API (Restlet 1.x Plugin)" (Provides Nexus Core REST API) and click on the Documentation link http://nexus-java/nexus-restlet1x-plugin/default/docs/index.html


This video seems the only serious attempt to document the REST API




Other useful commands:

#get the status of repository (the -u option is not necessary!)
curl -u admin:admin123 http://nexus-java/service/local/status

curl -X GET -u admin:admin123 http://nexus-java/service/local/users

#get list of all repositories, in xml format
curl http://nexus-java/service/local/all_repositories

#get list of all assets with "arquillian" in the name
curl http://nexus-java/service/local/data_index?q=arquillian

curl -i -H "Accept: application/xml" -H "Content-Type: application/xml" -X POST -v -trace-ascii -d "@repository-definition.xml" -u admin:admin123 http://nexus-java/service/local/repositories


See the curl documentation here https://curl.haxx.se/docs/manpage.html#-d



There are several github repositories:

https://github.com/sonatype/nexus-book-examples/tree/nexus-3.x


git clone https://github.com/sonatype/nexus-book-examples.git
cd nexus-book-examples/
git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/NEXUS-14940-deprecate-service-siesta
remotes/origin/master
remotes/origin/nexus-3.x
git checkout -b nexus-3.x origin/nexus-3.x


then open this in your firefox:
file:///home/centos/nexus-book-examples/scripting/apidocs/index.html


The source code is available in https://github.com/sonatype/nexus-public under
plugins/nexus-script-plugin :
https://github.com/sonatype/nexus-public/blob/master/plugins/nexus-script-plugin/src/main/java/org/sonatype/nexus/script/plugin/RepositoryApi.java


To parse XML in Python: https://docs.python.org/2/library/xml.etree.elementtree.html

To make curl requests (POST, GET) in Python: http://docs.python-requests.org/en/master/





Viewing all articles
Browse latest Browse all 1121

Trending Articles