Create and attach routing rule via Groovy scripting

Hello all,
I’ve been able to create, using the Nexus groovy scripts (documented in Nexus 3 Groovy Script development environment setup – Sonatype Support), some repos, roles, users and blobstores.

Now I’m trying to add routing-rules.

It’s pretty easy to create one; such code evaluated fine from Groovy:

RoutingRule routingRule =  new RoutingRule("com.company only API", "only com.company content", RoutingMode.BLOCK, ["^(?!/com/company)/.*\$"])

The part I could not figure out was how to persist this rule and attach it to an existing repository.

I thought it could have been as easy as adding configuration to an existing repository, something like:

Configuration mavenPublicConfiguration = repository.getRepositoryManager().get("maven-public").getConfiguration()["routing-rule"]= new RoutingRule()

But no, the closest thing I found was Configuration.setRoutingRuleId(EntityId id) and unfortunately, getting this id does not seem to be straightforward.

I could have @Injected services to my groovy code, but it felt like I was going too far…

The Rest API seems to have a resource for routing-rules(RoutingRulesApiResource) , but there does not seem to be an API to attach the routing rule to an existing repo.

Nexus community, is there a way to add and attach a routing rule to an existing repo via scripting or Rest API ?

Thank you!

You need to persist your routing rule using the RoutingRuleStore, once you do this you can use EntityHelper.id(routingRule) to obtain the EntityId to set on the configuration.

However you should also not modify the configuration that you get from a Repository, that won’t persist any change and in many cases the rest of Nexus will not be aware of your modification. Use Configuration.copy() to create a copy of the existing configuration, make your modifications, then use RepositoryManager.update()

We’ve added a few REST APIs for creating and modifying repositories but not all formats are complete yet.