-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
5,453 additions
and
455 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package registry | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"github.com/aviate-labs/agent-go/principal" | ||
v1 "github.com/aviate-labs/agent-go/registry/proto/v1" | ||
"google.golang.org/protobuf/proto" | ||
"sort" | ||
"strings" | ||
) | ||
|
||
type Client struct { | ||
dp *DataProvider | ||
deltas []*v1.RegistryDelta | ||
} | ||
|
||
func New() (*Client, error) { | ||
dp, err := NewDataProvider() | ||
if err != nil { | ||
return nil, err | ||
} | ||
deltas, _, err := dp.GetChangesSince(0) | ||
if err != nil { | ||
return nil, err | ||
} | ||
sort.Slice(deltas, func(i, j int) bool { | ||
return 0 < bytes.Compare(deltas[i].Key, deltas[j].Key) | ||
}) | ||
return &Client{ | ||
dp: dp, | ||
deltas: deltas, | ||
}, nil | ||
} | ||
|
||
func (c *Client) GetNNSSubnetID() (*principal.Principal, error) { | ||
v, _, err := c.dp.GetValueUpdate([]byte("nns_subnet_id"), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var nnsSubnetID v1.SubnetId | ||
if err := proto.Unmarshal(v, &nnsSubnetID); err != nil { | ||
return nil, err | ||
} | ||
return &principal.Principal{Raw: nnsSubnetID.PrincipalId.Raw}, nil | ||
} | ||
|
||
func (c *Client) GetNodeList() ([]*v1.NodeRecord, error) { | ||
var nodes []*v1.NodeRecord | ||
for _, delta := range c.deltas { | ||
key := string(delta.Key) | ||
if strings.HasPrefix(key, "node_record_") { | ||
for _, value := range delta.Values { | ||
record := new(v1.NodeRecord) | ||
if err := proto.Unmarshal(value.Value, record); err != nil { | ||
return nil, err | ||
} | ||
nodes = append(nodes, record) | ||
} | ||
} | ||
} | ||
return nodes, nil | ||
} | ||
|
||
func (c *Client) GetSubnetDetails(subnetID principal.Principal) (*v1.SubnetRecord, error) { | ||
v, _, err := c.dp.GetValueUpdate([]byte(fmt.Sprintf("subnet_record_%s", subnetID)), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var record v1.SubnetRecord | ||
if err := proto.Unmarshal(v, &record); err != nil { | ||
return nil, err | ||
} | ||
return &record, nil | ||
} | ||
|
||
func (c *Client) GetSubnetIDs() ([]principal.Principal, error) { | ||
v, _, err := c.dp.GetValueUpdate([]byte("subnet_list"), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var list v1.SubnetListRecord | ||
if err := proto.Unmarshal(v, &list); err != nil { | ||
return nil, err | ||
} | ||
var subnets []principal.Principal | ||
for _, subnet := range list.Subnets { | ||
subnets = append(subnets, principal.Principal{Raw: subnet}) | ||
} | ||
return subnets, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package registry | ||
|
||
import ( | ||
"fmt" | ||
"github.com/aviate-labs/agent-go/principal" | ||
"testing" | ||
) | ||
|
||
var client, _ = New() | ||
|
||
func TestClient_GetNodeList(t *testing.T) { | ||
nodes, err := client.GetNodeList() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if len(nodes) == 0 { | ||
t.Fatal("no nodes") | ||
} | ||
for _, node := range nodes { | ||
fmt.Println(node.GetXnet(), principal.Principal{Raw: node.NodeOperatorId}) | ||
} | ||
} | ||
|
||
func TestClient_GetSubnetIDs(t *testing.T) { | ||
subnetIDs, err := client.GetSubnetIDs() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if len(subnetIDs) == 0 { | ||
t.Fatal("no subnet IDs") | ||
} | ||
subnetID := subnetIDs[0] | ||
if _, err := client.GetSubnetDetails(subnetID); err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.