Exemplo n.º 1
0
async def createGroup():
    """ create a new group and link it to the parent group with 
    link name of group name
    """
    client = globals["client"]
    domain = globals["domain"]
    params = {"host": domain}
    base_req = getEndpoint()
    headers = getRequestHeaders()

    # create a new group
    req = base_req + "/groups"
    log.info("POST:" + req)
    globals["grp_request_count"] += 1
    group_name = globals["grp_request_count"]
    timeout = config.get("timeout")
    async with client.post(req,
                           headers=headers,
                           timeout=timeout,
                           params=params) as rsp:
        if rsp.status != 201:
            log.error("POST {} failed with status: {}, rsp: {}".format(
                req, rsp.status, str(rsp)))
            globals["grp_failed_posts"] += 1
            raise HttpProcessingError(code=rsp.status,
                                      message="Unexpected error")
        else:
            globals["group_count"] += 1
            log.info("group_count: {}".format(globals["group_count"]))
        group_json = await rsp.json()
        group_id = group_json["id"]

    # link group to parent
    root_id = globals["root"]
    group_name = "group_{}".format(group_name)
    req = base_req + "/groups/" + root_id + "/links/" + group_name
    data = {"id": group_id}
    log.info("PUT " + req)
    globals["lnk_request_count"] += 1
    async with client.put(req,
                          data=json.dumps(data),
                          headers=headers,
                          timeout=timeout,
                          params=params) as rsp:
        if rsp.status == 409:
            # another task has created this link already
            log.warn("got 409 in request: " + req)
        elif rsp.status != 201:
            globals["lnk_failed_posts"] += 1
            log.error("got http error: {} for request: {}, rsp: {}".format(
                rsp.status, req, rsp))
            raise HttpProcessingError(code=rsp.status,
                                      message="Unexpected error")
        else:
            link_created = True

    return group_id
Exemplo n.º 2
0
async def createGroup(parent_group, group_name):
    """ create a new group and link it to the parent group with 
    link name of group name
    """
    client = globals["client"]
    domain = globals["domain"]
    params = {"host": domain}
    base_req = getEndpoint()
    headers = getRequestHeaders()
    timeout = config.get("timeout")

    # TBD - replace with atomic create & link operation?
    
    # create a new group
    req = base_req + "/groups"
    log.info("POST:" + req)
    globals["request_count"] += 1
    async with client.post(req, headers=headers, params=params, timeout=timeout) as rsp:
        if rsp.status != 201:
            log.error("POST {} failed with status: {}, rsp: {}".format(req, rsp.status, str(rsp)))
            raise HttpProcessingError(code=rsp.status, message="Unexpected error")
        group_json = await rsp.json()
        group_id = group_json["id"]

    # link group to parent
    req = base_req + "/groups/" + parent_group + "/links/" + group_name
    data = {"id": group_id }
    link_created = False
    log.info("PUT " + req)
    globals["request_count"] += 1
    async with client.put(req, data=json.dumps(data), headers=headers, params=params, timeout=timeout) as rsp:
        if rsp.status == 409:
            # another task has created this link already
            log.warn("got 409 in request: " + req)
        elif rsp.status != 201:
            log.error("got http error: {} for request: {}, rsp: {}".format(rsp.status, req, rsp))
            raise HttpProcessingError(code=rsp.status, message="Unexpected error")
        else:
            link_created = True

    if not link_created:
        # fetch the existing link and return the group 
        log.info("GET " + req)
        globals["request_count"] += 1
        async with client.get(req, headers=headers, params=params, timeout=timeout) as rsp:
            if rsp.status != 200:
                log.warn("unexpected error (expected to find link) {} for request: {}".format(rsp.status, req))
                raise HttpProcessingError(code=rsp.status, message="Unexpected error")
            else:
                rsp_json = await rsp.json()
                link_json = rsp_json["link"]
                if link_json["class"] != "H5L_TYPE_HARD":
                    raise ValueError("Unexpected Link type: {}".format(link_json))
                group_id = link_json["id"]
    
    return group_id                
Exemplo n.º 3
0
async def verifyDomain(domain):
    """ create domain if it doesn't already exist
    """
    params = {"host": domain}
    headers = getRequestHeaders()
    client = globals["client"]
    req = getEndpoint() + '/'
    root_id = None
    log.info("GET " + req)
    timeout = config.get("timeout")
    async with client.get(req, headers=headers, timeout=timeout,
                          params=params) as rsp:
        if rsp.status == 200:
            domain_json = await rsp.json()
        else:
            log.info("got status: {}".format(rsp.status))
    if rsp.status == 200:
        root_id = domain_json["root"]
    elif rsp.status == 404:
        # create the domain
        setupDomain(domain)
        async with client.get(req,
                              headers=headers,
                              timeout=timeout,
                              params=params) as rsp:
            if rsp.status == 200:
                domain_json = await rsp.json()
                root_id = domain_json["root"]
            else:
                log.error("got status: {} for GET req: {}".format(
                    rsp.status, req))
                raise HttpProcessingError(code=rsp.status,
                                          message="Service error")
    globals["root"] = root_id
Exemplo n.º 4
0
async def verifyGroupPath(h5path):
    """ create any groups along the path that doesn't exist
    """
    #print("current task: ", asyncio.Task.current_task())
    client = globals["client"]
    domain = globals["domain"]
    h5path_cache = globals["h5path_cache"]
    params = {"host": domain}
    parent_group = h5path_cache['/']  # start with root
    group_names = h5path.split('/')
    
    headers = getRequestHeaders()
    
    base_req = getEndpoint() + '/groups/'
    next_path = '/'
    timeout = config.get("timeout")

    for group_name in group_names:
        if not group_name:
            continue  # skip empty names
        next_path += group_name
        if not next_path.endswith('/'):
            next_path += '/'  # prep for next roundtrips
        if next_path in h5path_cache:
            # we already have the group id
            parent_group = h5path_cache[next_path]    
            continue
        
        req = base_req + parent_group + "/links/" + group_name
        log.info("GET " + req)
        globals["request_count"] += 1
        async with client.get(req, headers=headers, params=params, timeout=timeout) as rsp:
            if rsp.status == 404:
                parent_group = await createGroup(parent_group, group_name)
            elif rsp.status != 200:
                raise HttpProcessingError(code=rsp.status, message="Unexpected error")
            else:
                rsp_json = await rsp.json()
                link_json = rsp_json["link"]
                if link_json["class"] != "H5L_TYPE_HARD":
                    raise ValueError("Unexpected Link type: {}".format(link_json))
                parent_group = link_json["id"]
                h5path_cache[next_path] = parent_group
    
    return parent_group