alter_configs_request.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package sarama
  2. // AlterConfigsRequest is an alter config request type
  3. type AlterConfigsRequest struct {
  4. Version int16
  5. Resources []*AlterConfigsResource
  6. ValidateOnly bool
  7. }
  8. // AlterConfigsResource is an alter config resource type
  9. type AlterConfigsResource struct {
  10. Type ConfigResourceType
  11. Name string
  12. ConfigEntries map[string]*string
  13. }
  14. func (a *AlterConfigsRequest) encode(pe packetEncoder) error {
  15. if err := pe.putArrayLength(len(a.Resources)); err != nil {
  16. return err
  17. }
  18. for _, r := range a.Resources {
  19. if err := r.encode(pe); err != nil {
  20. return err
  21. }
  22. }
  23. pe.putBool(a.ValidateOnly)
  24. return nil
  25. }
  26. func (a *AlterConfigsRequest) decode(pd packetDecoder, version int16) error {
  27. resourceCount, err := pd.getArrayLength()
  28. if err != nil {
  29. return err
  30. }
  31. a.Resources = make([]*AlterConfigsResource, resourceCount)
  32. for i := range a.Resources {
  33. r := &AlterConfigsResource{}
  34. err = r.decode(pd, version)
  35. if err != nil {
  36. return err
  37. }
  38. a.Resources[i] = r
  39. }
  40. validateOnly, err := pd.getBool()
  41. if err != nil {
  42. return err
  43. }
  44. a.ValidateOnly = validateOnly
  45. return nil
  46. }
  47. func (a *AlterConfigsResource) encode(pe packetEncoder) error {
  48. pe.putInt8(int8(a.Type))
  49. if err := pe.putString(a.Name); err != nil {
  50. return err
  51. }
  52. if err := pe.putArrayLength(len(a.ConfigEntries)); err != nil {
  53. return err
  54. }
  55. for configKey, configValue := range a.ConfigEntries {
  56. if err := pe.putString(configKey); err != nil {
  57. return err
  58. }
  59. if err := pe.putNullableString(configValue); err != nil {
  60. return err
  61. }
  62. }
  63. return nil
  64. }
  65. func (a *AlterConfigsResource) decode(pd packetDecoder, version int16) error {
  66. t, err := pd.getInt8()
  67. if err != nil {
  68. return err
  69. }
  70. a.Type = ConfigResourceType(t)
  71. name, err := pd.getString()
  72. if err != nil {
  73. return err
  74. }
  75. a.Name = name
  76. n, err := pd.getArrayLength()
  77. if err != nil {
  78. return err
  79. }
  80. if n > 0 {
  81. a.ConfigEntries = make(map[string]*string, n)
  82. for i := 0; i < n; i++ {
  83. configKey, err := pd.getString()
  84. if err != nil {
  85. return err
  86. }
  87. if a.ConfigEntries[configKey], err = pd.getNullableString(); err != nil {
  88. return err
  89. }
  90. }
  91. }
  92. return err
  93. }
  94. func (a *AlterConfigsRequest) key() int16 {
  95. return 33
  96. }
  97. func (a *AlterConfigsRequest) version() int16 {
  98. return a.Version
  99. }
  100. func (a *AlterConfigsRequest) headerVersion() int16 {
  101. return 1
  102. }
  103. func (a *AlterConfigsRequest) isValidVersion() bool {
  104. return a.Version >= 0 && a.Version <= 1
  105. }
  106. func (a *AlterConfigsRequest) requiredVersion() KafkaVersion {
  107. switch a.Version {
  108. case 1:
  109. return V2_0_0_0
  110. case 0:
  111. return V0_11_0_0
  112. default:
  113. return V2_0_0_0
  114. }
  115. }