portainer_host: use different way of handling tokens
This commit is contained in:
parent
99040eccb2
commit
02ea437f6e
@ -20,14 +20,15 @@ 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:
|
||||||
self.__read_current_config(config_file_path)
|
self.__read_current_config(config_file_path)
|
||||||
except EmptyConfigException:
|
except EmptyConfigException:
|
||||||
self.__create_config(config_file_path)
|
self.__create_config(config_file_path)
|
||||||
|
|
||||||
def __read_current_config(self, file_path):
|
def __read_current_config(self, file_path):
|
||||||
try:
|
try:
|
||||||
with open(file_path, 'r') as file:
|
with open(file_path, 'r') as file:
|
||||||
@ -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']
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user