From 08e69a1977753a582165d6f773ee7004b202c877 Mon Sep 17 00:00:00 2001 From: Thunder Date: Thu, 19 Oct 2023 12:42:20 +0300 Subject: [PATCH] Expose all and filter methods on the nodes module to list/filter all nodes (#1238) * Created 2 methods 'all' and 'filter' on the nodes module to list and filter the nodes, and added one more field named numberOfPublicIPs returns the number of public ips registered on the deployment. * Apply comments. * Rename NodeStatusFilter to NodeStatus * Update the NodeStatus. --- .../src/clients/tf-grid/contracts.ts | 1 + packages/grid_client/src/modules/models.ts | 7 +++++++ packages/grid_client/src/modules/nodes.ts | 19 ++++++++++++++++--- packages/grid_client/src/primitives/nodes.ts | 6 ++---- 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/packages/grid_client/src/clients/tf-grid/contracts.ts b/packages/grid_client/src/clients/tf-grid/contracts.ts index ac4dda2cb3..1b4c655140 100644 --- a/packages/grid_client/src/clients/tf-grid/contracts.ts +++ b/packages/grid_client/src/clients/tf-grid/contracts.ts @@ -60,6 +60,7 @@ class TFContracts extends Contracts { state createdAt nodeID + numberOfPublicIPs } rentContracts(where: {twinID_eq: ${options.twinId}, state_in: ${state}}, limit: $rentContractsCount) { contractID diff --git a/packages/grid_client/src/modules/models.ts b/packages/grid_client/src/modules/models.ts index ac4d31ec99..8d93c898c1 100644 --- a/packages/grid_client/src/modules/models.ts +++ b/packages/grid_client/src/modules/models.ts @@ -31,6 +31,12 @@ enum ContractStates { GracePeriod = "GracePeriod", } +export enum NodeStatus { + up = "up", + down = "down", + standBy = "standby", +} + //TODO: find a way to validate all fields are passed while casting data to any of these classes. class AlgorandAccountCreateModel { @Expose() @IsString() @IsNotEmpty() @IsAlphanumeric() @MaxLength(NameLength) name: string; @@ -584,6 +590,7 @@ class FilterOptions { @Expose() @IsOptional() @IsBoolean() hasGPU?: boolean; @Expose() @IsOptional() @IsBoolean() rentable?: boolean; @Expose() @IsOptional() @IsInt() @Min(1) rentedBy?: number; + @Expose() @IsOptional() @Transform(({ value }) => NodeStatus[value]) @IsEnum(NodeStatus) status?: NodeStatus; } enum CertificationType { diff --git a/packages/grid_client/src/modules/nodes.ts b/packages/grid_client/src/modules/nodes.ts index fd5a9a2887..af4631d976 100644 --- a/packages/grid_client/src/modules/nodes.ts +++ b/packages/grid_client/src/modules/nodes.ts @@ -1,10 +1,10 @@ -import urlJoin from "url-join"; - import { TFClient } from "../clients"; import { GridClientConfig } from "../config"; -import { events, send, validateInput } from "../helpers"; +import { events, validateInput } from "../helpers"; import { expose } from "../helpers/expose"; +import { capacity } from "./capacity"; import { + FilterOptions, NodeGetModel, NodePowerModel, RentContractCreateModel, @@ -15,8 +15,10 @@ import { checkBalance } from "./utils"; class Nodes { client: TFClient; + capacity: capacity; constructor(public config: GridClientConfig) { this.client = config.tfclient; + this.capacity = new capacity(config); } @expose @@ -79,6 +81,17 @@ class Nodes { async setNodePower(options: NodePowerModel) { return (await this.client.nodes.setPower(options)).apply(); } + + @expose + async all() { + return await this.capacity.getAllNodes(); + } + + @expose + @validateInput + async filter(options?: FilterOptions) { + return await this.capacity.filterNodes(options); + } } export { Nodes as nodes }; diff --git a/packages/grid_client/src/primitives/nodes.ts b/packages/grid_client/src/primitives/nodes.ts index 7acfda6a64..dba5df9549 100644 --- a/packages/grid_client/src/primitives/nodes.ts +++ b/packages/grid_client/src/primitives/nodes.ts @@ -3,12 +3,10 @@ import { QueryClient } from "@threefold/tfchain_client"; import { default as PrivateIp } from "private-ip"; import urlJoin from "url-join"; -import { GridClient } from "../client"; import { RMB } from "../clients"; import { Graphql } from "../clients/graphql/client"; -import { TFClient } from "../clients/tf-grid/client"; import { send } from "../helpers/requests"; -import { FarmFilterOptions, FilterOptions } from "../modules/models"; +import { FarmFilterOptions, FilterOptions, NodeStatus } from "../modules/models"; interface FarmInfo { name: string; @@ -348,7 +346,7 @@ class Nodes { city: options.city, dedicated: options.dedicated, available_for: options.availableFor, - status: "up", + status: options.status ? options.status : NodeStatus.up, page: options.page, size: options.size, has_gpu: options.hasGPU,