Creating repos via Rest API works; however some undesired settings are set

Hi all!

First post here. I’m a big fan of Nexus. I’m running the sonatype/nexus3:latest docker image with great success. I love the splash screen! I love most things about it to be honest. I just wish there was a dark mode :sunglasses: but I digress…

Last night I was inspired to script the creation of my nexus instance because of how heavily I rely on it. I’d like to be able to easily recreate my setup if ever needed. Here’s an example of what I’ve accomplished so far:

First, I used the Administration > System > API tab to gather the required JSON structure for each repo type, and subbed in my values.

Next, I wrote a simple Bash script to execute the POSTs via curl.

#!/bin/bash

# Nexus Instance & Credentials
IP="192.168.1.10"
PORT="8081"
USERNAME="admin"
PASSWORD="WebGUIPasswordHere"

# APT Repo Proxies
curl -u ${USERNAME}:${PASSWORD} -X POST "http://${IP}:${PORT}/service/rest/v1/repositories/apt/proxy" -H "Content-Type: application/json" -d @apt/proxy/apt-repo.json
curl -u ${USERNAME}:${PASSWORD} -X POST "http://${IP}:${PORT}/service/rest/v1/repositories/apt/proxy" -H "Content-Type: application/json" -d @apt/proxy/apt-salt.json

# Docker Hub Proxy
curl -u ${USERNAME}:${PASSWORD} -X POST "http://${IP}:${PORT}/service/rest/v1/repositories/docker/proxy" -H "Content-Type: application/json" -d @docker/proxy/docker.json

# Raw HTTP filesystems
curl -u ${USERNAME}:${PASSWORD} -X POST "http://${IP}:${PORT}/service/rest/v1/repositories/raw/hosted" -H "Content-Type: application/json" -d @raw/hosted/cloud-init.json
curl -u ${USERNAME}:${PASSWORD} -X POST "http://${IP}:${PORT}/service/rest/v1/repositories/raw/hosted" -H "Content-Type: application/json" -d @raw/hosted/vagrant.json

# Proxy of Saltstack Raw HTTP filesystem
curl -u ${USERNAME}:${PASSWORD} -X POST "http://${IP}:${PORT}/service/rest/v1/repositories/raw/proxy" -H "Content-Type: application/json" -d @raw/proxy/saltstack.json

# YUM/DNF Repo Proxies
curl -u ${USERNAME}:${PASSWORD} -X POST "http://${IP}:${PORT}/service/rest/v1/repositories/yum/proxy" -H "Content-Type: application/json" -d @yum/proxy/yum-repo.json
curl -u ${USERNAME}:${PASSWORD} -X POST "http://${IP}:${PORT}/service/rest/v1/repositories/yum/proxy" -H "Content-Type: application/json" -d @yum/proxy/yum-epel.json
curl -u ${USERNAME}:${PASSWORD} -X POST "http://${IP}:${PORT}/service/rest/v1/repositories/yum/proxy" -H "Content-Type: application/json" -d @yum/proxy/yum-salt.json

The above works great, with one caveat. Using the above apt-repo proxy repository as the example, all of the settings that would happily be blank if configured in the Web GUI (e.g. HTTP authentication) have “string” entered for username, password, etc. I tried “” but it complained that they cannot be blank.

Can someone point me in the right direction for how to use the JSON / POST to have these set up in a similar way to how they are when I point-and-click?

Many thanks, and keep up the great work.
-static

Hi static,
Glad to have you here. Try skipping the optional fields from your input JSON. When you browser the API section in Web UI it displays some example data that isn’t always necessary - it all depends on your individual case.

I think in your case I would suggest skipping the whole “authentication” block, “routingRule” and “replication” fields.

1 Like

Thanks for your reply! I really appreciate the help.

I just tried as you suggested (removing the appropriate commas, too) and that worked like a charm for all but this:

Is there a way to “uncheck” this option? I imagine it’s part of this block, but I can’t figure out what I should do:

"httpClient": {
    "blocked": false,
    "autoBlock": true,
    "connection": {
      "retries": 0,
      "userAgentSuffix": "string",
      "timeout": 60,
      "enableCircularRedirects": false,
      "enableCookies": false,
      "useTrustStore": false
    }
  }

Thanks again

Try dropping the “connection” block.

1 Like

You rock. They look perfect now! Thank you so much.

I hope the above helps anyone looking to “script” (in quotes because I’m not using the built-in groovy scripting) creation of repositories. If anyone would like to see more of the JSON files, please feel free to message me.