-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from GlueOps/fix/reformat-slackbot
feat: add button builder and pull tailscale code out
- Loading branch information
Showing
6 changed files
with
225 additions
and
414 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,181 +1,142 @@ | ||
import hetzner from '../util/hetzner/hetzner-servers.js'; | ||
import aws from '../util/aws/aws-server.js'; | ||
import buttonBuilder from '../util/button-builder.js'; | ||
|
||
export default { | ||
description: 'Sets up vm options', | ||
|
||
button: async ({ app, actionId, body, response }) => { | ||
switch(actionId) { | ||
case 'button_list_servers': { | ||
await aws.listServers({ app, body }); | ||
await hetzner.listServers({ app, body }); | ||
break; | ||
} | ||
case 'button_create_vm': { | ||
if (actionId === 'button_list_servers') { | ||
const servers = []; | ||
const blocks = []; // Use this to accumulate all blocks | ||
|
||
// Fetch servers from AWS and Hetzner | ||
servers.push(...await aws.listServers({ app, body })); | ||
servers.push(...await hetzner.listServers({ app, body })); | ||
|
||
// Check if there are any servers | ||
if (servers.length) { | ||
for (const server of servers) { | ||
const buttonsArray = [ | ||
{ text: "Start", actionId: `button_start_${server.cloud}`, value: JSON.stringify({ instanceId: server.serverID, vmID: server.serverID, region: server.region }) }, | ||
{ text: "Stop", actionId: `button_stop_${server.cloud}`, value: JSON.stringify({ instanceId: server.serverID, vmID: server.serverID, region: server.region }) }, | ||
{ text: "Delete", actionId: `button_delete_${server.cloud}`, value: JSON.stringify({ instanceId: server.serverID, serverName: server.serverName, region: server.region }) } | ||
]; | ||
|
||
// Build buttons and add them to blocks | ||
const buttonBlock = buttonBuilder({ | ||
buttonsArray, | ||
headerText: `Cloud: ${server.cloud}\nServer: ${server.serverName}\nServer ID: ${server.serverID}\nRegion: ${server.region}\nStatus: ${server.status}\nConnect: ${server.connect}`, | ||
fallbackText: "Device not supported to use VM command" | ||
}); | ||
|
||
// Push the blocks into the main blocks array | ||
blocks.push(...buttonBlock.blocks); | ||
} | ||
|
||
// Send the combined blocks in a single message | ||
await app.client.chat.postEphemeral({ | ||
channel: `${body.channel.id}`, | ||
user: `${body.user.id}`, | ||
text: 'Server List', | ||
blocks, // Combine all button blocks | ||
}); | ||
} else { | ||
await app.client.chat.postEphemeral({ | ||
channel: `${body.channel.id}`, | ||
user: `${body.user.id}`, | ||
text: "You don't currently have any servers" | ||
}); | ||
} | ||
} else if (actionId === 'button_create_vm') { | ||
const buttonsArray = [ | ||
{ text: "aws", actionId: "button_create_vm_aws" }, | ||
{ text: "hetzner", actionId: "button_create_vm_hetzner" }, | ||
]; | ||
const buttons = buttonBuilder({ buttonsArray, headerText: "choose your platform", fallbackText: "device unsupported" }); | ||
app.client.chat.postEphemeral({ | ||
channel: `${body.channel.id}`, | ||
user: `${body.user.id}`, | ||
blocks: [ | ||
{ | ||
"type": "section", | ||
"text": { | ||
"type": "mrkdwn", | ||
"text": "Select a platform to create the vm in:" | ||
} | ||
}, | ||
{ | ||
"type": "actions", | ||
"elements": [ | ||
{ | ||
"type": "button", | ||
"text": { | ||
"type": "plain_text", | ||
"text": "aws" | ||
}, | ||
"action_id": "button_create_vm_aws" | ||
}, | ||
{ | ||
"type": "button", | ||
"text": { | ||
"type": "plain_text", | ||
"text": "hetzner" | ||
}, | ||
"action_id": "button_create_vm_hetzner" | ||
} | ||
] | ||
} | ||
], | ||
text: "VM options" | ||
text: 'select a platform', | ||
...buttons | ||
}); | ||
break; | ||
} | ||
case 'button_start_aws': { | ||
} else if (actionId === 'button_start_aws') { | ||
const { instanceId, region } = JSON.parse(body.actions[0].value); | ||
|
||
aws.startServer({ app, body, instanceId, region }); | ||
break; | ||
} | ||
case 'button_stop_aws': { | ||
} else if (actionId === 'button_stop_aws') { | ||
const { instanceId, region } = JSON.parse(body.actions[0].value); | ||
|
||
aws.stopServer({ app, body, instanceId, region }); | ||
break; | ||
} | ||
case 'button_delete_aws': { | ||
} else if (actionId === 'button_delete_aws') { | ||
const { instanceId, serverName, region } = JSON.parse(body.actions[0].value); | ||
|
||
//delete the server | ||
aws.deleteServer({ app, body, instanceId, serverName, region }); | ||
break; | ||
} | ||
case 'button_start_hetzner': { | ||
} else if (actionId === 'button_start_hetzner') { | ||
const { vmID } = JSON.parse(body.actions[0].value); | ||
|
||
//start a hetzner server | ||
hetzner.startServer({ app, body, vmID }); | ||
break; | ||
} | ||
case 'button_stop_hetzner': { | ||
} else if (actionId === 'button_stop_hetzner') { | ||
const { vmID } = JSON.parse(body.actions[0].value); | ||
|
||
//stop a hetzner server | ||
hetzner.stopServer({ app, body, vmID }); | ||
break; | ||
} | ||
case 'button_delete_hetzner': { | ||
} else if (actionId === 'button_delete_hetzner') { | ||
const { serverName } = JSON.parse(body.actions[0].value); | ||
|
||
//delete the server | ||
hetzner.deleteServer({ app, body, serverName }); | ||
break; | ||
} | ||
case 'button_create_image_aws': { | ||
} else if (actionId.startsWith('button_create_image_aws')) { | ||
const { imageName, ami, region, instanceType } = JSON.parse(body.actions[0].value); | ||
aws.createServer({ app, body, imageName, ami, region, instanceType }); | ||
break; | ||
} | ||
case 'button_create_image_hetzner': { | ||
} else if (actionId.startsWith('button_create_image_hetzner')) { | ||
const { imageID, imageName, region, serverType } = JSON.parse(body.actions[0].value); | ||
hetzner.createServer({ app, body, imageID, imageName, region, serverType }); | ||
break; | ||
} | ||
case 'button_create_vm_hetzner': { | ||
} else if (actionId === 'button_create_vm_hetzner') { | ||
//select the hetzner server to create before calling the create server | ||
hetzner.selectRegion({ app, body }); | ||
break; | ||
} | ||
case 'button_create_vm_aws': { | ||
} else if (actionId === 'button_create_vm_aws') { | ||
//select the aws server to create before calling the create server | ||
aws.selectRegion({ app, body }); | ||
break; | ||
} | ||
case 'button_select_hetzner_server': { | ||
} else if (actionId.startsWith('button_select_hetzner_server')) { | ||
const data = JSON.parse(body.actions[0].value); | ||
//select the hetzner server to create before calling the create server | ||
hetzner.selectServer({ app, body, data }); | ||
break; | ||
} | ||
case 'button_select_aws_server': { | ||
} else if (actionId.startsWith('button_select_aws_server')) { | ||
const data = JSON.parse(body.actions[0].value); | ||
//select the asw server to create before calling the create server | ||
aws.selectServer({ app, body, data }); | ||
break; | ||
} | ||
case 'button_select_hetzner_image': { | ||
} else if (actionId.startsWith('button_select_hetzner_image')) { | ||
const data = JSON.parse(body.actions[0].value); | ||
//select the hetzner server to create before calling the create server | ||
hetzner.selectImage({ app, body, data }); | ||
break; | ||
} | ||
case 'button_select_aws_image': { | ||
} else if (actionId.startsWith('button_select_aws_image')) { | ||
const data = JSON.parse(body.actions[0].value); | ||
//select the asw server to create before calling the create server | ||
aws.selectImage({ app, body, data }); | ||
break; | ||
} | ||
default: { | ||
} else { | ||
response({ | ||
text: `This button is registered with the vm command, but does not have an action associated with it.` | ||
}); | ||
} | ||
} | ||
}, | ||
|
||
run: async ({ event, app }) => { | ||
const buttonsArray = [ | ||
{ text: "List Servers", actionId: "button_list_servers" }, | ||
{ text: "Create Server", actionId: "button_create_vm" }, | ||
]; | ||
const buttons = buttonBuilder({ buttonsArray, | ||
headerText: "Click one of the buttons below for VM options:", | ||
fallbackText: "device unsupported to use vm command" | ||
}); | ||
app.client.chat.postEphemeral({ | ||
channel: `${event.channel}`, | ||
user: `${event.user}`, | ||
blocks: [ | ||
{ | ||
"type": "section", | ||
"text": { | ||
"type": "mrkdwn", | ||
"text": "Click one of the buttons below for VM options:" | ||
} | ||
}, | ||
{ | ||
"type": "actions", | ||
"elements": [ | ||
{ | ||
"type": "button", | ||
"text": { | ||
"type": "plain_text", | ||
"text": "List Servers" | ||
}, | ||
"action_id": "button_list_servers" | ||
}, | ||
{ | ||
"type": "button", | ||
"text": { | ||
"type": "plain_text", | ||
"text": "Create Server" | ||
}, | ||
"action_id": "button_create_vm" | ||
} | ||
] | ||
} | ||
], | ||
text: "VM options" | ||
}) | ||
text: 'Create a vm', | ||
...buttons | ||
}); | ||
} | ||
} |
Oops, something went wrong.