alterconfigs.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package kafka
  2. import (
  3. "context"
  4. "fmt"
  5. "net"
  6. "time"
  7. "github.com/segmentio/kafka-go/protocol/alterconfigs"
  8. )
  9. // AlterConfigsRequest represents a request sent to a kafka broker to alter configs
  10. type AlterConfigsRequest struct {
  11. // Address of the kafka broker to send the request to.
  12. Addr net.Addr
  13. // List of resources to update.
  14. Resources []AlterConfigRequestResource
  15. // When set to true, topics are not created but the configuration is
  16. // validated as if they were.
  17. ValidateOnly bool
  18. }
  19. type AlterConfigRequestResource struct {
  20. // Resource Type
  21. ResourceType ResourceType
  22. // Resource Name
  23. ResourceName string
  24. // Configs is a list of configuration updates.
  25. Configs []AlterConfigRequestConfig
  26. }
  27. type AlterConfigRequestConfig struct {
  28. // Configuration key name
  29. Name string
  30. // The value to set for the configuration key.
  31. Value string
  32. }
  33. // AlterConfigsResponse represents a response from a kafka broker to an alter config request.
  34. type AlterConfigsResponse struct {
  35. // Duration for which the request was throttled due to a quota violation.
  36. Throttle time.Duration
  37. // Mapping of topic names to errors that occurred while attempting to create
  38. // the topics.
  39. //
  40. // The errors contain the kafka error code. Programs may use the standard
  41. // errors.Is function to test the error against kafka error codes.
  42. Errors map[AlterConfigsResponseResource]error
  43. }
  44. // AlterConfigsResponseResource
  45. type AlterConfigsResponseResource struct {
  46. Type int8
  47. Name string
  48. }
  49. // AlterConfigs sends a config altering request to a kafka broker and returns the
  50. // response.
  51. func (c *Client) AlterConfigs(ctx context.Context, req *AlterConfigsRequest) (*AlterConfigsResponse, error) {
  52. resources := make([]alterconfigs.RequestResources, len(req.Resources))
  53. for i, t := range req.Resources {
  54. configs := make([]alterconfigs.RequestConfig, len(t.Configs))
  55. for j, v := range t.Configs {
  56. configs[j] = alterconfigs.RequestConfig{
  57. Name: v.Name,
  58. Value: v.Value,
  59. }
  60. }
  61. resources[i] = alterconfigs.RequestResources{
  62. ResourceType: int8(t.ResourceType),
  63. ResourceName: t.ResourceName,
  64. Configs: configs,
  65. }
  66. }
  67. m, err := c.roundTrip(ctx, req.Addr, &alterconfigs.Request{
  68. Resources: resources,
  69. ValidateOnly: req.ValidateOnly,
  70. })
  71. if err != nil {
  72. return nil, fmt.Errorf("kafka.(*Client).AlterConfigs: %w", err)
  73. }
  74. res := m.(*alterconfigs.Response)
  75. ret := &AlterConfigsResponse{
  76. Throttle: makeDuration(res.ThrottleTimeMs),
  77. Errors: make(map[AlterConfigsResponseResource]error, len(res.Responses)),
  78. }
  79. for _, t := range res.Responses {
  80. ret.Errors[AlterConfigsResponseResource{
  81. Type: t.ResourceType,
  82. Name: t.ResourceName,
  83. }] = makeError(t.ErrorCode, t.ErrorMessage)
  84. }
  85. return ret, nil
  86. }