Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

90 linhas
3.1KB

  1. using Confluent.Kafka;
  2. using Confluent.Kafka.Admin;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.OpenApi.Extensions;
  5. using Newtonsoft.Json;
  6. using Newtonsoft.Json.Linq;
  7. using TelpoKafkaConsole.Model;
  8. using TelpoKafkaConsole.Service;
  9. using TelpoKafkaConsole.WebApi.Controllers.Api;
  10. using TelpoKafkaConsole.WebApi.Model.Request;
  11. // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
  12. namespace TelpoKafkaConsole.WebApi.Controllers
  13. {
  14. [Route("api/[controller]")]
  15. [ApiController]
  16. public class ScramCredentialsUserController : ControllerBase
  17. {
  18. private readonly KafkaAdminService _servicekafkaAdmin;
  19. public ScramCredentialsUserController(KafkaAdminService kafkaAdminService)
  20. {
  21. _servicekafkaAdmin = kafkaAdminService;
  22. }
  23. // GET: api/<ScramCredentialsUserController>
  24. [HttpGet]
  25. public async Task<ApiResponse<object>> Get()
  26. {
  27. var usersScram = await _servicekafkaAdmin.DescribeUserScramCredentialsAsync(new List<string>());
  28. var users = usersScram
  29. .Select(i => new {i.User, ScramCredentialInfos=i.ScramCredentialInfos
  30. .Select(s=>new { Mechanism=s.Mechanism.GetDisplayName(),s.Iterations }) })
  31. .Where(i=>!i.User.Equals("superuser"));
  32. return ApiResponse<object>.Success(users);
  33. }
  34. // GET api/<ScramCredentialsUserController>/5
  35. [HttpGet("{username}")]
  36. public async Task<ApiResponse<object>> GetAsync(string username)
  37. {
  38. var users = new List<string>
  39. {
  40. username
  41. };
  42. var usersScram = await _servicekafkaAdmin.DescribeUserScramCredentialsAsync(users);
  43. var firstUserScram = usersScram.FirstOrDefault();
  44. var user = new
  45. {
  46. firstUserScram?.User,
  47. ScramCredentialInfos = firstUserScram?.ScramCredentialInfos
  48. .Select(i => new
  49. {
  50. Mechanism = i.Mechanism.GetDisplayName(),
  51. i.Iterations
  52. })
  53. };
  54. return ApiResponse<object>.Success(user);
  55. }
  56. // POST api/<ScramCredentialsUserController>
  57. [HttpPost]
  58. public async Task<ApiResponse<string>> PostAsync([FromBody] UserReq user)
  59. {
  60. ScramCredentialsUser scramUser = new()
  61. {
  62. Name = user.Name,
  63. Password = user.Password,
  64. };
  65. await _servicekafkaAdmin.AlterUserScramCredentialsAsync(scramUser);
  66. return ApiResponse<string>.Success($"创建 Scram 用户{user.Name}成功");
  67. }
  68. [HttpDelete("{username}")]
  69. public async Task<ApiResponse<string>> Delete(string username)
  70. {
  71. ScramCredentialsUser scramUser = new()
  72. {
  73. Name = username
  74. };
  75. await _servicekafkaAdmin.AlterUserScramCredentialsAsync(scramUser,"DELETE");
  76. return ApiResponse<string>.Success($"删除 Scram 用户{username} 成功");
  77. }
  78. }
  79. }