Skip to content

Commit 5e1dd93

Browse files
authored
Merge pull request #281 from Azure/copilot/fix-publish-cloud-flag
Respect --cloud in publish pre-flight checks
2 parents 7b6f6c9 + 7d191d6 commit 5e1dd93

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

‎src/clients/apim-client.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,8 @@ export class ApimClient implements IApimClient {
774774

775775
async validatePreFlight(context: ApimServiceContext): Promise<void> {
776776
// Check resource group exists
777-
const rgUrl = `https://management.azure.com/subscriptions/${encodeURIComponent(context.subscriptionId)}/resourceGroups/${encodeURIComponent(context.resourceGroup)}?api-version=${ApimClient.RESOURCE_GROUP_API_VERSION}`;
777+
const armEndpoint = new URL(context.baseUrl).origin;
778+
const rgUrl = `${armEndpoint}/subscriptions/${encodeURIComponent(context.subscriptionId)}/resourceGroups/${encodeURIComponent(context.resourceGroup)}?api-version=${ApimClient.RESOURCE_GROUP_API_VERSION}`;
778779
let rgResponse: Response;
779780
try {
780781
rgResponse = await this.request(rgUrl, { method: 'GET' });

‎tests/unit/clients/apim-client.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
99
import { ApimClient, HttpError } from '../../../src/clients/apim-client.js';
1010
import { ResourceType } from '../../../src/models/resource-types.js';
1111
import { ApimServiceContext } from '../../../src/models/types.js';
12+
import { buildArmBaseUrl } from '../../../src/lib/cloud-config.js';
1213

1314
const testContext: ApimServiceContext = {
1415
subscriptionId: 'sub-1',
@@ -31,6 +32,45 @@ function makeResponse(
3132
});
3233
}
3334

35+
describe('ApimClient.validatePreFlight', () => {
36+
afterEach(() => {
37+
vi.restoreAllMocks();
38+
vi.unstubAllGlobals();
39+
});
40+
41+
it.each([
42+
['public', 'https://management.azure.com'],
43+
['usgov', 'https://management.usgovcloudapi.net'],
44+
['china', 'https://management.chinacloudapi.cn'],
45+
['germany', 'https://management.microsoftazure.de'],
46+
])('should use the %s endpoint for both pre-flight checks', async (cloud, endpoint) => {
47+
const client = new ApimClient();
48+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
49+
vi.spyOn(client as any, 'getToken').mockResolvedValue('fake-token');
50+
const fetchSpy = vi.fn().mockImplementation(() => Promise.resolve(makeResponse(200, {})));
51+
vi.stubGlobal('fetch', fetchSpy);
52+
const context: ApimServiceContext = {
53+
...testContext,
54+
resourceGroup: 'rg with spaces',
55+
baseUrl: buildArmBaseUrl(cloud, testContext.subscriptionId, 'rg with spaces', testContext.serviceName),
56+
};
57+
58+
await client.validatePreFlight(context);
59+
60+
expect(fetchSpy).toHaveBeenCalledTimes(2);
61+
expect(fetchSpy).toHaveBeenNthCalledWith(
62+
1,
63+
`${endpoint}/subscriptions/sub-1/resourceGroups/rg%20with%20spaces?api-version=2021-04-01`,
64+
expect.objectContaining({ method: 'GET' }),
65+
);
66+
expect(fetchSpy).toHaveBeenNthCalledWith(
67+
2,
68+
`${context.baseUrl}?api-version=${context.apiVersion}`,
69+
expect.objectContaining({ method: 'GET' }),
70+
);
71+
});
72+
});
73+
3474
describe('ApimClient.listResources', () => {
3575
let client: ApimClient;
3676
let fetchSpy: ReturnType<typeof vi.fn>;

0 commit comments

Comments
 (0)