-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
105 lines (87 loc) · 2.57 KB
/
database.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
package hive
import (
"bytes"
"encoding/json"
"fmt"
"log"
)
var (
ENDPOINT_DATABASE = `%v:%v/templeton/v1/ddl/database?user.name=%v`
ENDPOINT_DATABASE_DETAIL = `%v:%v/templeton/v1/ddl/database/%v?user.name=%v`
)
type ListDatabaseResponse struct {
Databases []string `json:"databases"`
}
type ShowDatabaseResponse struct {
Location string `json:"location"`
Params string `json:"params"`
Comment string `json:"comment"`
Database string `json:"database"`
}
type CreateDatabaseInput struct {
Comment string `json:"comment,omitempty"`
Location string `json:"location,omitempty"`
Properties interface{} `json:"properties,omitempty"`
}
type CreateDatabaseResponse struct {
Database string `json:"database"`
}
type DropDatabaseResponse struct {
Database string `json:"database"`
}
func (this *Client) ListDatabase() (*ListDatabaseResponse, error) {
endpoint := fmt.Sprintf(ENDPOINT_DATABASE, this.BaseUrl, this.Port, this.User)
resp, err := this.request(HTTP_GET, endpoint, nil)
if err != nil {
log.Println(err)
return nil, err
}
res := &ListDatabaseResponse{}
if err := json.NewDecoder(resp.Body).Decode(res); err != nil {
log.Println(err)
return nil, err
}
return res, nil
}
func (this *Client) ShowDatabase(database string) (*ShowDatabaseResponse, error) {
endpoint := fmt.Sprintf(ENDPOINT_DATABASE_DETAIL, this.BaseUrl, this.Port, database, this.User)
resp, err := this.request(HTTP_GET, endpoint, nil)
if err != nil {
return nil, err
}
res := &ShowDatabaseResponse{}
if err := json.NewDecoder(resp.Body).Decode(res); err != nil {
log.Println(err)
return nil, err
}
return res, nil
}
func (this *Client) CreateDatabase(database string, in *CreateDatabaseInput) (*CreateDatabaseResponse, error) {
body := new(bytes.Buffer)
json.NewEncoder(body).Encode(in)
endpoint := fmt.Sprintf(ENDPOINT_DATABASE_DETAIL, this.BaseUrl, this.Port, database, this.User)
resp, err := this.request(HTTP_PUT, endpoint, body)
if err != nil {
log.Println(err)
return nil, err
}
res := &CreateDatabaseResponse{}
if err := json.NewDecoder(resp.Body).Decode(res); err != nil {
log.Println(err)
return nil, err
}
return res, nil
}
func (this *Client) DropDatabase(database string) (*DropDatabaseResponse, error) {
endpoint := fmt.Sprintf(ENDPOINT_DATABASE_DETAIL, this.BaseUrl, this.Port, database, this.User)
resp, err := this.request(HTTP_DELETE, endpoint, nil)
if err != nil {
return nil, err
}
res := &DropDatabaseResponse{}
if err := json.NewDecoder(resp.Body).Decode(res); err != nil {
log.Println(err)
return nil, err
}
return res, nil
}