github.com/segmentio/kafka-go@v0.4.48-0.20240318174348-3f6244eb34fd/alterconfigs.go (about)

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