@cloud-copilot/iam-data
Version:
75 lines • 2.6 kB
JavaScript
import { readCategoryDetails, readDataFile } from './data.js';
import { iamServiceExists } from './services.js';
/**
* Get all service category keys.
*
* @returns an array of all service category keys
*/
export async function serviceCategories() {
return readDataFile('categories.json');
}
/**
* Check if a service category exists.
*
* @param category the category key to check, is case insensitive
* @returns true if the category exists, false otherwise
*/
export async function serviceCategoryExists(category) {
const categoryNames = await readDataFile('categoryNames.json');
return !!categoryNames[category.toLowerCase()];
}
/**
* Get the IAM service to category mapping.
*
* @returns a map of IAM service keys to category keys
*/
export async function iamServiceCategoryMap() {
return readDataFile('serviceCategories.json');
}
/**
* Get IAM service keys assigned to a service category.
*
* @param category the category key to get IAM services for, is case insensitive
* @throws an error if the category does not exist
* @returns sorted IAM service keys assigned to the category
*/
export async function iamServicesForCategory(category) {
const categoryDetails = await getServiceCategory(category);
return categoryDetails.services;
}
/**
* Get the service category key for an IAM service.
*
* @param service the IAM service key to get the category for, is case insensitive
* @throws an error if the service does not exist or has no category mapping
* @returns the category key for the IAM service
*/
export async function categoryForIamService(service) {
const serviceKey = service.toLowerCase();
const serviceExists = await iamServiceExists(serviceKey);
if (!serviceExists) {
throw new Error(`Service ${service} does not exist`);
}
const serviceCategories = await iamServiceCategoryMap();
const category = serviceCategories[serviceKey];
if (!category) {
throw new Error(`Category for service ${service} does not exist`);
}
return category;
}
/**
* Get details for a service category.
*
* @param category the category key to get details for, is case insensitive
* @throws an error if the category does not exist
* @returns details for the service category
*/
export async function getServiceCategory(category) {
const categoryKey = category.toLowerCase();
const exists = await serviceCategoryExists(categoryKey);
if (!exists) {
throw new Error(`Category ${category} does not exist`);
}
return readCategoryDetails(categoryKey);
}
//# sourceMappingURL=categories.js.map