Commit a2390a96 authored by Ahmad's avatar Ahmad

dsdsd

parent 31ee2ebd
Pipeline #230 canceled with stages
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const os = require('os'); const os = require('os');
const axios = require('axios');
// Reads and validates the DigitalOcean API token from Downloads // Step 1: Read & validate DigitalOcean API token
function readDigitalOceanToken() { function readDigitalOceanToken() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const downloadsDir = path.join(os.homedir(), 'Downloads'); const downloadsDir = path.join(os.homedir(), 'Downloads');
...@@ -33,13 +34,45 @@ function readDigitalOceanToken() { ...@@ -33,13 +34,45 @@ function readDigitalOceanToken() {
}); });
} }
// Main entry point // Step 2: Fetch available US regions
async function fetchUSRegions(token) {
try {
const response = await axios.get('https://api.digitalocean.com/v2/regions', {
headers: {
Authorization: `Bearer ${token}`,
},
});
const regions = response.data.regions;
// Filter: available + US regions (e.g., nyc, sfo)
const usRegions = regions.filter(
(region) => region.available && /^(nyc|sfo|chi|sea|dc|atl)/i.test(region.slug)
);
if (usRegions.length === 0) {
console.log('No available US regions found.');
} else {
console.log('Available US regions:');
usRegions.forEach((region) => {
console.log(`- ${region.slug}: ${region.name}`);
});
}
return usRegions;
} catch (error) {
throw new Error(`Failed to fetch regions: ${error.message}`);
}
}
// Main
async function main() { async function main() {
try { try {
const token = await readDigitalOceanToken(); const token = await readDigitalOceanToken();
console.log('Token successfully loaded:', token); console.log('Token loaded.');
// TODO: Call DigitalOcean API here const usRegions = await fetchUSRegions(token);
// Next: choose one of these regions to create a Droplet
} catch (err) { } catch (err) {
console.error('Error:', err.message); console.error('Error:', err.message);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment