Skip to content

Commit 4a180e7

Browse files
committed
support listing regions
1 parent 7855fb1 commit 4a180e7

10 files changed

Lines changed: 349 additions & 19 deletions

File tree

server/app/app.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ func (a *App) registerHandlers() {
119119
notificationRouter := authRouter.PathPrefix("/notification").Subrouter()
120120
vmRouter := authRouter.PathPrefix("/vm").Subrouter()
121121
k8sRouter := authRouter.PathPrefix("/k8s").Subrouter()
122+
regionRouter := authRouter.PathPrefix("/region").Subrouter()
122123

123124
// sub routes with no authorization
124125
unAuthUserRouter := versionRouter.PathPrefix("/user").Subrouter()
@@ -158,6 +159,8 @@ func (a *App) registerHandlers() {
158159
notificationRouter.HandleFunc("", WrapFunc(a.ListNotificationsHandler)).Methods("GET", "OPTIONS")
159160
notificationRouter.HandleFunc("/{id}", WrapFunc(a.UpdateNotificationsHandler)).Methods("PUT", "OPTIONS")
160161

162+
regionRouter.HandleFunc("", WrapFunc(a.ListRegionsHandler)).Methods("GET", "OPTIONS")
163+
161164
vmRouter.HandleFunc("", WrapFunc(a.DeployVMHandler)).Methods("POST", "OPTIONS")
162165
vmRouter.HandleFunc("/validate/{name}", WrapFunc(a.ValidateVMNameHandler)).Methods("Get", "OPTIONS")
163166
vmRouter.HandleFunc("/{id}", WrapFunc(a.GetVMHandler)).Methods("GET", "OPTIONS")

server/app/vm_handler.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ import (
99
"strings"
1010

1111
"github.com/codescalers/cloud4students/deployer"
12+
"github.com/codescalers/cloud4students/internal"
1213
"github.com/codescalers/cloud4students/middlewares"
1314
"github.com/codescalers/cloud4students/models"
1415
"github.com/codescalers/cloud4students/streams"
1516
"github.com/gorilla/mux"
1617
"github.com/rs/zerolog/log"
18+
"github.com/threefoldtech/tfgrid-sdk-go/grid-proxy/pkg/types"
1719
"gopkg.in/validator.v2"
1820
"gorm.io/gorm"
1921
)
@@ -352,3 +354,45 @@ func (a *App) DeleteAllVMsHandler(req *http.Request) (interface{}, Response) {
352354
Data: nil,
353355
}, Ok()
354356
}
357+
358+
// ListRegionsHandler returns all supported regions
359+
// Example endpoint: List all supported regions
360+
// @Summary List all supported regions
361+
// @Description List all supported regions
362+
// @Tags Region
363+
// @Accept json
364+
// @Produce json
365+
// @Security BearerAuth
366+
// @Success 200 {object} []string
367+
// @Failure 401 {object} Response
368+
// @Failure 500 {object} Response
369+
// @Router /region [get]
370+
func (a *App) ListRegionsHandler(req *http.Request) (interface{}, Response) {
371+
stats, err := a.deployer.TFPluginClient.GridProxyClient.Stats(req.Context(), types.StatsFilter{})
372+
if err != nil {
373+
log.Error().Err(err).Send()
374+
return nil, InternalServerError(errors.New(internalServerErrorMsg))
375+
}
376+
377+
graphql, err := internal.NewGraphQl(a.config.Account.Network)
378+
if err != nil {
379+
log.Error().Err(err).Send()
380+
return nil, InternalServerError(errors.New(internalServerErrorMsg))
381+
}
382+
383+
var countries []string
384+
for country := range stats.NodesDistribution {
385+
countries = append(countries, country)
386+
}
387+
388+
regions, err := graphql.ListRegions(countries)
389+
if err != nil {
390+
log.Error().Err(err).Send()
391+
return nil, InternalServerError(errors.New(internalServerErrorMsg))
392+
}
393+
394+
return ResponseMsg{
395+
Message: "Regions are found",
396+
Data: regions,
397+
}, Ok()
398+
}

server/app/wrapper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func BadRequest(err error) Response {
137137

138138
// InternalServerError result
139139
func InternalServerError(err error) Response {
140-
return Error(err, 0)
140+
return Error(err)
141141
}
142142

143143
// NotFound response

server/deployer/balance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
// GetBalance returns the current balance of the deployer account
99
func (d *Deployer) GetBalance() (float64, error) {
10-
balance, err := d.tfPluginClient.SubstrateConn.GetBalance(d.tfPluginClient.Identity)
10+
balance, err := d.TFPluginClient.SubstrateConn.GetBalance(d.TFPluginClient.Identity)
1111
if err != nil {
1212
return 0, errors.Wrap(err, "failed to get account balance with the given mnemonics")
1313
}

server/deployer/deployer.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ var (
4848
type Deployer struct {
4949
db models.DB
5050
Redis streams.RedisClient
51-
tfPluginClient deployer.TFPluginClient
51+
TFPluginClient deployer.TFPluginClient
5252
prices internal.Prices
5353

5454
vmDeployed chan bool
@@ -108,12 +108,12 @@ func (d *Deployer) PeriodicDeploy(ctx context.Context, sec int) {
108108
}
109109

110110
if len(vms) > 0 {
111-
err := d.tfPluginClient.NetworkDeployer.BatchDeploy(ctx, vmNets)
111+
err := d.TFPluginClient.NetworkDeployer.BatchDeploy(ctx, vmNets)
112112
if err != nil {
113113
log.Error().Err(err).Msg("failed to batch deploy network")
114114
}
115115

116-
err = d.tfPluginClient.DeploymentDeployer.BatchDeploy(ctx, vms)
116+
err = d.TFPluginClient.DeploymentDeployer.BatchDeploy(ctx, vms)
117117
if err != nil {
118118
log.Error().Err(err).Msg("failed to batch deploy vm")
119119
}
@@ -124,12 +124,12 @@ func (d *Deployer) PeriodicDeploy(ctx context.Context, sec int) {
124124
}
125125

126126
if len(clusters) > 0 {
127-
err := d.tfPluginClient.NetworkDeployer.BatchDeploy(ctx, k8sNets)
127+
err := d.TFPluginClient.NetworkDeployer.BatchDeploy(ctx, k8sNets)
128128
if err != nil {
129129
log.Error().Err(err).Msg("failed to batch deploy network")
130130
}
131131

132-
err = d.tfPluginClient.K8sDeployer.BatchDeploy(ctx, clusters)
132+
err = d.TFPluginClient.K8sDeployer.BatchDeploy(ctx, clusters)
133133
if err != nil {
134134
log.Error().Err(err).Msg("failed to batch deploy clusters")
135135
}
@@ -144,24 +144,24 @@ func (d *Deployer) PeriodicDeploy(ctx context.Context, sec int) {
144144
// CancelDeployment cancel deployments from grid
145145
func (d *Deployer) CancelDeployment(contractID uint64, netContractID uint64, dlType string, dlName string) error {
146146
// cancel deployment
147-
err := d.tfPluginClient.SubstrateConn.CancelContract(d.tfPluginClient.Identity, contractID)
147+
err := d.TFPluginClient.SubstrateConn.CancelContract(d.TFPluginClient.Identity, contractID)
148148
if err != nil {
149149
return err
150150
}
151151

152152
// cancel network
153-
err = d.tfPluginClient.SubstrateConn.CancelContract(d.tfPluginClient.Identity, netContractID)
153+
err = d.TFPluginClient.SubstrateConn.CancelContract(d.TFPluginClient.Identity, netContractID)
154154
if err != nil {
155155
return err
156156
}
157157

158158
// update state
159-
for node, contracts := range d.tfPluginClient.State.CurrentNodeDeployments {
159+
for node, contracts := range d.TFPluginClient.State.CurrentNodeDeployments {
160160
contracts = workloads.Delete(contracts, contractID)
161161
contracts = workloads.Delete(contracts, netContractID)
162-
d.tfPluginClient.State.CurrentNodeDeployments[node] = contracts
162+
d.TFPluginClient.State.CurrentNodeDeployments[node] = contracts
163163

164-
d.tfPluginClient.State.Networks.DeleteNetwork(fmt.Sprintf("%s%sNet", dlType, dlName))
164+
d.TFPluginClient.State.Networks.DeleteNetwork(fmt.Sprintf("%s%sNet", dlType, dlName))
165165
}
166166

167167
return nil

server/deployer/k8s_deployer.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,12 @@ func (d *Deployer) deployK8sClusterWithNetwork(ctx context.Context, k8sDeployInp
124124
}
125125

126126
// checks that network and k8s are deployed successfully
127-
loadedNet, err := d.tfPluginClient.State.LoadNetworkFromGrid(ctx, cluster.NetworkName)
127+
loadedNet, err := d.TFPluginClient.State.LoadNetworkFromGrid(ctx, cluster.NetworkName)
128128
if err != nil {
129129
return 0, 0, 0, errors.Wrapf(err, "failed to load network '%s' on nodes %v", cluster.NetworkName, network.Nodes)
130130
}
131131

132-
loadedCluster, err := d.tfPluginClient.State.LoadK8sFromGrid(ctx, []uint32{node}, cluster.Master.Name)
132+
loadedCluster, err := d.TFPluginClient.State.LoadK8sFromGrid(ctx, []uint32{node}, cluster.Master.Name)
133133
if err != nil {
134134
return 0, 0, 0, errors.Wrapf(err, "failed to load kubernetes cluster '%s' on nodes %v", cluster.Master.Name, network.Nodes)
135135
}
@@ -144,7 +144,7 @@ func (d *Deployer) loadK8s(
144144
networkContractID uint64, k8sContractID uint64,
145145
) (models.K8sCluster, error) {
146146
// load cluster
147-
resCluster, err := d.tfPluginClient.State.LoadK8sFromGrid(ctx, []uint32{node}, k8s.Master.Name)
147+
resCluster, err := d.TFPluginClient.State.LoadK8sFromGrid(ctx, []uint32{node}, k8s.Master.Name)
148148
if err != nil {
149149
return models.K8sCluster{}, err
150150
}
@@ -207,7 +207,7 @@ func (d *Deployer) getK8sAvailableNode(ctx context.Context, k models.K8sCluster)
207207
filter.Region = &k.Master.Region
208208
}
209209

210-
nodes, err := deployer.FilterNodes(ctx, d.tfPluginClient, filter, []uint64{*freeSRU}, nil, rootfs, 1)
210+
nodes, err := deployer.FilterNodes(ctx, d.TFPluginClient, filter, []uint64{*freeSRU}, nil, rootfs, 1)
211211
if err != nil {
212212
return 0, err
213213
}

server/deployer/vms_deployer.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (d *Deployer) deployVM(ctx context.Context, vmInput models.VM, sshKey strin
3838
filter.Region = &vmInput.Region
3939
}
4040

41-
nodeIDs, err := deployer.FilterNodes(ctx, d.tfPluginClient, filter, []uint64{*freeSRU}, nil, nil, 1)
41+
nodeIDs, err := deployer.FilterNodes(ctx, d.TFPluginClient, filter, []uint64{*freeSRU}, nil, nil, 1)
4242
if err != nil {
4343
return nil, 0, 0, err
4444
}
@@ -97,12 +97,12 @@ func (d *Deployer) deployVM(ctx context.Context, vmInput models.VM, sshKey strin
9797
}
9898

9999
// checks that network and vm are deployed successfully
100-
loadedNet, err := d.tfPluginClient.State.LoadNetworkFromGrid(ctx, dl.NetworkName)
100+
loadedNet, err := d.TFPluginClient.State.LoadNetworkFromGrid(ctx, dl.NetworkName)
101101
if err != nil {
102102
return nil, 0, 0, errors.Wrapf(err, "failed to load network '%s' on node %v", dl.NetworkName, dl.NodeID)
103103
}
104104

105-
loadedDl, err := d.tfPluginClient.State.LoadDeploymentFromGrid(ctx, nodeID, dl.Name)
105+
loadedDl, err := d.TFPluginClient.State.LoadDeploymentFromGrid(ctx, nodeID, dl.Name)
106106
if err != nil {
107107
return nil, 0, 0, errors.Wrapf(err, "failed to load vm '%s' on node %v", dl.Name, dl.NodeID)
108108
}

server/docs/docs.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,45 @@ const docTemplate = `{
10041004
}
10051005
}
10061006
},
1007+
"/region": {
1008+
"get": {
1009+
"security": [
1010+
{
1011+
"BearerAuth": []
1012+
}
1013+
],
1014+
"description": "List all supported regions",
1015+
"consumes": [
1016+
"application/json"
1017+
],
1018+
"produces": [
1019+
"application/json"
1020+
],
1021+
"tags": [
1022+
"Region"
1023+
],
1024+
"summary": "List all supported regions",
1025+
"responses": {
1026+
"200": {
1027+
"description": "OK",
1028+
"schema": {
1029+
"type": "array",
1030+
"items": {
1031+
"type": "string"
1032+
}
1033+
}
1034+
},
1035+
"401": {
1036+
"description": "Unauthorized",
1037+
"schema": {}
1038+
},
1039+
"500": {
1040+
"description": "Internal Server Error",
1041+
"schema": {}
1042+
}
1043+
}
1044+
}
1045+
},
10071046
"/set_admin": {
10081047
"put": {
10091048
"security": [

server/docs/swagger.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,31 @@ paths:
12821282
summary: Set user's notifications as seen
12831283
tags:
12841284
- Notification
1285+
/region:
1286+
get:
1287+
consumes:
1288+
- application/json
1289+
description: List all supported regions
1290+
produces:
1291+
- application/json
1292+
responses:
1293+
"200":
1294+
description: OK
1295+
schema:
1296+
items:
1297+
type: string
1298+
type: array
1299+
"401":
1300+
description: Unauthorized
1301+
schema: {}
1302+
"500":
1303+
description: Internal Server Error
1304+
schema: {}
1305+
security:
1306+
- BearerAuth: []
1307+
summary: List all supported regions
1308+
tags:
1309+
- Region
12851310
/set_admin:
12861311
put:
12871312
consumes:

0 commit comments

Comments
 (0)