def on_every_second(): ''' @summary: function called by tornado/ioloop every second It's used to clear expired locks ''' LOCK_MANAGER_INSTANCE.clean_expired_locks()
def get(self): ''' @summary: deals with GET request (getting a JSON HAL of resources) ''' resources = Resource(self.reverse_url("resources")) resources_names = LOCK_MANAGER_INSTANCE.get_resources_names() for resource_name in resources_names: tmp = LOCK_MANAGER_INSTANCE.get_resource_as_dict(resource_name) resource = Resource(self.reverse_url("resource", tmp['name']), {"name": tmp['name']}) resources.add_embedded_resource("resources", resource) self.set_header("Content-Type", "application/hal+json") self.finish(resources.to_json())
def post(self): ''' @summary: deals with POST for adding a resource ''' resource = self.get_argument('resource') title = self.get_argument('title') lifetime = int(self.get_argument('lifetime')) wait = int(self.get_argument('wait')) logging.info("Resource - %s - Lock Requested with (%s, %s, %s)" % (resource, title, lifetime, wait)) lock = Lock(resource, title, wait, lifetime) LOCK_MANAGER_INSTANCE.add_lock(resource, lock) logging.debug(lock.uid) self.render("requestlockresponse.html", name=resource, uid=lock.uid)
def post(self, name): ''' @summary: deals with POST request (acquiring locks on resource) @param name: name of the resource ''' raw_body = self.request.body.decode('utf-8') if len(raw_body) == 0: self.send_error(status_code=400, message="empty body") return lock = Lock.from_json(name, raw_body) if not(lock): self.send_error(status_code=400, message="invalid json body") return lock.set_callbacks(functools.partial(self.on_active_wrapper, name, lock), self.on_delete_wrapper) LOCK_MANAGER_INSTANCE.add_lock(name, lock)
def post(self, name): ''' @summary: deals with POST request (acquiring locks on resource) @param name: name of the resource ''' raw_body = self.request.body.decode('utf-8') if len(raw_body) == 0: self.send_error(status_code=400, message="empty body") return lock = Lock.from_json(name, raw_body) if not (lock): self.send_error(status_code=400, message="invalid json body") return lock.set_callbacks( functools.partial(self.on_active_wrapper, name, lock), self.on_delete_wrapper) LOCK_MANAGER_INSTANCE.add_lock(name, lock)
def delete(self, name): ''' @summary: deals with DELETE request (deleting the given resource) @param name: name of the resource ''' res = LOCK_MANAGER_INSTANCE.remove_resource(name) if res: self.send_status(204) else: self.send_error(404, message="no resource (with locks) found")
def get(self, name): ''' @summary: deals with GET request on /showresource ''' tmp = LOCK_MANAGER_INSTANCE.get_resource_as_dict(name) if tmp is not None: locks = tmp["locks"] self.render("showresource.html", name=name, locks=locks) else: self.render("showresource.html", name=name, locks={})
def post(self): ''' @summary: deals with POST for adding a resource ''' name = self.get_argument('name') uid = self.get_argument('uid') logging.info("Release Requested - %s - %s" % (name, uid)) res = LOCK_MANAGER_INSTANCE.delete_lock(name, uid) name = name + " - " + uid self.render("success.html", name=name, operation="released")
def get_resources_list(): ''' @summary: returns resources list ''' if CONF_READER.VALIDATE_RESOURCE is True: in_file_resources = FILE_OPERATIONS.get_resources() return in_file_resources else: in_file_resources = FILE_OPERATIONS.get_resources() mem_resources = LOCK_MANAGER_INSTANCE.get_resources_names() return in_file_resources + list(set(mem_resources) - set(in_file_resources))
def delete(self, name, uid): ''' @summary: deals with DELETE request (releasing a lock) @param name: name of the resource @param uid: uid of the lock ''' res = LOCK_MANAGER_INSTANCE.delete_lock(name, uid) if res: self.send_status(204) else: self.send_error(status_code=404, message="lock not found") return
def get(self, name): ''' @summary: deals with GET request (getting a JSON HAL of the resource) @param name: name of the resource ''' tmp = LOCK_MANAGER_INSTANCE.get_resource_as_dict(name) resource = Resource(self.reverse_url("resource", name), {"name": name}) if tmp: for lock_dict in tmp['locks']: lock = Resource(self.reverse_url("lock", name, lock_dict['uid']), lock_dict) resource.add_embedded_resource("locks", lock) self.set_header("Content-Type", "application/hal+json") self.finish(resource.to_json())
def post(self, name): ''' @summary: deals with POST request (acquiring locks on resource) @param name: name of the resource ''' raw_body = self.request.body.decode('utf-8') if len(raw_body) == 0: self.send_error(status_code=400, message="empty body") return lock = Lock.from_json(name, raw_body) if not (lock): self.send_error(status_code=400, message="invalid json body") return elif CONF_READER.VALIDATE_RESOURCE is True: if FILE_OPERATIONS.is_resource_valid(name) is False: self.send_error( status_code=400, message= "invalid resource, add resource before requesting lock") return lock.set_callbacks( functools.partial(self.on_active_wrapper, name, lock), self.on_delete_wrapper) LOCK_MANAGER_INSTANCE.add_lock(name, lock)
def get(self, name): ''' @summary: deals with GET request (getting a JSON HAL of the resource) @param name: name of the resource ''' tmp = LOCK_MANAGER_INSTANCE.get_resource_as_dict(name) resource = Resource(self.reverse_url("resource", name), {"name": name}) if tmp: for lock_dict in tmp['locks']: lock = Resource( self.reverse_url("lock", name, lock_dict['uid']), lock_dict) resource.add_embedded_resource("locks", lock) self.set_header("Content-Type", "application/hal+json") self.finish(resource.to_json())
def get(self, name, uid): ''' @summary: deals with GET request @param name: name of the resource @param uid: uid of the lock ''' lock = LOCK_MANAGER_INSTANCE.get_lock(name, uid) if lock: self.set_header('Content-Type', 'application/hal+json') hal_lock = Resource(href=self.reverse_url("lock", name, lock.uid), properties=lock.to_dict()) hal_resource_link = Link(href=self.reverse_url("resource", name)) hal_lock.add_link(rel="resource", link=hal_resource_link, multiple=False) self.write(hal_lock.to_json()) else: self.send_error(status_code=404, message="lock not found") return
def get(self): ''' @summary: deals with GET request on /showallresource ''' resources = {} total_resources = GetResources.get_resources_list() for resource_name in total_resources: tmp = LOCK_MANAGER_INSTANCE.get_resource_as_dict(resource_name) if tmp is not None: if len(tmp.get('locks')) > 0: resources.update({resource_name: "Locked"}) else: resources.update({resource_name: ""}) else: resources.update({resource_name: ""}) logging.debug("Current Resources - %s" % resources) self.render("showallresources.html", resources=resources)
def delete(self): ''' @summary: deals with DELETE request (deleting all resources) ''' LOCK_MANAGER_INSTANCE.remove_all_resources() self.send_status(204)