This repository was archived by the owner on Apr 17, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdatabases.go
More file actions
101 lines (83 loc) · 2.54 KB
/
databases.go
File metadata and controls
101 lines (83 loc) · 2.54 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
92
93
94
95
96
97
98
99
100
101
package fossil
import (
"encoding/json"
"fmt"
"time"
)
//***** Structures *****//
// Database contains all the information of a specific Pterodactyl database
type Database struct {
ID int `json:"id"`
Server int `json:"server"`
Host int `json:"host"`
Database string `json:"database"`
Username string `json:"username"`
Remote string `json:"remote"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
//***** Requests *****//
// GetDatabases fetches all the associated databases for a server
func (c *ApplicationCredentials) GetDatabases(sid int) (dbs []*Database, err error) {
bytes, err := c.query(fmt.Sprintf("servers/%d/databases", sid), "GET", nil)
if err != nil {
return
}
var wrapper struct {
Data []struct {
Database *Database `json:"attributes"`
}
}
err = json.Unmarshal(bytes, &wrapper)
if err != nil {
return
}
for _, db := range wrapper.Data {
dbs = append(dbs, db.Database)
}
return dbs, nil
}
// GetDatabase fetches, if present, the database matching the id in the server's databases
func (c *ApplicationCredentials) GetDatabase(sid int, dbid int) (db *Database, err error) {
bytes, err := c.query(fmt.Sprintf("servers/%d/databases/%d", sid, dbid), "GET", nil)
if err != nil {
return
}
var wrapper struct {
Database *Database `json:"attributes"`
}
err = json.Unmarshal(bytes, &wrapper)
if err != nil {
return
}
return wrapper.Database, nil
}
// CreateDatabase creates a new database based on the provided information
func (c *ApplicationCredentials) CreateDatabase(sid int, db *Database) (err error) {
type databaseCreate struct {
Database string `json:"database"`
Remote string `json:"remote"`
Host int `json:"host"`
}
dbStruct := databaseCreate{
Database: db.Database,
Remote: db.Remote,
Host: db.Host,
}
bytes, err := json.Marshal(dbStruct)
if err != nil {
return err
}
_, err = c.query(fmt.Sprintf("servers/%d/databases", sid), "POST", bytes)
return
}
// ResetDatabasePassword resets the password for the specified database of the specified server
func (c *ApplicationCredentials) ResetDatabasePassword(sid int, dbid int) (err error) {
_, err = c.query(fmt.Sprintf("servers/%d/databases/%d/reset-password", sid, dbid), "POST", nil)
return
}
// DeleteDatabase marks the specified database in the specified server for deletion
func (c *ApplicationCredentials) DeleteDatabase(sid int, dbid int) (err error) {
_, err = c.query(fmt.Sprintf("servers/%d/databases/%d", sid, dbid), "DELETE", nil)
return
}