Skip to content

Commit 3d47219

Browse files
committed
add more swapi plugins/packages/modules
1 parent 714686d commit 3d47219

32 files changed

+2824
-0
lines changed

backups/controllers.go

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
package backups
2+
3+
import (
4+
"net/http"
5+
"sync"
6+
7+
"github.com/gin-gonic/gin"
8+
)
9+
10+
var b sync.Map
11+
12+
// @Summary Get all backuped services
13+
// @Description get backuped services
14+
// @Tags backups
15+
// @Produce json
16+
// @Success 200 {object} string "ok"
17+
// @Router /backups [get]
18+
func GetBackupsStatus(c *gin.Context) {
19+
var services = make(map[string]Backup)
20+
21+
b.Range(func(rawKey, rawVal interface{}) bool {
22+
k, ok := rawKey.(string)
23+
v, ok := rawVal.(Backup)
24+
25+
if !ok {
26+
return false
27+
}
28+
29+
services[k] = v
30+
return true
31+
})
32+
33+
c.IndentedJSON(http.StatusOK, gin.H{
34+
"code": http.StatusOK,
35+
"message": "ok, dumping all sockets",
36+
"backups": services,
37+
})
38+
return
39+
}
40+
41+
// @Summary Get backup status by project/service
42+
// @Description get backup status by project/service
43+
// @Tags backups
44+
// @Produce json
45+
// @Param host path string true "backup service name"
46+
// @Success 200 {string} string "ok"
47+
// @Router /backups/{service} [get]
48+
func GetBackupStatusByServiceName(c *gin.Context) {
49+
var name string = c.Param("service")
50+
var service Backup
51+
52+
rawService, ok := b.Load(name)
53+
service, ok = rawService.(Backup)
54+
if !ok {
55+
c.IndentedJSON(http.StatusNotFound, gin.H{
56+
"code": http.StatusNotFound,
57+
"message": "service not found",
58+
})
59+
return
60+
}
61+
62+
c.IndentedJSON(http.StatusOK, gin.H{
63+
"code": http.StatusOK,
64+
"message": "dumping requested backuped service's status",
65+
"backup": service,
66+
})
67+
return
68+
}
69+
70+
// @Summary Adding new backuped serivce
71+
// @Description add new backuped service
72+
// @Tags backups
73+
// @Produce json
74+
// @Param request body backups.Backup true "query params"
75+
// @Success 200 {object} backups.Backup
76+
// @Router /backups [post]
77+
func PostBackupService(c *gin.Context) {
78+
var newService = &Backup{}
79+
80+
if err := c.BindJSON(newService); err != nil {
81+
c.JSON(http.StatusBadRequest, gin.H{
82+
"code": http.StatusBadRequest,
83+
"message": "cannot parse input JSON stream",
84+
})
85+
return
86+
}
87+
88+
if _, found := b.Load(newService.ServiceName); found {
89+
c.IndentedJSON(http.StatusConflict, gin.H{
90+
"code": http.StatusConflict,
91+
"message": "service already exists",
92+
"name": newService.ServiceName,
93+
})
94+
return
95+
}
96+
97+
b.Store(newService.ServiceName, newService)
98+
99+
c.IndentedJSON(http.StatusCreated, gin.H{
100+
"code": http.StatusCreated,
101+
"message": "new project added",
102+
"service": newService,
103+
})
104+
return
105+
}
106+
107+
// @Summary Update backup status by service
108+
// @Description update backup status by service
109+
// @Tags backups
110+
// @Produce json
111+
// @Param request body backups.Backup.ServiceName true "query params"
112+
// @Success 200 {object} backups.Backup
113+
// @Router /backups/{service} [put]
114+
func UpdateBackupStatusByServiceName(c *gin.Context) {
115+
var updatedService Backup
116+
var postedService = &Backup{}
117+
var name string = c.Param("service")
118+
119+
rawService, ok := b.Load(name)
120+
updatedService, ok = rawService.(Backup)
121+
122+
if !ok {
123+
c.IndentedJSON(http.StatusNotFound, gin.H{
124+
"code": http.StatusNotFound,
125+
"message": "service not found",
126+
"name": name,
127+
})
128+
return
129+
}
130+
131+
if err := c.BindJSON(postedService); err != nil {
132+
c.JSON(http.StatusBadRequest, gin.H{
133+
"code": http.StatusBadRequest,
134+
"message": "cannot parse input JSON stream",
135+
})
136+
return
137+
}
138+
139+
// manually update important report fields
140+
updatedService.Timestamp = postedService.Timestamp
141+
updatedService.LastStatus = postedService.LastStatus
142+
updatedService.FileName = postedService.FileName
143+
updatedService.Size = postedService.Size
144+
145+
b.Store(name, updatedService)
146+
147+
c.IndentedJSON(http.StatusOK, gin.H{
148+
"code": http.StatusOK,
149+
"message": "service updated",
150+
"backup": updatedService,
151+
})
152+
return
153+
}
154+
155+
// (PUT /backups/{service}/active)
156+
// @Summary Acitive/inactive backup toggle by its ServiceName
157+
// @Description active/inactive backup toggle by its ServiceName
158+
// @Tags backups
159+
// @Produce json
160+
// @Param service_name path string true "service name"
161+
// @Success 200 {object} backups.Backup
162+
// @Router /backups/{service}/active [put]
163+
func ActiveToggleBackupByServiceName(c *gin.Context) {
164+
var service Backup
165+
var name string = c.Param("service")
166+
167+
rawService, ok := b.Load(name)
168+
if !ok {
169+
c.IndentedJSON(http.StatusNotFound, gin.H{
170+
"message": "service not found",
171+
"code": http.StatusNotFound,
172+
"name": name,
173+
})
174+
return
175+
}
176+
177+
service, typeOk := rawService.(Backup)
178+
if !typeOk {
179+
c.IndentedJSON(http.StatusConflict, gin.H{
180+
"message": "stored value is not type Backup",
181+
"code": http.StatusConflict,
182+
})
183+
return
184+
}
185+
186+
// inverse the Active field value
187+
service.Active = !service.Active
188+
189+
b.Store(name, service)
190+
191+
c.IndentedJSON(http.StatusOK, gin.H{
192+
"code": http.StatusOK,
193+
"message": "backuped service active toggle pressed!",
194+
"backup": service,
195+
})
196+
return
197+
}
198+
199+
// @Summary Delete backup service by its Name
200+
// @Description delete backup service by its Name
201+
// @Tags backups
202+
// @Produce json
203+
// @Success 200 {string} string "ok"
204+
// @Router /backups/{service} [delete]
205+
func DeleteBackupByServiceName(c *gin.Context) {
206+
var name string = c.Param("service")
207+
208+
if _, ok := b.Load(name); !ok {
209+
c.IndentedJSON(http.StatusNotFound, gin.H{
210+
"code": http.StatusNotFound,
211+
"message": "service not found",
212+
"name": name,
213+
})
214+
return
215+
}
216+
217+
b.Delete(name)
218+
219+
c.IndentedJSON(http.StatusOK, gin.H{
220+
"code": http.StatusOK,
221+
"message": "service deleted by Name",
222+
"name": name,
223+
})
224+
return
225+
}
226+
227+
// @Summary Upload backups dump backup -- restores all backup services
228+
// @Description upload backups JSON dump
229+
// @Tags backups
230+
// @Accept json
231+
// @Produce json
232+
// @Router /backups/restore [post]
233+
func PostDumpRestore(c *gin.Context) {
234+
var importServices = &Backups{}
235+
var service Backup
236+
237+
if err := c.BindJSON(importServices); err != nil {
238+
c.IndentedJSON(http.StatusBadRequest, gin.H{
239+
"code": http.StatusBadRequest,
240+
"message": "cannot parse input JSON stream",
241+
})
242+
return
243+
}
244+
245+
for _, service = range importServices.Backups {
246+
b.Store(service.ServiceName, service)
247+
}
248+
249+
c.IndentedJSON(http.StatusCreated, gin.H{
250+
"code": http.StatusCreated,
251+
"message": "backuped services imported, omitting output",
252+
})
253+
return
254+
}

backups/models.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package backups
2+
3+
type Backups struct {
4+
// Array of to be backuped services.
5+
//Backups []Backup `json:"backups"`
6+
Backups map[string]Backup `json:"backups"`
7+
}
8+
9+
type Backup struct {
10+
// Backuped service name -- unique identifier.
11+
ServiceName string `json:"service_name" binding:"required" validation:"required"`
12+
13+
// More verbose description of such service backup.
14+
Description string `json:"description"`
15+
16+
// Last status string of such backup (e.g. success, failure).
17+
LastStatus string `json:"last_status" default:"unknown"`
18+
19+
// UNIX timestamp of the last provided backup.
20+
Timestamp int `json:"timestamp"`
21+
22+
// Size of the gzip/tar archive.
23+
Size string `json:"backup_size"`
24+
25+
// Name of the compressed backup file.
26+
FileName string `json:"file_name"`
27+
28+
// Path to the file on destination machine.
29+
FileDestination string `json:"file_destination"`
30+
31+
// Dumping script, git URL.
32+
ExecutorURL string `json:"executor_url"`
33+
34+
// Boolean indicating if the service is to be backuped.
35+
Active bool `json:"active" default:false`
36+
}

backups/routes.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package backups
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
)
6+
7+
func Routes(g *gin.RouterGroup) {
8+
g.GET("/",
9+
GetBackupsStatus)
10+
g.POST("/",
11+
PostBackupService)
12+
g.GET("/:service",
13+
GetBackupStatusByServiceName)
14+
g.PUT("/:service",
15+
UpdateBackupStatusByServiceName)
16+
g.DELETE("/:service",
17+
DeleteBackupByServiceName)
18+
g.PUT("/:service/active",
19+
ActiveToggleBackupByServiceName)
20+
g.POST("/restore",
21+
PostDumpRestore)
22+
}

0 commit comments

Comments
 (0)