Exemplo n.º 1
0
 def rest_api_token(self, expiration=3600):
     __logger__.debug("getting token @ time %s", time.time())
     return get_response('{}/rest-server/api/v1/token'.format(self.pai_uri),
                         body={
                             'username': self.user,
                             'password': self.password,
                             'expiration': expiration
                         }).json()['token']
Exemplo n.º 2
0
 def process(self, args):
     __logger__.debug('Parsed arguments to %s', args)
     actor = self.actor if self.single_action else self.actions[args.action]
     actor.check_arguments(args)
     actor.restore(args)
     result = actor.do_action(args)
     actor.store(args)
     return result
Exemplo n.º 3
0
 def available_resources(self):
     my_virtual_clusters = self.rest_api_user()["virtualCluster"]
     if isinstance(my_virtual_clusters, str):
         my_virtual_clusters = my_virtual_clusters.split(",")
     __logger__.debug("user %s belongs to %s", self.user,
                      my_virtual_clusters)
     resources = self.virtual_cluster_available_resources()
     return {k: v for k, v in resources.items() if k in my_virtual_clusters}
Exemplo n.º 4
0
 def __init__(self, pai_uri: str=None, user: str=None, password: str=None, storages: list=[], default_storage_alias: str=None, **kwargs):
     __logger__.debug('creating cluster from info %s', get_args())
     self.pai_uri = pai_uri
     self.user = user
     self.password = password
     self.storages = storages
     self.default_storage_alias = default_storage_alias
     self.storage_clients = {}
     for k, v in kwargs.items():
         setattr(self, k, v)
     for cfg in storages:
         self.add_storage(**cfg)
     if len(storages) == 1:
         self.default_storage_alias = storages[0]["storage_alias"]
Exemplo n.º 5
0
def pretty_print_POST(req):
    """
    At this point it is completely built and ready
    to be fired; it is "prepared".

    However pay attention at the formatting used in
    this function because it is programmed to be pretty
    printed and may differ from the actual request.
    """
    __logger__.debug('{}\n{}\n{}\n\n{}'.format(
        '-----------START-----------',
        req.method + ' ' + req.url,
        '\n'.join('{}: {}'.format(k, v) for k, v in req.headers.items()),
        req.body,
    ))
Exemplo n.º 6
0
def to_file(obj, fname: str, fmt=None, **kwargs):
    if not fmt:
        _, ext = os.path.splitext(fname)
        if ext in __json_exts__:
            fmt, dic = json, dict(indent=4)
        elif ext in __yaml_exts__:
            import yaml
            fmt, dic = yaml, dict(default_flow_style=False)
        else:
            raise NotImplementedError
        dic.update(kwargs)
    else:
        dic = kwargs
    with safe_open(fname, 'w') as fp:
        fmt.dump(obj, fp, **dic)
        __logger__.debug("serialize object to file %s", fname)
Exemplo n.º 7
0
 def retry(self, f_exit, func, *args, **kwargs):
     t, i = 0, 0
     while True:
         try:
             x = func(*args, **kwargs)
             if f_exit(x):
                 if not self.silent:
                     to_screen("ready: {}".format(x))
                 return x
         except NotReadyError as identifier:
             __logger__.debug("condition not satisfied", identifier)
         if not self.silent:
             to_screen("not ready yet: {}".format(x))
         i, t = i + 1, t + self.t_sleep
         if self.max_try and i >= self.max_try or self.timeout and t >= self.timeout:
             return None
         if self.t_sleep:
             time.sleep(self.t_sleep)
Exemplo n.º 8
0
def get_response(
        path: str,
        headers: dict = {'Content-Type': 'application/json'},
        body: dict = dict(),
        method: str = 'POST',
        allowed_status=[200],  # type: list[int]
        max_try: int = 1) -> Response:
    """
    Send request to REST server and get the response.

    Args:
        path (str): REST server path
        headers (dict, optional): Defaults to {'Content-Type': 'application/json'}. request headers
        body (dict, optional): Defaults to dict(). data body of the request (default is json format)
        method (str, optional): Defaults to 'POST'. POST / PUT / GET
        allowed_status (list, optional): Defaults to [200]. raise exception if the status_code of response not in the list

    Returns:
        [Response]: request response
    """
    num, successful = 0, False
    # deal with body format
    dic = dict(headers=headers)
    if headers.get('Content-Type', 'application/json'):
        dic["json"] = body
    else:
        dic["data"] = body
    while num < max_try:
        num += 1
        response = request(method, path, **dic)
        __logger__.debug('----------Response-------------\n%s',
                         dump.dump_all(response).decode('utf-8'))
        if response.status_code in allowed_status:
            successful = True
            break
    assert successful, (response.status_code, response.reason)
    return response
Exemplo n.º 9
0
 def process(self, a: list):
     __logger__.debug('Received arguments %s', a)
     __logger__.debug("Received arguments %s", a)
     args = self.parser.parse_args(a)
     return self.process_args(args)
Exemplo n.º 10
0
 def process_args(self, args):
     __logger__.debug("Parsed arguments %s", args)
     if not args.scene:
         raise ArgumentError
     return self.scenes[args.scene].process(args)