I have created a groovy script that is used for updating the a proxy repositories blocked status. The intent here is I need to automate sometimes flipping a handful of proxies to being blocked (my understanding is this will treat the proxy similar to hosted).
This is the script I had created:
import org.sonatype.nexus.repository.config.Configuration
import org.sonatype.nexus.repository.Repository
import org.sonatype.nexus.repository.manager.RepositoryManager
import groovy.json.JsonOutput;
import groovy.json.JsonSlurper;
parsed_args = new JsonSlurper().parseText(args)
def repositoryName = parsed_args.repositoryName
def isBlocked = parsed_args.isBlocked
def repositoryManager = container.lookup(org.sonatype.nexus.repository.manager.RepositoryManager.class.name)
def repository = repositoryManager.get(repositoryName)
if (repository == null) {
throw new IllegalStateException("Repository not found; unable to block. name: " + repositoryName)
}
if (!"proxy".equalsIgnoreCase(repository.getType().getValue())) {
throw new IllegalStateException("Repository is not a proxy repository; unable to block. name: " + repository.getName() + "; type: " + repository.getType().getValue())
}
def configuration = repository.getConfiguration()
if (configuration == null) {
throw new IllegalStateException("Repository configuration is null; unable to block. name: " + repository.getName())
}
def attributes = configuration.getAttributes()
if (attributes == null) {
throw new IllegalStateException("Repository attributes are null; unable to block. name: " + repository.getName())
}
def httpclient = attributes.get("httpclient")
if (httpclient == null) {
throw new IllegalStateException("Repository httpclient attributes are null; unable to block. name: " + repository.getName())
}
httpclient.put("blocked", isBlocked)
def result = [
"params.repositoryName": repositoryName,
"params.isBlocked": isBlocked,
"httpclient.blocked": httpclient.get("blocked"),
"result": httpclient.get("blocked") == isBlocked]
return JsonOutput.toJson(result)
When I run this script the response is what I would expect:
{
"name": "block-proxy-repository",
"result": "{\"params.repositoryName\":\"my-private-proxy\",\"params.isBlocked\":true,\"httpclient.blocked\":true,\"result\":true}"
}
When I query from the rest api the value is flipped:
"httpClient": {
"blocked": true,
"autoBlock": false,
...
But when I check the GUI in nexus the checkbox is not checked:
When I restart the nexus, the GUI remains the same and the rest api returns a reverted change:
"httpClient": {
"blocked": false,
"autoBlock": false,
...
This leads me to believe I am missing something in how to persist the repository change.
I know I could do this through the Rest API; the problem is I dont want to modify its httpClient.authentication
parameters. What I am trying to do is: here is a list of proxies, flip there blocked status. I should be able to modify a single field without having to redefine the entire repository (this is why I went with scripting because from what I can tell you can’t modify single fields via the rest api without redefining everything). I would also prefer my automation script not require a pulldown from our secret vault each time I need to do an update.
I couldn’t find an example of this in nexus-scripting-examples/nexus-script-example at master · sonatype-nexus-community/nexus-scripting-examples · GitHub so I pieced together a bunch of this reviewing
- nexus-public/components/nexus-repository-config/src/main/java/org/sonatype/nexus/repository/manager/RepositoryManager.java at main · sonatype/nexus-public · GitHub
- nexus-public/components/nexus-repository-config/src/main/java/org/sonatype/nexus/repository/config/Configuration.java at main · sonatype/nexus-public · GitHub
- nexus-public/components/nexus-repository-config/src/main/java/org/sonatype/nexus/repository/Repository.java at main · sonatype/nexus-public · GitHub
I want to avoid the complexity of recreating/reconfiguring the entirity of the repository when flipping a status.