-
Notifications
You must be signed in to change notification settings - Fork 7
/
sshkey.go
84 lines (74 loc) · 2.28 KB
/
sshkey.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
package main
import (
"bufio"
"context"
"os"
"github.com/gridscale/gsclient-go/v3"
log "github.com/sirupsen/logrus"
)
var emptyCtx = context.Background()
// exampleSSHkey is an example of SSH-key, don't use it in production
const exampleSSHkey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDPUCeKyPUNBZOikJKx+Id7udqm/ZKArvCn2AqwwRr02 john@example.com"
func main() {
uuid := os.Getenv("GRIDSCALE_UUID")
token := os.Getenv("GRIDSCALE_TOKEN")
config := gsclient.DefaultConfiguration(uuid, token)
client := gsclient.NewClient(config)
log.Info("gridscale client configured")
log.Info("Create SSH-key: Press 'Enter' to continue...")
bufio.NewReader(os.Stdin).ReadBytes('\n')
cSSHkey, err := client.CreateSshkey(
emptyCtx,
gsclient.SshkeyCreateRequest{
Name: "go-client-ssh-key",
Sshkey: exampleSSHkey,
})
if err != nil {
log.Error("Create SSH-key has failed with error", err)
return
}
log.WithFields(log.Fields{
"sshkey_uuid": cSSHkey.ObjectUUID,
}).Info("SSH-key successfully created")
defer func() {
err := client.DeleteSshkey(emptyCtx, cSSHkey.ObjectUUID)
if err != nil {
log.Error("Delete SSH-key has failed with error", err)
return
}
log.Info("SSH-key successfully deleted")
}()
// Get a SSH-key to update
sshkey, err := client.GetSshkey(emptyCtx, cSSHkey.ObjectUUID)
if err != nil {
log.Error("Get SSH-key has failed with error", err)
return
}
log.Info("Update SSH-key: Press 'Enter' to continue...")
bufio.NewReader(os.Stdin).ReadBytes('\n')
err = client.UpdateSshkey(
emptyCtx,
sshkey.Properties.ObjectUUID,
gsclient.SshkeyUpdateRequest{
Name: "updated SSH-key",
Sshkey: sshkey.Properties.Sshkey,
Labels: &sshkey.Properties.Labels,
})
if err != nil {
log.Error("Update SSH-key has failed with error", err)
return
}
log.Info("SSH-key successfully updated")
log.Info("Get SSH-key's events: Press 'Enter' to continue...")
bufio.NewReader(os.Stdin).ReadBytes('\n')
events, err := client.GetSshkeyEventList(emptyCtx, sshkey.Properties.ObjectUUID)
if err != nil {
log.Error("Get SSH-key's events has failed with error", err)
return
}
log.WithFields(log.Fields{
"events": events,
}).Info("SSH-key's events successfully retrieved")
log.Info("Delete SSH-key: Press 'Enter' to continue...")
bufio.NewReader(os.Stdin).ReadBytes('\n')
}