From 929404fadd54db65b4eb09174b512335cee32f61 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Sat, 17 Feb 2024 21:55:25 +0100 Subject: [PATCH] fix(core): update --- package.json | 4 +++- pnpm-lock.yaml | 40 +++++++++++++++++++++++++++---------- qenv.yml | 2 ++ test/test.ts | 26 +++++++++++++++++++++--- ts/00_commitinfo_data.ts | 2 +- ts/classes.account.ts | 9 +++++++++ ts/classes.server.ts | 41 +++++++++++++++++++++++--------------- ts/hetznercloud.plugins.ts | 7 +++++++ 8 files changed, 100 insertions(+), 31 deletions(-) create mode 100644 qenv.yml diff --git a/package.json b/package.json index d13ed9e..7d161a1 100644 --- a/package.json +++ b/package.json @@ -19,12 +19,14 @@ "@git.zone/tsbundle": "^2.0.5", "@git.zone/tsrun": "^1.2.46", "@git.zone/tstest": "^1.0.44", + "@push.rocks/qenv": "^6.0.5", "@push.rocks/tapbundle": "^5.0.15", "@types/node": "^20.8.7" }, "dependencies": { "@push.rocks/smartrequest": "^2.0.21", - "@tempfix/hetzner-openapi": "^1.0.4" + "@tempfix/hetzner-openapi": "^1.0.4", + "@tsclass/tsclass": "^4.0.52" }, "repository": { "type": "git", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 44e35a5..d9f4ff0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -11,6 +11,9 @@ dependencies: '@tempfix/hetzner-openapi': specifier: ^1.0.4 version: 1.0.4 + '@tsclass/tsclass': + specifier: ^4.0.52 + version: 4.0.52 devDependencies: '@git.zone/tsbuild': @@ -25,6 +28,9 @@ devDependencies: '@git.zone/tstest': specifier: ^1.0.44 version: 1.0.86(@types/node@20.11.10)(sinon@17.0.1) + '@push.rocks/qenv': + specifier: ^6.0.5 + version: 6.0.5 '@push.rocks/tapbundle': specifier: ^5.0.15 version: 5.0.15(sinon@17.0.1) @@ -74,7 +80,7 @@ packages: '@push.rocks/smartstream': 3.0.30 '@push.rocks/smarttime': 4.0.6 '@push.rocks/webstore': 2.0.13 - '@tsclass/tsclass': 4.0.46 + '@tsclass/tsclass': 4.0.52 '@types/express': 4.17.21 body-parser: 1.20.2 cors: 2.8.5 @@ -134,6 +140,12 @@ packages: js-tokens: 4.0.0 dev: true + /@configvault.io/interfaces@1.0.17: + resolution: {integrity: sha512-bEcCUR2VBDJsTin8HQh8Uw/mlYl2v8A3jMIaQ+MTB9Hrqd6CZL2dL7iJdWyFl/3EIX+LDxWFR+Oq7liIq7w+1Q==} + dependencies: + '@api.global/typedrequest-interfaces': 3.0.1 + dev: true + /@cspotcode/source-map-support@0.8.1: resolution: {integrity: sha512-IchNf6dN4tHoMFIn/7OE8LWZ19Y6q/67Bmf6vnGREv8RSbBVb9LPJxEcnwrcwX6ixSvaiGoomAUvu4YSxXrVgw==} engines: {node: '>=12'} @@ -596,6 +608,16 @@ packages: symbol-tree: 3.2.4 dev: true + /@push.rocks/qenv@6.0.5: + resolution: {integrity: sha512-Id/eSKKqSDUGe+0Cp5HEJ58J1iVv1jQseLUMs9kFTPYwG+NJSETUCRsJV50w5cPv8bRFcSkSU+xVbUbOc1p29A==} + dependencies: + '@api.global/typedrequest': 3.0.4 + '@configvault.io/interfaces': 1.0.17 + '@push.rocks/smartfile': 11.0.4 + '@push.rocks/smartlog': 3.0.3 + '@push.rocks/smartpath': 5.0.11 + dev: true + /@push.rocks/smartbrowser@2.0.6: resolution: {integrity: sha512-Ne+KCVhV/DROc1rHRRw59K6h0+LpQAK9fdOUtgDZ7laLPmB/tmnbUh3IuRDNcIY1iVA9pydoobwjnTjVgio9eQ==} dependencies: @@ -817,7 +839,7 @@ packages: '@push.rocks/smartpromise': 4.0.3 '@push.rocks/smartpuppeteer': 2.0.2 '@push.rocks/smartunique': 3.0.6 - '@tsclass/tsclass': 4.0.46 + '@tsclass/tsclass': 4.0.52 '@types/express': 4.17.21 express: 4.18.2 pdf-merger-js: 3.4.0 @@ -880,7 +902,7 @@ packages: '@push.rocks/smartxml': 1.0.8 '@push.rocks/smartyaml': 2.0.5 '@push.rocks/webrequest': 3.0.34 - '@tsclass/tsclass': 4.0.46 + '@tsclass/tsclass': 4.0.52 dev: true /@push.rocks/smartsocket@2.0.24: @@ -1321,11 +1343,10 @@ packages: type-fest: 2.19.0 dev: true - /@tsclass/tsclass@4.0.46: - resolution: {integrity: sha512-UovPUvlozv1ftJp4KW5tt4MP/LQCNP3lSCinjyIrLkopOymczyzONUGvSAAwOBf1XBE9bO0tI4KtRuXWN9XBXQ==} + /@tsclass/tsclass@4.0.52: + resolution: {integrity: sha512-yjASmfnDvgWA1OKYXbz4diLIrPBSk5BpPStKuVkAhrhN8Xw4lc6/oSiJpsosEd8GDwr/FPsY2lgN8/5674vM0w==} dependencies: - type-fest: 4.10.1 - dev: true + type-fest: 4.10.2 /@tsconfig/node10@1.0.9: resolution: {integrity: sha512-jNsYVVxU8v5g43Erja32laIDHXeoNvFEpX33OK4d6hljo3jDhCBDhx5dhCCTMWUojscpAagGiRkBKxpdl9fxqA==} @@ -5232,10 +5253,9 @@ packages: engines: {node: '>=12.20'} dev: true - /type-fest@4.10.1: - resolution: {integrity: sha512-7ZnJYTp6uc04uYRISWtiX3DSKB/fxNQT0B5o1OUeCqiQiwF+JC9+rJiZIDrPrNCLLuTqyQmh4VdQqh/ZOkv9MQ==} + /type-fest@4.10.2: + resolution: {integrity: sha512-anpAG63wSpdEbLwOqH8L84urkL6PiVIov3EMmgIhhThevh9aiMQov+6Btx0wldNcvm4wV+e2/Rt1QdDwKHFbHw==} engines: {node: '>=16'} - dev: true /type-is@1.6.18: resolution: {integrity: sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==} diff --git a/qenv.yml b/qenv.yml new file mode 100644 index 0000000..a530f95 --- /dev/null +++ b/qenv.yml @@ -0,0 +1,2 @@ +required: + - 'HETZNER_API_TOKEN' \ No newline at end of file diff --git a/test/test.ts b/test/test.ts index 03ca288..12e87fe 100644 --- a/test/test.ts +++ b/test/test.ts @@ -1,8 +1,28 @@ import { expect, expectAsync, tap } from '@push.rocks/tapbundle'; -import * as hetznercloud from '../ts/index.js' +import * as hetznercloud from '../ts/index.js'; +import * as qenv from '@push.rocks/qenv'; +const testQenv = new qenv.Qenv('./', './.nogit/'); -tap.test('first test', async () => { - console.log(hetznercloud) +let testAccount: hetznercloud.HetznerAccount; + +tap.test('should create a valid hetzer account', async () => { + testAccount = new hetznercloud.HetznerAccount(await testQenv.getEnvVarOnDemand('HETZNER_API_TOKEN')); + expect(testAccount).toBeInstanceOf(hetznercloud.HetznerAccount); +}); + +tap.test('should be able to list all servers', async () => { + const servers = await testAccount.getServers(); + expect(servers).toBeArray(); + console.log(JSON.stringify(servers, null, 2)); +}) + +tap.test('should be able to create a server', async () => { + const newServer = await testAccount.createServer({ + name: 'testserver', + location: 'nbg1' + }); + expect(newServer).toBeInstanceOf(hetznercloud.HetznerServer); + console.log(newServer); }) tap.start() diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 2b1f46d..5ec1bac 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@apiclient.xyz/hetznercloud', - version: '1.0.7', + version: '1.0.8', description: 'an unofficial api client for the hetzner cloud api' } diff --git a/ts/classes.account.ts b/ts/classes.account.ts index fad4049..a410066 100644 --- a/ts/classes.account.ts +++ b/ts/classes.account.ts @@ -11,6 +11,10 @@ export class HetznerAccount { return HetznerServer.getServers(this); } + public async createServer(optionsArg: plugins.tsclass.typeFestOwn.SecondArgument) { + return HetznerServer.create(this, optionsArg); + } + /** * request things from the hetzner API * @param methodArg @@ -19,6 +23,9 @@ export class HetznerAccount { */ public request = async (methodArg: string, pathArg: string, payloadArg: any) => { const url = `https://api.hetzner.cloud/v1${pathArg}`; + console.log(`Url: ${url}`); + console.log(`Method: ${methodArg}`); + console.log(`Payload: ${JSON.stringify(payloadArg, null, 2)}`); const response = await plugins.smartrequest.request(url, { method: methodArg, headers: { @@ -26,6 +33,8 @@ export class HetznerAccount { }, requestBody: payloadArg, }); + console.log(response.statusCode); + console.log(response.body); return response; } } diff --git a/ts/classes.server.ts b/ts/classes.server.ts index 0cad1a6..d301561 100644 --- a/ts/classes.server.ts +++ b/ts/classes.server.ts @@ -4,24 +4,33 @@ import * as types from './types.js'; export class HetznerServer { // STATIC - public static async create(hetznerAccountRefArg: HetznerAccount, optionsArg: { - name: string, - datacenter: 'nbg1-dc3', - }) { + public static async create( + hetznerAccountRefArg: HetznerAccount, + optionsArg: { + name: string; + location: 'nbg1'; + } + ) { const server = new HetznerServer(hetznerAccountRefArg); const createServerUrl = '/servers'; - const createServerPayload: types.TServerCreateRequestBody = - { - name: optionsArg.name, - datacenter: optionsArg.datacenter, - image: '', - server_type: '', - start_after_create: true, - user_data: '', - }; - - const response = await server.hetznerAccountRef.request('POST', createServerUrl, createServerPayload); + const createServerPayload: types.TServerCreateRequestBody = { + name: optionsArg.name, + image: 'ubuntu-22.04', + server_type: 'cx11', + start_after_create: true, + labels: {}, + location: optionsArg.location, + public_net: { + enable_ipv4: true, + enable_ipv6: true, + }, + }; + const response = await server.hetznerAccountRef.request( + 'POST', + createServerUrl, + createServerPayload + ); server.data = (response.body as types.TServerCreateResponseBody).server; return server; } @@ -29,7 +38,7 @@ export class HetznerServer { public static async getServers(hetznerAccountRefArg: HetznerAccount) { const serversGetUrl = '/servers'; const response = await hetznerAccountRefArg.request('GET', serversGetUrl, {}); - const serversDataArray = (response.body as types.TServersGetResponseBody).servers; + const serversDataArray = (response.body as types.TServersGetResponseBody).servers; const servers: HetznerServer[] = []; for (const serverData of serversDataArray) { const server = new HetznerServer(hetznerAccountRefArg); diff --git a/ts/hetznercloud.plugins.ts b/ts/hetznercloud.plugins.ts index 30dc783..60c2d70 100644 --- a/ts/hetznercloud.plugins.ts +++ b/ts/hetznercloud.plugins.ts @@ -10,3 +10,10 @@ import * as smartrequest from '@push.rocks/smartrequest'; export { smartrequest, } + +// @tsclass scope +import * as tsclass from '@tsclass/tsclass'; + +export { + tsclass, +}