-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
209 lines (175 loc) · 5.59 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright(C) 2022 PingCAP. All Rights Reserved.
package main
import (
"fmt"
"os"
)
const (
Host = "https://api.tidbcloud.com"
)
// getSpecifications returns all the available specifications
func getSpecifications() (*GetSpecificationsResp, error) {
var (
url = fmt.Sprintf("%s/api/v1beta/clusters/provider/regions", Host)
result GetSpecificationsResp
)
_, err := doGET(url, nil, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func getDedicatedSpec(specifications *GetSpecificationsResp) (*Specification, error) {
for _, i := range specifications.Items {
if i.ClusterType == "DEDICATED" {
return &i, nil
}
}
return nil, fmt.Errorf("No specification found")
}
// getAllProjects list all projects in current organization
func getAllProjects() ([]Project, error) {
var (
url = fmt.Sprintf("%s/api/v1beta/projects", Host)
result GetAllProjectsResp
)
_, err := doGET(url, nil, &result)
if err != nil {
return nil, err
}
return result.Items, nil
}
// createDedicatedCluster create a cluster in the given project
func createDedicatedCluster(projectID uint64, spec *Specification) (*CreateClusterResp, error) {
var (
url = fmt.Sprintf("%s/api/v1beta/projects/%d/clusters", Host, projectID)
result CreateClusterResp
)
// We have check the boundary in main function
tidbSpec := spec.Tidb[0]
tikvSpec := spec.Tikv[0]
payload := CreateClusterReq{
Name: "tidbcloud-sample-1", // NOTE change to your cluster name
ClusterType: spec.ClusterType,
CloudProvider: spec.CloudProvider,
Region: spec.Region,
Config: ClusterConfig{
RootPassword: "your secret password", // NOTE change to your cluster password, we generate a random password here
Port: 4000,
Components: Components{
TiDB: &ComponentTiDB{
NodeSize: tidbSpec.NodeSize,
NodeQuantity: tidbSpec.NodeQuantityRange.Min,
},
TiKV: &ComponentTiKV{
NodeSize: tikvSpec.NodeSize,
StorageSizeGib: tikvSpec.StorageSizeGibRange.Min,
NodeQuantity: tikvSpec.NodeQuantityRange.Min,
},
},
IPAccessList: []IPAccess{
{
CIDR: "0.0.0.0/0",
Description: "Allow Access from Anywhere.",
},
},
},
}
_, err := doPOST(url, payload, &result)
if err != nil {
return nil, err
}
return &result, nil
}
// getClusterByID return detail status of given cluster
func getClusterByID(projectID, clusterID uint64) (*GetClusterResp, error) {
var (
url = fmt.Sprintf("%s/api/v1beta/projects/%d/clusters/%d", Host, projectID, clusterID)
result GetClusterResp
)
_, err := doGET(url, nil, &result)
if err != nil {
return nil, err
}
return &result, nil
}
// deleteClusterByID delete a cluster
func deleteClusterByID(projectID, clusterID uint64) error {
url := fmt.Sprintf("%s/api/v1beta/projects/%d/clusters/%d", Host, projectID, clusterID)
_, err := doDELETE(url, nil, nil)
if err != nil {
return err
}
return nil
}
func main() {
fmt.Printf("\nWelcome to the TiDB Cloud demo!\n\n")
// Step 0: we need to initialize http client first
var (
publicKey = os.Getenv("TIDBCLOUD_PUBLIC_KEY")
privateKey = os.Getenv("TIDBCLOUD_PRIVATE_KEY")
)
if publicKey == "" || privateKey == "" {
fmt.Printf("Please set TIDBCLOUD_PUBLIC_KEY(%s), TIDBCLOUD_PRIVATE_KEY(%s) in environment variable first\n", publicKey, privateKey)
return
}
fmt.Printf("Step 0: initialize HTTP client\n")
err := initClient(publicKey, privateKey)
if err != nil {
fmt.Printf("Failed to init HTTP client\n")
return
}
// Step 1: query all the projects in current organization
fmt.Printf("Step 1: get all projects\n")
specs, err := getSpecifications()
if err != nil {
fmt.Printf("Failed to get specifications: %s\n", err)
return
}
dedicatedSpec, err := getDedicatedSpec(specs)
if err != nil {
fmt.Printf("Failed to get dedicated specification: %s\n", err)
return
}
projects, err := getAllProjects()
if err != nil {
fmt.Printf("Failed to get all projects: %s\n", err)
return
}
// Quit if we don't have any project, or can't find a valid specification
if len(projects) < 1 {
fmt.Printf("Failed to find any project in current organization\n")
return
}
if len(dedicatedSpec.Tidb) < 1 || len(dedicatedSpec.Tikv) < 1 {
fmt.Printf("Invalid specification: no available TiDB(%d)/TiKV(%d) specifications\n", len(dedicatedSpec.Tidb), len(dedicatedSpec.Tikv))
return
}
// Step 2: select our first project, and then create a cluster
project := projects[0]
fmt.Printf("Step 2: create dedicated cluster in project %d\n", project.ID)
cluster, err := createDedicatedCluster(project.ID, dedicatedSpec)
if err != nil {
fmt.Printf("Failed to create dedicated cluster: %s\n", err)
return
}
// Step 3: check the status of created cluster
fmt.Printf("Step 3: get cluster by id %d\n", cluster.ClusterID)
resp, err := getClusterByID(project.ID, cluster.ClusterID)
if err != nil {
fmt.Printf("Failed to get cluster detail of %d: %s\n", cluster.ClusterID, err)
return
}
fmt.Printf("Cluster detail: %+v\n", resp)
//You will get `connection_strings` from the response after the cluster's status is
//`AVAILABLE`. Then, you can connect to TiDB using the default user, host, and port in `connection_strings`.
//fmt.Printf("Connection string: %s", resp.ConnectionStrings.Standard)
// Step 4: delete the newly created cluster
//fmt.Printf("Step 4: delete cluster by id %d\n", cluster.ClusterID)
//err = deleteClusterByID(project.ID, cluster.ClusterID)
//if err != nil {
//fmt.Printf("Failed to delete cluster %d: %s\n", cluster.ClusterID, err)
//return
//}
fmt.Printf("\nYou have created a dedicated cluster(don't forget to delete it).\n")
}