Pages

Monday, September 26, 2016

Nexus Rest API

Nexus is a collection of services available for us to automate. With the services available we can integrate them in the work flow that we need. Nexus also exposes the services as Rest services so that we can use them to automate things. In this article we will see how we can use the nexus rest api to automate things.

While nexus has a rich set of Rest end points for every thing from core actions like storing artifacts in the repository to creating users and repositories, but the documentation is little touch to find. Here are the steps you need to take to access this documentation:
  1. Login as an Administrator
  2. In the Administration section of the left-hand menu, click on Plugin Console to open the Plugin Console.
  3. Once in the Plugin Console you will see a list of Nexus Plugins. Click on a plugin to view the APIs it provides.
  4. Once you select a plugin, you should see a list of APIs. For example, the video below shows the Core API, click on the link in the Plugin Console to view the REST API documentation.

We will use the CURL command available in Linux to make the rest calls.  Nexus uses a single end point with changing services to communicate on the rest.

/nexus/service/local is the endpoint and by adding the service after this we can make a rest call to automate different things. Below are the few nexus services that we can use to communicate

nexus_status_service: status
nexus_users_service: users
nexus_repositories_service: repositories
nexus_roles_service: roles

Lets see a few examples on making use of the nexus rest API

Simple Get calls

Retrieve the status of the Nexus Server -
curl http://localhost:8081/nexus/service/local/status

Retrieve the available nexus repositories

Authenticated REST - GET requests  
User Details– In order to retrieve some details we need to send the credentials details. Curl Command also allows us to pass the credentials. In order to retrieve a user details the Rest call needs the credentials and this can be done as,

Delete Repositories
We can also pass other HTTP parameter like DELETE using the curl command and make a Rest call as,

curl -X DELETE -v -u admin:admin123 http://localhost:8081/nexus/service/local/repositories/my-releases-xml

Post Calls – The most important call will be the Post call since we will be using this to create things. Lets see on creating a repository using the CURL post call along with the XML payload

Create a XML file new_user.xml with user details as,

<?xml version="1.0" encoding="UTF-8"?>
<user-request>
  <data>
    <userId>master</userId>
    <email>master@gmail.com</email>
    <status>active</status>
    <firstName>mind</firstName>
    <resourceURI>http://puppet.jas.com:8081/nexus/service/local/roles/repository-any-full</resourceURI>
    <roles>
      <role>npm-all-view</role>
    </roles>
    <lastName>minder</lastName>
    <password>test123</password>
  </data>
</user-request>

In the above snippet we are creating a new user master. In order to make the call we can call the CURL command as,

curl -i -H "Accept: application/xml" -H "Content-Type: application/xml; charset=UTF-8"  -v -d "@new_user.xml" -u admin:admin123 http://puppet.jas.com:8081/nexus/service/local/users

In the above command we are using the users service to make the rest call and create a user by passing the xml payload that we created in the file. Once we ran the curl command we can see the output as below which includes the necessary return code details.

* About to connect() to puppet.jas.com port 8081 (#0)
*   Trying 172.16.202.95...
* Connected to puppet.jas.com (172.16.202.95) port 8081 (#0)
* Server auth using Basic with user 'admin'
> POST /nexus/service/local/users HTTP/1.1
> Authorization: Basic YWRtaW46YWRtaW4xMjM=
> User-Agent: curl/7.29.0
> Host: puppet.jas.com:8081
> Accept: application/xml
> Content-Type: application/xml; charset=UTF-8
> Content-Length: 424
>
* upload completely sent off: 424 out of 424 bytes
< HTTP/1.1 201 Created
HTTP/1.1 201 Created
< Date: Wed, 31 Aug 2016 08:27:23 GMT
Date: Wed, 31 Aug 2016 08:27:23 GMT
< X-Frame-Options: SAMEORIGIN
X-Frame-Options: SAMEORIGIN
< X-Content-Type-Options: nosniff
X-Content-Type-Options: nosniff
< Server: Nexus/2.13.0-01 Noelios-Restlet-Engine/1.1.6-SONATYPE-5348-V8
Server: Nexus/2.13.0-01 Noelios-Restlet-Engine/1.1.6-SONATYPE-5348-V8
< Content-Type: application/xml; charset=UTF-8
Content-Type: application/xml; charset=UTF-8
< Content-Length: 388
Content-Length: 388

<
<user-response>
  <data>
    <resourceURI>http://puppet.jas.com:8081/nexus/service/local/users/master</resourceURI>
    <userId>master</userId>
    <password>test123</password>
    <firstName>mind</firstName>
    <lastName>minder</lastName>
    <status>active</status>
    <email>master@gmail.com</email>
    <roles>
      <role>npm-all-view</role>
    </roles>
  </data>
* Connection #0 to host puppet.jas.com left intact
</user-response>

Besides passing the XML file as payload , nexus also accepts the JSON content as payload even and CURL command allows us to do so. Create a file repo.json with the repository details as,

[puppet@root$:/test]$  cat repo.json
{
    "data": {
        "repoType": "proxy",
        "id": "somerepo",
        "name": "Some Repo Name",
        "browseable": true,
        "indexable": true,
        "notFoundCacheTTL": 1440,
        "artifactMaxAge": -1,
        "metadataMaxAge": 1440,
        "itemMaxAge": 1440,
        "repoPolicy": "RELEASE",
        "provider": "maven2",
        "providerRole": "org.sonatype.nexus.proxy.repository.Repository",
        "downloadRemoteIndexes": true,
        "autoBlockActive": true,
        "fileTypeValidation": true,
        "exposed": true,
        "checksumPolicy": "WARN",
        "remoteStorage": {
            "remoteStorageUrl": "http://puppet.jas.com:8081/local",
            "authentication": null,
            "connectionSettings": null
        }
    }
}

Now we can run the CURL command as,

curl -H "Content-Type: application/json" -X POST -d @virtual.json -u admin:admin123 http://puppet.jas.com:8081/nexus/service/local/repositories

In the above command we have changed the content-type to json unlike xml.

That’s all about using the nexus Rest api and curl command.  Below are the few examples on using nexus Rest api

Get Single Repository Details
curl -k http://puppet.jas.com:8081/nexus/service/local/repositories/releases -u

Get Repository Status
curl -k http://puppet.jas.com:8081/nexus/service/local/repository_statuses -u admin:admin123

Getting a list of all users, now with authenticating as admin user
curl -X GET -u admin:admin123 http://localhost:8081/nexus/service/local/users

Retive the account Details
curl -k http://puppet.jas.com:8081/nexus/service/local/user_account/admin -u admin:admin123

Get the list of users available
curl -X GET -u admin:admin123 http://172.16.202.96:9091/nexus/service/local/users

Get the list of repositories available
curl http://172.16.202.96:9091/nexus/service/local/repositories

Hope this Helps.

1 comment :

  1. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in Nexus, kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor led training on Nexus. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us:
    Name : Arunkumar U
    Email : arun@maxmunus.com
    Skype id: training_maxmunus
    Contact No.-+91-9738507310
    Company Website –http://www.maxmunus.com

    ReplyDelete