/
cloud.go
241 lines (225 loc) · 5.66 KB
/
cloud.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
// RAINBOND, Application Management Platform
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Rainbond,
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
// must be obtained first.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package controller
import (
"net/http"
"github.com/go-chi/chi"
"github.com/goodrain/rainbond/api/handler"
api_model "github.com/goodrain/rainbond/api/model"
httputil "github.com/goodrain/rainbond/util/http"
)
//CloudManager cloud manager
type CloudManager struct{}
var defaultCloudManager *CloudManager
//GetCloudRouterManager get cloud Manager
func GetCloudRouterManager() *CloudManager {
if defaultCloudManager != nil {
return defaultCloudManager
}
defaultCloudManager = &CloudManager{}
return defaultCloudManager
}
//Show Show
func (c *CloudManager) Show(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("cloud urls"))
}
//CreateToken CreateToken
// swagger:operation POST /cloud/auth cloud createToken
//
// 产生token
//
// create token
//
// ---
// consumes:
// - application/json
// - application/x-protobuf
//
// produces:
// - application/json
// - application/xml
//
// responses:
// default:
// schema:
// "$ref": "#/responses/commandResponse"
// description: 统一返回格式
func (c *CloudManager) CreateToken(w http.ResponseWriter, r *http.Request) {
var gt api_model.GetUserToken
if ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, >.Body, nil); !ok {
return
}
ti, err := handler.GetCloudManager().TokenDispatcher(>)
if err != nil {
err.Handle(r, w)
return
}
httputil.ReturnSuccess(r, w, ti)
}
//GetTokenInfo GetTokenInfo
// swagger:operation GET /cloud/auth/{eid} cloud getTokenInfo
//
// 获取tokeninfo
//
// get token info
//
// ---
// consumes:
// - application/json
// - application/x-protobuf
//
// produces:
// - application/json
// - application/xml
//
// responses:
// default:
// schema:
// "$ref": "#/responses/commandResponse"
// description: 统一返回格式
func (c *CloudManager) GetTokenInfo(w http.ResponseWriter, r *http.Request) {
eid := chi.URLParam(r, "eid")
ti, err := handler.GetCloudManager().GetTokenInfo(eid)
if err != nil {
err.Handle(r, w)
return
}
httputil.ReturnSuccess(r, w, ti)
}
//UpdateToken UpdateToken
// swagger:operation PUT /cloud/auth/{eid} cloud updateToken
//
// 更新token
//
// update token info
//
// ---
// consumes:
// - application/json
// - application/x-protobuf
//
// produces:
// - application/json
// - application/xml
//
// responses:
// default:
// schema:
// "$ref": "#/responses/commandResponse"
// description: 统一返回格式
func (c *CloudManager) UpdateToken(w http.ResponseWriter, r *http.Request) {
var ut api_model.UpdateToken
if ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &ut.Body, nil); !ok {
return
}
eid := chi.URLParam(r, "eid")
err := handler.GetCloudManager().UpdateTokenTime(eid, ut.Body.ValidityPeriod)
if err != nil {
err.Handle(r, w)
return
}
httputil.ReturnSuccess(r, w, nil)
}
//GetAPIManager GetAPIManager
// swagger:operation GET /cloud/api/manager cloud GetAPIManager
//
// 获取api管理
//
// get api manager
//
// ---
// consumes:
// - application/json
// - application/x-protobuf
//
// produces:
// - application/json
// - application/xml
//
// responses:
// default:
// schema:
// "$ref": "#/responses/commandResponse"
// description: 统一返回格式
func (c *CloudManager) GetAPIManager(w http.ResponseWriter, r *http.Request) {
apiMap := handler.GetTokenIdenHandler().GetAPIManager()
httputil.ReturnSuccess(r, w, apiMap)
}
//AddAPIManager AddAPIManager
// swagger:operation POST /cloud/api/manager cloud addAPIManager
//
// 添加api管理
//
// get api manager
//
// ---
// consumes:
// - application/json
// - application/x-protobuf
//
// produces:
// - application/json
// - application/xml
//
// responses:
// default:
// schema:
// "$ref": "#/responses/commandResponse"
// description: 统一返回格式
func (c *CloudManager) AddAPIManager(w http.ResponseWriter, r *http.Request) {
var am api_model.APIManager
if ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &am.Body, nil); !ok {
return
}
err := handler.GetTokenIdenHandler().AddAPIManager(&am)
if err != nil {
err.Handle(r, w)
return
}
httputil.ReturnSuccess(r, w, nil)
}
//DeleteAPIManager DeleteAPIManager
// swagger:operation DELETE /cloud/api/manager cloud deleteAPIManager
//
// 删除api管理
//
// delete api manager
//
// ---
// consumes:
// - application/json
// - application/x-protobuf
//
// produces:
// - application/json
// - application/xml
//
// responses:
// default:
// schema:
// "$ref": "#/responses/commandResponse"
// description: 统一返回格式
func (c *CloudManager) DeleteAPIManager(w http.ResponseWriter, r *http.Request) {
var am api_model.APIManager
if ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &am.Body, nil); !ok {
return
}
err := handler.GetTokenIdenHandler().DeleteAPIManager(&am)
if err != nil {
err.Handle(r, w)
return
}
httputil.ReturnSuccess(r, w, nil)
}