Skip to content

add node registrar client #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
3c60200
add node client with account and nodes functions signatures
Eslam-Nawara Feb 17, 2025
12c5380
add sign request and parse response helpers
Eslam-Nawara Feb 17, 2025
4dd5b29
add signature for farm functions
Eslam-Nawara Feb 17, 2025
4fe9206
add account methods implementation
Eslam-Nawara Feb 17, 2025
b4d2cf5
add farms implementation
Eslam-Nawara Feb 19, 2025
24fe635
add node implementation
Eslam-Nawara Feb 19, 2025
08f4d7a
update accounts and farms to filter and update with options
Eslam-Nawara Feb 19, 2025
c51c466
fix client create account signature
Eslam-Nawara Feb 20, 2025
db6fe6c
add relays and RMBEncKey on creating account
Eslam-Nawara Feb 23, 2025
116099c
add mock tests for account and client
Eslam-Nawara Feb 23, 2025
ee6c0c4
add tests for node and farm in registrar client
Eslam-Nawara Feb 24, 2025
8e4109c
fix registrar client
Eslam-Nawara Feb 24, 2025
18931e7
ensure twin is set on setting zos version
Eslam-Nawara Feb 24, 2025
d88d7cb
fix uptime tests
Eslam-Nawara Feb 25, 2025
0c310f4
remove pk from ensure account
Eslam-Nawara Feb 25, 2025
b0dded7
add registrar client readme file
Eslam-Nawara Feb 26, 2025
caf590c
remove twin id from create farm
Eslam-Nawara Feb 26, 2025
8fcef4f
update node registrar client to accept private key instead of seed
Eslam-Nawara Feb 26, 2025
3aaaf8f
Add mnemonics to registrar client (#23)
Eslam-Nawara Mar 5, 2025
41cbe7e
fix update farm error parsing
Eslam-Nawara Mar 9, 2025
7908c98
add stellar address
Eslam-Nawara Mar 9, 2025
c5b1cff
support create/update farm with stellar address
Eslam-Nawara Mar 9, 2025
79f9bcb
fix farm tests
Eslam-Nawara Mar 9, 2025
0761f4f
update stellarAddress json constraints
Eslam-Nawara Mar 10, 2025
b9fc076
fix typos in client
Eslam-Nawara Mar 10, 2025
3eb0fb7
add functions documentation
Eslam-Nawara Mar 10, 2025
55d9ee5
use base64 encoding for signature in create account
Eslam-Nawara Mar 20, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/btcsuite/btcd v0.22.0-beta/go.mod h1:9n5ntfhhHQBIhUvlhDvD3Qg6fRUj4jkN0VB8L8svzOA=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457/go.mod h1:pRgIJT+bRLFKnoM1ldnzKoxTIn14Yxz928LQRYYgIN0=
golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0=
Expand Down
296 changes: 296 additions & 0 deletions node-registrar/client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,296 @@
# ThreeFold Grid Node Registrar Client

A Go client for interacting with the ThreeFold Grid Node Registrar service. Facilitates node registration and management on the ThreeFold Grid.

## Overview

The Node Registrar Client enables communication with the ThreeFold Grid's node registration service. It provides methods to:

* Register nodes
* Manage node metadata
* Retrieve node information
* Delete node registrations

## Features

### Version

* **Get Zos Version**: Loads zos version for the current network
* **Set Zos Version**: Set zos version to specific version (can only be done by the network admin)

### Accounts

* **Create Account**: Create new account on the registrar with uniqe key.
* **Update Account**: Update the account configuration (relays & rmbEncKey).
* **Ensure Account**: Ensures that an account is created with specific seed/mnemonic.
* **Get Account**: Get an account using either its twin\_id or its public\_key.

### Farms

* **Create Farm**: Create new farm on the registrar with uniqe name.
* **update Farm**: Update farm configuration (farm\_id, dedicated).
* **Get Farm**: Get a farm using its farm\_id.

### Node

* **Register Node**: Register physical/virtual nodes with on TFGrid.
* **Update Node**: Update node configuration (farm\_id, interfaces, resources, location, secure\_boot, virtualized).
* **Get Node**: Fetch registered node details using (node\_id, twin\_id, farm\_id).
* **Update Node Uptime**: Update node Uptime.

### API Methods

#### Version Operations

| Method | Description | Parameters | Returns |
|-----------------|----------------------------------|------------------------------------|---------------------|
| GetZosVersion | Get current zos version | None | (ZosVersion, error) |
| SetZosVersion | Update zos version (admin-only) | version string, safeToUpgrade bool | error |

#### Account Management

| Method | Description | Parameters | Returns |
|----------------|---------------------------|-----------------------------------|------------------|
| CreateAccount | Create new account | relays []string, rmbEncKey string | (Account, error) |
| EnsureAccount | Create account if missing | relays []string, rmbEncKey string | (Account, error) |
| GetAccount | Get account by twin ID | twinID uint64 | (Account, error) |
| GetAccountByPK | Get account by public key | publicKey string | (Account, error) |
| UpdateAccount | Modify account config | ...UpdateOption | error |

#### Farm Operations

| Method | Description | Parameters | Returns |
|-------------|------------------------|--------------------------------------------|-----------------|
| CreateFarm | Register new farm | name string, twinID uint64, dedicated bool | (uint64, error) |
| UpdateFarm | Modify farm properties | farmID uint64, ...UpdateOption | error |
| GetFarm | Get farm by ID | farmID uint64 | (Farm, error) |
| ListFarms | List farms | ...ListOption | ([]Farm, error) |

#### Node Operations

| Method | Description | Parameters | Returns |
|-----------------|-----------------------|------------------------------------------------|-----------------|
| RegisterNode | Register new node | farmID uint64, twinID uint64, interfaces []Interface, location Location, resources Resources, serial string, secureBoot bool, virtualized bool | (uint64, error) |
| UpdateNode | Modify node config | ...UpdateOption | error |
| GetNode | Get node by node ID | nodeID uint64 | (Node, error) |
| GetNodeByTwinID | Get node by twin ID | twinID uint64 | (Node, error) |
| ListNodes | List nodes | ...ListOption | ([]Node, error) |
| ReportUptime | Submit uptime metrics | report UptimeReport | error |

## Installation

```bash
go get github.com/threefoldtech/tfgrid4-sdk-go/node-registrar/client
```

## Usage

### Initialize Client

```go
import (
"github.com/threefoldtech/tfgrid4-sdk-go/node-registrar/client"
)

func main() {
registrarURL := "https://registrar.dev4.grid.tf/v1"

// Generate 128-bit entropy (12-word mnemonic)
entropy, err := bip39.NewEntropy(128)
if err != nil {
panic(err)
}

// Generate mnemonic from entropy
mnemonic, err = bip39.NewMnemonic(entropy)
if err != nil {
panic(err)
}
fmt.Println("New Mnemonic:", mnemonic)

cli, err := client.NewRegistrarClient(registrarURL, mnemonic)
if err != nil {
panic(err)
}
}
```

### Get Zos Version

```go
version, err := c.GetZosVersion()
if err != nil {
log.Fatal().Err(err).Msg("failed to set registrar version")
}

log.Info().Msgf("%s version is: %+v", network, version)
```

### Set Zos Version (ONLY for network admin)

```go
err := c.SetZosVersion("v0.1.8", true)
if err != nil {
log.Fatal().Err(err).Msg("failed to set registrar version")
}

log.Info().Msg("version is updated successfully")
```

### Create Account

```go
account, err := c.CreateAccount(relays, rmbEncKey)
if err != nil {
log.Fatal().Err(err).Msg("failed to create new account on registrar")
}

log.Info().Uint64("twinID", account.TwinID).Msg("account created successfully")

```

### Get Account

#### Get Account By Public Key

```go
account, err := c.GetAccountByPK(pk)
if err != nil {
log.Fatal().Err(err).Msg("failed to get account from registrar")
}
log.Info().Any("account", account).Send()
```

#### Get Account By Twin ID

```go
account, err := c.GetAccount(id)
if err != nil {
log.Fatal().Err(err).Msg("failed to get account from registrar")
}
log.Info().Any("account", account).Send()

```

### Update Account

```go
err := c.UpdateAccount(client.UpdateAccountWithRelays(relays), client.UpdateAccountWithRMBEncKey(rmbEncKey))
if err != nil {
log.Fatal().Err(err).Msg("failed to get account from registrar")
}
log.Info().Msg("account updated successfully")
```

### Ensure Account

```go
account, err := c.EnsureAccount(relays, rmbEncKey)
if err != nil {
log.Fatal().Err(err).Msg("failed to ensure account account from registrar")
}
log.Info().Any("account", account).Send()
```

### Create Farm

```go
id, err := c.CreateFarm(farmName, twinID, false)
if err != nil {
log.Fatal().Err(err).Msg("failed to create new farm on registrar")
}

log.Info().Uint64("farmID", id).Msg("farm created successfully")
```

### Get Farm

```go
farm, err := c.GetFarm(id)
if err != nil {
log.Fatal().Err(err).Msg("failed to get farm from registrar")
}
log.Info().Any("farm", farm).Send()
```

### List Farms

```go
farms, err := c.ListFarms(ListFarmWithName(name))
if err != nil {
log.Fatal().Err(err).Msg("failed to list farms from registrar")
}
log.Info().Any("farm", farms[0]).Send()
```

### Update Farm

```go
err := c.UpdateFarm(farmID, client.UpdateFarmWithName(name))
if err != nil {
log.Fatal().Err(err).Msg("failed to get farm from registrar")
}
log.Info().Msg("farm updated successfully")
```

### Register a Node

```go
id, err := c.RegisterNode(farmID, twinID, interfaces, location, resources, serialNumber, secureBoot, virtualized)
if err != nil {
log.Fatal().Err(err).Msg("failed to register a new node on registrar")
}
log.Info().Uint64("nodeID", id).Msg("node registered successfully")
```

### Get Node

#### Get Node With Node ID

```go
node, err := c.GetNode(id)
if err != nil {
log.Fatal().Err(err).Msg("failed to get node from registrar")
}
log.Info().Any("node", node).Send()
```

#### Get Node With Twin ID

```go
node, err := c.GetNodeByTwinID(id)
if err != nil {
log.Fatal().Err(err).Msg("failed to get node from registrar")
}
log.Info().Any("node", node).Send()
```

### List Nodes

```go
nodes, err := c.ListNodes(client.ListNodesWithFarmID(id))
if err != nil {
log.Fatal().Err(err).Msg("failed to list nodes from registrar")
}
log.Info().Any("node", node[0]).Send()
```

### Update Node

```go
err := c.UpdateNode(client.UpdateNodesWithLocation(location))
if err != nil {
log.Fatal().Err(err).Msg("failed to update node location on the registrar")
}
log.Info().Msg("node updated successfully")
```

### Update Node Uptime

```go
err := c.ReportUptime(report)
if err != nil {
log.Fatal().Err(err).Msg("failed to update node uptime in the registrar")
}
log.Info().Msg("node uptime is updated successfully")
```
Loading
Loading