This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
/
db.go
112 lines (96 loc) · 3.07 KB
/
db.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
package postgresql
import (
"fmt"
"strings"
"time"
)
type DB struct {
Id string
Name string
Plan string
client *Client
}
func (d *DB) Info() (dbi DBInfo, err error) {
err = d.client.Get(d.IsStarterPlan(), "/"+d.Id, &dbi)
return
}
func (d *DB) Ingress() error {
return d.client.Put(d.IsStarterPlan(), "/"+d.Id+"/ingress", nil)
}
// Whether the DB is a starter plan and should communicate with the starter API.
// Plan names ending in "dev" or "basic" are currently handled by the starter
// API while all others are handled by the production API.
func (d *DB) IsStarterPlan() bool {
return strings.HasSuffix(d.Plan, "dev") ||
strings.HasSuffix(d.Plan, "basic") ||
// special exception for devcloud plans:
strings.HasSuffix(d.Plan, "devcloud")
}
func (d *DB) Reset() error {
return d.client.Put(d.IsStarterPlan(), "/"+d.Id+"/reset", nil)
}
func (d *DB) RotateCredentials() error {
return d.client.Post(d.IsStarterPlan(), "/"+d.Id+"/credentials_rotation", nil)
}
func (d *DB) Unfollow() error {
return d.client.Put(d.IsStarterPlan(), "/"+d.Id+"/unfollow", nil)
}
func (d *DB) WaitStatus() (ws WaitStatus, err error) {
err = d.client.Get(d.IsStarterPlan(), "/"+d.Id+"/wait_status", &ws)
return
}
type WaitStatus struct {
}
type DBInfo struct {
AvailableForIngress bool `json:"available_for_ingress"`
CreatedAt string `json:"created_at"`
CurrentTransaction string `json:"current_transaction"`
DatabaseName string `json:"database_name"`
DatabasePassword string `json:"database_password"`
DatabaseUser string `json:"database_user"`
Following string `json:"following"`
Info InfoEntryList `json:"info"`
IsInRecovery bool `json:"is_in_recovery?"`
NumBytes int `json:"num_bytes"`
NumConnections int `json:"num_connections"`
NumConnectionsWaiting int `json:"num_connections_waiting"`
NumTables int `json:"num_tables"`
Plan string `json:"plan"`
PostgresqlVersion string `json:"postgresql_version"`
ResourceURL string `json:"resource_url"`
ServicePort int `json:"service_port"`
StatusUpdatedAt time.Time `json:"status_updated_at"`
Standalone bool `json:"standalone?"`
TargetTransaction string `json:"target_transaction"`
}
func (dbi *DBInfo) IsFollower() bool {
return dbi.Following != ""
}
type InfoEntryList []InfoEntry
func (iel *InfoEntryList) Named(name string) *InfoEntry {
if iel == nil {
return nil
}
for i := range *iel {
if (*iel)[i].Name == name {
return &(*iel)[i]
}
}
return nil
}
func (iel *InfoEntryList) GetString(key string) (valstr string, resolve bool) {
ie := iel.Named(key)
if ie == nil {
return
}
resolve = ie.ResolveDBName
if len(ie.Values) > 0 {
valstr = fmt.Sprintf("%v", ie.Values[0])
}
return
}
type InfoEntry struct {
Name string
ResolveDBName bool `json:"resolve_db_name"`
Values []interface{}
}