Skip to content

Commit

Permalink
assert client is None
Browse files Browse the repository at this point in the history
  • Loading branch information
asaiacai committed Sep 29, 2024
1 parent 2881508 commit ae76a80
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 deletions sky/provision/do/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,35 +40,35 @@ class DigitalOceanError(Exception):

def _init_client():
global _client, CREDENTIALS_PATH
if _client is None:
CREDENTIALS_PATH = None
credentials_found = 0
for path in POSSIBLE_CREDENTIALS_PATHS:
if os.path.exists(path):
CREDENTIALS_PATH = path
credentials_found += 1
logger.debug(f'Digital Ocean credential path found at {path}')
assert credentials_found == 1, 'more than 1 credential file found'
if CREDENTIALS_PATH is None:
raise DigitalOceanError(
'no credentials file found from '
f'the following paths {POSSIBLE_CREDENTIALS_PATHS}')

auth_contexts = common_utils.read_yaml(
CREDENTIALS_PATH)['auth-contexts']
for context, api_token in auth_contexts.items():
try:
test_client = do.pydo.Client(token=api_token)
test_client.droplets.list()
logger.debug(f'using {context} context')
_client = test_client
break
except do.exceptions().HttpResponseError:
continue
else:
raise DigitalOceanError(
'no valid api tokens found try '
'setting a new API token with `doctl auth init`')
assert _client is None
CREDENTIALS_PATH = None
credentials_found = 0
for path in POSSIBLE_CREDENTIALS_PATHS:
if os.path.exists(path):
CREDENTIALS_PATH = path
credentials_found += 1
logger.debug(f'Digital Ocean credential path found at {path}')
assert credentials_found == 1, 'more than 1 credential file found'
if CREDENTIALS_PATH is None:
raise DigitalOceanError(
'no credentials file found from '
f'the following paths {POSSIBLE_CREDENTIALS_PATHS}')

auth_contexts = common_utils.read_yaml(
CREDENTIALS_PATH)['auth-contexts']
for context, api_token in auth_contexts.items():
try:
test_client = do.pydo.Client(token=api_token)
test_client.droplets.list()
logger.debug(f'using {context} context')
_client = test_client
break
except do.exceptions().HttpResponseError:
continue
else:
raise DigitalOceanError(
'no valid api tokens found try '
'setting a new API token with `doctl auth init`')
return _client


Expand Down

0 comments on commit ae76a80

Please sign in to comment.