-
Notifications
You must be signed in to change notification settings - Fork 1
/
sshkey.go
66 lines (51 loc) · 1.47 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
package tumblebug
import (
"fmt"
"net/http"
"github.com/beego/beego/v2/core/validation"
"github.com/cloud-barista/poc-cicd-ladybug/src/core/model"
logger "github.com/sirupsen/logrus"
)
type SSHKey struct {
Model
Config string `json:"connectionName"`
Username string `json:"username"`
PrivateKey string `json:"privateKey"` // output
}
func NewSSHKey(ns string, name string, conf string) *SSHKey {
return &SSHKey{
Model: Model{Name: name, namespace: ns},
Config: conf,
}
}
func (self *SSHKey) GET() (bool, error) {
return self.execute(http.MethodGet, fmt.Sprintf("/ns/%s/resources/sshKey/%s", self.namespace, self.Name), fmt.Sprintf(`{"connectionName" : "%s"}`, self.Config), &self)
}
func (self *SSHKey) POST() error {
// validation
valid := validation.Validation{}
valid.Required(self.Username, "username")
if err := self.validate(valid); err != nil {
return err
}
_, err := self.execute(http.MethodPost, fmt.Sprintf("/ns/%s/resources/sshKey", self.namespace), self, &self)
if err != nil {
return err
}
return nil
}
func (self *SSHKey) DELETE(ns string) error {
exist, err := self.GET()
if err != nil {
return err
}
if exist {
_, err := self.execute(http.MethodDelete, fmt.Sprintf("/ns/%s/resources/sshKey/%s", self.namespace, self.Name), fmt.Sprintf(`{"connectionName" : "%s"}`, self.Config), model.Status{})
if err != nil {
return err
}
} else {
logger.Infof("delete sshkey skip (name=%s, cause=not found)", self.Name)
}
return nil
}