jenkinsci / lockable-resources-plugin

Lock resources against concurrent use
https://plugins.jenkins.io/lockable-resources
MIT License
87 stars 185 forks source link

Feature request: ability to add/remove lockable resources through API #103

Open tstibbs opened 6 years ago

tstibbs commented 6 years ago

It would be really useful to able to add/remove lockable resources via an API (either REST or through the groovy console). This would enable the locks to represent real world non-static things - i.e. as you scale your infrastructure, you can scale your locks to reflect this.

simontunnat commented 4 years ago

I also would like some kind of REST API for this, so I could lock stuff in one Jenkins instance from a another Jenkins instance.

I can use the GET calls that the lockable resources frontend uses, but i don't think it was intended for that kind of usage: https://jenkins/lockable-resources/reserve?resource=lockname https://jenkins/lockable-resources/unreserve?resource=lockname

When I used the "reserve" endpoint it would always return with status code 302, even if the lock had already been locked by someone else. The "unreserve" endpoint on the other side does fail with status code 403 if the lock is not currently locked or locked by someone else (the get call was performed with a none admin user).

alexandre-perrin commented 4 years ago

I also wish there was also a REST API for this. In the meantime I created a python API to control lockable resources (reserve, unreserve, list etc). https://pypi.org/project/jenkins-lockable-resources/

Hope that can help.

odin- commented 3 years ago

I also would like a REST API. We have a resource that can be used externally from outside Jenkins. E.g. I would like to lock the resource from outside Jenkins.

bcamishrapr commented 2 years ago

@odin- For the time being , you can put all the logic in a groovy code (groovy code support locking functionality) in a scripted or a declarative pipeline and then call your job remotely using curl or from some other point.

jimklimov commented 2 years ago

FWIW, a PR to define an /api endpoint was recently merged (with no further details at that time, just exposing whatever is properly marked in the codebase).

There is also JCasC support to pre-define resources, and a way to define "ephemeral" resources by just trying to lock a non-existing one.

Other than that, groovy code, scripted pipelines and Jenkins Shared Libraries are in position to import the LockableResourcesManager and/or LockableResource classes and call their methods for lower-level manipulations.

gaspardpetit commented 2 years ago

This was recently closed, but I'm also pushing for something similar in https://github.com/jenkinsci/lockable-resources-plugin/pull/305 with a updateLock step. Yes the LockableResourcesManager can be pulled into groovy, but using it directly is not a great experience. The updateLock would either set attributes to an existing lock or create a new one (not ephemeral) if it doesn't exist. I'd be happy to hear feedback to make it work with your use cases as well.

vishnugovind commented 9 months ago

Is there any API documentation on what is supported?

JonasScharpf commented 3 weeks ago

You can create a new resource with this Groovy Code and finally get the list of all available resources with this code snippet

import org.jenkins.plugins.lockableresources.LockableResourcesManager

def manager = org.jenkins.plugins.lockableresources.LockableResourcesManager.get();

// Create a new resource with a name and optional labels
def resourceName = "MyNewResource";
def resourceLabels = "my-label1 my-label2"; // labels are space separated

// see https://github.com/jenkinsci/lockable-resources-plugin/blob/1f0dff5784766ab0925d4024ec6211a1969c0598/src/main/java/org/jenkins/plugins/lockableresources/LockableResourcesManager.java#L831
def newResource = manager.createResourceWithLabel(resourceName, resourceLabels);

// Save the new resource
manager.save();

// print all resources
println("All resources: ${manager.getResources()}");