infrastructure.routes.tsā¢6.27 kB
import { Router, Request, Response } from 'express';
import { InfrastructureService } from '../services/infrastructure.service';
import { validateResourceQuery } from '../utils/validators';
import { logger } from '../utils/logger';
export const createInfrastructureRoutes = (infrastructureService: InfrastructureService): Router => {
const router = Router();
/**
* @swagger
* /api/infrastructure/deploy:
* post:
* summary: Deploy AWS infrastructure
* tags: [Infrastructure]
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - resourceType
* - action
* - parameters
* properties:
* resourceType:
* type: string
* enum: [ec2, s3, dynamodb, lambda, iam-role]
* action:
* type: string
* enum: [create]
* parameters:
* type: object
* responses:
* 200:
* description: Resource successfully deployed
* 400:
* description: Invalid request
* 500:
* description: Server error
*/
router.post('/deploy', async (req: Request, res: Response) => {
try {
const query = validateResourceQuery({ ...req.body, action: 'create' });
const result = await infrastructureService.processQuery(query);
res.json(result);
} catch (error: any) {
logger.error('Error deploying infrastructure:', error);
res.status(error.message.includes('Validation') ? 400 : 500).json({
success: false,
message: error.message,
});
}
});
/**
* @swagger
* /api/infrastructure/destroy:
* delete:
* summary: Destroy AWS infrastructure
* tags: [Infrastructure]
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - resourceType
* - action
* - resourceId
* properties:
* resourceType:
* type: string
* enum: [ec2, s3, dynamodb, lambda, iam-role]
* action:
* type: string
* enum: [delete]
* resourceId:
* type: string
* responses:
* 200:
* description: Resource successfully destroyed
* 400:
* description: Invalid request
* 404:
* description: Resource not found
* 500:
* description: Server error
*/
router.delete('/destroy', async (req: Request, res: Response) => {
try {
const query = validateResourceQuery({ ...req.body, action: 'delete' });
const result = await infrastructureService.processQuery(query);
res.json(result);
} catch (error: any) {
logger.error('Error destroying infrastructure:', error);
const statusCode = error.message.includes('Validation') ? 400 :
error.message.includes('not found') ? 404 : 500;
res.status(statusCode).json({
success: false,
message: error.message,
});
}
});
/**
* @swagger
* /api/infrastructure/query:
* post:
* summary: Query infrastructure using structured query
* tags: [Infrastructure]
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* responses:
* 200:
* description: Query successfully processed
* 400:
* description: Invalid query
* 500:
* description: Server error
*/
router.post('/query', async (req: Request, res: Response) => {
try {
const query = validateResourceQuery(req.body.query || req.body);
const result = await infrastructureService.processQuery(query);
res.json(result);
} catch (error: any) {
logger.error('Error processing query:', error);
res.status(error.message.includes('Validation') ? 400 : 500).json({
success: false,
message: error.message,
});
}
});
/**
* @swagger
* /api/infrastructure/resources:
* get:
* summary: Get all deployed resources
* tags: [Infrastructure]
* responses:
* 200:
* description: List of all deployed resources
* 500:
* description: Server error
*/
router.get('/resources', async (req: Request, res: Response) => {
try {
const resources = infrastructureService.getAllResources();
res.json({
success: true,
resources,
});
} catch (error: any) {
logger.error('Error getting resources:', error);
res.status(500).json({
success: false,
message: error.message,
});
}
});
/**
* @swagger
* /api/infrastructure/resources/{resourceId}:
* get:
* summary: Get a specific resource by ID
* tags: [Infrastructure]
* parameters:
* - in: path
* name: resourceId
* required: true
* schema:
* type: string
* description: The resource ID
* responses:
* 200:
* description: Resource details
* 404:
* description: Resource not found
* 500:
* description: Server error
*/
router.get('/resources/:resourceId', async (req: Request, res: Response) => {
try {
const resource = infrastructureService.getResourceStatus(req.params.resourceId);
if (!resource) {
return res.status(404).json({
success: false,
message: 'Resource not found',
});
}
res.json({
success: true,
resource,
});
} catch (error: any) {
logger.error('Error getting resource:', error);
res.status(500).json({
success: false,
message: error.message,
});
}
});
return router;
};