0

Create a New Database Cluster

by
Published Dec 20, 2024

To create a database cluster, send a POST request to `/v2/databases`.

Script digitalocean Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Digitalocean = {
3
  token: string;
4
};
5
/**
6
 * Create a New Database Cluster
7
 * To create a database cluster, send a POST request to `/v2/databases`.
8
 */
9
export async function main(
10
  auth: Digitalocean,
11
  body: {
12
    id?: string;
13
    name: string;
14
    engine: "pg" | "mysql" | "redis" | "mongodb" | "kafka" | "opensearch";
15
    version?: string;
16
    semantic_version?: string;
17
    num_nodes: number;
18
    size: string;
19
    region: string;
20
    status?: "creating" | "online" | "resizing" | "migrating" | "forking";
21
    created_at?: string;
22
    private_network_uuid?: string;
23
    tags?: string[];
24
    db_names?: string[];
25
    ui_connection?: {
26
      uri?: string;
27
      host?: string;
28
      port?: number;
29
      user?: string;
30
      password?: string;
31
      ssl?: false | true;
32
    } & {};
33
    connection?: {
34
      uri?: string;
35
      database?: string;
36
      host?: string;
37
      port?: number;
38
      user?: string;
39
      password?: string;
40
      ssl?: false | true;
41
    } & {};
42
    private_connection?: {
43
      uri?: string;
44
      database?: string;
45
      host?: string;
46
      port?: number;
47
      user?: string;
48
      password?: string;
49
      ssl?: false | true;
50
    } & {};
51
    standby_connection?: {
52
      uri?: string;
53
      database?: string;
54
      host?: string;
55
      port?: number;
56
      user?: string;
57
      password?: string;
58
      ssl?: false | true;
59
    } & {};
60
    standby_private_connection?: {
61
      uri?: string;
62
      database?: string;
63
      host?: string;
64
      port?: number;
65
      user?: string;
66
      password?: string;
67
      ssl?: false | true;
68
    } & {};
69
    users?: {
70
      name: string;
71
      role?: "primary" | "normal";
72
      password?: string;
73
      access_cert?: string;
74
      access_key?: string;
75
      mysql_settings?: {
76
        auth_plugin: "mysql_native_password" | "caching_sha2_password";
77
      };
78
      settings?: {
79
        pg_allow_replication?: false | true;
80
        opensearch_acl?: {
81
          index?: string;
82
          permission?: "deny" | "admin" | "read" | "readwrite" | "write";
83
        }[];
84
        acl?: {
85
          id?: string;
86
          topic: string;
87
          permission: "admin" | "consume" | "produce" | "produceconsume";
88
        }[];
89
      };
90
    }[];
91
    maintenance_window?: {
92
      day: string;
93
      hour: string;
94
      pending?: false | true;
95
      description?: string[];
96
    } & {};
97
    project_id?: string;
98
    rules?: {
99
      uuid?: string;
100
      cluster_uuid?: string;
101
      type: "droplet" | "k8s" | "ip_addr" | "tag" | "app";
102
      value: string;
103
      created_at?: string;
104
    }[];
105
    version_end_of_life?: string;
106
    version_end_of_availability?: string;
107
    storage_size_mib?: number;
108
    metrics_endpoints?: { host?: string; port?: number }[];
109
  } & {
110
    backup_restore?: { database_name: string; backup_created_at?: string };
111
  },
112
) {
113
  const url = new URL(`https://api.digitalocean.com/v2/databases`);
114

115
  const response = await fetch(url, {
116
    method: "POST",
117
    headers: {
118
      "Content-Type": "application/json",
119
      Authorization: "Bearer " + auth.token,
120
    },
121
    body: JSON.stringify(body),
122
  });
123
  if (!response.ok) {
124
    const text = await response.text();
125
    throw new Error(`${response.status} ${text}`);
126
  }
127
  return await response.json();
128
}
129