Example #1
0
def create_user(name, directory_service):

    client = get_api_client()

    params = {"length": 1000}
    user_name_uuid_map = client.user.get_name_uuid_map(params)

    if user_name_uuid_map.get("name"):
        LOG.error("User with name {} already exists".format(name))
        sys.exit(-1)

    user_payload = {
        "spec": {
            "resources": {
                "directory_service_user": {
                    "user_principal_name":
                    name,
                    "directory_service_reference":
                    Ref.DirectoryService(directory_service),
                }
            }
        },
        "metadata": {
            "kind": "user",
            "spec_version": 0
        },
    }

    res, err = client.user.create(user_payload)
    if err:
        LOG.error(err)
        sys.exit(-1)

    res = res.json()
    stdout_dict = {
        "name": name,
        "uuid": res["metadata"]["uuid"],
        "execution_context": res["status"]["execution_context"],
    }
    click.echo(json.dumps(stdout_dict, indent=4, separators=(",", ": ")))

    LOG.info("Polling on user creation task")
    task_state = watch_task(res["status"]["execution_context"]["task_uuid"],
                            poll_interval=5)
    if task_state in ERGON_TASK.FAILURE_STATES:
        LOG.exception("User creation task went to {} state".format(task_state))
        sys.exit(-1)

    # Update users in cache
    LOG.info("Updating users cache ...")
    Cache.sync_table(cache_type=CACHE.ENTITY.USER)
    LOG.info("[Done]")
Example #2
0
def create_user(name, directory_service):

    client = get_api_client()

    params = {"length": 1000}
    user_name_uuid_map = client.user.get_name_uuid_map(params)

    if user_name_uuid_map.get("name"):
        LOG.error("User with name {} already exists".format(name))
        sys.exit(-1)

    user_payload = {
        "spec": {
            "resources": {
                "directory_service_user": {
                    "user_principal_name":
                    name,
                    "directory_service_reference":
                    Ref.DirectoryService(directory_service),
                }
            }
        },
        "metadata": {
            "kind": "user",
            "spec_version": 0
        },
    }

    res, err = client.user.create(user_payload)
    if err:
        LOG.error(err)
        sys.exit(-1)

    res = res.json()
    stdout_dict = {
        "name": name,
        "uuid": res["metadata"]["uuid"],
        "execution_context": res["status"]["execution_context"],
    }
    click.echo(json.dumps(stdout_dict, indent=4, separators=(",", ": ")))