Skip to content

Commit

Permalink
chore(atlas-service, indexes): remove all the domain specific parts o…
Browse files Browse the repository at this point in the history
…f automation agent request helper (#6305)

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

* chore(indexes): add some tests
  • Loading branch information
gribnoysup authored Oct 1, 2024
1 parent 9279a78 commit 4229486
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 351 deletions.
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

0 comments on commit 4229486

Please sign in to comment.