Skip to content

Commit

Permalink
Merge branch 'development' of github.com:threefoldtech/tfgrid-sdk-ts …
Browse files Browse the repository at this point in the history
…into development_rewriteProfileManager
  • Loading branch information
0oM4R committed Feb 2, 2025
2 parents 5723b8c + 5cdf91e commit ba96570
Show file tree
Hide file tree
Showing 19 changed files with 413 additions and 84 deletions.
2 changes: 1 addition & 1 deletion packages/grid_client/docs/network/mycelium_network.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ This configuration is set on [workload data](../../src/zos/zmachine.ts) and [net

In case user needs to support mycelium:

- flag `mycelium` field in workload with true, also the `planetary` field should be flagged with true.
- flag `mycelium` field in workload with true.
- User can support hex seed which ip will be generated from or they'll be generated automatically.
- providing seeds, that's what makes ip fixed from anywhere.
> Note user can't use same network seed in multiple nodes.
Expand Down
38 changes: 38 additions & 0 deletions packages/grid_client/src/clients/tf-grid/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ export interface GqlContracts {
export interface GqlConsumption extends GqlContracts {
contractBillReports: GqlContractBillReports[];
}
export interface GqlDiscountPackage {
contractBillReports: GqlContractBillReports[];
}

export interface GqlContractBillReports {
id: string;
Expand Down Expand Up @@ -107,6 +110,11 @@ export interface GetConsumptionOptions {
id: number;
}

export interface GetDiscountPackageOptions {
graphqlURL: string;
id: number;
}

export interface CancelMyContractOptions {
graphqlURL: string;
}
Expand Down Expand Up @@ -251,7 +259,37 @@ class TFContracts extends Contracts {
throw err;
}
}
/**
* Get contract discount package
* @param {GetDiscountPackageOptions} options
* @returns {Promie<DiscountLevel>}
*/
async getDiscountPackage(options: GetDiscountPackageOptions): Promise<DiscountLevel> {
const gqlClient = new Graphql(options.graphqlURL);

const body = `query getConsumption($contractId: BigInt!){
contractBillReports(where: {contractID_eq: $contractId} , orderBy: timestamp_DESC) {
discountReceived
}
}`;

try {
const response = await gqlClient.query(body, { contractId: options.id });

const gqlDiscountPackage: GqlDiscountPackage = response["data"] as GqlDiscountPackage;
const billReports = gqlDiscountPackage.contractBillReports;
if (billReports.length === 0) {
return "None";
} else {
const discountPackage = billReports[billReports.length - 1].discountReceived;
return discountPackage;
}
} catch (err) {
(err as Error).message = formatErrorMessage(`Error getting discount package for contract ${options.id}.`, err);
throw err;
}
}
/**
* Get contract consumption per hour in TFT.
*
Expand Down
16 changes: 15 additions & 1 deletion packages/grid_client/src/modules/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as PATH from "path";

import {
ContractsOverdue,
type DiscountLevel,
GqlContracts,
GqlNameContract,
GqlNodeContract,
Expand All @@ -31,6 +32,7 @@ import {
BatchCancelContractsModel,
ContractCancelModel,
ContractConsumption,
ContractDiscountPackage,
ContractGetByNodeIdAndHashModel,
ContractGetModel,
ContractLockModel,
Expand Down Expand Up @@ -523,7 +525,19 @@ class Contracts {
async setDedicatedNodeExtraFee(options: SetDedicatedNodeExtraFeesModel) {
return (await this.client.contracts.setDedicatedNodeExtraFee(options)).apply();
}

/**
* Get contract discount package
* @param {ContractDiscountPackage} options
* @returns {Promie<DiscountLevel>}
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
async getDiscountPackage(options: ContractDiscountPackage): Promise<DiscountLevel> {
return this.client.contracts.getDiscountPackage({ id: options.id, graphqlURL: this.config.graphqlURL });
}
/**
* Get contract consumption per hour in TFT.
*
Expand Down
6 changes: 6 additions & 0 deletions packages/grid_client/src/modules/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,10 @@ class ContractConsumption {
@Expose() @IsInt() @Min(1) id: number;
}

class ContractDiscountPackage {
@Expose() @IsInt() @Min(1) id: number;
}

class ContractLockModel extends ContractConsumption {}

class TwinCreateModel {
Expand Down Expand Up @@ -635,6 +639,7 @@ class FilterOptions {
@Expose() @IsOptional() @Transform(({ value }) => NodeStatus[value]) @IsEnum(NodeStatus) status?: NodeStatus;
@Expose() @IsOptional() @IsString() region?: string;
@Expose() @IsOptional() @IsBoolean() healthy?: boolean;
@Expose() @IsOptional() @IsInt() rentableOrRentedBy?: number;
@Expose() @IsOptional() @IsBoolean() planetary?: boolean;
@Expose() @IsOptional() @IsBoolean() mycelium?: boolean;
@Expose() @IsOptional() @IsBoolean() wireguard?: boolean;
Expand Down Expand Up @@ -974,6 +979,7 @@ export {
ContractsByTwinId,
ContractsByAddress,
ContractConsumption,
ContractDiscountPackage,
ContractLockModel,
TwinCreateModel,
TwinGetModel,
Expand Down
1 change: 1 addition & 0 deletions packages/grid_client/src/primitives/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ class Nodes {
healthy: options.healthy,
sort_by: SortBy.FreeCRU,
sort_order: SortOrder.Desc,
rentable_or_rented_by: options.rentableOrRentedBy,
features: options.features,
};

Expand Down
2 changes: 1 addition & 1 deletion packages/grid_client/src/zos/znet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ class Znet extends WorkloadData {
}
}

export { Znet, Peer };
export { Znet, Peer, Mycelium };
18 changes: 14 additions & 4 deletions packages/grid_client/tests/modules/vm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ test("TC1228 - VM: Deploy a VM", async () => {
farmId: 1,
availableFor: await gridClient.twins.get_my_twin_id(),
features: [Features.wireguard],
nodeExclude: [11],
} as FilterOptions);
} catch (error) {
//Log the resources that were not found.
Expand All @@ -88,6 +89,7 @@ test("TC1228 - VM: Deploy a VM", async () => {
farmId: 1,
availableFor: await gridClient.twins.get_my_twin_id(),
features: [Features.wireguard],
nodeExclude: [11],
} as FilterOptions);
}
const nodeId = await getOnlineNode(nodes);
Expand Down Expand Up @@ -234,6 +236,7 @@ test("TC2847 - VM: Deploy a VM With Mycelium", async () => {
farmId: 1,
availableFor: await gridClient.twins.get_my_twin_id(),
features: [Features.wireguard],
nodeExclude: [11],
} as FilterOptions);
} catch (error) {
//Log the resources that were not found.
Expand All @@ -253,6 +256,7 @@ test("TC2847 - VM: Deploy a VM With Mycelium", async () => {
farmId: 1,
availableFor: await gridClient.twins.get_my_twin_id(),
features: [Features.wireguard],
nodeExclude: [11],
} as FilterOptions);
}
const nodeId = await getOnlineNode(nodes);
Expand Down Expand Up @@ -403,6 +407,7 @@ test("TC1229 - VM: Deploy a VM With a Disk", async () => {
farmId: 1,
availableFor: await gridClient.twins.get_my_twin_id(),
features: [Features.wireguard],
nodeExclude: [11],
} as FilterOptions);
} catch (error) {
//Log the resources that were not found.
Expand All @@ -423,6 +428,7 @@ test("TC1229 - VM: Deploy a VM With a Disk", async () => {
farmId: 1,
availableFor: await gridClient.twins.get_my_twin_id(),
features: [Features.wireguard],
nodeExclude: [11],
} as FilterOptions);
}
const nodeId = await getOnlineNode(nodes);
Expand Down Expand Up @@ -499,9 +505,9 @@ test("TC1229 - VM: Deploy a VM With a Disk", async () => {
//Verify that the disk was added successfully.
await ssh.execCommand("df -h").then(async function (result) {
const splittedRes = result.stdout.split("\n");
log(splittedRes[4]);
expect(splittedRes[4]).toContain(mountPoint);
expect(splittedRes[4]).toContain(diskSize.toString());
log(splittedRes[5]);
expect(splittedRes[5]).toContain(mountPoint);
expect(splittedRes[5]).toContain(diskSize.toString());
});
} finally {
//Disconnect from the machine
Expand Down Expand Up @@ -727,7 +733,11 @@ test("TC1230 - VM: Deploy Multiple VMs on Different Nodes", async () => {
log(result.stdout);
expect(result.stdout).toContain(vmEnvVarValue[maxIterations]);
});

await ssh.execCommand("apk add util-linux").then(function (result) {
if (result.stderr) {
throw new Error("Failed to install util-linux");
}
});
//Verify VM Resources(CPU)
await ssh.execCommand("lscpu").then(async function (result) {
const splittedRes = result.stdout.split("\n");
Expand Down
Loading

0 comments on commit ba96570

Please sign in to comment.