|
| 1 | +package nodeclient |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/threefoldtech/tfgrid-sdk-go/messenger" |
| 7 | + "github.com/threefoldtech/zosbase/pkg" |
| 8 | + "github.com/threefoldtech/zosbase/pkg/api" |
| 9 | + "github.com/threefoldtech/zosbase/pkg/capacity/dmi" |
| 10 | + "github.com/threefoldtech/zosbase/pkg/diagnostics" |
| 11 | + "github.com/threefoldtech/zosbase/pkg/geoip" |
| 12 | + "github.com/threefoldtech/zosbase/pkg/gridtypes" |
| 13 | +) |
| 14 | + |
| 15 | +type NodeClient struct { |
| 16 | + rpcClient *messenger.JSONRPCClient |
| 17 | + destination string |
| 18 | +} |
| 19 | + |
| 20 | +// TODO: paramter types in cleint/apirpc |
| 21 | +func NewNodeClient(msgr *messenger.Messenger, destination string) *NodeClient { |
| 22 | + return &NodeClient{ |
| 23 | + rpcClient: messenger.NewJSONRPCClient(msgr), |
| 24 | + destination: destination, |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +// System Methods |
| 29 | + |
| 30 | +func (c *NodeClient) GetNodeVersion(ctx context.Context) (api.Version, error) { |
| 31 | + var version api.Version |
| 32 | + if err := c.rpcClient.Call(ctx, c.destination, "system.version", nil, &version); err != nil { |
| 33 | + return api.Version{}, err |
| 34 | + } |
| 35 | + return version, nil |
| 36 | +} |
| 37 | + |
| 38 | +func (c *NodeClient) GetSystemDMI(ctx context.Context) (dmi.DMI, error) { |
| 39 | + var dmiInfo dmi.DMI |
| 40 | + if err := c.rpcClient.Call(ctx, c.destination, "system.dmi", nil, &dmiInfo); err != nil { |
| 41 | + return dmi.DMI{}, err |
| 42 | + } |
| 43 | + return dmiInfo, nil |
| 44 | +} |
| 45 | + |
| 46 | +func (c *NodeClient) GetSystemHypervisor(ctx context.Context) (string, error) { |
| 47 | + var hypervisor string |
| 48 | + if err := c.rpcClient.Call(ctx, c.destination, "system.hypervisor", nil, &hypervisor); err != nil { |
| 49 | + return "", err |
| 50 | + } |
| 51 | + return hypervisor, nil |
| 52 | +} |
| 53 | + |
| 54 | +func (c *NodeClient) GetSystemDiagnostics(ctx context.Context) (diagnostics.Diagnostics, error) { |
| 55 | + var diag diagnostics.Diagnostics |
| 56 | + if err := c.rpcClient.Call(ctx, c.destination, "system.diagnostics", nil, &diag); err != nil { |
| 57 | + return diagnostics.Diagnostics{}, err |
| 58 | + } |
| 59 | + return diag, nil |
| 60 | +} |
| 61 | + |
| 62 | +func (c *NodeClient) GetSystemNodeFeatures(ctx context.Context) ([]pkg.NodeFeature, error) { |
| 63 | + var features []pkg.NodeFeature |
| 64 | + if err := c.rpcClient.Call(ctx, c.destination, "system.features", nil, &features); err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + return features, nil |
| 68 | +} |
| 69 | + |
| 70 | +// Monitor/Performance Methods |
| 71 | + |
| 72 | +func (c *NodeClient) GetPerfSpeed(ctx context.Context) (pkg.IperfTaskResult, error) { |
| 73 | + var result pkg.IperfTaskResult |
| 74 | + if err := c.rpcClient.Call(ctx, c.destination, "monitor.speed", nil, &result); err != nil { |
| 75 | + return pkg.IperfTaskResult{}, err |
| 76 | + } |
| 77 | + return result, nil |
| 78 | +} |
| 79 | + |
| 80 | +func (c *NodeClient) GetPerfHealth(ctx context.Context) (pkg.HealthTaskResult, error) { |
| 81 | + var result pkg.HealthTaskResult |
| 82 | + if err := c.rpcClient.Call(ctx, c.destination, "monitor.health", nil, &result); err != nil { |
| 83 | + return pkg.HealthTaskResult{}, err |
| 84 | + } |
| 85 | + return result, nil |
| 86 | +} |
| 87 | + |
| 88 | +func (c *NodeClient) GetPerfPublicIp(ctx context.Context) (pkg.PublicIpTaskResult, error) { |
| 89 | + var result pkg.PublicIpTaskResult |
| 90 | + if err := c.rpcClient.Call(ctx, c.destination, "monitor.publicip", nil, &result); err != nil { |
| 91 | + return pkg.PublicIpTaskResult{}, err |
| 92 | + } |
| 93 | + return result, nil |
| 94 | +} |
| 95 | + |
| 96 | +func (c *NodeClient) GetPerfBenchmark(ctx context.Context) (pkg.CpuBenchTaskResult, error) { |
| 97 | + var result pkg.CpuBenchTaskResult |
| 98 | + if err := c.rpcClient.Call(ctx, c.destination, "monitor.benchmark", nil, &result); err != nil { |
| 99 | + return pkg.CpuBenchTaskResult{}, err |
| 100 | + } |
| 101 | + return result, nil |
| 102 | +} |
| 103 | + |
| 104 | +func (c *NodeClient) GetPerfAll(ctx context.Context) (pkg.AllTaskResult, error) { |
| 105 | + var result pkg.AllTaskResult |
| 106 | + if err := c.rpcClient.Call(ctx, c.destination, "monitor.all", nil, &result); err != nil { |
| 107 | + return pkg.AllTaskResult{}, err |
| 108 | + } |
| 109 | + return result, nil |
| 110 | +} |
| 111 | + |
| 112 | +// Network Methods |
| 113 | + |
| 114 | +func (c *NodeClient) GetNetworkWGPorts(ctx context.Context) ([]uint, error) { |
| 115 | + var ports []uint |
| 116 | + if err := c.rpcClient.Call(ctx, c.destination, "network.wg_ports", nil, &ports); err != nil { |
| 117 | + return nil, err |
| 118 | + } |
| 119 | + return ports, nil |
| 120 | +} |
| 121 | + |
| 122 | +func (c *NodeClient) GetNetworkPublicConfig(ctx context.Context) (pkg.PublicConfig, error) { |
| 123 | + var config pkg.PublicConfig |
| 124 | + if err := c.rpcClient.Call(ctx, c.destination, "network.public_config", nil, &config); err != nil { |
| 125 | + return pkg.PublicConfig{}, err |
| 126 | + } |
| 127 | + return config, nil |
| 128 | +} |
| 129 | + |
| 130 | +func (c *NodeClient) GetNetworkHasIPv6(ctx context.Context) (bool, error) { |
| 131 | + var hasIPv6 bool |
| 132 | + if err := c.rpcClient.Call(ctx, c.destination, "network.has_ipv6", nil, &hasIPv6); err != nil { |
| 133 | + return false, err |
| 134 | + } |
| 135 | + return hasIPv6, nil |
| 136 | +} |
| 137 | + |
| 138 | +func (c *NodeClient) GetNetworkPublicIPs(ctx context.Context) ([]string, error) { |
| 139 | + var ips []string |
| 140 | + if err := c.rpcClient.Call(ctx, c.destination, "network.public_ips", nil, &ips); err != nil { |
| 141 | + return nil, err |
| 142 | + } |
| 143 | + return ips, nil |
| 144 | +} |
| 145 | + |
| 146 | +func (c *NodeClient) GetNetworkPrivateIPs(ctx context.Context, networkName string) ([]string, error) { |
| 147 | + params := map[string]any{ |
| 148 | + "network_name": networkName, |
| 149 | + } |
| 150 | + var ips []string |
| 151 | + if err := c.rpcClient.Call(ctx, c.destination, "network.private_ips", params, &ips); err != nil { |
| 152 | + return nil, err |
| 153 | + } |
| 154 | + return ips, nil |
| 155 | +} |
| 156 | + |
| 157 | +func (c *NodeClient) GetNetworkInterfaces(ctx context.Context) (pkg.Interfaces, error) { |
| 158 | + var interfaces pkg.Interfaces |
| 159 | + if err := c.rpcClient.Call(ctx, c.destination, "network.interfaces", nil, &interfaces); err != nil { |
| 160 | + return pkg.Interfaces{}, err |
| 161 | + } |
| 162 | + return interfaces, nil |
| 163 | +} |
| 164 | + |
| 165 | +func (c *NodeClient) SetNetworkPublicNIC(ctx context.Context, device string) error { |
| 166 | + params := map[string]any{ |
| 167 | + "device": device, |
| 168 | + } |
| 169 | + return c.rpcClient.Call(ctx, c.destination, "network.set_public_nic", params, nil) |
| 170 | +} |
| 171 | + |
| 172 | +func (c *NodeClient) GetNetworkPublicNIC(ctx context.Context) (pkg.ExitDevice, error) { |
| 173 | + var device pkg.ExitDevice |
| 174 | + if err := c.rpcClient.Call(ctx, c.destination, "network.get_public_nic", nil, &device); err != nil { |
| 175 | + return pkg.ExitDevice{}, err |
| 176 | + } |
| 177 | + return device, nil |
| 178 | +} |
| 179 | + |
| 180 | +// Deployment Methods |
| 181 | + |
| 182 | +func (c *NodeClient) DeploymentDeploy(ctx context.Context, deployment gridtypes.Deployment) error { |
| 183 | + return c.rpcClient.Call(ctx, c.destination, "deployment.deploy", deployment, nil) |
| 184 | +} |
| 185 | + |
| 186 | +func (c *NodeClient) DeploymentUpdate(ctx context.Context, deployment gridtypes.Deployment) error { |
| 187 | + return c.rpcClient.Call(ctx, c.destination, "deployment.update", deployment, nil) |
| 188 | +} |
| 189 | + |
| 190 | +func (c *NodeClient) DeploymentGet(ctx context.Context, contractID uint64) (gridtypes.Deployment, error) { |
| 191 | + params := map[string]any{ |
| 192 | + "contract_id": contractID, |
| 193 | + } |
| 194 | + var deployment gridtypes.Deployment |
| 195 | + if err := c.rpcClient.Call(ctx, c.destination, "deployment.get", params, &deployment); err != nil { |
| 196 | + return gridtypes.Deployment{}, err |
| 197 | + } |
| 198 | + return deployment, nil |
| 199 | +} |
| 200 | + |
| 201 | +func (c *NodeClient) DeploymentList(ctx context.Context) ([]gridtypes.Deployment, error) { |
| 202 | + var deployments []gridtypes.Deployment |
| 203 | + if err := c.rpcClient.Call(ctx, c.destination, "deployment.list", nil, &deployments); err != nil { |
| 204 | + return nil, err |
| 205 | + } |
| 206 | + return deployments, nil |
| 207 | +} |
| 208 | + |
| 209 | +func (c *NodeClient) DeploymentChanges(ctx context.Context, contractID uint64) ([]gridtypes.Workload, error) { |
| 210 | + params := map[string]any{ |
| 211 | + "contract_id": contractID, |
| 212 | + } |
| 213 | + var changes []gridtypes.Workload |
| 214 | + if err := c.rpcClient.Call(ctx, c.destination, "deployment.changes", params, &changes); err != nil { |
| 215 | + return nil, err |
| 216 | + } |
| 217 | + return changes, nil |
| 218 | +} |
| 219 | + |
| 220 | +// Other Methods |
| 221 | + |
| 222 | +func (c *NodeClient) GetGpuList(ctx context.Context) ([]pkg.GPUInfo, error) { |
| 223 | + var gpus []pkg.GPUInfo |
| 224 | + if err := c.rpcClient.Call(ctx, c.destination, "gpu.list", nil, &gpus); err != nil { |
| 225 | + return nil, err |
| 226 | + } |
| 227 | + return gpus, nil |
| 228 | +} |
| 229 | + |
| 230 | +func (c *NodeClient) GetStoragePools(ctx context.Context) ([]pkg.PoolMetrics, error) { |
| 231 | + var pools []pkg.PoolMetrics |
| 232 | + if err := c.rpcClient.Call(ctx, c.destination, "storage.pools", nil, &pools); err != nil { |
| 233 | + return nil, err |
| 234 | + } |
| 235 | + return pools, nil |
| 236 | +} |
| 237 | + |
| 238 | +func (c *NodeClient) GetStatistics(ctx context.Context) (pkg.Counters, error) { |
| 239 | + var stats pkg.Counters |
| 240 | + if err := c.rpcClient.Call(ctx, c.destination, "statistics", nil, &stats); err != nil { |
| 241 | + return pkg.Counters{}, err |
| 242 | + } |
| 243 | + return stats, nil |
| 244 | +} |
| 245 | + |
| 246 | +func (c *NodeClient) GetLocation(ctx context.Context) (geoip.Location, error) { |
| 247 | + var location geoip.Location |
| 248 | + if err := c.rpcClient.Call(ctx, c.destination, "location.get", nil, &location); err != nil { |
| 249 | + return geoip.Location{}, err |
| 250 | + } |
| 251 | + return location, nil |
| 252 | +} |
0 commit comments