无法使用Python通过Rundeck API检索作业

时间:2017-04-07 15:20:00

标签: python rundeck

我正在探索rundeck api以返回一份工作列表。我通过UI手动创建了两个作业。但是,我最终通过该api获得了0个工作。我很难弄清楚这个过程中发生了什么。

以下是我的代码示例:

import urllib2
import logging
import ssl
RUNDECK_TOKEN = 'My_API_Token'

class Rundeck():
    def get_full_url(self):
        return 'https://rdurl:8443/rundeckpro/api/14/project/Sandbox/jobs?authtoken=' + RUNDECK_TOKEN


    def make_api_call(self):
        context = ssl._create_unverified_context()
        opener = urllib2.build_opener(urllib2.HTTPSHandler(context=context))
        request = urllib2.Request(self.get_full_url())
        try:
            return opener.open(request, timeout=30)
        except urllib2.HTTPError as err:
            if err.code == 409:
                return 'Running'
        except Exception as e:
            logging.error('Exception : %s', e)
            return False


if __name__ == '__main__':
    jobs = Rundeck()
    print jobs.get_full_url()
    print jobs.make_api_call().read()

输出结果为:

<jobs count='0' />

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

你的脚本对我来说非常好。

检查您的Rundeck ACL,您的api密钥所有者可能没有您创建的作业的读取权限。

测试输出:

tmp $ python test.py 
http://localhost:4440/api/14/project/TestProject/jobs?authtoken=OyFXX1q4UzhTUe7deOUIPJKkrUnEwZlo
<jobs count='3'>
  <job id='63385df9-8898-48e9-ae8a-047b548915cd' href='http://localhost:4440/api/18/job/63385df9-8898-48e9-ae8a-047b548915cd' permalink='http://localhost:4440/project/TestProject/job/show/63385df9-8898-48e9-ae8a-047b548915cd'>
    <name>TEST</name>
    <group />
    <project>TestProject</project>
    <description></description>
  </job>
  <job id='02a41aaa-eb50-4831-8762-80b798468cbe' href='http://localhost:4440/api/18/job/02a41aaa-eb50-4831-8762-80b798468cbe' permalink='http://localhost:4440/project/TestProject/job/show/02a41aaa-eb50-4831-8762-80b798468cbe'>
    <name>TestJob</name>
    <group />
    <project>TestProject</project>
    <description>TestJob Description</description>
  </job>
  <job id='9b2ac9e9-0350-4494-a463-b43ba1e458ab' href='http://localhost:4440/api/18/job/9b2ac9e9-0350-4494-a463-b43ba1e458ab' permalink='http://localhost:4440/project/TestProject/job/show/9b2ac9e9-0350-4494-a463-b43ba1e458ab'>
    <name>TestJob2</name>
    <group />
    <project>TestProject</project>
    <description></description>
  </job>
</jobs>