github.com/aiven/aiven-go-client@v1.36.0/kafka_acl.go (about) 1 package aiven 2 3 import ( 4 "fmt" 5 ) 6 7 type ( 8 // KafkaACLHandler is the client which interacts with the Kafka ACL endpoints 9 // on Aiven. 10 KafkaACLHandler struct { 11 client *Client 12 } 13 14 // CreateKafkaACLRequest are the parameters used to create a Kafka ACL entry. 15 CreateKafkaACLRequest struct { 16 Permission string `json:"permission"` 17 Topic string `json:"topic"` 18 Username string `json:"username"` 19 } 20 21 // KafkaACLResponse represents the response from Aiven after interacting with 22 // the Kafka ACL API. 23 KafkaACLResponse struct { 24 APIResponse 25 ACL []*KafkaACL `json:"acl"` 26 } 27 ) 28 29 // Create creates new Kafka ACL entry. 30 func (h *KafkaACLHandler) Create(project, service string, req CreateKafkaACLRequest) (*KafkaACL, error) { 31 path := buildPath("project", project, "service", service, "acl") 32 bts, err := h.client.doPostRequest(path, req) 33 if err != nil { 34 return nil, err 35 } 36 37 var rsp KafkaACLResponse 38 if err := checkAPIResponse(bts, &rsp); err != nil { 39 return nil, err 40 } 41 42 // The server doesn't return the ACL we created but list of all ACLs currently 43 // defined. Need to find the correct one manually. There could be multiple ACLs 44 // with same attributes. Assume the one that was created is the last one matching. 45 var foundACL *KafkaACL 46 for _, acl := range rsp.ACL { 47 if acl.Permission == req.Permission && acl.Topic == req.Topic && acl.Username == req.Username { 48 foundACL = acl 49 } 50 } 51 52 if foundACL == nil { 53 return nil, fmt.Errorf("created ACL not found from response ACL list") 54 } 55 56 return foundACL, nil 57 } 58 59 // Get gets a specific Kafka ACL. 60 func (h *KafkaACLHandler) Get(project, serviceName, aclID string) (*KafkaACL, error) { 61 // There's no API for getting individual ACL entry. List instead and filter from there 62 acls, err := h.List(project, serviceName) 63 if err != nil { 64 return nil, err 65 } 66 67 for _, acl := range acls { 68 if acl.ID == aclID { 69 return acl, nil 70 } 71 } 72 73 err = Error{Message: fmt.Sprintf("ACL with ID %v not found", aclID), Status: 404} 74 return nil, err 75 } 76 77 // List lists all the Kafka ACL entries. 78 func (h *KafkaACLHandler) List(project, serviceName string) ([]*KafkaACL, error) { 79 // There's no API for listing Kafka ACL entries. Need to get them from 80 // service info instead 81 service, err := h.client.Services.Get(project, serviceName) 82 if err != nil { 83 return nil, err 84 } 85 86 return service.ACL, nil 87 } 88 89 // Delete deletes a specific Kafka ACL entry. 90 func (h *KafkaACLHandler) Delete(project, serviceName, aclID string) error { 91 path := buildPath("project", project, "service", serviceName, "acl", aclID) 92 bts, err := h.client.doDeleteRequest(path, nil) 93 if err != nil { 94 return err 95 } 96 97 return checkAPIResponse(bts, nil) 98 }