-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
91 lines (75 loc) · 2.09 KB
/
main.go
File metadata and controls
91 lines (75 loc) · 2.09 KB
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
package main
import (
"context"
"fmt"
"os"
sqladmin "google.golang.org/api/sqladmin/v1beta4"
)
var (
pId = "project_id"
oId = "operation_id"
)
func main() {
ctx := context.Background()
status, err := GetOperationStatus(ctx, pId, oId)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(status)
}
func ListInstances(ctx context.Context, projectId string) ([]*sqladmin.DatabaseInstance, error) {
// Create the Google Cloud SQL service.
service, err := sqladmin.NewService(ctx)
if err != nil {
return nil, err
}
// List instances for the project ID.
instances, err := service.Instances.List(projectId).Do()
if err != nil {
return nil, err
}
return instances.Items, nil
}
func RequestExport(ctx context.Context, projectId string, instanceId string, fileName string) error {
// Create the Google Cloud SQL service.
service, err := sqladmin.NewService(ctx)
if err != nil {
return err
}
exportRequest := &sqladmin.InstancesExportRequest{
ExportContext: &sqladmin.ExportContext{
Uri: "google_cloud_storage_uri",
Databases: []string{"google_cloud_sql_database_name"},
Kind: "sql#exportContext",
CsvExportOptions: &sqladmin.ExportContextCsvExportOptions{SelectQuery: "select * from tokens;"},
FileType: "CSV",
Offload: true,
},
}
operation, err := service.Instances.Export(projectId, instanceId, exportRequest).Do()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(operation.Name)
fmt.Println(operation.Status)
return nil
}
func GetOperationStatus(ctx context.Context, projectId string, operationName string) (string, error) {
service, err := sqladmin.NewService(ctx)
if err != nil {
return "", err
}
operation, err := service.Operations.Get(projectId, operationName).Do()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// クエリ実行中にエラーが生じたなら、OperationErrors型の変数が入る。そうでないなら、nilになる。
fmt.Println(operation.Error.Errors)
for _, err := range operation.Error.Errors {
fmt.Println(err)
}
return operation.Status, nil
}