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

     1  package aiven
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  )
     7  
     8  type (
     9  	// KafkaConnectorsHandler Aiven go-client handler for Kafka Connectors
    10  	KafkaConnectorsHandler struct {
    11  		client *Client
    12  	}
    13  
    14  	// KafkaConnectorConfig represents a configuration for Kafka Connectors
    15  	KafkaConnectorConfig map[string]string
    16  
    17  	// KafkaConnector represents Kafka Connector
    18  	KafkaConnector struct {
    19  		Name   string `json:"name"`
    20  		Config KafkaConnectorConfig
    21  		Plugin KafkaConnectorPlugin
    22  		Tasks  []KafkaConnectorTask
    23  	}
    24  
    25  	// KafkaConnectorTask represents Kafka Connector Task
    26  	KafkaConnectorTask struct {
    27  		Connector string
    28  		Task      int
    29  	}
    30  
    31  	// KafkaConnectorPlugin represents Kafka Connector Plugin
    32  	KafkaConnectorPlugin struct {
    33  		Author           string `json:"author"`
    34  		Class            string `json:"class"`
    35  		DocumentationURL string `json:"docURL"`
    36  		Title            string `json:"title"`
    37  		Type             string `json:"type"`
    38  		Version          string `json:"version"`
    39  	}
    40  
    41  	// KafkaConnectorsResponse represents Kafka Connectors API response
    42  	KafkaConnectorsResponse struct {
    43  		APIResponse
    44  		Connectors []KafkaConnector
    45  	}
    46  
    47  	// KafkaConnectorResponse represents single Kafka Connector API response
    48  	KafkaConnectorResponse struct {
    49  		APIResponse
    50  		Connector KafkaConnector
    51  	}
    52  
    53  	// KafkaConnectorStatusResponse represents single Kafka Connector API status response
    54  	KafkaConnectorStatusResponse struct {
    55  		APIResponse
    56  		Status KafkaConnectorStatus `json:"status"`
    57  	}
    58  
    59  	// KafkaConnectorStatus represents the status of a kafka connector
    60  	KafkaConnectorStatus struct {
    61  		State string                     `json:"state"`
    62  		Tasks []KafkaConnectorTaskStatus `json:"tasks"`
    63  	}
    64  
    65  	// KafkaConnectorTaskStatus represents the status of a kafka connector task
    66  	KafkaConnectorTaskStatus struct {
    67  		Id    int    `json:"id"`
    68  		State string `json:"state"`
    69  		Trace string `json:"trace"`
    70  	}
    71  )
    72  
    73  // Create creates Kafka Connector attached to Kafka or Kafka Connector service based on configuration
    74  func (h *KafkaConnectorsHandler) Create(project, service string, c KafkaConnectorConfig) error {
    75  	path := buildPath("project", project, "service", service, "connectors")
    76  	bts, err := h.client.doPostRequest(path, c)
    77  	if err != nil {
    78  		return err
    79  	}
    80  
    81  	return checkAPIResponse(bts, nil)
    82  }
    83  
    84  // Delete deletes Kafka Connector by name
    85  func (h *KafkaConnectorsHandler) Delete(project, service, name string) error {
    86  	path := buildPath("project", project, "service", service, "connectors", name)
    87  	bts, err := h.client.doDeleteRequest(path, nil)
    88  	if err != nil {
    89  		return err
    90  	}
    91  
    92  	return checkAPIResponse(bts, nil)
    93  }
    94  
    95  // List lists all available Kafka Connectors for a service
    96  func (h *KafkaConnectorsHandler) List(project, service string) (*KafkaConnectorsResponse, error) {
    97  	path := buildPath("project", project, "service", service, "connectors")
    98  	bts, err := h.client.doGetRequest(path, nil)
    99  	if err != nil {
   100  		return nil, err
   101  	}
   102  
   103  	var rsp KafkaConnectorsResponse
   104  	if err := checkAPIResponse(bts, &rsp); err != nil {
   105  		return nil, err
   106  	}
   107  
   108  	return &rsp, nil
   109  }
   110  
   111  // GetByName gets a KafkaConnector by name
   112  func (h *KafkaConnectorsHandler) GetByName(project, service, name string) (*KafkaConnector, error) {
   113  	path := buildPath("project", project, "service", service, "connectors")
   114  	bts, err := h.client.doGetRequest(path, nil)
   115  	if err != nil {
   116  		return nil, err
   117  	}
   118  
   119  	var rsp KafkaConnectorsResponse
   120  	if err := checkAPIResponse(bts, &rsp); err != nil {
   121  		return nil, err
   122  	}
   123  
   124  	for i := range rsp.Connectors {
   125  		con := rsp.Connectors[i]
   126  		if con.Name == name {
   127  			return &con, nil
   128  		}
   129  	}
   130  
   131  	// TODO: This is a hack. We pretend that this was an API call all along, even though this is only convenience
   132  	// It is acceptable because all other functions here have the contract that we return a non nil result if the
   133  	// error is nil. So for the sake of API consistency we pretend that the remote API returned this error.
   134  	return nil, Error{
   135  		Message: fmt.Sprintf("no kafka connector with name '%s' found in project '%s' for service '%s'", name, project, service),
   136  		Status:  http.StatusNotFound,
   137  	}
   138  }
   139  
   140  // Get the status of a single Kafka Connector by name
   141  func (h *KafkaConnectorsHandler) Status(project, service, name string) (*KafkaConnectorStatusResponse, error) {
   142  	path := buildPath("project", project, "service", service, "connectors", name, "status")
   143  	bts, err := h.client.doGetRequest(path, nil)
   144  	if err != nil {
   145  		return nil, err
   146  	}
   147  
   148  	var rsp KafkaConnectorStatusResponse
   149  	if err := checkAPIResponse(bts, &rsp); err != nil {
   150  		return nil, err
   151  	}
   152  	return &rsp, nil
   153  }
   154  
   155  // Update updates a Kafka Connector configuration by Connector AuthenticationMethodName
   156  func (h *KafkaConnectorsHandler) Update(project, service, name string, c KafkaConnectorConfig) (*KafkaConnectorResponse, error) {
   157  	path := buildPath("project", project, "service", service, "connectors", name)
   158  	bts, err := h.client.doPutRequest(path, c)
   159  	if err != nil {
   160  		return nil, err
   161  	}
   162  
   163  	var rsp KafkaConnectorResponse
   164  	if err := checkAPIResponse(bts, &rsp); err != nil {
   165  		return nil, err
   166  	}
   167  	return &rsp, nil
   168  }