github.com/deanMdreon/kafka-go@v0.4.32/alterconfigs.go (about)

     1  package kafka
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net"
     7  	"time"
     8  
     9  	"github.com/deanMdreon/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
    58  type AlterConfigsResponseResource struct {
    59  	Type int8
    60  	Name string
    61  }
    62  
    63  // AlterConfigs sends a config altering request to a kafka broker and returns the
    64  // response.
    65  func (c *Client) AlterConfigs(ctx context.Context, req *AlterConfigsRequest) (*AlterConfigsResponse, error) {
    66  	resources := make([]alterconfigs.RequestResources, len(req.Resources))
    67  
    68  	for i, t := range req.Resources {
    69  		configs := make([]alterconfigs.RequestConfig, len(t.Configs))
    70  		for j, v := range t.Configs {
    71  			configs[j] = alterconfigs.RequestConfig{
    72  				Name:  v.Name,
    73  				Value: v.Value,
    74  			}
    75  		}
    76  		resources[i] = alterconfigs.RequestResources{
    77  			ResourceType: int8(t.ResourceType),
    78  			ResourceName: t.ResourceName,
    79  			Configs:      configs,
    80  		}
    81  	}
    82  
    83  	m, err := c.roundTrip(ctx, req.Addr, &alterconfigs.Request{
    84  		Resources:    resources,
    85  		ValidateOnly: req.ValidateOnly,
    86  	})
    87  
    88  	if err != nil {
    89  		return nil, fmt.Errorf("kafka.(*Client).AlterConfigs: %w", err)
    90  	}
    91  
    92  	res := m.(*alterconfigs.Response)
    93  	ret := &AlterConfigsResponse{
    94  		Throttle: makeDuration(res.ThrottleTimeMs),
    95  		Errors:   make(map[AlterConfigsResponseResource]error, len(res.Responses)),
    96  	}
    97  
    98  	for _, t := range res.Responses {
    99  		ret.Errors[AlterConfigsResponseResource{
   100  			Type: t.ResourceType,
   101  			Name: t.ResourceName,
   102  		}] = makeError(t.ErrorCode, t.ErrorMessage)
   103  	}
   104  
   105  	return ret, nil
   106  }