github.com/streamdal/segmentio-kafka-go@v0.4.47-streamdal/incrementalalterconfigs.go (about)

     1  package kafka
     2  
     3  import (
     4  	"context"
     5  	"net"
     6  
     7  	"github.com/segmentio/kafka-go/protocol/incrementalalterconfigs"
     8  )
     9  
    10  type ConfigOperation int8
    11  
    12  const (
    13  	ConfigOperationSet      ConfigOperation = 0
    14  	ConfigOperationDelete   ConfigOperation = 1
    15  	ConfigOperationAppend   ConfigOperation = 2
    16  	ConfigOperationSubtract ConfigOperation = 3
    17  )
    18  
    19  // IncrementalAlterConfigsRequest is a request to the IncrementalAlterConfigs API.
    20  type IncrementalAlterConfigsRequest struct {
    21  	// Addr is the address of the kafka broker to send the request to.
    22  	Addr net.Addr
    23  
    24  	// Resources contains the list of resources to update configs for.
    25  	Resources []IncrementalAlterConfigsRequestResource
    26  
    27  	// ValidateOnly indicates whether Kafka should validate the changes without actually
    28  	// applying them.
    29  	ValidateOnly bool
    30  }
    31  
    32  // IncrementalAlterConfigsRequestResource contains the details of a single resource type whose
    33  // configs should be altered.
    34  type IncrementalAlterConfigsRequestResource struct {
    35  	// ResourceType is the type of resource to update.
    36  	ResourceType ResourceType
    37  
    38  	// ResourceName is the name of the resource to update (i.e., topic name or broker ID).
    39  	ResourceName string
    40  
    41  	// Configs contains the list of config key/values to update.
    42  	Configs []IncrementalAlterConfigsRequestConfig
    43  }
    44  
    45  // IncrementalAlterConfigsRequestConfig describes a single config key/value pair that should
    46  // be altered.
    47  type IncrementalAlterConfigsRequestConfig struct {
    48  	// Name is the name of the config.
    49  	Name string
    50  
    51  	// Value is the value to set for this config.
    52  	Value string
    53  
    54  	// ConfigOperation indicates how this config should be updated (e.g., add, delete, etc.).
    55  	ConfigOperation ConfigOperation
    56  }
    57  
    58  // IncrementalAlterConfigsResponse is a response from the IncrementalAlterConfigs API.
    59  type IncrementalAlterConfigsResponse struct {
    60  	// Resources contains details of each resource config that was updated.
    61  	Resources []IncrementalAlterConfigsResponseResource
    62  }
    63  
    64  // IncrementalAlterConfigsResponseResource contains the response details for a single resource
    65  // whose configs were updated.
    66  type IncrementalAlterConfigsResponseResource struct {
    67  	// Error is set to a non-nil value if an error occurred while updating this specific
    68  	// config.
    69  	Error error
    70  
    71  	// ResourceType is the type of resource that was updated.
    72  	ResourceType ResourceType
    73  
    74  	// ResourceName is the name of the resource that was updated.
    75  	ResourceName string
    76  }
    77  
    78  func (c *Client) IncrementalAlterConfigs(
    79  	ctx context.Context,
    80  	req *IncrementalAlterConfigsRequest,
    81  ) (*IncrementalAlterConfigsResponse, error) {
    82  	apiReq := &incrementalalterconfigs.Request{
    83  		ValidateOnly: req.ValidateOnly,
    84  	}
    85  
    86  	for _, res := range req.Resources {
    87  		apiRes := incrementalalterconfigs.RequestResource{
    88  			ResourceType: int8(res.ResourceType),
    89  			ResourceName: res.ResourceName,
    90  		}
    91  
    92  		for _, config := range res.Configs {
    93  			apiRes.Configs = append(
    94  				apiRes.Configs,
    95  				incrementalalterconfigs.RequestConfig{
    96  					Name:            config.Name,
    97  					Value:           config.Value,
    98  					ConfigOperation: int8(config.ConfigOperation),
    99  				},
   100  			)
   101  		}
   102  
   103  		apiReq.Resources = append(
   104  			apiReq.Resources,
   105  			apiRes,
   106  		)
   107  	}
   108  
   109  	protoResp, err := c.roundTrip(
   110  		ctx,
   111  		req.Addr,
   112  		apiReq,
   113  	)
   114  	if err != nil {
   115  		return nil, err
   116  	}
   117  
   118  	resp := &IncrementalAlterConfigsResponse{}
   119  
   120  	apiResp := protoResp.(*incrementalalterconfigs.Response)
   121  	for _, res := range apiResp.Responses {
   122  		resp.Resources = append(
   123  			resp.Resources,
   124  			IncrementalAlterConfigsResponseResource{
   125  				Error:        makeError(res.ErrorCode, res.ErrorMessage),
   126  				ResourceType: ResourceType(res.ResourceType),
   127  				ResourceName: res.ResourceName,
   128  			},
   129  		)
   130  	}
   131  
   132  	return resp, nil
   133  }