Skip to content

Commit

Permalink
Expose all and filter methods on the nodes module to list/filter all …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
Mahmoud-Emad authored Oct 19, 2023
1 parent d7ffc70 commit 08e69a1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/grid_client/src/clients/tf-grid/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class TFContracts extends Contracts {
state
createdAt
nodeID
numberOfPublicIPs
}
rentContracts(where: {twinID_eq: ${options.twinId}, state_in: ${state}}, limit: $rentContractsCount) {
contractID
Expand Down
7 changes: 7 additions & 0 deletions packages/grid_client/src/modules/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
19 changes: 16 additions & 3 deletions packages/grid_client/src/modules/nodes.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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 };
6 changes: 2 additions & 4 deletions packages/grid_client/src/primitives/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 08e69a1

Please sign in to comment.