-
Notifications
You must be signed in to change notification settings - Fork 7
/
keys.go
91 lines (66 loc) 路 2.92 KB
/
keys.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
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"
)
func drawKeysTable(keys []godo.Key) string {
buffer := new(bytes.Buffer)
w := new(tabwriter.Writer)
w.Init(buffer, 8, 8, 0, '\t', 0)
fmt.Fprintf(w, "\n |%s|%s|%s|", "ID", "Name", "Public Key")
fmt.Fprintf(w, "\n |%s|%s|%s|", "------", "----", "------")
for _, key := range keys {
fmt.Fprintf(w, "\n |%d|%s|%s|", key.ID, key.Name, key.PublicKey)
}
w.Flush()
return buffer.String()
}
func (p *Plugin) listSSHKeysCommandFunc(client client.DigitalOceanService, args *model.CommandArgs) (*model.CommandResponse, *model.AppError) {
opts := &godo.ListOptions{}
keys, response, err := client.ListSSHKeys(context.TODO(), opts)
if err != nil {
p.API.LogError("failed to fetch ssh keys", "response", response, "Err", err.Error())
return p.responsef(args, "Error while fetching ssh keys list"),
&model.AppError{Message: err.Error()}
}
if len(keys) == 0 {
return p.responsef(args, "You don't have any ssh keys defined. Use `/do create-key <name> <publicKey>` to create one"), nil
}
return p.responsef(args, drawKeysTable(keys)), nil
}
func (p *Plugin) createSSHKeysCommandFunc(client client.DigitalOceanService, args *model.CommandArgs, name, publicKey string) (*model.CommandResponse, *model.AppError) {
keyCreateRequest := &godo.KeyCreateRequest{
Name: name,
PublicKey: publicKey,
}
key, response, err := client.CreateSSHKey(context.TODO(), keyCreateRequest)
if err != nil {
p.API.LogError("failed to create ssh key", "response", response, "Err", err.Error())
return p.responsef(args, "Error while creating SSH key. %s", err.Error()),
&model.AppError{Message: err.Error()}
}
return p.responsef(args, "Successfully created SSH key %s", key.Name), nil
}
func (p *Plugin) retrieveSSHKeyCommandFunc(client client.DigitalOceanService, args *model.CommandArgs, id int) (*model.CommandResponse, *model.AppError) {
key, response, err := client.GetSSHKeyByID(context.TODO(), id)
if err != nil {
p.API.LogError("Failed to retrieve SSH key", "id", id, "response", response, "Err", err.Error())
return p.responsef(args, "Failed to retrieve SSH key with ID `%d`", id),
&model.AppError{Message: err.Error()}
}
return p.responsef(args, drawKeysTable([]godo.Key{*key})), nil
}
func (p *Plugin) deleteSSHKeyCommandFunc(client client.DigitalOceanService, args *model.CommandArgs, id int) (*model.CommandResponse, *model.AppError) {
response, err := client.DeleteSSHKeyByID(context.TODO(), id)
if err != nil {
p.API.LogError("Failed to delete SSH key", "id", id, "response", response, "Err", err.Error())
return p.responsef(args, "Failed to delete SSH key with ID `%d`", id),
&model.AppError{Message: err.Error()}
}
return p.responsef(args, "Successfully delete SSH key with ID `%d`", id), nil
}