portainer_host: use different way of handling tokens

This commit is contained in:
tylen 2025-03-04 21:03:52 +00:00
parent 99040eccb2
commit 02ea437f6e

View File

@ -20,7 +20,8 @@ class PortainerHost(BaseCLi):
super().__init__() super().__init__()
self.host_url = host_url self.host_url = host_url
self.api_url = f'{host_url}/api' self.api_url = f'{host_url}/api'
self.jwt_config = [] self.jwt_config = {}
self.jwt_token = ''
home_directory = os.path.expanduser("~") home_directory = os.path.expanduser("~")
config_file_path = os.path.join(home_directory, DEFAULT_PORTAINER_JWT_CONFIG_FILE) config_file_path = os.path.join(home_directory, DEFAULT_PORTAINER_JWT_CONFIG_FILE)
try: try:
@ -36,17 +37,17 @@ class PortainerHost(BaseCLi):
except FileNotFoundError: except FileNotFoundError:
print(f"Configuration does not exist. Creating...") print(f"Configuration does not exist. Creating...")
raise EmptyConfigException() raise EmptyConfigException()
try:
hostnames = [config['hostname'] for config in self.jwt_config] self.jwt_token = self.jwt_config[self.host_url]
if self.host_url not in hostnames: except KeyError:
print(f"No token present in configuration file for host {self.host_url}. Creating...")
raise EmptyConfigException() raise EmptyConfigException()
def __create_config(self, file_path): def __create_config(self, file_path):
config = { self.jwt_token = self.__get_jwt_token()
"hostname": self.host_url, self.jwt_config[self.host_url] = {
"jwt": self.__get_jwt_token(), "jwt": self.jwt_token,
} }
self.jwt_config.append(config)
with open(file_path, 'w') as file: with open(file_path, 'w') as file:
json.dump(self.jwt_config, file, indent=4) json.dump(self.jwt_config, file, indent=4)
@ -74,3 +75,4 @@ class PortainerHost(BaseCLi):
data = response.json() data = response.json()
return data['jwt'] return data['jwt']