Skip to content
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
81 changes: 47 additions & 34 deletions l10n/bundle.l10n.json

Large diffs are not rendered by default.

153 changes: 108 additions & 45 deletions specs/account-overview.md

Large diffs are not rendered by default.

46 changes: 44 additions & 2 deletions src/cosmosdb/AzureResourceMetadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@
*--------------------------------------------------------------------------------------------*/

import { type DatabaseAccountGetResults } from '@azure/arm-cosmosdb';
import { createHttpHeaders } from '@azure/core-rest-pipeline';
import { type IActionContext } from '@microsoft/vscode-azext-utils';
import { type AzureSubscription } from '@microsoft/vscode-azureresources-api';
import { vi } from 'vitest';
import { createCosmosDBManagementClient } from '../utils/azureClients';
import { armTestSubscription, createArmTestClient, createArmTestContext } from './armTestUtils';
import { AzureResourceMetadata } from './AzureResourceMetadata';
import { SERVERLESS_CAPABILITY_NAME } from './cosmosdb-shared-constants';

// These are only needed so the module under test can be imported; the getters under test never call them.
vi.mock('@microsoft/vscode-azext-utils', () => ({
callWithTelemetryAndErrorHandling: vi.fn(),
callWithTelemetryAndErrorHandling: vi.fn(
async (_event: string, action: (context: IActionContext) => Promise<unknown>) => action(createArmTestContext()),
),
}));
vi.mock('../utils/azureClients', () => ({
createCosmosDBManagementClient: vi.fn(),
Expand All @@ -35,6 +40,43 @@ class TestableAzureResourceMetadata extends AzureResourceMetadata {
}

describe('AzureResourceMetadata', () => {
describe('account snapshot', () => {
it.each([
[true, true],
[false, false],
[undefined, undefined],
[null, undefined],
['false', undefined],
])('preserves the unmodeled change-feed flag %j as %j', async (value, expected) => {
const client = createArmTestClient();
vi.mocked(createCosmosDBManagementClient).mockResolvedValue(client);
const send = vi.spyOn(client.pipeline, 'sendRequest').mockImplementation(async (_http, request) => ({
request,
status: 200,
headers: createHttpHeaders(),
bodyAsText: JSON.stringify({
properties: {
documentEndpoint: 'https://account.documents.azure.com',
enableAllVersionsAndDeletesChangeFeed: value,
},
}),
}));
const result = await AzureResourceMetadata.create({
id: 'account-id',
name: 'account',
resourceGroup: 'rg',
subscription: armTestSubscription,
azureResourceType: { type: 'microsoft.documentdb/databaseaccounts' },
raw: {},
});
expect(result?.databaseAccount).toMatchObject({
enableAllVersionsAndDeletesChangeFeed: expected,
documentEndpoint: 'https://account.documents.azure.com',
});
expect(send).toHaveBeenCalledOnce();
});
});

describe('documentEndpoint', () => {
it('returns the document endpoint when present', () => {
const metadata = new TestableAzureResourceMetadata({
Expand Down
30 changes: 27 additions & 3 deletions src/cosmosdb/AzureResourceMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ type DeepReadonly<T> = T extends (...args: never[]) => unknown
? { readonly [K in keyof T]: DeepReadonly<T[K]> }
: T;

type DatabaseAccountSnapshot = DatabaseAccountGetResults & {
enableAllVersionsAndDeletesChangeFeed?: boolean;
};

export class AzureResourceMetadata {
/**
* This is only one available way to get AzureResourceMetadata, so we require the following props
Expand Down Expand Up @@ -70,12 +74,32 @@ export class AzureResourceMetadata {
throw new Error(l10n.t('Failed to create CosmosDB management client.'));
}

const databaseAccount = await client.databaseAccounts.get(resourceGroup, accountName);
let enableAllVersionsAndDeletesChangeFeed: boolean | undefined;
const databaseAccount = await client.databaseAccounts.get(resourceGroup, accountName, {
onResponse: (response) => {
// The SDK drops this unmodeled property, so preserve only its boolean value from the raw response.
Comment thread
bk201- marked this conversation as resolved.
Resolved
const body: unknown = response.parsedBody;
if (typeof body === 'object' && body !== null && 'properties' in body) {
const properties = body.properties;
if (
typeof properties === 'object' &&
properties !== null &&
'enableAllVersionsAndDeletesChangeFeed' in properties &&
typeof properties.enableAllVersionsAndDeletesChangeFeed === 'boolean'
) {
enableAllVersionsAndDeletesChangeFeed = properties.enableAllVersionsAndDeletesChangeFeed;
}
}
},
});

// Database account has to have document endpoint
nonNullProp(databaseAccount, 'documentEndpoint', `of the database account ${accountId}`);

return new AzureResourceMetadata(subscription, accountId, accountName, resourceGroup, databaseAccount);
return new AzureResourceMetadata(subscription, accountId, accountName, resourceGroup, {
...databaseAccount,
enableAllVersionsAndDeletesChangeFeed,
});
});
}

Expand All @@ -84,7 +108,7 @@ export class AzureResourceMetadata {
public readonly accountId: string,
public readonly accountName: string,
public readonly resourceGroup: string,
public readonly databaseAccount: DeepReadonly<DatabaseAccountGetResults>,
public readonly databaseAccount: DeepReadonly<DatabaseAccountSnapshot>,
) {
// The constructor is intentionally simple; all logic is in the static create method
}
Expand Down
7 changes: 4 additions & 3 deletions src/panels/accountOverview/advisoryText.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('advisory text', () => {
expect(parts.filter((part) => part.highlighted)).toEqual([{ text: '400 RU/s', highlighted: true }]);
});

it('marks detector evidence without changing the deferred minimum-capacity verdict', () => {
it('marks minimum-capacity evidence while withholding no-op lowering guidance', () => {
const container = 'order-`2026`';
const finding = new OverProvisioningDetector().evaluate(
5,
Expand All @@ -70,8 +70,9 @@ describe('advisory text', () => {
'0 RU/s',
'~400 RU/s',
]);
expect(rationale.map((part) => part.text).join('')).toContain(`Container "${container}" stayed at 5%`);
expect(finding?.suggestedAction).toContain('from `400 RU/s` toward `~400 RU/s`');
expect(rationale.map((part) => part.text).join('')).toContain(`Offer "${container}" stayed at 5%`);
expect(finding?.suggestedAction).toContain('No throughput reduction is available');
expect(finding?.actionEligibility).toBe('blocked');
expect(finding?.thresholdReference).toBe('Threshold: p99 RU < `30%` and peak < `90%`');
});
});
Loading
Loading