github.com/hack0072008/kafka-go@v1.0.1/describeconfigs.go (about)

     1  package kafka
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net"
     7  	"time"
     8  
     9  	"github.com/hack0072008/kafka-go/protocol/describeconfigs"
    10  )
    11  
    12  // DescribeConfigsRequest represents a request sent to a kafka broker to describe configs
    13  type DescribeConfigsRequest 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 []DescribeConfigRequestResource
    19  
    20  	// Ignored if API version is less than v1
    21  	IncludeSynonyms bool
    22  
    23  	// Ignored if API version is less than v3
    24  	IncludeDocumentation bool
    25  }
    26  
    27  type ResourceType int8
    28  
    29  const (
    30  	// See https://github.com/apache/kafka/blob/trunk/clients/src/main/java/org/apache/kafka/common/config/ConfigResource.java#L36
    31  	ResourceTypeUnknown ResourceType = 0
    32  	ResourceTypeTopic   ResourceType = 2
    33  	ResourceTypeBroker  ResourceType = 4
    34  )
    35  
    36  type DescribeConfigRequestResource struct {
    37  	// Resource Type
    38  	ResourceType ResourceType
    39  
    40  	// Resource Name
    41  	ResourceName string
    42  
    43  	// ConfigNames is a list of configurations to update.
    44  	ConfigNames []string
    45  }
    46  
    47  // DescribeConfigsResponse represents a response from a kafka broker to a describe config request.
    48  type DescribeConfigsResponse struct {
    49  	// The amount of time that the broker throttled the request.
    50  	Throttle time.Duration
    51  
    52  	// Resources
    53  	Resources []DescribeConfigResponseResource
    54  }
    55  
    56  // DescribeConfigResponseResource
    57  type DescribeConfigResponseResource struct {
    58  	// Resource Type
    59  	ResourceType int8
    60  
    61  	// Resource Name
    62  	ResourceName string
    63  
    64  	// Error
    65  	Error error
    66  
    67  	// ConfigEntries
    68  	ConfigEntries []DescribeConfigResponseConfigEntry
    69  }
    70  
    71  // DescribeConfigResponseConfigEntry
    72  type DescribeConfigResponseConfigEntry struct {
    73  	ConfigName  string
    74  	ConfigValue string
    75  	ReadOnly    bool
    76  
    77  	// Ignored if API version is greater than v0
    78  	IsDefault bool
    79  
    80  	// Ignored if API version is less than v1
    81  	ConfigSource int8
    82  
    83  	IsSensitive bool
    84  
    85  	// Ignored if API version is less than v1
    86  	ConfigSynonyms []DescribeConfigResponseConfigSynonym
    87  
    88  	// Ignored if API version is less than v3
    89  	ConfigType int8
    90  
    91  	// Ignored if API version is less than v3
    92  	ConfigDocumentation string
    93  }
    94  
    95  // DescribeConfigResponseConfigSynonym
    96  type DescribeConfigResponseConfigSynonym struct {
    97  	// Ignored if API version is less than v1
    98  	ConfigName string
    99  
   100  	// Ignored if API version is less than v1
   101  	ConfigValue string
   102  
   103  	// Ignored if API version is less than v1
   104  	ConfigSource int8
   105  }
   106  
   107  // DescribeConfigs sends a config altering request to a kafka broker and returns the
   108  // response.
   109  func (c *Client) DescribeConfigs(ctx context.Context, req *DescribeConfigsRequest) (*DescribeConfigsResponse, error) {
   110  	resources := make([]describeconfigs.RequestResource, len(req.Resources))
   111  
   112  	for i, t := range req.Resources {
   113  		resources[i] = describeconfigs.RequestResource{
   114  			ResourceType: int8(t.ResourceType),
   115  			ResourceName: t.ResourceName,
   116  			ConfigNames:  t.ConfigNames,
   117  		}
   118  	}
   119  
   120  	m, err := c.roundTrip(ctx, req.Addr, &describeconfigs.Request{
   121  		Resources:            resources,
   122  		IncludeSynonyms:      req.IncludeSynonyms,
   123  		IncludeDocumentation: req.IncludeDocumentation,
   124  	})
   125  
   126  	if err != nil {
   127  		return nil, fmt.Errorf("kafka.(*Client).DescribeConfigs: %w", err)
   128  	}
   129  
   130  	res := m.(*describeconfigs.Response)
   131  	ret := &DescribeConfigsResponse{
   132  		Throttle:  makeDuration(res.ThrottleTimeMs),
   133  		Resources: make([]DescribeConfigResponseResource, len(res.Resources)),
   134  	}
   135  
   136  	for i, t := range res.Resources {
   137  
   138  		configEntries := make([]DescribeConfigResponseConfigEntry, len(t.ConfigEntries))
   139  		for j, v := range t.ConfigEntries {
   140  
   141  			configSynonyms := make([]DescribeConfigResponseConfigSynonym, len(v.ConfigSynonyms))
   142  			for k, cs := range v.ConfigSynonyms {
   143  				configSynonyms[k] = DescribeConfigResponseConfigSynonym{
   144  					ConfigName:   cs.ConfigName,
   145  					ConfigValue:  cs.ConfigValue,
   146  					ConfigSource: cs.ConfigSource,
   147  				}
   148  			}
   149  
   150  			configEntries[j] = DescribeConfigResponseConfigEntry{
   151  				ConfigName:          v.ConfigName,
   152  				ConfigValue:         v.ConfigValue,
   153  				ReadOnly:            v.ReadOnly,
   154  				ConfigSource:        v.ConfigSource,
   155  				IsDefault:           v.IsDefault,
   156  				IsSensitive:         v.IsSensitive,
   157  				ConfigSynonyms:      configSynonyms,
   158  				ConfigType:          v.ConfigType,
   159  				ConfigDocumentation: v.ConfigDocumentation,
   160  			}
   161  		}
   162  
   163  		ret.Resources[i] = DescribeConfigResponseResource{
   164  			ResourceType:  t.ResourceType,
   165  			ResourceName:  t.ResourceName,
   166  			Error:         makeError(t.ErrorCode, t.ErrorMessage),
   167  			ConfigEntries: configEntries,
   168  		}
   169  	}
   170  
   171  	return ret, nil
   172  }