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.
Read More

Ansible Rest Calls


Rest API’s calls are now a days the main API to integrate with different applications. If you want to connect to an existing infrastructure we can expose the infrastructure as rest API and other applications can you the API to make calls.

Ansible also provides us a way to make the Rest calls using URI module. The URI module allows us to send XML or JSON payload and get the necessary details. In this article we will see how we can use the URI module and make the Rest calls. As for the article I will be using the Nexus artifactory to connect which run on the 8081 Port. The URL are specified in the vars/main.yml file.

Make a Get Call - Using the Ansible URI module to make get call is pretty easy. We can use

- name: Maker a Call
  uri:
   url: "{{nexus_url}}/repositories"
   method: GET
   user: admin
   password: admin123
   force_basic_auth: yes
   return_content: yes

In the above snippet, I am making a Rest call to the “nexus_url/respositories” URL by passing them the user name and password along with the method as “Get” and the return_content.  We can use the Ansible debug to check the obtained response.

Make a Post call – Making a Post call needs a little extra details. Now for making a post call we need the JSON data. We can both create a file with JSON data and pass that to the file or we can include the JSON data at the same place. Lets create a JSON file first as

{
    "data": {
        "repoType": "proxy",
        "id": "somerepo1",
        "name": "Some Repo Name1",
        "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
        }
    }
}

Save the file as proxy.json and move the file to the files location. Now we can use the proxy.json using the lookup in ansible as,

- name: Make a Proxy Repository
  uri:
    url: "{{ nexus_url }}/repositories"
    method: POST
    body: "{{ lookup('file','proxy.json') }}"
    user: admin
    password: admin123
    force_basic_auth: yes
    body_format: json
    HEADER_Content-Type: application/json
    HEADER_Accept: application/json,version=2
    return_content: yes
    status_code: 201

IN the above snippet I passed the JSON file an argument to the  Ansible lookup. Besides this I passed the user name , password and most importantly the body_format , Header fields etc. We also need to pass the return code of the Rest call so that Ansible will compare the return status code with the status code that we defined. In the above example I passed the status code as 201. In this case the Ansible will check the rest call status code and passed status code to check if that is success or not. Even though the rest call is success if the status codes are not same Ansible will throw an error.

If we want to pass the JSON format in the body itself without using the file, we can use

- name: Make a Hosted Repository
  uri:
    url: "{{nexus_url}}/{{ nexus_endpoint }}/{{ nexus_repositories_service }}"
    method: POST
    body: >
     {
      data: {
        "repoType": "{{ hosted_repo_data.repoType }}",
        "id": "{{ repoId }}",
        "name": "{{ repoName }}",
        "provider": "{{ repoProvider}}",
        "browseable": {{ hosted_repo_data.browseable }},
        "repoPolicy": {{ hosted_repo_data.repoPolicy }},
        "providerRole": {{ hosted_repo_data.providerRole }}
           }
     }
    user: "{{ nexus_admin }}"
    password: "{{ nexus_admin_password }}"
    force_basic_auth: yes
    body_format: json
    HEADER_Content-Type: application/json
    HEADER_Accept: application/json,version=2
    return_content: yes
    status_code: 201

The body can be added directory to the Body method in the Ansible JSON call.

That’s all about Ansible URL module and rest calls. Hope this helps. 
Read More