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

chore(atlas-service, indexes): remove all the domain specific parts of automation agent request helper #6305

Merged
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
109 changes: 80 additions & 29 deletions packages/atlas-service/src/atlas-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ import {
import type { Logger } from '@mongodb-js/compass-logging';
import type { PreferencesAccess } from 'compass-preferences-model';
import type { AtlasClusterMetadata } from '@mongodb-js/connection-info';
import type {
AutomationAgentRequestTypes,
AutomationAgentResponse,
} from './make-automation-agent-op-request';
import { makeAutomationAgentOpRequest } from './make-automation-agent-op-request';

export type AtlasServiceOptions = {
defaultHeaders?: Record<string, string>;
Expand Down Expand Up @@ -104,34 +99,90 @@ export class AtlasService {
},
});
}
automationAgentFetch<OpType extends keyof AutomationAgentRequestTypes>(
atlasMetadata: Pick<
AtlasClusterMetadata,
| 'projectId'
| 'clusterUniqueId'
| 'regionalBaseUrl'
| 'metricsType'
| 'metricsId'
>,
opType: OpType,
opBody: Omit<
AutomationAgentRequestTypes[OpType],
'clusterId' | 'serverlessId'
>
): Promise<AutomationAgentResponse<OpType>> {
async automationAgentRequest(
atlasMetadata: AtlasClusterMetadata,
opType: string,
opBody: Record<string, unknown>
): Promise<{ _id: string; requestType: string } | undefined> {
const opBodyClusterId =
atlasMetadata.metricsType === 'serverless'
? { serverlessId: atlasMetadata.clusterUniqueId }
: { clusterId: atlasMetadata.metricsId };
return makeAutomationAgentOpRequest(
this.authenticatedFetch.bind(this),
this.regionalizedCloudEndpoint(atlasMetadata),
atlasMetadata.projectId,
opType,
Object.assign(
opBodyClusterId,
opBody
) as AutomationAgentRequestTypes[OpType]
const requestUrl = this.regionalizedCloudEndpoint(
atlasMetadata,
`/explorer/v1/groups/${atlasMetadata.projectId}/requests/${opType}`
);
const json = await this.authenticatedFetch(requestUrl, {
method: 'POST',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify({ ...opBodyClusterId, ...opBody }),
}).then((res) => {
if (
res.headers
.get('content-type')
?.toLowerCase()
.includes('application/json')
) {
return res.json();
}
});
assertAutomationAgentRequestResponse(json, opType);
return json;
}
async automationAgentAwait<T>(
atlasMetadata: AtlasClusterMetadata,
opType: string,
requestId: string
): Promise<{
_id: string;
requestType: string;
response: T[];
}> {
const requestUrl = this.regionalizedCloudEndpoint(
atlasMetadata,
`/explorer/v1/groups/${atlasMetadata.projectId}/requests/${requestId}/types/${opType}/await`
);
const json = await this.authenticatedFetch(requestUrl, {
method: 'GET',
}).then((res) => {
return res.json();
});
assertAutomationAgentAwaitResponse<T>(json, opType);
return json;
}
}

function assertAutomationAgentRequestResponse(
json: any,
opType: string
): asserts json is { _id: string; requestType: string } {
if (
Object.prototype.hasOwnProperty.call(json, '_id') &&
Object.prototype.hasOwnProperty.call(json, 'requestType') &&
json.requestType === opType
) {
return;
}
throw new Error(
'Got unexpected backend response for automation agent request'
);
}

function assertAutomationAgentAwaitResponse<T>(
json: any,
opType: string
): asserts json is { _id: string; requestType: string; response: T[] } {
if (
Object.prototype.hasOwnProperty.call(json, '_id') &&
Object.prototype.hasOwnProperty.call(json, 'requestType') &&
Object.prototype.hasOwnProperty.call(json, 'response') &&
json.requestType === opType
) {
return;
}
throw new Error(
'Got unexpected backend response for automation agent request await'
);
}
124 changes: 0 additions & 124 deletions packages/atlas-service/src/make-automation-agent-op-request.spec.ts

This file was deleted.

Loading
Loading