Skip to content

Commit

Permalink
feat: add delete-server action (#6)
Browse files Browse the repository at this point in the history
Closes #6
  • Loading branch information
MarcScheib authored Sep 6, 2024
1 parent db80aa6 commit d553ec9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 17 deletions.
6 changes: 3 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
name: 'Hetzner Cloud API Action'
description: 'Interact with the Hetzner Cloud API from a GitHub'
inputs:
verbose:
description: 'Whether to output verbose logs'
required: false
api-token:
description: 'The Hetzner Cloud API token'
required: true
Expand Down Expand Up @@ -31,6 +28,9 @@ inputs:
server-start-after-create:
description: 'Whether to start the server after creating it'
required: false
server-id:
description: 'The ID of the server to perform the action on'
required: false
runs:
using: 'node20'
main: 'dist/index.js'
29 changes: 22 additions & 7 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26700,6 +26700,16 @@ class HetznerCloudApiClient {
'Content-Type': 'application/json',
});
}

/**
* Delete a server.
*
* @param {string} serverId - Server ID.
* @returns {Promise} - Promise representing the request.
*/
deleteServer(serverId) {
return this.#request('DELETE', `/servers/${serverId}`, null);
}
}

;// CONCATENATED MODULE: ./src/hetzner-cloud.js
Expand All @@ -26712,18 +26722,17 @@ class HetznerCloudApiClient {
async function run() {
try {
const inputs = {
verbose: core.getInput('verbose') === 'true',
apiToken: core.getInput('api-token'),
action: core.getInput('action'),
};

const logger = (message, verboseMessage) => {
if (inputs.verbose) {
core.info(verboseMessage || message);
} else {
core.info(message);
const logger = (message, debugMessage) => {
core.info(message);

if (debugMessage) {
core.debug(debugMessage);
}
}
};

const client = new HetznerCloudApiClient(inputs.apiToken);

Expand Down Expand Up @@ -26752,6 +26761,12 @@ async function run() {
logger('Server created', JSON.stringify(await response.json()));
}

if (inputs.action === 'delete-server') {
const serverId = core.getInput('server-id');
logger(`Deleting server ${serverId} from Hetzner Cloud API...`);
const response = await client.deleteServer(serverId);
logger('Server deleted', JSON.stringify(await response.json()));
}
} catch (error) {
core.setFailed(error.message);
}
Expand Down
10 changes: 10 additions & 0 deletions src/hetzner-api-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,14 @@ export class HetznerCloudApiClient {
'Content-Type': 'application/json',
});
}

/**
* Delete a server.
*
* @param {string} serverId - Server ID.
* @returns {Promise} - Promise representing the request.
*/
deleteServer(serverId) {
return this.#request('DELETE', `/servers/${serverId}`, null);
}
}
19 changes: 12 additions & 7 deletions src/hetzner-cloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@ import { HetznerCloudApiClient } from './hetzner-api-client.js';
export async function run() {
try {
const inputs = {
verbose: core.getInput('verbose') === 'true',
apiToken: core.getInput('api-token'),
action: core.getInput('action'),
};

const logger = (message, verboseMessage) => {
if (inputs.verbose) {
core.info(verboseMessage || message);
} else {
core.info(message);
const logger = (message, debugMessage) => {
core.info(message);

if (debugMessage) {
core.debug(debugMessage);
}
}
};

const client = new HetznerCloudApiClient(inputs.apiToken);

Expand Down Expand Up @@ -47,6 +46,12 @@ export async function run() {
logger('Server created', JSON.stringify(await response.json()));
}

if (inputs.action === 'delete-server') {
const serverId = core.getInput('server-id');
logger(`Deleting server ${serverId} from Hetzner Cloud API...`);
const response = await client.deleteServer(serverId);
logger('Server deleted', JSON.stringify(await response.json()));
}
} catch (error) {
core.setFailed(error.message);
}
Expand Down

0 comments on commit d553ec9

Please sign in to comment.