github.com/hoveychen/kafka-go@v0.4.42/createacls.go (about)

     1  package kafka
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net"
     7  	"time"
     8  
     9  	"github.com/hoveychen/kafka-go/protocol/createacls"
    10  )
    11  
    12  // CreateACLsRequest represents a request sent to a kafka broker to add
    13  // new ACLs.
    14  type CreateACLsRequest struct {
    15  	// Address of the kafka broker to send the request to.
    16  	Addr net.Addr
    17  
    18  	// List of ACL to create.
    19  	ACLs []ACLEntry
    20  }
    21  
    22  // CreateACLsResponse represents a response from a kafka broker to an ACL
    23  // creation request.
    24  type CreateACLsResponse struct {
    25  	// The amount of time that the broker throttled the request.
    26  	Throttle time.Duration
    27  
    28  	// List of errors that occurred while attempting to create
    29  	// the ACLs.
    30  	//
    31  	// The errors contain the kafka error code. Programs may use the standard
    32  	// errors.Is function to test the error against kafka error codes.
    33  	Errors []error
    34  }
    35  
    36  type ACLPermissionType int8
    37  
    38  const (
    39  	ACLPermissionTypeUnknown ACLPermissionType = 0
    40  	ACLPermissionTypeAny     ACLPermissionType = 1
    41  	ACLPermissionTypeDeny    ACLPermissionType = 2
    42  	ACLPermissionTypeAllow   ACLPermissionType = 3
    43  )
    44  
    45  type ACLOperationType int8
    46  
    47  const (
    48  	ACLOperationTypeUnknown         ACLOperationType = 0
    49  	ACLOperationTypeAny             ACLOperationType = 1
    50  	ACLOperationTypeAll             ACLOperationType = 2
    51  	ACLOperationTypeRead            ACLOperationType = 3
    52  	ACLOperationTypeWrite           ACLOperationType = 4
    53  	ACLOperationTypeCreate          ACLOperationType = 5
    54  	ACLOperationTypeDelete          ACLOperationType = 6
    55  	ACLOperationTypeAlter           ACLOperationType = 7
    56  	ACLOperationTypeDescribe        ACLOperationType = 8
    57  	ACLOperationTypeClusterAction   ACLOperationType = 9
    58  	ACLOperationTypeDescribeConfigs ACLOperationType = 10
    59  	ACLOperationTypeAlterConfigs    ACLOperationType = 11
    60  	ACLOperationTypeIdempotentWrite ACLOperationType = 12
    61  )
    62  
    63  type ACLEntry struct {
    64  	ResourceType        ResourceType
    65  	ResourceName        string
    66  	ResourcePatternType PatternType
    67  	Principal           string
    68  	Host                string
    69  	Operation           ACLOperationType
    70  	PermissionType      ACLPermissionType
    71  }
    72  
    73  // CreateACLs sends ACLs creation request to a kafka broker and returns the
    74  // response.
    75  func (c *Client) CreateACLs(ctx context.Context, req *CreateACLsRequest) (*CreateACLsResponse, error) {
    76  	acls := make([]createacls.RequestACLs, 0, len(req.ACLs))
    77  
    78  	for _, acl := range req.ACLs {
    79  		acls = append(acls, createacls.RequestACLs{
    80  			ResourceType:        int8(acl.ResourceType),
    81  			ResourceName:        acl.ResourceName,
    82  			ResourcePatternType: int8(acl.ResourcePatternType),
    83  			Principal:           acl.Principal,
    84  			Host:                acl.Host,
    85  			Operation:           int8(acl.Operation),
    86  			PermissionType:      int8(acl.PermissionType),
    87  		})
    88  	}
    89  
    90  	m, err := c.roundTrip(ctx, req.Addr, &createacls.Request{
    91  		Creations: acls,
    92  	})
    93  	if err != nil {
    94  		return nil, fmt.Errorf("kafka.(*Client).CreateACLs: %w", err)
    95  	}
    96  
    97  	res := m.(*createacls.Response)
    98  	ret := &CreateACLsResponse{
    99  		Throttle: makeDuration(res.ThrottleTimeMs),
   100  		Errors:   make([]error, 0, len(res.Results)),
   101  	}
   102  
   103  	for _, t := range res.Results {
   104  		ret.Errors = append(ret.Errors, makeError(t.ErrorCode, t.ErrorMessage))
   105  	}
   106  
   107  	return ret, nil
   108  }