137 lines
		
	
	
		
			6.6 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			137 lines
		
	
	
		
			6.6 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using Core.Blueprint.API.Controllers;
 | |
| using Core.Blueprint.External.Clients.Blueprint;
 | |
| using Core.Blueprint.External.Clients.Blueprint.Requests.KeyVault;
 | |
| using Lib.Architecture.BuildingBlocks;
 | |
| using Microsoft.AspNetCore.Mvc;
 | |
| using System.Text.Json;
 | |
| 
 | |
| namespace Core.Secret.API.Controllers
 | |
| {
 | |
|     /// <summary>
 | |
|     /// Handles all requests for secret.
 | |
|     /// </summary>
 | |
|     [ApiVersion("1.0")]
 | |
|     //[Route("api/v{version:apiVersion}/[controller]")]
 | |
|     [Consumes("application/json")]
 | |
|     [Produces("application/json")]
 | |
|     [ApiController]
 | |
|     public class KeyVaultController(IBlueprintServiceClient blueprintServiceClient, ILogger<KeyVaultController> logger) : BaseController(logger)
 | |
|     {
 | |
|         /// <summary>
 | |
|         /// Creates a new secret.
 | |
|         /// </summary>
 | |
|         [HttpPost("Create")]
 | |
|         [ProducesResponseType(StatusCodes.Status200OK)]
 | |
|         [ProducesResponseType(StatusCodes.Status400BadRequest)]
 | |
|         [ProducesResponseType(StatusCodes.Status401Unauthorized)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
 | |
|         [ProducesResponseType(StatusCodes.Status500InternalServerError)]
 | |
|         public async Task<IActionResult> CreateSecretService(CreateSecretRequest newSecret, CancellationToken cancellationToken)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 logger.LogInformation($"{nameof(CreateSecretService)} - Request received - Payload: {JsonSerializer.Serialize(newSecret)}");
 | |
| 
 | |
|                 if (newSecret == null) return BadRequest("Invalid secret object");
 | |
| 
 | |
|                 if (string.IsNullOrEmpty(newSecret.Name)) return BadRequest("Invalid secret name");
 | |
| 
 | |
|                 if (string.IsNullOrEmpty(newSecret.Value)) return BadRequest("Invalid secret description");
 | |
| 
 | |
|                 return await Handle(() => blueprintServiceClient.CreateSecretService(newSecret, cancellationToken)).ConfigureAwait(false);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 logger.LogError($"{nameof(CreateSecretService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(newSecret)}");
 | |
|                 throw;
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Gets the secret by identifier.
 | |
|         /// </summary>
 | |
|         [HttpPost("GetSecretByName")]
 | |
|         [ProducesResponseType(StatusCodes.Status200OK)]
 | |
|         [ProducesResponseType(StatusCodes.Status400BadRequest)]
 | |
|         [ProducesResponseType(StatusCodes.Status401Unauthorized)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
 | |
|         [ProducesResponseType(StatusCodes.Status500InternalServerError)]
 | |
|         public async Task<IActionResult> GetSecretByNameService(GetSecretRequest request, CancellationToken cancellationToken)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 logger.LogInformation($"{nameof(GetSecretByNameService)} - Request received - Payload: {JsonSerializer.Serialize(request)}");
 | |
| 
 | |
|                 if (string.IsNullOrEmpty(request.Name)) return BadRequest("Invalid secret name");
 | |
| 
 | |
|                 return await Handle(() => blueprintServiceClient.GetSecretByNameService(request, cancellationToken)).ConfigureAwait(false);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 logger.LogError($"{nameof(GetSecretByNameService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(request)}");
 | |
|                 throw;
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Updates a full secret by identifier.
 | |
|         /// </summary>
 | |
|         [HttpPut("Update")]
 | |
|         [ProducesResponseType(StatusCodes.Status200OK)]
 | |
|         [ProducesResponseType(StatusCodes.Status400BadRequest)]
 | |
|         [ProducesResponseType(StatusCodes.Status401Unauthorized)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
 | |
|         [ProducesResponseType(StatusCodes.Status500InternalServerError)]
 | |
|         public async Task<IActionResult> UpdateSecretService(UpdateSecretRequest newSecret, CancellationToken cancellationToken)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 logger.LogInformation($"{nameof(UpdateSecretService)} - Request received - Payload: {JsonSerializer.Serialize(newSecret)}");
 | |
| 
 | |
|                 if (newSecret == null) return BadRequest("Invalid secret object");
 | |
| 
 | |
|                 if (string.IsNullOrEmpty(newSecret.Name)) return BadRequest("Invalid secret name");
 | |
| 
 | |
|                 if (string.IsNullOrEmpty(newSecret.Value)) return BadRequest("Invalid secret value");
 | |
| 
 | |
|                 return await Handle(() => blueprintServiceClient.UpdateSecretService(newSecret, cancellationToken)).ConfigureAwait(false);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 logger.LogError($"{nameof(UpdateSecretService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(newSecret)}");
 | |
|                 throw;
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Deletes the secret by identifier.
 | |
|         /// </summary>
 | |
|         [HttpPost("Delete")]
 | |
|         [ProducesResponseType(StatusCodes.Status200OK)]
 | |
|         [ProducesResponseType(StatusCodes.Status400BadRequest)]
 | |
|         [ProducesResponseType(StatusCodes.Status401Unauthorized)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
 | |
|         [ProducesResponseType(StatusCodes.Status500InternalServerError)]
 | |
|         public async Task<IActionResult> DeleteSecretService(DeleteSecretRequest request, CancellationToken cancellationToken)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 logger.LogInformation($"{nameof(DeleteSecretService)} - Request received - Payload: {JsonSerializer.Serialize(request)}");
 | |
| 
 | |
|                 if (string.IsNullOrEmpty(request.Name)) return BadRequest("Invalid secret name");
 | |
| 
 | |
|                 return await Handle(() => blueprintServiceClient.DeleteSecretService(request, cancellationToken)).ConfigureAwait(false);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 logger.LogError($"{nameof(DeleteSecretService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(request)}");
 | |
|                 throw;
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| }
 | 
