Skip to content

Commit

Permalink
Merge pull request #1337 from threefoldtech/development_access_nodes
Browse files Browse the repository at this point in the history
Get only the available access nodes for the user while filtering them
  • Loading branch information
AhmedHanafy725 authored Nov 1, 2023
2 parents 2b364e9 + 65412a1 commit eda22e9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/grid_client/src/high_level/machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class VMHL extends HighLevelBase {
let hasAccessNode = false;
let accessNodes: Record<string, unknown> = {};
if (addAccess) {
accessNodes = await this.nodes.getAccessNodes();
accessNodes = await this.nodes.getAccessNodes(this.config.twinId);
for (const accessNode of Object.keys(accessNodes)) {
if (network.nodeExists(Number(accessNode))) {
hasAccessNode = true;
Expand Down
4 changes: 2 additions & 2 deletions packages/grid_client/src/primitives/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class Network {
throw Error(`Node ${node_id} does not exist in the network. Please add it first`);
}
events.emit("logs", `Adding access to node ${node_id}`);
const accessNodes = await this.capacity.getAccessNodes();
const accessNodes = await this.capacity.getAccessNodes(this.config.twinId);
if (Object.keys(accessNodes).includes(node_id.toString())) {
if (ipv4 && !accessNodes[node_id]["ipv4"]) {
throw Error(`Node ${node_id} does not have ipv4 public config.`);
Expand Down Expand Up @@ -279,7 +279,7 @@ class Network {
return this._accessNodes;
}
const accessNodes: number[] = [];
const allAccessNodes = await this.capacity.getAccessNodes();
const allAccessNodes = await this.capacity.getAccessNodes(this.config.twinId);
for (const accessNode of Object.keys(allAccessNodes)) {
if (this.nodeExists(+accessNode)) {
accessNodes.push(+accessNode);
Expand Down
24 changes: 14 additions & 10 deletions packages/grid_client/src/primitives/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,21 +112,25 @@ class Nodes {
});
}

async getAccessNodes(): Promise<Record<string, unknown>> {
async getAccessNodes(availableFor?: number): Promise<Record<string, unknown>> {
const accessNodes = {};
const nodes = await this.filterNodes({ accessNodeV4: true, accessNodeV6: true });
for (const node of nodes) {
const ipv4 = node.publicConfig.ipv4;
const ipv6 = node.publicConfig.ipv6;
const domain = node.publicConfig.domain;
if (PrivateIp(ipv4.split("/")[0]) === false) {
accessNodes[+node.nodeId] = { ipv4: ipv4, ipv6: ipv6, domain: domain };
let nodes: NodeInfo[] = [];
let page = 1;
do {
nodes = await this.filterNodes({ accessNodeV4: true, accessNodeV6: true, availableFor, page });
for (const node of nodes) {
const ipv4 = node.publicConfig.ipv4;
const ipv6 = node.publicConfig.ipv6;
const domain = node.publicConfig.domain;
if (PrivateIp(ipv4.split("/")[0]) === false) {
accessNodes[+node.nodeId] = { ipv4: ipv4, ipv6: ipv6, domain: domain };
}
}
}
page++;
} while (nodes.length);
if (Object.keys(accessNodes).length === 0) {
throw Error("Couldn't find any node with public config");
}
console.log(accessNodes);
return accessNodes;
}

Expand Down

0 comments on commit eda22e9

Please sign in to comment.