# Create and attach routing rule via Groovy scripting

**URL:** <https://community.sonatype.com/t/create-and-attach-routing-rule-via-groovy-scripting/3073>\
**Category:** Sonatype Nexus Repository\
**Tags:** groovy, nexus-repository\
**Created:** [December 3, 2019, 3:49pm UTC](https://community.sonatype.com/t/create-and-attach-routing-rule-via-groovy-scripting/3073 "2019-12-03T15:49:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![anthonydahanne](https://yyz1.discourse-cdn.com/flex047/user_avatar/community.sonatype.com/anthonydahanne/32/962_2.png) [@anthonydahanne](https://community.sonatype.com/u/anthonydahanne)\
**Post date:** [December 3, 2019, 3:49pm UTC](https://community.sonatype.com/t/create-and-attach-routing-rule-via-groovy-scripting/3073/1 "2019-12-03T15:49:56Z")

</div>

Hello all,  
I’ve been able to create, using the Nexus groovy scripts (documented in [Nexus 3 Groovy Script development environment setup – Sonatype Support](https://support.sonatype.com/hc/en-us/articles/115015812727-Nexus-3-Groovy-Script-development-environment-setup)), 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 `@Inject`ed 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!

---

<div class="post-metadata">

**Author:** ![mpiggott](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@mpiggott](https://community.sonatype.com/u/mpiggott)\
**Post date:** [December 6, 2019, 7:24pm UTC](https://community.sonatype.com/t/create-and-attach-routing-rule-via-groovy-scripting/3073/2 "2019-12-06T19:24:49Z")

</div>

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.
