Skip to content

Commit 0c6c21d

Browse files
committed
fix deployment handlers and twinid in context
1 parent d738187 commit 0c6c21d

File tree

12 files changed

+305
-282
lines changed

12 files changed

+305
-282
lines changed

cmds/modules/api_gateway/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func action(cli *cli.Context) error {
106106
api.SetupRoutes(router)
107107

108108
go func() {
109-
if err := apirpc.Run(port, msgBrokerCon, manager, redis); err != nil {
109+
if err := apirpc.Run(ctx, port, msgBrokerCon, manager, redis); err != nil {
110110
log.Error().Err(err).Msg("failed to run rpc server")
111111
return
112112
}

pkg/api/api.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package api
22

33
import (
4+
"context"
45
"fmt"
56

67
substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
@@ -23,7 +24,7 @@ type Api struct {
2324
farmerID uint32
2425
}
2526

26-
func NewApi(manager substrate.Manager, client zbus.Client, msgBrokerCon string) (*Api, error) {
27+
func NewApi(ctx context.Context, manager substrate.Manager, client zbus.Client, msgBrokerCon string) (*Api, error) {
2728
sub, err := manager.Substrate()
2829
if err != nil {
2930
return nil, err

pkg/api/deployment.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,29 @@ import (
44
"context"
55
"fmt"
66

7-
"github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer"
87
"github.com/threefoldtech/zos/pkg/gridtypes"
98
)
109

11-
func (a *Api) DeploymentDeployHandler(ctx context.Context, deployment gridtypes.Deployment) error {
12-
return a.provisionStub.CreateOrUpdate(ctx, peer.GetTwinID(ctx), deployment, false)
10+
func (a *Api) DeploymentDeployHandler(ctx context.Context, twinId uint32, deployment gridtypes.Deployment) error {
11+
return a.provisionStub.CreateOrUpdate(ctx, twinId, deployment, false)
1312
}
1413

15-
func (g *Api) DeploymentUpdateHandler(ctx context.Context, deployment gridtypes.Deployment) error {
16-
return g.provisionStub.CreateOrUpdate(ctx, peer.GetTwinID(ctx), deployment, true)
14+
func (g *Api) DeploymentUpdateHandler(ctx context.Context, twinId uint32, deployment gridtypes.Deployment) error {
15+
return g.provisionStub.CreateOrUpdate(ctx, twinId, deployment, true)
1716
}
1817

1918
func (g *Api) DeploymentDeleteHandler(ctx context.Context) error {
2019
return fmt.Errorf("deletion over the Api is disabled, please cancel your contract instead")
2120
}
2221

23-
func (g *Api) DeploymentGetHandler(ctx context.Context, contractId uint64) (gridtypes.Deployment, error) {
24-
return g.provisionStub.Get(ctx, peer.GetTwinID(ctx), contractId)
22+
func (g *Api) DeploymentGetHandler(ctx context.Context, twinId uint32, contractId uint64) (gridtypes.Deployment, error) {
23+
return g.provisionStub.Get(ctx, twinId, contractId)
2524
}
2625

27-
func (g *Api) DeploymentListHandler(ctx context.Context) ([]gridtypes.Deployment, error) {
28-
return g.provisionStub.List(ctx, peer.GetTwinID(ctx))
26+
func (g *Api) DeploymentListHandler(ctx context.Context, twinId uint32) ([]gridtypes.Deployment, error) {
27+
return g.provisionStub.List(ctx, twinId)
2928
}
3029

31-
func (g *Api) DeploymentChangesHandler(ctx context.Context, contractId uint64) ([]gridtypes.Workload, error) {
32-
return g.provisionStub.Changes(ctx, peer.GetTwinID(ctx), contractId)
30+
func (g *Api) DeploymentChangesHandler(ctx context.Context, twinId uint32, contractId uint64) ([]gridtypes.Workload, error) {
31+
return g.provisionStub.Changes(ctx, twinId, contractId)
3332
}

pkg/api/network.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"net"
77

8-
"github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer"
98
"github.com/threefoldtech/zos/pkg"
109
"github.com/threefoldtech/zos/pkg/gridtypes"
1110
)
@@ -68,6 +67,6 @@ func (g *Api) NetworkListPublicIPsHandler(ctx context.Context) ([]string, error)
6867
return g.provisionStub.ListPublicIPs(ctx)
6968
}
7069

71-
func (g *Api) NetworkListPrivateIPsHandler(ctx context.Context, networkName gridtypes.Name) ([]string, error) {
72-
return g.provisionStub.ListPrivateIPs(ctx, peer.GetTwinID(ctx), networkName)
70+
func (g *Api) NetworkListPrivateIPsHandler(ctx context.Context, twinId uint32, networkName gridtypes.Name) ([]string, error) {
71+
return g.provisionStub.ListPrivateIPs(ctx, twinId, networkName)
7372
}

pkg/apirpc/readme.md

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,3 @@ echo '{"method": "zos.SystemVersion", "params": [], "id": 1}' | nc -q 1 localhos
1212
{"jsonrpc":"2.0","result":{"zos":"0.0.0","zinit":"v0.2.11"},"id":1}
1313
```
1414

15-
16-
---
17-
- you can assign values directly to reply
18-
- you only can return/take an object or simple type, not arrays can be defined for both limit from tool and net/rpc
19-
- json tags is necessary, it is the way for convertion
20-
- returning error works
21-
22-
todos:
23-
- sperate perf methods
24-
- don't leave any map or interface in the api
25-
- continue fixing spec/generator/service
26-
- maunally convert. less error-prone
27-
- test all handlers carefully
28-
- reset/clean commit/description with breaking changes

pkg/apirpc/server.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package apirpc
22

33
import (
4+
"context"
45
"fmt"
56
"net"
67
"net/rpc"
@@ -11,8 +12,8 @@ import (
1112
"github.com/threefoldtech/zbus"
1213
)
1314

14-
func Run(port uint, broker string, manager substrate.Manager, redis zbus.Client) error {
15-
service, err := NewApiService(broker, manager, redis)
15+
func Run(ctx context.Context, port uint, broker string, manager substrate.Manager, redis zbus.Client) error {
16+
service, err := NewApiService(ctx, broker, manager, redis)
1617
if err != nil {
1718
return fmt.Errorf("failed to create api service: %w", err)
1819
}
@@ -33,7 +34,29 @@ func Run(port uint, broker string, manager substrate.Manager, redis zbus.Client)
3334
log.Error().Err(err).Send()
3435
continue
3536
}
36-
log.Info().Msg("recieved")
37+
38+
SetTwinId(ctx, GetTwinFromIp(conn.RemoteAddr().String()))
39+
log.Info().Uint32("twinId", GetTwinID(ctx)).Msg("got rpc request")
3740
go jsonrpc.ServeConn(conn)
3841
}
3942
}
43+
44+
type twinId struct{}
45+
46+
func GetTwinFromIp(ip string) uint32 {
47+
// placeholder, waiting for implementation in mycelium
48+
return 29
49+
}
50+
51+
func SetTwinId(ctx context.Context, id uint32) context.Context {
52+
return context.WithValue(ctx, twinId{}, id)
53+
}
54+
55+
func GetTwinID(ctx context.Context) uint32 {
56+
twin, ok := ctx.Value(twinId{}).(uint32)
57+
if !ok {
58+
panic("failed to load twin id from context")
59+
}
60+
61+
return twin
62+
}

0 commit comments

Comments
 (0)