forked from harness/harness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.go
126 lines (114 loc) · 3.02 KB
/
registry.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
package server
import (
"net/http"
"github.com/drone/drone/model"
"github.com/drone/drone/router/middleware/session"
"github.com/gin-gonic/gin"
)
// GetRegistry gets the name registry from the database and writes
// to the response in json format.
func GetRegistry(c *gin.Context) {
var (
repo = session.Repo(c)
name = c.Param("registry")
)
registry, err := Config.Services.Registries.RegistryFind(repo, name)
if err != nil {
c.String(404, "Error getting registry %q. %s", name, err)
return
}
c.JSON(200, registry.Copy())
}
// PostRegistry persists the registry to the database.
func PostRegistry(c *gin.Context) {
repo := session.Repo(c)
in := new(model.Registry)
if err := c.Bind(in); err != nil {
c.String(http.StatusBadRequest, "Error parsing request. %s", err)
return
}
registry := &model.Registry{
RepoID: repo.ID,
Address: in.Address,
Username: in.Username,
Password: in.Password,
Token: in.Token,
Email: in.Email,
}
if err := registry.Validate(); err != nil {
c.String(400, "Error inserting registry. %s", err)
return
}
if err := Config.Services.Registries.RegistryCreate(repo, registry); err != nil {
c.String(500, "Error inserting registry %q. %s", in.Address, err)
return
}
c.JSON(200, in.Copy())
}
// PatchRegistry updates the registry in the database.
func PatchRegistry(c *gin.Context) {
var (
repo = session.Repo(c)
name = c.Param("registry")
)
in := new(model.Registry)
err := c.Bind(in)
if err != nil {
c.String(http.StatusBadRequest, "Error parsing request. %s", err)
return
}
registry, err := Config.Services.Registries.RegistryFind(repo, name)
if err != nil {
c.String(404, "Error getting registry %q. %s", name, err)
return
}
if in.Username != "" {
registry.Username = in.Username
}
if in.Password != "" {
registry.Password = in.Password
}
if in.Token != "" {
registry.Token = in.Token
}
if in.Email != "" {
registry.Email = in.Email
}
if err := registry.Validate(); err != nil {
c.String(400, "Error updating registry. %s", err)
return
}
if err := Config.Services.Registries.RegistryUpdate(repo, registry); err != nil {
c.String(500, "Error updating registry %q. %s", in.Address, err)
return
}
c.JSON(200, in.Copy())
}
// GetRegistryList gets the registry list from the database and writes
// to the response in json format.
func GetRegistryList(c *gin.Context) {
repo := session.Repo(c)
list, err := Config.Services.Registries.RegistryList(repo)
if err != nil {
c.String(500, "Error getting registry list. %s", err)
return
}
// copy the registry detail to remove the sensitive
// password and token fields.
for i, registry := range list {
list[i] = registry.Copy()
}
c.JSON(200, list)
}
// DeleteRegistry deletes the named registry from the database.
func DeleteRegistry(c *gin.Context) {
var (
repo = session.Repo(c)
name = c.Param("registry")
)
if err := Config.Services.Registries.RegistryDelete(repo, name); err != nil {
c.String(500, "Error deleting registry %q. %s", name, err)
return
}
c.String(204, "")
}