def write(self): if not self.uuid: raise EduvpnException('uuid field not set') fields = [f for f in dir(self) if not f.startswith('_') and not callable(getattr(self, f))] d = {field: getattr(self, field) for field in fields} p = path.join(providers_path, self.uuid + '.json') logger.info("storing metadata in {}".format(p)) serialized = json.dumps(d) mkdir_p(providers_path) with open(p, 'w') as f: f.write(serialized)
def write_distributed_token(self): # type: () -> None tokens = get_distributed_tokens() if self.discovery_uri in tokens: tokens[self.discovery_uri]['token'] = self.token else: error = "updating distributed token for {} but it isn't present in {}, recreating" logger.error(error.format(self.discovery_uri, distibuted_tokens_path)) tokens[self.discovery_uri] = {'token': self.token, 'token_endpoint': self.token_endpoint} serialized = json.dumps(tokens) mkdir_p(others_path) with open(distibuted_tokens_path, 'w') as f: logger.info(u"updating distributed token for {} to {}".format(self.discovery_uri, distibuted_tokens_path)) f.write(serialized)
def write(self): # type: () -> None if not self.uuid: # raise EduvpnException('uuid field not set') logger.warning(u"uuid field not yet set") return fields = [f for f in dir(self) if not f.startswith('_') and not callable(getattr(self, f))] d = {field: getattr(self, field) for field in fields} p = os.path.join(providers_path, self.uuid + '.json') logger.info(u"storing metadata in {}".format(p)) serialized = json.dumps(d) mkdir_p(providers_path) with open(p, 'w') as f: f.write(serialized) if self.authorization_type == 'distributed': self.write_distributed_token()
def update_token(self, token): self.token = token if self.authorization_type == 'distributed': tokens = get_distributed_tokens() if self.discovery_uri in tokens: tokens[self.discovery_uri]['token'] = token else: error = "updating distributed token for {} but it isn't present in {}, recreating" logger.error(error.format(self.discovery_uri, distibuted_tokens_path)) tokens[self.discovery_uri] = {'token': token, 'token_endpoint': self.token_endpoint} serialized = json.dumps(tokens) mkdir_p(others_path) with open(distibuted_tokens_path, 'w') as f: logger.info("updating distributed token for {} to {}".format(self.discovery_uri, distibuted_tokens_path)) f.write(serialized) else: self.write()
def test_mkdir_p(self): mkdir_p('/tmp/test/test/test')