Coverage for harbor_cli/harbor/client.py: 22%

24 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-02-09 12:09 +0100

1from __future__ import annotations 

2 

3from typing import TYPE_CHECKING 

4 

5from harborapi import HarborAsyncClient 

6 

7from ..logs import logger 

8from ..output.prompts import str_prompt 

9 

10if TYPE_CHECKING: 

11 from ..config import HarborCLIConfig 

12 

13# It's unlikely that we will authenticate with multiple 

14# Harbor instances, so keeping it in a dict is probably overkill 

15 

16_CLIENTS = {} # type: dict[str, HarborAsyncClient] 

17 

18 

19def get_client(config: HarborCLIConfig) -> HarborAsyncClient: 

20 if _CLIENTS.get(config.harbor.url) is None: 

21 # Instantiate harbor client with credentials dict from config 

22 _CLIENTS[config.harbor.url] = HarborAsyncClient(**(config.harbor.credentials)) 

23 return _CLIENTS[config.harbor.url] 

24 

25 

26def setup_client(config: HarborCLIConfig) -> HarborAsyncClient: 

27 """Setup the HarborAsyncClient for the application.""" 

28 # We want to log all problems before prompting for input, 

29 # so we do these checks pre-emptively 

30 has_url = True if config.harbor.url else False 

31 has_auth = True if config.harbor.has_auth_method else False 

32 if not has_url: 

33 logger.warning("Harbor API URL missing from configuration file.") 

34 if not has_auth: 

35 logger.warning("Harbor authentication method missing from configuration file.") 

36 

37 if not has_url: 

38 config.harbor.url = str_prompt("Harbor API URL") 

39 

40 # We need one of the available auth methods to be specified 

41 # If not, prompt for username and password 

42 if not has_auth: 

43 # TODO: refactor this so we can re-use username and password 

44 # prompts from commands.cli.init! 

45 if not config.harbor.username: 

46 config.harbor.username = str_prompt("Username") 

47 if not config.harbor.secret: 

48 config.harbor.secret = str_prompt("Password", password=True) # type: ignore # pydantic.SecretStr 

49 

50 return get_client(config)