def schedule_job(topic_name, context): log.info('scheduling job on topic %s' % topic_name) topic_res = dci_topic.list(context, where='name:' + topic_name) if topic_res.status_code == 200: topics = topic_res.json()['topics'] log.debug('topics: %s' % topics) if len(topics) == 0: log.error('topic %s not found' % topic_name) sys.exit(1) topic_id = topics[0]['id'] schedule = dci_job.schedule(context, topic_id=topic_id) if schedule.status_code == 201: scheduled_job_id = schedule.json()['job']['id'] scheduled_job = dci_job.get(context, scheduled_job_id, embed='topic,remoteci,components') if scheduled_job.status_code == 200: job_id = scheduled_job.json()['job']['id'] dci_jobstate.create(context, status='new', comment='job scheduled', job_id=job_id) return scheduled_job.json() else: log.error('error getting schedule info: %s' % scheduled_job.text) else: log.error('error scheduling: %s' % schedule.text) else: log.error('error getting the list of topics: %s' % topic_res.text) return None
def get_topic(topic_name): context = build_signature_context() t = dci_topic.list(context, where="name:%s" % topic_name) t.raise_for_status() topics = t.json()["topics"] if len(topics) == 0: print("Ensure you have access to topic %s" % topic_name) print("Contact your EPM for more information.") return return topics[0]
def main(): module = AnsibleModule( argument_spec=dict( state=dict(default='present', choices=['present', 'absent'], type='str'), # Authentication related parameters # login=dict(required=False, type='str'), password=dict(required=False, type='str'), url=dict(required=False, type='str'), # Resource related parameters # id=dict(type='str'), name=dict(type='str'), label=dict(type='str'), ), ) if not dciclient_found: module.fail_json(msg='The python dciclient module is required') login, password, url = get_details(module) if not login or not password: module.fail_json(msg='login and/or password have not been specified') ctx = dci_context.build_dci_context(url, login, password, 'Ansible') # Action required: List all topics # Endpoint called: /topics GET via dci_topic.list() # # List all topics if module_params_empty(module.params): res = dci_topic.list(ctx) # Action required: Delete the topic matching topic id # Endpoint called: /topics/<topic_id> DELETE via dci_topic.delete() # # If the topic exists and it has been succesfully deleted the changed is # set to true, else if the topic does not exist changed is set to False elif module.params['state'] == 'absent': if not module.params['id']: module.fail_json(msg='id parameter is required') res = dci_topic.delete(ctx, module.params['id']) # Action required: Retrieve topic informations # Endpoint called: /topic/<topic_id> GET via dci_topic.get() # # Get topic informations elif module.params[ 'id'] and not module.params['name'] and not module.params['label']: res = dci_topic.get(ctx, module.params['id']) # Action required: Update an existing topic # Endpoint called: /topics/<topic_id> PUT via dci_topic.update() # # Update the topic with the specified characteristics. elif module.params['id']: res = dci_topic.get(ctx, module.params['id']) if res.status_code not in [400, 401, 404, 409]: kwargs = { 'id': module.params['id'], 'etag': res.json()['topic']['etag'] } if module.params['name']: kwargs['name'] = module.params['name'] if module.params['label']: kwargs['label'] = module.params['label'] res = dci_topic.update(ctx, **kwargs) # Action required: Creat a topic with the specified content # Endpoint called: /topics POST via dci_topic.create() # # Create the new topic. else: if not module.params['name']: module.fail_json(msg='name parameter must be specified') kwargs = {'name': module.params['name']} if module.params['label']: kwargs['label'] = module.params['label'] res = dci_topic.create(ctx, **kwargs) try: result = res.json() if res.status_code == 404: module.fail_json(msg='The resource does not exist') if res.status_code == 409: result = dci_topic.get(ctx, module.params['name']).json() result['changed'] = False else: result['changed'] = True except: result = {} result['changed'] = True module.exit_json(**result)
def list(context, args): params = { k: getattr(args, k) for k in ["sort", "limit", "offset", "where"] } return topic.list(context, **params)
# to get the last job of a remoteci, we loop over all job sorted by date # and get the first status to have the last status of a remoteci. print("[*] Get all jobs") ljobs = [ r for r in base.iter(gcontext, 'jobs', sort='-created_at', embed='components,jobdefinition', limit=128) ] # get all topics to get the association topic_id -> topic_name # which will be used below print("[*] Get all topics") ltopics = topic.list(gcontext).json()['topics'] topicids_to_name = {} for current_topic in ltopics: topicids_to_name[current_topic['id']] = current_topic['name'] # get all teams to get association team_id -> team_name print("[*] Get all teams") lteams = team.list(gcontext).json()['teams'] teams_to_name = {} for current_team in lteams: teams_to_name[current_team['id']] = current_team['name'] # get all remotecis to get the association remoteci_id -> remoteci_name # which will be used below print("[*] Get all remotecis") lremotecis = remoteci.list(gcontext).json()['remotecis']