def create_jobdefinition(dci_context, components, test_ids, topic_id, jobdef_name=None): # If at least one component doesn't exist in the database then a new # jobdefinition must be created. at_least_one = False component_ids = [] names = [] for cmpt in components: names.append(cmpt['name']) created_cmpt = component.create(dci_context, **cmpt) if created_cmpt.status_code == 201: at_least_one = True elif created_cmpt.status_code == 422: created_cmpt = component.get(dci_context, cmpt['name']) created_cmpt_name = created_cmpt.json()['component']['name'] component_ids.append(created_cmpt.json()['component']['id']) if at_least_one: if jobdef_name is None: jobdef_name = created_cmpt_name jobdef = jobdefinition.create(dci_context, jobdef_name, topic_id) if jobdef.status_code == 201: jobdef_id = jobdef.json()['jobdefinition']['id'] for cmpt_id in component_ids: jobdefinition.add_component(dci_context, jobdef_id, cmpt_id) for test_id in test_ids: jobdefinition.add_test(dci_context, jobdef_id, test_id) print("Jobdefinition '%s' created." % jobdef_name) else: print("Error on jobdefinition creation: '%s'", jobdef.json()) else: print("No jobdefinition created.")
def create(context, name, topic_id, priority, component_types): """create(context, name, priority, topic_id) Create a jobdefinition. >>> dcictl jobdefinition-create [OPTIONS] :param string name: Name of the jobdefinition [required] :param string topic_id: ID of the topic for this jobdefinition [required] :param integer priority: Priority for this jobdefinition """ if component_types: component_types = component_types.split(',') result = jobdefinition.create(context, name=name, priority=priority, topic_id=topic_id, component_types=component_types) utils.format_output(result, context.format, None, jobdefinition.TABLE_HEADERS)
def test_job_list(runner, dci_context, team_id, topic_id, remoteci_id, component_id): kwargs = {'name': 'tname', 'topic_id': topic_id, 'component_types': ['git_review']} jd = jobdefinition.create(dci_context, **kwargs).json() jobdefinition_id = jd['jobdefinition']['id'] kwargs = {'name': 'test_jobdefinition', 'team_id': team_id} test_id = test.create(dci_context, **kwargs).json()['test']['id'] jobdefinition.add_test(dci_context, jobdefinition_id, test_id) kwargs = {'name': 'test_remoteci', 'team_id': team_id} test_id = test.create(dci_context, **kwargs).json()['test']['id'] remoteci.add_test(dci_context, remoteci_id, test_id) job_id = job.schedule( dci_context, remoteci_id, topic_id).json()['job']['id'] result = runner.invoke(['job-list-test', job_id]) assert len(result['tests']) == 2 assert result['tests'][0]['name'] == 'test_jobdefinition' assert result['tests'][1]['name'] == 'test_remoteci'
def job_id(dci_context): my_team = team.create(dci_context, name='tname').json()['team'] my_remoteci = remoteci.create(dci_context, name='tname', team_id=my_team['id'], data={'remoteci': 'remoteci'}).json() my_remoteci_id = my_remoteci['remoteci']['id'] my_test = test.create( dci_context, name='tname', data={'test': 'test'}).json() my_test_id = my_test['test']['id'] my_jobdefinition = jobdefinition.create( dci_context, name='tname', test_id=my_test_id).json() my_component = component.create( dci_context, name='hihi', type='git_review', data={'component': 'component'}).json() my_component_id = my_component['component']['id'] jobdefinition.add_component(dci_context, my_jobdefinition['jobdefinition']['id'], my_component_id) my_job = job.schedule(dci_context, my_remoteci_id).json() return my_job['job']['id']
components = [get_khaleesi_component(), get_khaleesi_settings_component(), get_khaleesi_installer_component(), get_khaleesi_puddle_component()] # If at least one component doesn't exist in the database then a new # jobdefinition must be created. at_least_one = False component_ids = [] for cmpt in components: created_cmpt = component.create(dci_context, **cmpt) if created_cmpt.status_code == 201: print("Create component '%s', type '%s'" % (cmpt['name'], cmpt['type'])) component_ids.append(created_cmpt.json()['component']['id']) at_least_one = True if at_least_one: jobdef_name = "Khaleesi - OSP 8 - FV2" jobdef = jobdefinition.create(dci_context, jobdef_name, khaleesi_tempest_test_id) if jobdef.status_code == 201: jobdef_id = jobdef.json()['jobdefinition']['id'] for cmpt_id in component_ids: jobdefinition.add_component(dci_context, jobdef_id, cmpt_id) print("Jobdefinition '%s' created." % jobdef_name) else: print("Error on jobdefinition creation: '%s'", jobdef.json()) else: print("No jobdefinition created.")
def main(): module = AnsibleModule( argument_spec=dict( state=dict(default='present', choices=['present', 'absent'], type='str'), # Authentication related parameters # dci_login=dict(required=False, type='str'), dci_password=dict(required=False, type='str'), dci_cs_url=dict(required=False, type='str'), # Resource related parameters # id=dict(type='str'), name=dict(type='str'), priority=dict(type='int'), topic_id=dict(type='str'), active=dict(type='bool'), comment=dict(type='str'), component_types=dict(type='list'), ), ) 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 jobdefinitions # Endpoint called: /jobdefinitions GET via dci_jobdefinition.list() # # List all jobdefinitions if module_params_empty(module.params): res = dci_jobdefinition.list(ctx) # Action required: Delete the jobdefinition matching jobdefinition id # Endpoint called: /jobdefinitions/<jobdefinition_id> DELETE via dci_jobdefinition.delete() # # If the jobdefinition exists and it has been succesfully deleted the changed is # set to true, else if the jobdefinition 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_jobdefinition.get(ctx, module.params['id']) if res.status_code not in [400, 401, 404, 409]: kwargs = { 'id': module.params['id'], 'etag': res.json()['jobdefinition']['etag'] } res = dci_jobdefinition.delete(ctx, **kwargs) # Action required: Retrieve jobdefinition informations # Endpoint called: /jobdefinitions/<jobdefinition_id> GET via dci_jobdefinition.get() # # Get jobdefinition informations elif module.params[ 'id'] and not module.params['comment'] and not module.params[ 'component_types'] and module.params['active'] is None: res = dci_jobdefinition.get(ctx, module.params['id']) # Action required: Update an existing jobdefinition # Endpoint called: /jobdefinitions/<jobdefinition_id> PUT via dci_jobdefinition.update() # # Update the jobdefinition with the specified characteristics. elif module.params['id']: res = dci_jobdefinition.get(ctx, module.params['id']) if res.status_code not in [400, 401, 404, 409]: kwargs = { 'id': module.params['id'], 'etag': res.json()['jobdefinition']['etag'] } if module.params['comment']: kwargs['comment'] = module.params['comment'] if module.params['component_types']: kwargs['component_types'] = module.params['component_types'] if module.params['active'] is not None: kwargs['active'] = module.params['active'] res = dci_jobdefinition.update(ctx, **kwargs) # Action required: Creat a jobdefinition with the specified content # Endpoint called: /jobdefinitions POST via dci_jobdefinition.create() # # Create the new jobdefinition. else: if not module.params['name']: module.fail_json(msg='name parameter must be specified') if not module.params['topic_id']: module.fail_json(msg='topic_id parameter must be specified') kwargs = { 'name': module.params['name'], 'topic_id': module.params['topic_id'] } if module.params['priority']: kwargs['priority'] = module.params['priority'] if module.params['comment']: kwargs['comment'] = module.params['comment'] if module.params['component_types']: kwargs['component_types'] = module.params['component_types'] if module.params['active'] is not None: kwargs['active'] = module.params['active'] res = dci_jobdefinition.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['changed'] = False else: result['changed'] = True except: result = {} result['changed'] = True module.exit_json(**result)
def create(context, name, test_id, priority): result = jobdefinition.create(context, name=name, test_id=test_id, priority=priority) utils.format_output(result.json(), context.format, jobdefinition.RESOURCE[:-1])