0

Update Topic for a Kafka Cluster

by
Published Dec 20, 2024

To update a topic attached to a Kafka cluster, send a PUT request to `/v2/databases/$DATABASE_ID/topics/$TOPIC_NAME`. The result will be a JSON object with a `topic` key.

Script digitalocean Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Digitalocean = {
3
  token: string;
4
};
5
/**
6
 * Update Topic for a Kafka Cluster
7
 * To update a topic attached to a Kafka cluster, send a PUT request to
8
`/v2/databases/$DATABASE_ID/topics/$TOPIC_NAME`.
9

10
The result will be a JSON object with a `topic` key.
11

12
 */
13
export async function main(
14
  auth: Digitalocean,
15
  database_cluster_uuid: string,
16
  topic_name: string,
17
  body: {
18
    replication_factor?: number;
19
    partition_count?: number;
20
    config?: {
21
      cleanup_policy?: "delete" | "compact" | "compact_delete";
22
      compression_type?:
23
        | "producer"
24
        | "gzip"
25
        | "snappy"
26
        | "Iz4"
27
        | "zstd"
28
        | "uncompressed";
29
      delete_retention_ms?: number;
30
      file_delete_delay_ms?: number;
31
      flush_messages?: number;
32
      flush_ms?: number;
33
      index_interval_bytes?: number;
34
      max_compaction_lag_ms?: number;
35
      max_message_bytes?: number;
36
      message_down_conversion_enable?: false | true;
37
      message_format_version?:
38
        | "0.8.0"
39
        | "0.8.1"
40
        | "0.8.2"
41
        | "0.9.0"
42
        | "0.10.0-IV0"
43
        | "0.10.0-IV1"
44
        | "0.10.1-IV0"
45
        | "0.10.1-IV1"
46
        | "0.10.1-IV2"
47
        | "0.10.2-IV0"
48
        | "0.11.0-IV0"
49
        | "0.11.0-IV1"
50
        | "0.11.0-IV2"
51
        | "1.0-IV0"
52
        | "1.1-IV0"
53
        | "2.0-IV0"
54
        | "2.0-IV1"
55
        | "2.1-IV0"
56
        | "2.1-IV1"
57
        | "2.1-IV2"
58
        | "2.2-IV0"
59
        | "2.2-IV1"
60
        | "2.3-IV0"
61
        | "2.3-IV1"
62
        | "2.4-IV0"
63
        | "2.4-IV1"
64
        | "2.5-IV0"
65
        | "2.6-IV0"
66
        | "2.7-IV0"
67
        | "2.7-IV1"
68
        | "2.7-IV2"
69
        | "2.8-IV0"
70
        | "2.8-IV1"
71
        | "3.0-IV0"
72
        | "3.0-IV1"
73
        | "3.1-IV0"
74
        | "3.2-IV0"
75
        | "3.3-IV0"
76
        | "3.3-IV1"
77
        | "3.3-IV2"
78
        | "3.3-IV3";
79
      message_timestamp_type?: "create_time" | "log_append_time";
80
      min_cleanable_dirty_ratio?: number;
81
      min_compaction_lag_ms?: number;
82
      min_insync_replicas?: number;
83
      preallocate?: false | true;
84
      retention_bytes?: number;
85
      retention_ms?: number;
86
      segment_bytes?: number;
87
      segment_jitter_ms?: number;
88
      segment_ms?: number;
89
    };
90
  },
91
) {
92
  const url = new URL(
93
    `https://api.digitalocean.com/v2/databases/${database_cluster_uuid}/topics/${topic_name}`,
94
  );
95

96
  const response = await fetch(url, {
97
    method: "PUT",
98
    headers: {
99
      "Content-Type": "application/json",
100
      Authorization: "Bearer " + auth.token,
101
    },
102
    body: JSON.stringify(body),
103
  });
104
  if (!response.ok) {
105
    const text = await response.text();
106
    throw new Error(`${response.status} ${text}`);
107
  }
108
  return await response.json();
109
}
110