From 1bd093736039b432502e4cd2cbb29c15dc2220b7 Mon Sep 17 00:00:00 2001 From: otherview Date: Wed, 24 Jul 2024 16:23:56 +0100 Subject: [PATCH] adding client --- entrypoint/client/client.go | 87 ++++++++++++++++++++++++++++++ entrypoint/client/storage_inmem.go | 27 ++++++++++ 2 files changed, 114 insertions(+) create mode 100644 entrypoint/client/client.go create mode 100644 entrypoint/client/storage_inmem.go diff --git a/entrypoint/client/client.go b/entrypoint/client/client.go new file mode 100644 index 0000000..36197e7 --- /dev/null +++ b/entrypoint/client/client.go @@ -0,0 +1,87 @@ +package client + +import ( + "encoding/json" + "fmt" + "github.com/vechain/networkhub/hub" + "github.com/vechain/networkhub/network" + "github.com/vechain/networkhub/preset" +) + +type Client struct { + networkHub *hub.NetworkHub + presets *preset.Networks + storage *Storage +} + +func New(networkHub *hub.NetworkHub, presets *preset.Networks) *Client { + return &Client{ + networkHub: networkHub, + presets: presets, + storage: NewInMemStorage(), + } +} + +func (c *Client) Stop(id string) error { + return c.networkHub.StopNetwork(id) +} + +func (c *Client) Start(id string) error { + return c.networkHub.StartNetwork(id) +} + +func (c *Client) Config(config string) (string, error) { + var netCfg network.Network + + if err := json.Unmarshal([]byte(config), &netCfg); err != nil { + return "", err + } + return c.config(&netCfg) +} + +func (c *Client) LoadExistingNetworks() error { + nets, err := c.storage.LoadExistingNetworks() + if err != nil { + return fmt.Errorf("unable to load existing networks: %w", err) + } + + for networkID, net := range nets { + loadedID, err := c.networkHub.LoadNetworkConfig(net) + if err != nil { + return err + } + + if networkID != loadedID { + return fmt.Errorf("unexpected networkID loaded: storedID:%s configuredID:%s", networkID, loadedID) + } + } + + return nil +} + +func (c *Client) Preset(presetNetwork string, environment, artifactPath string) (string, error) { + netCfg, err := c.presets.Load(presetNetwork, &preset.APIConfigPayload{Environment: environment, ArtifactPath: artifactPath}) + if err != nil { + return "", fmt.Errorf("unable to load network preset: %w", err) + } + return c.config(netCfg) +} + +func (c *Client) config(netCfg *network.Network) (string, error) { + networkID, err := c.networkHub.LoadNetworkConfig(netCfg) + if err != nil { + return "", fmt.Errorf("unable to load config: %w", err) + } + + networkInst, err := c.networkHub.GetNetwork(networkID) + if err != nil { + return "", fmt.Errorf("unable to retrieve network: %w", err) + } + + err = c.storage.Store(networkID, networkInst) + if err != nil { + return "", fmt.Errorf("unable to store network: %w", err) + } + + return networkID, nil +} diff --git a/entrypoint/client/storage_inmem.go b/entrypoint/client/storage_inmem.go new file mode 100644 index 0000000..23917a3 --- /dev/null +++ b/entrypoint/client/storage_inmem.go @@ -0,0 +1,27 @@ +package client + +import ( + "github.com/vechain/networkhub/network" + "log/slog" +) + +type Storage struct { + path string + storage map[string]*network.Network +} + +func NewInMemStorage() *Storage { + return &Storage{} +} + +func (s *Storage) Store(networkID string, net *network.Network) error { + // Add/Update the network entry + s.storage[networkID] = net + + slog.Info("Network saved to memory") + return nil +} + +func (s *Storage) LoadExistingNetworks() (map[string]*network.Network, error) { + return s.storage, nil +}