Skip to content
This repository was archived by the owner on Sep 17, 2019. It is now read-only.

Commit 3922e44

Browse files
committed
oas interface
1 parent 731b323 commit 3922e44

10 files changed

Lines changed: 606 additions & 5 deletions

File tree

‎common/oas.go‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package common
2+
3+
import (
4+
"fmt"
5+
"moduleab_server/oas"
6+
7+
"github.com/astaxie/beego"
8+
)
9+
10+
var DefaultOasClient *oas.OasClient
11+
12+
func InitOasClient(endpoint string) error {
13+
oasPort, err := beego.AppConfig.Int("aliapi::oasport")
14+
if err != nil {
15+
return fmt.Errorf("Bad config value type (expect int): apiapi::oasport")
16+
}
17+
oasUseSSL, err := beego.AppConfig.Bool("aliapi::oasusessl")
18+
if err != nil {
19+
return fmt.Errorf("Bad config value type (expect bool): apiapi::oasport")
20+
}
21+
22+
DefaultClient = oas.NewOasClient(
23+
a.Endpoint,
24+
beego.AppConfig.String("aliapi::apikey"),
25+
beego.AppConfig.String("aliapi::secret"),
26+
oasPort,
27+
oasUseSSL,
28+
)
29+
return nil
30+
}
31+
32+
func GetOasVaultId(name string) (string, error) {
33+
v := new(oas.VaultsList)
34+
for {
35+
id, v, err := DefaultClient.ListVaults(-1, v.Marker)
36+
beego.Debug("OAS request ID:", id)
37+
if err != nil {
38+
39+
return "", err
40+
}
41+
for _, xv := range v.VaultList {
42+
if xv.VaultName == name {
43+
return xv.VaultID, nil
44+
}
45+
}
46+
if v.Marker == "" {
47+
return "", fmt.Errorf("Vault not found")
48+
}
49+
}
50+
}

‎conf/app.conf‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,10 @@ mysqlprefex = ""
1818
[aliapi]
1919
apikey= "TestAbcd"
2020
secret="TestAAA"
21+
oasport=80
22+
oasusessl=false
23+
24+
[redis]
25+
host = "127.0.0.1:6379"
26+
password = ""
27+
key = "ModuleAB"

‎controllers/app_sets.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func (a *AppSetsController) Post() {
2525
}
2626
a.Ctx.Output.SetStatus(http.StatusBadRequest)
2727
a.ServeJSON()
28+
return
2829
}
2930
beego.Debug("[C] Got data:", appSet)
3031
id, err := models.AddAppSet(appSet)
@@ -36,6 +37,7 @@ func (a *AppSetsController) Post() {
3637
}
3738
a.Ctx.Output.SetStatus(http.StatusInternalServerError)
3839
a.ServeJSON()
40+
return
3941
}
4042

4143
beego.Debug("[C] Got id:", id)
@@ -44,6 +46,7 @@ func (a *AppSetsController) Post() {
4446
}
4547
a.Ctx.Output.SetStatus(http.StatusCreated)
4648
a.ServeJSON()
49+
return
4750
}
4851

4952
// @router /:name [get]
@@ -63,15 +66,18 @@ func (a *AppSetsController) Get() {
6366
beego.Warn("[C] Got error:", err)
6467
a.Ctx.Output.SetStatus(http.StatusInternalServerError)
6568
a.ServeJSON()
69+
return
6670
}
6771
a.Data["json"] = appSets
6872
if len(appSets) == 0 {
6973
beego.Debug("[C] Got nothing with name:", name)
7074
a.Ctx.Output.SetStatus(http.StatusNotFound)
7175
a.ServeJSON()
76+
return
7277
} else {
7378
a.Ctx.Output.SetStatus(http.StatusOK)
7479
a.ServeJSON()
80+
return
7581
}
7682
}
7783
}
@@ -88,15 +94,18 @@ func (a *AppSetsController) GetAll() {
8894
beego.Warn("[C] Got error:", err)
8995
a.Ctx.Output.SetStatus(http.StatusInternalServerError)
9096
a.ServeJSON()
97+
return
9198
}
9299
a.Data["json"] = appSets
93100
if len(appSets) == 0 {
94101
beego.Debug("[C] Got nothing")
95102
a.Ctx.Output.SetStatus(http.StatusNotFound)
96103
a.ServeJSON()
104+
return
97105
} else {
98106
a.Ctx.Output.SetStatus(http.StatusOK)
99107
a.ServeJSON()
108+
return
100109
}
101110
}
102111

@@ -117,11 +126,13 @@ func (a *AppSetsController) Delete() {
117126
beego.Warn("[C] Got error:", err)
118127
a.Ctx.Output.SetStatus(http.StatusInternalServerError)
119128
a.ServeJSON()
129+
return
120130
}
121131
if len(appSets) == 0 {
122132
beego.Debug("[C] Got nothing with name:", name)
123133
a.Ctx.Output.SetStatus(http.StatusNotFound)
124134
a.ServeJSON()
135+
return
125136
}
126137
err = models.DeleteAppSet(appSets[0])
127138
if err != nil {
@@ -132,10 +143,12 @@ func (a *AppSetsController) Delete() {
132143
beego.Warn("[C] Got error:", err)
133144
a.Ctx.Output.SetStatus(http.StatusInternalServerError)
134145
a.ServeJSON()
146+
return
135147

136148
}
137149
a.Ctx.Output.SetStatus(http.StatusNoContent)
138150
a.ServeJSON()
151+
return
139152
}
140153
}
141154

@@ -156,11 +169,13 @@ func (a *AppSetsController) Put() {
156169
beego.Warn("[C] Got error:", err)
157170
a.Ctx.Output.SetStatus(http.StatusInternalServerError)
158171
a.ServeJSON()
172+
return
159173
}
160174
if len(appSets) == 0 {
161175
beego.Debug("[C] Got nothing with name:", name)
162176
a.Ctx.Output.SetStatus(http.StatusNotFound)
163177
a.ServeJSON()
178+
return
164179
}
165180

166181
err = json.Unmarshal(a.Ctx.Input.RequestBody, appSet)
@@ -173,6 +188,7 @@ func (a *AppSetsController) Put() {
173188
}
174189
a.Ctx.Output.SetStatus(http.StatusBadRequest)
175190
a.ServeJSON()
191+
return
176192
}
177193
beego.Debug("[C] Got appSet data:", appSet)
178194
err = models.UpdateAppSet(appSet)
@@ -184,9 +200,11 @@ func (a *AppSetsController) Put() {
184200
beego.Warn("[C] Got error:", err)
185201
a.Ctx.Output.SetStatus(http.StatusInternalServerError)
186202
a.ServeJSON()
203+
return
187204

188205
}
189206
a.Ctx.Output.SetStatus(http.StatusAccepted)
190207
a.ServeJSON()
208+
return
191209
}
192210
}

‎controllers/error.go‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package controllers
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/astaxie/beego"
7+
)
8+
9+
type ErrorController struct {
10+
beego.Controller
11+
}
12+
13+
func (e *ErrorController) Error404() {
14+
e.Ctx.Output.SetStatus(http.StatusNotFound)
15+
e.Data["json"] = map[string]string{
16+
"message": "API not implemented.",
17+
}
18+
e.ServeJSON()
19+
}

‎controllers/hosts.go‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func (h *HostsController) Post() {
3030
}
3131
h.Ctx.Output.SetStatus(http.StatusBadRequest)
3232
h.ServeJSON()
33+
return
3334
}
3435
beego.Debug("[C] Got data:", host)
3536
id, err := models.AddHost(host)
@@ -41,6 +42,7 @@ func (h *HostsController) Post() {
4142
}
4243
h.Ctx.Output.SetStatus(http.StatusInternalServerError)
4344
h.ServeJSON()
45+
return
4446
}
4547

4648
beego.Debug("[C] Got id:", id)
@@ -49,6 +51,7 @@ func (h *HostsController) Post() {
4951
}
5052
h.Ctx.Output.SetStatus(http.StatusCreated)
5153
h.ServeJSON()
54+
return
5255
}
5356

5457
// @Title getHost
@@ -73,15 +76,18 @@ func (h *HostsController) Get() {
7376
beego.Warn("[C] Got error:", err)
7477
h.Ctx.Output.SetStatus(http.StatusInternalServerError)
7578
h.ServeJSON()
79+
return
7680
}
7781
h.Data["json"] = hosts
7882
if len(hosts) == 0 {
7983
beego.Debug("[C] Got nothing with name:", name)
8084
h.Ctx.Output.SetStatus(http.StatusNotFound)
8185
h.ServeJSON()
86+
return
8287
} else {
8388
h.Ctx.Output.SetStatus(http.StatusOK)
8489
h.ServeJSON()
90+
return
8591
}
8692
}
8793
}
@@ -101,15 +107,18 @@ func (h *HostsController) GetAll() {
101107
beego.Warn("[C] Got error:", err)
102108
h.Ctx.Output.SetStatus(http.StatusInternalServerError)
103109
h.ServeJSON()
110+
return
104111
}
105112
h.Data["json"] = hosts
106113
if len(hosts) == 0 {
107114
beego.Debug("[C] Got nothing")
108115
h.Ctx.Output.SetStatus(http.StatusNotFound)
109116
h.ServeJSON()
117+
return
110118
} else {
111119
h.Ctx.Output.SetStatus(http.StatusOK)
112120
h.ServeJSON()
121+
return
113122
}
114123
}
115124

@@ -134,11 +143,13 @@ func (h *HostsController) Delete() {
134143
beego.Warn("[C] Got error:", err)
135144
h.Ctx.Output.SetStatus(http.StatusInternalServerError)
136145
h.ServeJSON()
146+
return
137147
}
138148
if len(hosts) == 0 {
139149
beego.Debug("[C] Got nothing with name:", name)
140150
h.Ctx.Output.SetStatus(http.StatusNotFound)
141151
h.ServeJSON()
152+
return
142153
}
143154
err = models.DeleteHost(hosts[0])
144155
if err != nil {
@@ -149,10 +160,12 @@ func (h *HostsController) Delete() {
149160
beego.Warn("[C] Got error:", err)
150161
h.Ctx.Output.SetStatus(http.StatusInternalServerError)
151162
h.ServeJSON()
163+
return
152164

153165
}
154166
h.Ctx.Output.SetStatus(http.StatusNoContent)
155167
h.ServeJSON()
168+
return
156169
}
157170
}
158171

@@ -167,6 +180,8 @@ func (h *HostsController) Put() {
167180
if name != "" {
168181
host := &models.Hosts{
169182
Name: name,
183+
// 外键关系也需要初始化,否则会出现问题,反向关系则不用
184+
AppSet: new(models.AppSets),
170185
}
171186
hosts, err := models.GetHosts(host)
172187
if err != nil {
@@ -177,15 +192,16 @@ func (h *HostsController) Put() {
177192
beego.Warn("[C] Got error:", err)
178193
h.Ctx.Output.SetStatus(http.StatusInternalServerError)
179194
h.ServeJSON()
195+
return
180196
}
181197
if len(hosts) == 0 {
182198
beego.Debug("[C] Got nothing with name:", name)
183199
h.Ctx.Output.SetStatus(http.StatusNotFound)
184200
h.ServeJSON()
201+
return
185202
}
186203

187204
err = json.Unmarshal(h.Ctx.Input.RequestBody, host)
188-
host.Id = hosts[0].Id
189205
if err != nil {
190206
beego.Warn("[C] Got error:", err)
191207
h.Data["json"] = map[string]string{
@@ -194,6 +210,11 @@ func (h *HostsController) Put() {
194210
}
195211
h.Ctx.Output.SetStatus(http.StatusBadRequest)
196212
h.ServeJSON()
213+
return
214+
}
215+
host.Id = hosts[0].Id
216+
if host.AppSet != nil {
217+
host.AppSet = hosts[0].AppSet
197218
}
198219
beego.Debug("[C] Got host data:", host)
199220
err = models.UpdateHost(host)
@@ -205,9 +226,11 @@ func (h *HostsController) Put() {
205226
beego.Warn("[C] Got error:", err)
206227
h.Ctx.Output.SetStatus(http.StatusInternalServerError)
207228
h.ServeJSON()
229+
return
208230

209231
}
210232
h.Ctx.Output.SetStatus(http.StatusAccepted)
211233
h.ServeJSON()
234+
return
212235
}
213236
}

0 commit comments

Comments
 (0)