@c8y/toolkit
Version:
Toolkit for Cumulocity applications, allows to e.g. deploy an application to an instance of Cumulocity.
57 lines • 2.8 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.deleteTenants = deleteTenants;
const client_1 = require("./client");
const chalk_1 = __importDefault(require("chalk"));
async function deleteTenants(options) {
const { user, password, url, tenant } = options;
const client = await (0, client_1.createClient)({ user, password, url, tenant });
console.log(chalk_1.default.green('Provided credentials are valid.'));
const { data: allTenants } = await client.tenant.list({
pageSize: 2000,
withApps: false,
withParents: true
});
const { companyName, age, includeChildren } = options;
const tenantsWithMatchingCompany = allTenants.filter((t) => {
return t.company && t.company.includes(companyName);
});
const milisecondsAgeOffset = age ? age * 60 * 60 * 1000 : 0;
const minimumAge = new Date(Date.now() - milisecondsAgeOffset);
const tenantsWithCorrectAge = tenantsWithMatchingCompany.filter((t) => {
return t.creationTime && new Date(t.creationTime) < minimumAge;
});
console.log(chalk_1.default.blue(`Found ${tenantsWithCorrectAge.length} tenants where the company name includes "${companyName}" and which are older than "${minimumAge.toISOString()}"`));
const childTenantsToRemove = [];
if (includeChildren) {
for (const tenant of tenantsWithCorrectAge) {
const tenantsWhereParentMatches = allTenants.filter(t => t.parent === tenant.id);
childTenantsToRemove.push(...tenantsWhereParentMatches);
}
console.log(chalk_1.default.blue(`Including ${childTenantsToRemove.length} child tenants for deletion...`));
}
const allTenantsToDelete = [...childTenantsToRemove, ...tenantsWithCorrectAge];
let deletionFailures = 0;
for (const tenant of allTenantsToDelete) {
console.log(chalk_1.default.blue(`Deleting tenant ${tenant.domain} (${tenant.id})...`));
try {
await client.tenant.delete(tenant.id);
console.log(chalk_1.default.green(`Tenant ${tenant.domain} (${tenant.id}) deleted successfully.`));
}
catch (e) {
console.error(chalk_1.default.red(`Failed to delete tenant ${tenant.domain} (${tenant.id}):`), e);
deletionFailures++;
}
}
if (deletionFailures > 0) {
console.error(chalk_1.default.red(`Failed to delete ${deletionFailures} tenants.`));
throw new Error('Some tenants could not be deleted.');
}
else {
console.log(chalk_1.default.green('All specified tenants deleted successfully.'));
}
}
//# sourceMappingURL=delete-tenants.js.map