github.com/aiven/aiven-go-client@v1.36.0/kafka_schema_registry_acl.go (about)

     1  package aiven
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  type (
     8  	// KafkaSchemaRegistryACLHandler is the client which interacts with the Kafka Schema Registry ACL endpoints
     9  	// on Aiven.
    10  	KafkaSchemaRegistryACLHandler struct {
    11  		client *Client
    12  	}
    13  
    14  	// CreateKafkaSchemaRegistryACLRequest are the parameters used to create a Kafka Schema Registry ACL entry.
    15  	CreateKafkaSchemaRegistryACLRequest struct {
    16  		Permission string `json:"permission"`
    17  		Resource   string `json:"resource"`
    18  		Username   string `json:"username"`
    19  	}
    20  
    21  	// KafkaSchemaRegistryACLResponse represents the response from Aiven after interacting with
    22  	// the Kafka Schema Registry ACL API.
    23  	KafkaSchemaRegistryACLResponse struct {
    24  		APIResponse
    25  		ACL []*KafkaSchemaRegistryACL `json:"acl"`
    26  	}
    27  )
    28  
    29  // Create creates new Kafka Schema Registry ACL entry.
    30  func (h *KafkaSchemaRegistryACLHandler) Create(project, service string, req CreateKafkaSchemaRegistryACLRequest) (*KafkaSchemaRegistryACL, error) {
    31  	path := buildPath("project", project, "service", service, "kafka", "schema-registry", "acl")
    32  	bts, err := h.client.doPostRequest(path, req)
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  
    37  	var rsp KafkaSchemaRegistryACLResponse
    38  	if err := checkAPIResponse(bts, &rsp); err != nil {
    39  		return nil, err
    40  	}
    41  
    42  	// The server doesn't return the Schema Registry 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 *KafkaSchemaRegistryACL
    46  	for _, acl := range rsp.ACL {
    47  		if acl.Permission == req.Permission && acl.Resource == req.Resource && 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 Schema Registry ACL.
    60  func (h *KafkaSchemaRegistryACLHandler) Get(project, serviceName, aclID string) (*KafkaSchemaRegistryACL, 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("Schema Registry ACL with ID %v not found", aclID), Status: 404}
    74  	return nil, err
    75  }
    76  
    77  // List lists all the Kafka Schema Registry ACL entries.
    78  func (h *KafkaSchemaRegistryACLHandler) List(project, serviceName string) ([]*KafkaSchemaRegistryACL, error) {
    79  	// Get Kafka Schema Registry ACL entries from service info, as in Kafka ACLs.
    80  	service, err := h.client.Services.Get(project, serviceName)
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  
    85  	return service.SchemaRegistryACL, nil
    86  }
    87  
    88  // Delete deletes a specific Kafka Schema Registry ACL entry.
    89  func (h *KafkaSchemaRegistryACLHandler) Delete(project, serviceName, aclID string) error {
    90  	path := buildPath("project", project, "service", serviceName, "kafka", "schema-registry", "acl", aclID)
    91  	bts, err := h.client.doDeleteRequest(path, nil)
    92  	if err != nil {
    93  		return err
    94  	}
    95  
    96  	return checkAPIResponse(bts, nil)
    97  }