Add create and delete application and service principal scripts
This commit is contained in:
60
bin/create-app-and-sp.mjs
Normal file
60
bin/create-app-and-sp.mjs
Normal file
@@ -0,0 +1,60 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { config } from "../public-config.js";
|
||||
import {
|
||||
createApp,
|
||||
createSp,
|
||||
getApp,
|
||||
getGraphClient,
|
||||
getServicePrincipal,
|
||||
} from "../src/graph.js";
|
||||
import { parseArgs } from "node:util";
|
||||
|
||||
async function usage() {
|
||||
console.log("Usage: create-app-and-sp.mjs --app-name <name>");
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const { client } = await getGraphClient({
|
||||
tenantId: config.tenantId,
|
||||
clientId: config.clientId,
|
||||
});
|
||||
|
||||
const args = parseArgs({
|
||||
options: {
|
||||
"app-name": {
|
||||
type: "string",
|
||||
short: "n",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!args.values["app-name"]) {
|
||||
await usage();
|
||||
return;
|
||||
}
|
||||
|
||||
console.log("Will create app with name:", args.values["app-name"]);
|
||||
|
||||
let app = await getApp(client, args.values["app-name"]);
|
||||
if (!app) {
|
||||
app = await createApp(client, args.values["app-name"]);
|
||||
console.log("Created app:", app.appId);
|
||||
}
|
||||
|
||||
let sp = await getServicePrincipal(client, app.appId);
|
||||
if (!sp) {
|
||||
sp = await createSp(client, app.appId);
|
||||
console.log("Created service principal:", sp.id);
|
||||
}
|
||||
|
||||
console.log(`The application and associated service principal are ready.
|
||||
App ID: ${app.appId}
|
||||
Service Principal ID: ${sp.id}`);
|
||||
}
|
||||
|
||||
await main().catch((e) => {
|
||||
console.error("Error in main:", e);
|
||||
console.error(e.stack);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user