-
Notifications
You must be signed in to change notification settings - Fork 108
feat: add atlas-create-free-cluster atlas-inspect-cluster tools #29
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
Changes from 2 commits
4efb5cf
376b555
9097962
a0eec19
e22d4bd
5033797
10dd98b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,21 +27,32 @@ | |
"check": "npm run check:lint && npm run check:format", | ||
"check:lint": "eslint .", | ||
"check:format": "prettier -c .", | ||
"reformat": "prettier --write ." | ||
"reformat": "prettier --write .", | ||
"generate:download": "curl -Lo ./scripts/spec.json https://github.com/mongodb/openapi/raw/refs/heads/main/openapi/v2/openapi-2025-03-12.json", | ||
"generate:filter": "tsx ./scripts/filter.ts > ./scripts/filteredSpec.json < ./scripts/spec.json", | ||
"generate:bundle": "redocly bundle --ext json --remove-unused-components ./scripts/filteredSpec.json --output ./scripts/bundledSpec.json", | ||
"generate:openapi": "openapi-typescript ./scripts/bundledSpec.json --root-types-no-schema-prefix --root-types --output ./src/common/atlas/openapi.d.ts", | ||
"generate:clear": "rm -rf ./scripts/bundledSpec.json ./scripts/filteredSpec.json ./scripts/spec.json", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Super nit, but these seem to be very dependent on the order of execution, so not sure if it's that useful to have them as separate scripts - it doesn't seem like they'd be really useful on their own. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the only reason they are separated is that it was challenging to implement in one go, we could move it to bash but it also felt a bit weird to do so There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. replaced with a bash |
||
"generate": "npm run generate:download && npm run generate:filter && npm run generate:bundle && npm run generate:openapi && npm run generate:clear" | ||
}, | ||
"license": "Apache-2.0", | ||
"devDependencies": { | ||
"@eslint/js": "^9.24.0", | ||
"@modelcontextprotocol/inspector": "^0.8.2", | ||
"@modelcontextprotocol/sdk": "^1.8.0", | ||
"@redocly/cli": "^1.34.2", | ||
"@types/node": "^22.14.0", | ||
"@types/simple-oauth2": "^5.0.7", | ||
"eslint": "^9.24.0", | ||
"eslint-config-prettier": "^10.1.1", | ||
"globals": "^16.0.0", | ||
"openapi-types": "^12.1.3", | ||
"openapi-typescript": "^7.6.1", | ||
"prettier": "^3.5.3", | ||
"tsx": "^4.19.3", | ||
"typescript": "^5.8.2", | ||
"typescript-eslint": "^8.29.1" | ||
"typescript-eslint": "^8.29.1", | ||
"yaml": "^2.7.1" | ||
}, | ||
"dependencies": { | ||
"@mongodb-js/devtools-connect": "^3.7.2", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { OpenAPIV3_1 } from "openapi-types"; | ||
|
||
async function readStdin() { | ||
return new Promise<string>((resolve, reject) => { | ||
let data = ""; | ||
process.stdin.setEncoding("utf8"); | ||
process.stdin.on("error", (err) => { | ||
reject(err); | ||
}); | ||
process.stdin.on("data", (chunk) => { | ||
data += chunk; | ||
}); | ||
process.stdin.on("end", () => { | ||
resolve(data); | ||
}); | ||
}); | ||
} | ||
|
||
function filterOpenapi(openapi: OpenAPIV3_1.Document): OpenAPIV3_1.Document { | ||
const allowedOperations = [ | ||
"listProjects", | ||
"getProject", | ||
"createProject", | ||
"listClusters", | ||
"createCluster", | ||
"listClustersForAllProjects", | ||
]; | ||
|
||
const filteredPaths = {}; | ||
|
||
for (const path in openapi.paths) { | ||
const filteredMethods = {} as OpenAPIV3_1.PathItemObject; | ||
for (const method in openapi.paths[path]) { | ||
if (allowedOperations.includes(openapi.paths[path][method].operationId)) { | ||
filteredMethods[method] = openapi.paths[path][method]; | ||
} | ||
} | ||
if (Object.keys(filteredMethods).length > 0) { | ||
filteredPaths[path] = filteredMethods; | ||
} | ||
} | ||
|
||
return { ...openapi, paths: filteredPaths }; | ||
} | ||
|
||
async function main() { | ||
const openapiText = await readStdin(); | ||
const openapi = JSON.parse(openapiText) as OpenAPIV3_1.Document; | ||
const filteredOpenapi = filterOpenapi(openapi); | ||
console.log(JSON.stringify(filteredOpenapi)); | ||
} | ||
|
||
main().catch((error) => { | ||
console.error("Error:", error); | ||
process.exit(1); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { ApiClient } from "./client"; | ||
import { State } from "../../state"; | ||
|
||
export async function ensureAuthenticated(state: State, apiClient: ApiClient): Promise<void> { | ||
if (!(await isAuthenticated(state, apiClient))) { | ||
throw new Error("Not authenticated"); | ||
} | ||
} | ||
|
||
export async function isAuthenticated(state: State, apiClient: ApiClient): Promise<boolean> { | ||
switch (state.auth.status) { | ||
case "not_auth": | ||
return false; | ||
case "requested": | ||
try { | ||
if (!state.auth.code) { | ||
return false; | ||
} | ||
await apiClient.retrieveToken(state.auth.code.device_code); | ||
return !!state.auth.token; | ||
} catch { | ||
return false; | ||
} | ||
case "issued": | ||
if (!state.auth.token) { | ||
return false; | ||
} | ||
return await apiClient.validateToken(); | ||
default: | ||
throw new Error("Unknown authentication status"); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can create an issue if it requires more changes - can we have per-path or per operationID files?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the tool supports it