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

     1  package kafka
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net"
     7  	"time"
     8  
     9  	"github.com/segmentio/kafka-go/protocol/describeuserscramcredentials"
    10  )
    11  
    12  // DescribeUserScramCredentialsRequest represents a request sent to a kafka broker to
    13  // describe user scram credentials.
    14  type DescribeUserScramCredentialsRequest struct {
    15  	// Address of the kafka broker to send the request to.
    16  	Addr net.Addr
    17  
    18  	// List of Scram users to describe
    19  	Users []UserScramCredentialsUser
    20  }
    21  
    22  type UserScramCredentialsUser struct {
    23  	Name string
    24  }
    25  
    26  // DescribeUserScramCredentialsResponse represents a response from a kafka broker to a describe user
    27  // credentials request.
    28  type DescribeUserScramCredentialsResponse struct {
    29  	// The amount of time that the broker throttled the request.
    30  	Throttle time.Duration
    31  
    32  	// Top level error that occurred while attempting to describe
    33  	// the user scram credentials.
    34  	//
    35  	// The errors contain the kafka error code. Programs may use the standard
    36  	// errors.Is function to test the error against kafka error codes.
    37  	Error error
    38  
    39  	// List of described user scram credentials.
    40  	Results []DescribeUserScramCredentialsResponseResult
    41  }
    42  
    43  type DescribeUserScramCredentialsResponseResult struct {
    44  	User            string
    45  	CredentialInfos []DescribeUserScramCredentialsCredentialInfo
    46  	Error           error
    47  }
    48  
    49  type DescribeUserScramCredentialsCredentialInfo struct {
    50  	Mechanism  ScramMechanism
    51  	Iterations int
    52  }
    53  
    54  // DescribeUserScramCredentials sends a user scram credentials describe request to a kafka broker and returns
    55  // the response.
    56  func (c *Client) DescribeUserScramCredentials(ctx context.Context, req *DescribeUserScramCredentialsRequest) (*DescribeUserScramCredentialsResponse, error) {
    57  	users := make([]describeuserscramcredentials.RequestUser, len(req.Users))
    58  
    59  	for userIdx, user := range req.Users {
    60  		users[userIdx] = describeuserscramcredentials.RequestUser{
    61  			Name: user.Name,
    62  		}
    63  	}
    64  
    65  	m, err := c.roundTrip(ctx, req.Addr, &describeuserscramcredentials.Request{
    66  		Users: users,
    67  	})
    68  	if err != nil {
    69  		return nil, fmt.Errorf("kafka.(*Client).DescribeUserScramCredentials: %w", err)
    70  	}
    71  
    72  	res := m.(*describeuserscramcredentials.Response)
    73  	responseResults := make([]DescribeUserScramCredentialsResponseResult, len(res.Results))
    74  
    75  	for responseIdx, responseResult := range res.Results {
    76  		credentialInfos := make([]DescribeUserScramCredentialsCredentialInfo, len(responseResult.CredentialInfos))
    77  
    78  		for credentialInfoIdx, credentialInfo := range responseResult.CredentialInfos {
    79  			credentialInfos[credentialInfoIdx] = DescribeUserScramCredentialsCredentialInfo{
    80  				Mechanism:  ScramMechanism(credentialInfo.Mechanism),
    81  				Iterations: int(credentialInfo.Iterations),
    82  			}
    83  		}
    84  		responseResults[responseIdx] = DescribeUserScramCredentialsResponseResult{
    85  			User:            responseResult.User,
    86  			CredentialInfos: credentialInfos,
    87  			Error:           makeError(responseResult.ErrorCode, responseResult.ErrorMessage),
    88  		}
    89  	}
    90  	ret := &DescribeUserScramCredentialsResponse{
    91  		Throttle: makeDuration(res.ThrottleTimeMs),
    92  		Error:    makeError(res.ErrorCode, res.ErrorMessage),
    93  		Results:  responseResults,
    94  	}
    95  
    96  	return ret, nil
    97  }