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

     1  package aiven
     2  
     3  type (
     4  	// KafkaTopicConfig represents a Kafka Topic Config on Aiven.
     5  	KafkaTopicConfig struct {
     6  		CleanupPolicy                   string   `json:"cleanup_policy,omitempty"`
     7  		CompressionType                 string   `json:"compression_type,omitempty"`
     8  		DeleteRetentionMs               *int64   `json:"delete_retention_ms,omitempty"`
     9  		FileDeleteDelayMs               *int64   `json:"file_delete_delay_ms,omitempty"`
    10  		FlushMessages                   *int64   `json:"flush_messages,omitempty"`
    11  		FlushMs                         *int64   `json:"flush_ms,omitempty"`
    12  		IndexIntervalBytes              *int64   `json:"index_interval_bytes,omitempty"`
    13  		MaxCompactionLagMs              *int64   `json:"max_compaction_lag_ms,omitempty"`
    14  		MaxMessageBytes                 *int64   `json:"max_message_bytes,omitempty"`
    15  		MessageDownconversionEnable     *bool    `json:"message_downconversion_enable,omitempty"`
    16  		MessageFormatVersion            string   `json:"message_format_version,omitempty"`
    17  		MessageTimestampDifferenceMaxMs *int64   `json:"message_timestamp_difference_max_ms,omitempty"`
    18  		MessageTimestampType            string   `json:"message_timestamp_type,omitempty"`
    19  		MinCleanableDirtyRatio          *float64 `json:"min_cleanable_dirty_ratio,omitempty"`
    20  		MinCompactionLagMs              *int64   `json:"min_compaction_lag_ms,omitempty"`
    21  		MinInsyncReplicas               *int64   `json:"min_insync_replicas,omitempty"`
    22  		Preallocate                     *bool    `json:"preallocate,omitempty"`
    23  		RetentionBytes                  *int64   `json:"retention_bytes,omitempty"`
    24  		RetentionMs                     *int64   `json:"retention_ms,omitempty"`
    25  		SegmentBytes                    *int64   `json:"segment_bytes,omitempty"`
    26  		SegmentIndexBytes               *int64   `json:"segment_index_bytes,omitempty"`
    27  		SegmentJitterMs                 *int64   `json:"segment_jitter_ms,omitempty"`
    28  		SegmentMs                       *int64   `json:"segment_ms,omitempty"`
    29  		UncleanLeaderElectionEnable     *bool    `json:"unclean_leader_election_enable,omitempty"`
    30  	}
    31  
    32  	// KafkaTopicConfigResponse represents a Kafka Topic Config on Aiven.
    33  	KafkaTopicConfigResponse struct {
    34  		CleanupPolicy                   KafkaTopicConfigResponseString `json:"cleanup_policy,omitempty"`
    35  		CompressionType                 KafkaTopicConfigResponseString `json:"compression_type,omitempty"`
    36  		DeleteRetentionMs               KafkaTopicConfigResponseInt    `json:"delete_retention_ms,omitempty"`
    37  		FileDeleteDelayMs               KafkaTopicConfigResponseInt    `json:"file_delete_delay_ms,omitempty"`
    38  		FlushMessages                   KafkaTopicConfigResponseInt    `json:"flush_messages,omitempty"`
    39  		FlushMs                         KafkaTopicConfigResponseInt    `json:"flush_ms,omitempty"`
    40  		IndexIntervalBytes              KafkaTopicConfigResponseInt    `json:"index_interval_bytes,omitempty"`
    41  		MaxCompactionLagMs              KafkaTopicConfigResponseInt    `json:"max_compaction_lag_ms,omitempty"`
    42  		MaxMessageBytes                 KafkaTopicConfigResponseInt    `json:"max_message_bytes,omitempty"`
    43  		MessageDownconversionEnable     KafkaTopicConfigResponseBool   `json:"message_downconversion_enable,omitempty"`
    44  		MessageFormatVersion            KafkaTopicConfigResponseString `json:"message_format_version,omitempty"`
    45  		MessageTimestampDifferenceMaxMs KafkaTopicConfigResponseInt    `json:"message_timestamp_difference_max_ms,omitempty"`
    46  		MessageTimestampType            KafkaTopicConfigResponseString `json:"message_timestamp_type,omitempty"`
    47  		MinCleanableDirtyRatio          KafkaTopicConfigResponseFloat  `json:"min_cleanable_dirty_ratio,omitempty"`
    48  		MinCompactionLagMs              KafkaTopicConfigResponseInt    `json:"min_compaction_lag_ms,omitempty"`
    49  		MinInsyncReplicas               KafkaTopicConfigResponseInt    `json:"min_insync_replicas,omitempty"`
    50  		Preallocate                     KafkaTopicConfigResponseBool   `json:"preallocate,omitempty"`
    51  		RetentionBytes                  KafkaTopicConfigResponseInt    `json:"retention_bytes,omitempty"`
    52  		RetentionMs                     KafkaTopicConfigResponseInt    `json:"retention_ms,omitempty"`
    53  		SegmentBytes                    KafkaTopicConfigResponseInt    `json:"segment_bytes,omitempty"`
    54  		SegmentIndexBytes               KafkaTopicConfigResponseInt    `json:"segment_index_bytes,omitempty"`
    55  		SegmentJitterMs                 KafkaTopicConfigResponseInt    `json:"segment_jitter_ms,omitempty"`
    56  		SegmentMs                       KafkaTopicConfigResponseInt    `json:"segment_ms,omitempty"`
    57  		UncleanLeaderElectionEnable     KafkaTopicConfigResponseBool   `json:"unclean_leader_election_enable,omitempty"`
    58  		Tags                            []KafkaTopicTag                `json:"tags,omitempty"`
    59  	}
    60  
    61  	KafkaTopicTag struct {
    62  		Key   string `json:"key"`
    63  		Value string `json:"value"`
    64  	}
    65  
    66  	KafkaTopicConfigResponseString struct {
    67  		Source   string `json:"source"`
    68  		Value    string `json:"value"`
    69  		Synonyms []struct {
    70  			Source string `json:"source"`
    71  			Value  string `json:"value"`
    72  			Name   string `json:"name"`
    73  		} `json:"synonyms"`
    74  	}
    75  
    76  	KafkaTopicConfigResponseInt struct {
    77  		Source   string `json:"source"`
    78  		Value    int64  `json:"value"`
    79  		Synonyms []struct {
    80  			Source string `json:"source"`
    81  			Value  int64  `json:"value"`
    82  			Name   string `json:"name"`
    83  		} `json:"synonyms"`
    84  	}
    85  
    86  	KafkaTopicConfigResponseBool struct {
    87  		Source   string `json:"source"`
    88  		Value    bool   `json:"value"`
    89  		Synonyms []struct {
    90  			Source string `json:"source"`
    91  			Value  bool   `json:"value"`
    92  			Name   string `json:"name"`
    93  		} `json:"synonyms"`
    94  	}
    95  
    96  	KafkaTopicConfigResponseFloat struct {
    97  		Source   string  `json:"source"`
    98  		Value    float64 `json:"value"`
    99  		Synonyms []struct {
   100  			Source string  `json:"source"`
   101  			Value  float64 `json:"value"`
   102  			Name   string  `json:"name"`
   103  		} `json:"synonyms"`
   104  	}
   105  
   106  	// KafkaTopic represents a Kafka Topic on Aiven.
   107  	KafkaTopic struct {
   108  		CleanupPolicy         string                   `json:"cleanup_policy"`
   109  		MinimumInSyncReplicas int                      `json:"min_insync_replicas"`
   110  		Partitions            []*Partition             `json:"partitions"`
   111  		Replication           int                      `json:"replication"`
   112  		RetentionBytes        int                      `json:"retention_bytes"`
   113  		RetentionHours        *int                     `json:"retention_hours,omitempty"`
   114  		State                 string                   `json:"state"`
   115  		TopicName             string                   `json:"topic_name"`
   116  		Config                KafkaTopicConfigResponse `json:"config"`
   117  		Tags                  []KafkaTopicTag          `json:"tags,omitempty"`
   118  	}
   119  
   120  	// KafkaListTopic represents kafka list topic model on Aiven.
   121  	KafkaListTopic struct {
   122  		CleanupPolicy         string `json:"cleanup_policy"`
   123  		MinimumInSyncReplicas int    `json:"min_insync_replicas"`
   124  		Partitions            int    `json:"partitions"`
   125  		Replication           int    `json:"replication"`
   126  		RetentionBytes        int    `json:"retention_bytes"`
   127  		RetentionHours        *int64 `json:"retention_hours,omitempty"`
   128  		State                 string `json:"state"`
   129  		TopicName             string `json:"topic_name"`
   130  	}
   131  
   132  	// Partition represents a Kafka partition.
   133  	Partition struct {
   134  		ConsumerGroups []*ConsumerGroup `json:"consumer_groups"`
   135  		EarliestOffset int64            `json:"earliest_offset"`
   136  		ISR            int              `json:"isr"`
   137  		LatestOffset   int64            `json:"latest_offset"`
   138  		Partition      int              `json:"partition"`
   139  		Size           int64            `json:"size"`
   140  	}
   141  
   142  	// ConsumerGroup is the group used in partitions.
   143  	ConsumerGroup struct {
   144  		GroupName string `json:"group_name"`
   145  		Offset    int64  `json:"offset"`
   146  	}
   147  
   148  	// KafkaTopicsHandler is the client which interacts with the kafka endpoints
   149  	// on Aiven.
   150  	KafkaTopicsHandler struct {
   151  		client *Client
   152  	}
   153  
   154  	// CreateKafkaTopicRequest are the parameters used to create a kafka topic.
   155  	CreateKafkaTopicRequest struct {
   156  		CleanupPolicy         *string          `json:"cleanup_policy,omitempty"`
   157  		MinimumInSyncReplicas *int             `json:"min_insync_replicas,omitempty"`
   158  		Partitions            *int             `json:"partitions,omitempty"`
   159  		Replication           *int             `json:"replication,omitempty"`
   160  		RetentionBytes        *int             `json:"retention_bytes,omitempty"`
   161  		RetentionHours        *int             `json:"retention_hours,omitempty"`
   162  		TopicName             string           `json:"topic_name"`
   163  		Config                KafkaTopicConfig `json:"config"`
   164  		Tags                  []KafkaTopicTag  `json:"tags,omitempty"`
   165  	}
   166  
   167  	// UpdateKafkaTopicRequest are the parameters used to update a kafka topic.
   168  	UpdateKafkaTopicRequest struct {
   169  		MinimumInSyncReplicas *int             `json:"min_insync_replicas,omitempty"`
   170  		Partitions            *int             `json:"partitions,omitempty"`
   171  		Replication           *int             `json:"replication,omitempty"`
   172  		RetentionBytes        *int             `json:"retention_bytes,omitempty"`
   173  		RetentionHours        *int             `json:"retention_hours,omitempty"`
   174  		Config                KafkaTopicConfig `json:"config"`
   175  		Tags                  []KafkaTopicTag  `json:"tags,omitempty"`
   176  	}
   177  
   178  	// KafkaTopicResponse is the response for Kafka Topic requests.
   179  	KafkaTopicResponse struct {
   180  		APIResponse
   181  		Topic *KafkaTopic `json:"topic"`
   182  	}
   183  
   184  	// KafkaTopicsResponse is the response for listing kafka topics.
   185  	KafkaTopicsResponse struct {
   186  		APIResponse
   187  		Topics []*KafkaListTopic `json:"topics"`
   188  	}
   189  
   190  	// KafkaV2TopicsResponse is the response for listing kafka topics specific for API V2 endpoint.
   191  	KafkaV2TopicsResponse struct {
   192  		APIResponse
   193  		Topics []*KafkaTopic `json:"topics"`
   194  	}
   195  )
   196  
   197  // Create creats a specific kafka topic.
   198  func (h *KafkaTopicsHandler) Create(project, service string, req CreateKafkaTopicRequest) error {
   199  	path := buildPath("project", project, "service", service, "topic")
   200  	bts, err := h.client.doPostRequest(path, req)
   201  	if err != nil {
   202  		return err
   203  	}
   204  
   205  	return checkAPIResponse(bts, nil)
   206  }
   207  
   208  // Get gets a specific kafka topic.
   209  func (h *KafkaTopicsHandler) Get(project, service, topic string) (*KafkaTopic, error) {
   210  	path := buildPath("project", project, "service", service, "topic", topic)
   211  	bts, err := h.client.doGetRequest(path, nil)
   212  	if err != nil {
   213  		return nil, err
   214  	}
   215  
   216  	var r KafkaTopicResponse
   217  	errR := checkAPIResponse(bts, &r)
   218  
   219  	return r.Topic, errR
   220  }
   221  
   222  // List lists all the kafka topics.
   223  func (h *KafkaTopicsHandler) List(project, service string) ([]*KafkaListTopic, error) {
   224  	path := buildPath("project", project, "service", service, "topic")
   225  	bts, err := h.client.doGetRequest(path, nil)
   226  	if err != nil {
   227  		return nil, err
   228  	}
   229  
   230  	var r KafkaTopicsResponse
   231  	errR := checkAPIResponse(bts, &r)
   232  
   233  	return r.Topics, errR
   234  }
   235  
   236  // Update updates a specific topic with the given parameters.
   237  func (h *KafkaTopicsHandler) Update(project, service, topic string, req UpdateKafkaTopicRequest) error {
   238  	path := buildPath("project", project, "service", service, "topic", topic)
   239  	bts, err := h.client.doPutRequest(path, req)
   240  	if err != nil {
   241  		return err
   242  	}
   243  
   244  	return checkAPIResponse(bts, nil)
   245  }
   246  
   247  // Delete deletes a specific kafka topic.
   248  func (h *KafkaTopicsHandler) Delete(project, service, topic string) error {
   249  	path := buildPath("project", project, "service", service, "topic", topic)
   250  	bts, err := h.client.doDeleteRequest(path, nil)
   251  	if err != nil {
   252  		return err
   253  	}
   254  
   255  	return checkAPIResponse(bts, nil)
   256  }
   257  
   258  // V2List lists selected kafka topics using v2 API endpoint.
   259  func (h *KafkaTopicsHandler) V2List(project, service string, topics []string) ([]*KafkaTopic, error) {
   260  	type v2ListRequest struct {
   261  		TopicNames []string `json:"topic_names"`
   262  	}
   263  
   264  	req := v2ListRequest{TopicNames: topics}
   265  
   266  	path := buildPath("project", project, "service", service, "topic")
   267  	bts, err := h.client.doV2PostRequest(path, req)
   268  	if err != nil {
   269  		return nil, err
   270  	}
   271  
   272  	var r KafkaV2TopicsResponse
   273  	errR := checkAPIResponse(bts, &r)
   274  
   275  	return r.Topics, errR
   276  }