joelee2012 / api4jenkins

Python sync/async client library for Jenkins API
https://api4jenkins.readthedocs.io/
Apache License 2.0
91 stars 22 forks source link

Unable to create agent/node #106

Open NicoR45 opened 3 days ago

NicoR45 commented 3 days ago

Hi, I'm trying to create an agent.

. Version
Jenkins 2.426.3
Python 3.12.7
api4jenkins 2.0.3

My code is:

from api4jenkins import Jenkins

j = Jenkins(url_base, auth=(MyUser, MyPassword), timeout=10)

j.nodes.create(r"{'nodeDescription': '', 'numExecutors': 1, 'remoteFS': '/home/jenkins', 'labelString': '', 'mode': 'NORMAL', 'retentionStrategy': {'stapler-class': 'hudson.slaves.RetentionStrategy$Always'}, 'nodeProperties': {'stapler-class-bag': 'true'}, 'launcher': {'stapler-class': 'hudson.slaves.JNLPLauncher'}}'")

Returned traceback :

Traceback (most recent call last):
  File "b:\MyPath\python\test.py", line 58, in <module>
    j.nodes.create(r"{'nodeDescription': '', 'numExecutors': 1, 'remoteFS': '/home/jenkins', 'labelString': '', 'mode': 'NORMAL', 'retentionStrategy': {'stapler-class': 'hudson.slaves.RetentionStrategy$Always'}, 'nodeProperties': {'stapler-class-bag': 'true'}, 'launcher': {'stapler-class': 'hudson.slaves.JNLPLauncher'}}'")
  File "C:\Users\MyUser\bin\python-3.12.7\inst\Lib\site-packages\api4jenkins\node.py", line 84, in create
    self.handle_req('POST', 'doCreateItem',
  File "C:\Users\MyUser\bin\python-3.12.7\inst\Lib\site-packages\api4jenkins\item.py", line 98, in handle_req
    return self._request(method, self.url + entry, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MyUser\bin\python-3.12.7\inst\Lib\site-packages\httpx\_client.py", line 837, in request
    return self.send(request, auth=auth, follow_redirects=follow_redirects)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MyUser\bin\python-3.12.7\inst\Lib\site-packages\httpx\_client.py", line 926, in send
    response = self._send_handling_auth(
               ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MyUser\bin\python-3.12.7\inst\Lib\site-packages\httpx\_client.py", line 954, in _send_handling_auth
    response = self._send_handling_redirects(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MyUser\bin\python-3.12.7\inst\Lib\site-packages\httpx\_client.py", line 1011, in _send_handling_redirects
    raise exc
  File "C:\Users\MyUser\bin\python-3.12.7\inst\Lib\site-packages\httpx\_client.py", line 994, in _send_handling_redirects
    hook(response)
  File "C:\Users\MyUser\bin\python-3.12.7\inst\Lib\site-packages\api4jenkins\http.py", line 29, in check_response
    raise BadRequestError(f'400 {response.headers["X-Error"]}')
api4jenkins.exceptions.BadRequestError: 400 ':' est un caractère dangereux #translation --> "is a dangerous character"

What have I done wrong? What character ':' does the error message refer to?

joelee2012 commented 3 days ago

@NicoR45 you should pass the dict instead of string.

from api4jenkins import Jenkins

j = Jenkins(url_base, auth=(MyUser, MyPassword), timeout=10)
args = {'nodeDescription': '', 'numExecutors': 1, 'remoteFS': '/home/jenkins', 'labelString': '', 'mode': 'NORMAL', 'retentionStrategy': {'stapler-class': 'hudson.slaves.RetentionStrategy$Always'}, 'nodeProperties': {'stapler-class-bag': 'true'}, 'launcher': {'stapler-class': 'hudson.slaves.JNLPLauncher'}}
j.nodes.create(**args)
joelee2012 commented 3 days ago

here you can find example: https://api4jenkins.readthedocs.io/en/latest/user/example.html#node

NicoR45 commented 2 days ago

Hi, Indeed... although I thought I'd tried with the arguments in a dict. I don't know what I've done :confused:

For those interested in this small issue, I had to add the 'name' argument to the dict:

Traceback (most recent call last):
  File "b:\MyPath\python\test.py", line 59, in <module>
    j.nodes.create(**args)
TypeError: Nodes.create() missing 1 required positional argument: 'name'

After correction:

args = {'name': 'MyAgent test', 'nodeDescription': '', 'numExecutors': 1, 'remoteFS': '/home/jenkins', 'labelString': '', 'mode': 'NORMAL', 'retentionStrategy': {'stapler-class': 'hudson.slaves.RetentionStrategy$Always'}, 'nodeProperties': {'stapler-class-bag': 'true'}, 'launcher': {'stapler-class': 'hudson.slaves.JNLPLauncher'}}
j.nodes.create(**args)

Thank you joelee2012 :thumbsup: