示例#1
0
def delete_objects_cli(object_name, env, chunk_size=300):
    client = config_from_env(env=env)
    client.login()
    del_obj_info = get_object_info(client, object_name)
    row_count = 1
    total_del = 0
    current_page = 1
    while row_count > 0:
        try:
            del_dict = search_object(client,
                                     object_id=del_obj_info.busObId,
                                     fields=["RecId"],
                                     pageSize=chunk_size,
                                     pageNumber=current_page).json()
            row_count = len(del_dict['businessObjects'])
            click.echo("{} of {} records found".format(row_count,
                                                       del_dict['totalRows']))
            if row_count > 0:
                del_requests = create_delete_requests(
                    del_obj_info, del_dict['businessObjects'])
                r = client.post("api/V1/deletebusinessobjectbatch",
                                data=del_requests)
                logger.debug(r.content)
            total_del += row_count
            current_page += 1
            click.echo("{} objects deleted".format(total_del))
        except Exception as e:
            row_count = 0
            click.echo("expection {}".format(e))
    return
示例#2
0
def get_schema_cli(object_name,
                   output_file=None,
                   env=None,
                   fields=None,
                   object_id=None,
                   **kwargs):
    logger.info(f'Current Env: {env}')
    client = config_from_env(env=env)

    client.login()
    obj_id = None
    if object_id:
        obj_id = object_id
    elif not object_name:
        object_name = click.prompt("Please type an object name")
    o = get_object_schema(client, object_name, obj_id)

    fd = o.fieldDefinitions

    if not output_file:
        # output_file = get_save_file_path()
        click.echo(o)
    else:
        with open(output_file, 'w') as out:
            out.writelines(o)

    return o
示例#3
0
def run_onestep_cli(object_name, env, onestep_name, scope, scope_owner):
    logger.info(f'Current Env: {env}')
    cfg = config_from_env(env=env)
    url_part = cfg.host[:cfg.host.find("API")]
    url = f"{url_part}Service/api.asmx?WSDL"
    client = create_client(url, cfg.user, cfg.password)
    response = run_one_step(client,
                            object_name_or_id=object_name,
                            scope=scope,
                            scope_owner=scope_owner,
                            onestep_name_or_id=onestep_name)
    return response
示例#4
0
def create_object(object_name,
                  ask_file,
                  input_file=None,
                  env=None,
                  cfg_file=None,
                  object_data=None):
    """

    :param cfg_file:
    :param object_name: name of the Cherwell object
    :param ask_file: pass this flag if you want to be prompted for filename
    :param input_file: filename of data for object you want to create
    :param env: variable storing location of configuration file
    :param object_data: key value pairs for creating object
    :return:
    """
    if env:
        client = config_from_env(env=env)
    elif cfg_file:
        client = config_from_file(cfg_file)
    else:
        click.BadParameter("stuff")
        click.echo(
            "You must provide cfg path (--cfg_path) or environment variable containing it (--cfg_var)"
        )
        return

    client.login()
    # if no file is provided but ask_file is passed then it prompts for file, if neither is provided
    # assumes that object data is provided, otherwise there is an error
    if not input_file:
        if ask_file:
            input_file = get_open_file_path()
        elif object_data:
            input_file = "temp_file.txt"
            create_temp_file(object_data, input_file)
        else:
            raise ValueError("no file or object data provided")

    # click.echo(input_file)
    response = update_object_from_file(client=client,
                                       file_name=input_file,
                                       object_name=object_name,
                                       delimiter=",",
                                       encoding='utf-9-sig')
    count = 0
    for r in response.json()['responses']:
        count += 1
        if r['hasError']:
            logger.debug("{}:{}".format(r, str(count)))
    return response
示例#5
0
def update_object_cli(object_name, file_name=None, env=None):
    client = config_from_env(env=env)
    client.login()
    if not file_name:
        file_name = get_open_file_path()
    click.echo(file_name)
    response = update_object_from_file(client=client,
                                       file_name=file_name,
                                       object_name=object_name,
                                       delimiter=",",
                                       encoding='utf-9-sig')
    count = 0
    for r in response.json()['responses']:
        count += 1
        if r['hasError']:
            click.echo("{}:{}".format(r, str(count)))
            logger.debug("{}:{}".format(r, str(count)))
    return response
示例#6
0
def search_object_cli(object_name,
                      output_file=None,
                      env=None,
                      fields="",
                      **kwargs):
    if not object_name:
        object_name = click.prompt("Please type an object name")
        if not object_name:
            click.MissingParameter("No Object Name was provided")
            return
    client = config_from_env(env=env)
    client.login()
    if not output_file:
        output_file = get_save_file_path()
    response = search_object(client,
                             object_name=object_name,
                             fields=fields,
                             **kwargs)
    data_dict = NameValueExtractor(response).create_dict()
    dict_to_csv(data_dict, data_dict[0].keys(), output_file, mode="w")

    return response