-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
151 lines (126 loc) · 4 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
// Copyright(C) 2022 PingCAP. All Rights Reserved.
package main
import (
"fmt"
"os"
)
const (
Host = "https://api.tidbcloud.com"
)
// 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
}
// createDevCluster create a cluster in the given project
func createDevCluster(projectID uint64) (*CreateClusterResp, error) {
var (
url = fmt.Sprintf("%s/api/v1beta/projects/%d/clusters", Host, projectID)
result CreateClusterResp
)
payload := CreateClusterReq{
Name: "tidbcloud-sample-1", // NOTE change to your cluster name
ClusterType: "DEVELOPER",
CloudProvider: "AWS",
Region: "us-east-1",
Config: ClusterConfig{
RootPassword: "your secret password", // NOTE change to your cluster password, we generate a random password here
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")
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
if len(projects) < 1 {
fmt.Printf("Failed to find any project in current organization\n")
return
}
// Step 2: select our first project, and then create a cluster
project := projects[0]
fmt.Printf("Step 2: create developer cluster in project %d\n", project.ID)
cluster, err := createDevCluster(project.ID)
if err != nil {
fmt.Printf("Failed to create developer 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 developer cluster(don't forget to delete it).\n")
}