Skip to content

Commit a37a0e8

Browse files
committed
add NewZosAPIWithFarmerID function to zos api light
1 parent 66f89ed commit a37a0e8

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

pkg/zos_api_light/zos_api.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package zosapi
22

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

67
"github.com/patrickmn/go-cache"
8+
substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
79
"github.com/threefoldtech/zbus"
810
"github.com/threefoldtech/zosbase/pkg/capacity"
911
"github.com/threefoldtech/zosbase/pkg/diagnostics"
12+
"github.com/threefoldtech/zosbase/pkg/environment"
1013
"github.com/threefoldtech/zosbase/pkg/stubs"
1114
)
1215

@@ -29,7 +32,43 @@ type ZosAPI struct {
2932
inMemCache *cache.Cache
3033
}
3134

32-
func NewZosAPI(client zbus.Client, farmerID uint32, msgBrokerCon string) (ZosAPI, error) {
35+
func NewZosAPI(manager substrate.Manager, client zbus.Client, msgBrokerCon string) (ZosAPI, error) {
36+
sub, err := manager.Substrate()
37+
if err != nil {
38+
return ZosAPI{}, err
39+
}
40+
defer sub.Close()
41+
diagnosticsManager, err := diagnostics.NewDiagnosticsManager(msgBrokerCon, client)
42+
if err != nil {
43+
return ZosAPI{}, err
44+
}
45+
storageModuleStub := stubs.NewStorageModuleStub(client)
46+
api := ZosAPI{
47+
oracle: capacity.NewResourceOracle(storageModuleStub),
48+
versionMonitorStub: stubs.NewVersionMonitorStub(client),
49+
systemMonitorStub: stubs.NewSystemMonitorStub(client),
50+
provisionStub: stubs.NewProvisionStub(client),
51+
networkerLightStub: stubs.NewNetworkerLightStub(client),
52+
statisticsStub: stubs.NewStatisticsStub(client),
53+
storageStub: storageModuleStub,
54+
performanceMonitorStub: stubs.NewPerformanceMonitorStub(client),
55+
diagnosticsManager: diagnosticsManager,
56+
}
57+
farm, err := sub.GetFarm(uint32(environment.MustGet().FarmID))
58+
if err != nil {
59+
return ZosAPI{}, fmt.Errorf("failed to get farm: %w", err)
60+
}
61+
62+
farmer, err := sub.GetTwin(uint32(farm.TwinID))
63+
if err != nil {
64+
return ZosAPI{}, err
65+
}
66+
api.farmerID = uint32(farmer.ID)
67+
api.inMemCache = cache.New(cacheDefaultExpiration, cacheDefaultCleanup)
68+
return api, nil
69+
}
70+
71+
func NewZosAPIWithFarmerID(client zbus.Client, farmerID uint32, msgBrokerCon string) (ZosAPI, error) {
3372
diagnosticsManager, err := diagnostics.NewDiagnosticsManager(msgBrokerCon, client)
3473
if err != nil {
3574
return ZosAPI{}, err

0 commit comments

Comments
 (0)