Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix #890) Fix Undefined Error with No Subs in Azure Profile #892

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
16 changes: 9 additions & 7 deletions src/cli/commands/login/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async function setupProjectCredentials(options: SWACLIConfig, credentialChain: T
const tenants = await listTenants(credentialChain);
if (tenants.length === 0) {
throw new Error(
`No Azure tenants found in your account.\n Please read https://docs.microsoft.com/azure/cost-management-billing/manage/troubleshoot-sign-in-issue`
`No Azure tenants found in your account.\n Please read https://docs.microsoft.com/azure/cost-management-billing/manage/troubleshoot-sign-in-issue`,
);
} else if (tenants.length === 1) {
logger.silly(`Found 1 tenant: ${tenants[0].tenantId}`);
Expand All @@ -81,7 +81,7 @@ async function setupProjectCredentials(options: SWACLIConfig, credentialChain: T
const subscriptions = await listSubscriptions(credentialChain);
if (subscriptions.length === 0) {
throw new Error(
`No valid subscription found for tenant ${tenantId}.\n Please read https://docs.microsoft.com/azure/cost-management-billing/manage/no-subscriptions-found`
`No valid subscription found for tenant ${tenantId}.\n Please read https://docs.microsoft.com/azure/cost-management-billing/manage/no-subscriptions-found`,
);
} else if (subscriptions.length === 1) {
logger.silly(`Found 1 subscription: ${subscriptions[0].subscriptionId}`);
Expand Down Expand Up @@ -109,7 +109,7 @@ async function storeProjectCredentialsInEnvFile(
subscriptionId: string | undefined,
tenantId: string | undefined,
clientId: string | undefined,
clientSecret: string | undefined
clientSecret: string | undefined,
) {
const envFile = path.join(process.cwd(), ENV_FILENAME);
const envFileExists = existsSync(envFile);
Expand Down Expand Up @@ -162,10 +162,12 @@ async function tryGetAzTenantAndSubscription(options: SWACLIConfig) {
const azureProfile = await safeReadJson(AZURE_LOGIN_CONFIG);
if (azureProfile) {
const allSubscriptions = (azureProfile as AzureProfile).subscriptions;
const defaultAzureInfo = allSubscriptions.find((subscription) => subscription.isDefault == true);
if (defaultAzureInfo) {
options.tenantId = defaultAzureInfo.tenantId;
options.subscriptionId = defaultAzureInfo.id;
if (allSubscriptions) {
const defaultAzureInfo = allSubscriptions.find((subscription) => subscription.isDefault == true);
if (defaultAzureInfo) {
options.tenantId = defaultAzureInfo.tenantId;
options.subscriptionId = defaultAzureInfo.id;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/swa.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,5 +442,5 @@ declare type AzureLoginInfo = {

declare interface AzureProfile {
installationId: string;
subscriptions: AzureLoginInfo[];
subscriptions?: AzureLoginInfo[];
}
Loading