/
databases.go
146 lines (108 loc) 路 5.15 KB
/
databases.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
package main
import (
"bytes"
"context"
"fmt"
"github.com/digitalocean/godo"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/phillipahereza/mattermost-plugin-digitalocean/server/client"
"text/tabwriter"
"time"
)
func (p *Plugin) listDatabaseClustersCommandFunc(client client.DigitalOceanService, args *model.CommandArgs) (*model.CommandResponse, *model.AppError) {
opts := &godo.ListOptions{}
databases, response, err := client.ListDatabaseClusters(context.TODO(), opts)
if err != nil {
p.API.LogError("failed to fetch databases", "response", response, "Err", err.Error())
return p.responsef(args, "Error while fetching databases list"),
&model.AppError{Message: err.Error()}
}
if len(databases) == 0 {
return p.responsef(args, "You don't have any databases configured"), nil
}
buffer := new(bytes.Buffer)
w := new(tabwriter.Writer)
w.Init(buffer, 8, 8, 0, '\t', 0)
fmt.Fprintf(w, "\n |%s|%s|%s|%s|%s|%s|", "ID", "Name", "Engine", "Status", "Size", "Region")
fmt.Fprintf(w, "\n |%s|%s|%s|%s|%s|%s|", "------", "----", "----", "----", "----", "----")
for _, db := range databases {
fmt.Fprintf(w, "\n |%s|%s|%s|%s|%s|%s|", db.ID, db.Name, db.EngineSlug, db.Status, db.SizeSlug, db.RegionSlug)
}
w.Flush()
return p.responsef(args, buffer.String()), nil
}
func (p *Plugin) listDatabaseClusterBackupsCommandFunc(client client.DigitalOceanService, args *model.CommandArgs, id string) (*model.CommandResponse, *model.AppError) {
backups, response, err := client.ListDatabaseClusterBackups(context.TODO(), id, nil)
if err != nil {
p.API.LogError("failed to get backups for database", "id", id, "response", response, "Err", err.Error())
return p.responsef(args, "Error while fetching backups list for database cluster %s", id),
&model.AppError{Message: err.Error()}
}
if len(backups) == 0 {
return p.responsef(args, "You don't have any cluster backups"), nil
}
buffer := new(bytes.Buffer)
w := new(tabwriter.Writer)
w.Init(buffer, 8, 8, 0, '\t', 0)
fmt.Fprintf(w, "\n |%s|%s|", "Created At", "Size (GB)")
fmt.Fprintf(w, "\n |%s|%s|", "------", "----")
for _, backup := range backups {
fmt.Fprintf(w, "\n |%s|%f|", backup.CreatedAt.Format(time.RFC822), backup.SizeGigabytes)
}
w.Flush()
return p.responsef(args, buffer.String()), nil
}
func (p *Plugin) addUserToDatabaseClusterCommandFunc(client client.DigitalOceanService, args *model.CommandArgs, id, name string) (*model.CommandResponse, *model.AppError) {
dbUserCreateReq := &godo.DatabaseCreateUserRequest{
Name: name,
MySQLSettings: nil,
}
user, response, err := client.CreateDatabaseUser(context.TODO(), id, dbUserCreateReq)
if err != nil {
p.API.LogError("failed to create user for database", "id", id, "response", response, "Err", err.Error())
return p.responsef(args, "Error while creating a user on database %s because %s", id, err.Error()),
&model.AppError{Message: err.Error()}
}
return p.responsef(args, "Name: `%s`\t Password: `%s`\t Role: `%s`", user.Name, user.Password, user.Role), nil
}
func (p *Plugin) listDatabaseClusterUsersCommandFunc(client client.DigitalOceanService, args *model.CommandArgs, id string) (*model.CommandResponse, *model.AppError) {
users, response, err := client.ListDatabaseClusterUsers(context.TODO(), id, nil)
if err != nil {
p.API.LogError("failed to get users for database", "id", id, "response", response, "Err", err.Error())
return p.responsef(args, "Error while fetching users for database cluster %s", id),
&model.AppError{Message: err.Error()}
}
if len(users) == 0 {
return p.responsef(args, "You don't have any cluster backups"), nil
}
usersList := ""
for _, user := range users {
usersList += fmt.Sprintf("- Name: `%s`\t Role: `%s`\n", user.Name, user.Role)
}
return p.responsef(args, usersList), nil
}
func (p *Plugin) deleteDatabaseClusterUserCommandFunc(client client.DigitalOceanService, args *model.CommandArgs, id, userName string) (*model.CommandResponse, *model.AppError) {
response, err := client.DeleteDatabaseClusterUser(context.TODO(), id, userName)
if err != nil {
p.API.LogError("failed to delete user for database", "id", id, "name", userName, "response", response, "Err", err.Error())
return p.responsef(args, "Error while deleting user %s for database cluster %s because %s", userName, id, err.Error()),
&model.AppError{Message: err.Error()}
}
return p.responsef(args, "Successfully deleted user %s from database %s", userName, id), nil
}
func (p *Plugin) listDatabasesInClusterCommandFunc(client client.DigitalOceanService, args *model.CommandArgs, id string) (*model.CommandResponse, *model.AppError) {
dbs, response, err := client.ListDatabasesInCluster(context.TODO(), id, nil)
if err != nil {
p.API.LogError("failed to list databases in this cluster", "id", id, "response", response, "Err", err.Error())
return p.responsef(args, "Error listing databases in the cluster %s because %s", id, err.Error()),
&model.AppError{Message: err.Error()}
}
if len(dbs) == 0 {
return p.responsef(args, "You don't have any cluster backups"), nil
}
dbList := ""
for i, db := range dbs {
dbList += fmt.Sprintf("%d. Name: `%s`\n", i+1, db.Name)
}
return p.responsef(args, dbList), nil
}