Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions cfa/cloudops/_cloudclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,23 +168,51 @@ def __init__(
self.task_id_ints = False
self.task_id_max = 0

def check_credentials(self):
def check_credentials(self) -> pd.DataFrame:
"""Check credentials and return accessible Azure resource information.

Returns:
pd.DataFrame: A DataFrame containing subscription information and the
configured Batch and Storage account names.
"""
logger.debug("Checking credentials by listing subscriptions.")
cred = self.cred.default_credential

columns = [
"subscription_id",
"subscription_name",
"state",
"batch_account",
"storage_account",
]

try:
subscription_client = SubscriptionClient(cred)
# List subscriptions
sub_list = [sub for sub in subscription_client.subscriptions.list()]
sub_list = list(subscription_client.subscriptions.list())

rows = []
for subscription in sub_list:
logger.info("Found subscription via credential.")
logger.info(f"Subscription ID: {subscription.subscription_id}")
logger.info(f"Subscription Name: {subscription.display_name}")
logger.info(f"State: {subscription.state}")
logger.info("-" * 30)

rows.append(
{
"subscription_id": subscription.subscription_id,
"subscription_name": subscription.display_name,
"state": subscription.state,
"batch_account": self.cred.azure_batch_account,
"storage_account": self.cred.azure_blob_storage_account,
}
)

logger.debug("Successfully found subscriptions.")
return pd.DataFrame(rows, columns=columns)
except Exception as e:
logger.exception(f"Error checking credentials: {e}")
return pd.DataFrame(columns=columns)

def create_pool(
self,
Expand Down
35 changes: 29 additions & 6 deletions tests/test_cloudclient_more.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def cloud_client_more(monkeypatch):
return CloudClient(dotenv_path=None, use_sp=False, use_federated=False)


def test_check_credentials_env_default_sp(cloud_client_more, monkeypatch):
def test_check_credentials_returns_dataframe(cloud_client_more, monkeypatch):
seen_creds = []

class FakeSub:
Expand All @@ -58,11 +58,25 @@ def fake_subscription_client(cred):
"cfa.cloudops._cloudclient.SubscriptionClient", fake_subscription_client
)

cloud_client_more.check_credentials()
cloud_client_more.check_credentials()
cloud_client_more.check_credentials()
result = cloud_client_more.check_credentials()

assert seen_creds == ["default-cred", "default-cred", "default-cred"]
assert seen_creds == ["default-cred"]
assert list(result.columns) == [
"subscription_id",
"subscription_name",
"state",
"batch_account",
"storage_account",
]
assert result.to_dict(orient="records") == [
{
"subscription_id": "sub-1",
"subscription_name": "sub-name",
"state": "Enabled",
"batch_account": "acct",
"storage_account": "blobacct",
}
]


def test_check_credentials_handles_exception(cloud_client_more, monkeypatch):
Expand All @@ -71,7 +85,16 @@ def test_check_credentials_handles_exception(cloud_client_more, monkeypatch):
lambda cred: (_ for _ in ()).throw(RuntimeError("boom")),
)

cloud_client_more.check_credentials()
result = cloud_client_more.check_credentials()

assert result.empty
assert list(result.columns) == [
"subscription_id",
"subscription_name",
"state",
"batch_account",
"storage_account",
]


def test_delete_job_and_schedule_methods(cloud_client_more):
Expand Down
Loading