Last active
October 23, 2024 18:54
-
-
Save Sdy603/a00a82cb4c0ce170670fb766e0744d6c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| const { Client } = require('pg'); | |
| const axios = require('axios'); | |
| // Debug mode | |
| const debug = true; // Set to false for real API calls | |
| // Database connection string | |
| const connectionString = '[CONNECTION STRING FROM DX]'; | |
| // DX API endpoint and token | |
| const dxApiUrl = 'https://api.getdx.com/services.sync'; | |
| const dxApiToken = '[DX_API_TOKEN]'; // Replace with your actual token | |
| // Create PostgreSQL client | |
| const client = new Client({ connectionString }); | |
| async function fetchTeams() { | |
| const query = `SELECT name, source_id FROM dx_teams;`; | |
| const result = await client.query(query); | |
| return result.rows; | |
| } | |
| async function fetchExistingReferenceIds(serviceName) { | |
| const query = ` | |
| SELECT source_id AS reference_id | |
| FROM service_identities | |
| WHERE name LIKE $1; | |
| `; | |
| const result = await client.query(query, [`%${serviceName}%`]); | |
| return new Set(result.rows.map(row => row.reference_id)); // Avoid duplicates | |
| } | |
| async function syncServices(services) { | |
| try { | |
| const response = await axios.post( | |
| dxApiUrl, | |
| { services }, | |
| { | |
| headers: { | |
| Authorization: `Bearer ${dxApiToken}`, | |
| 'Content-Type': 'application/json', | |
| }, | |
| } | |
| ); | |
| console.log('Service sync response:', response.data); | |
| } catch (error) { | |
| console.error('Error syncing services:', error); | |
| } | |
| } | |
| async function manageServiceCatalog() { | |
| try { | |
| await client.connect(); | |
| console.log('Connected to the database.'); | |
| const teams = await fetchTeams(); | |
| // Example service data for insertion | |
| const newServices = [ | |
| { name: 'Service A', dx_team_id: 'team-source-id-1' }, | |
| { name: 'Service B', dx_team_id: 'team-source-id-2' }, | |
| ]; | |
| for (const service of newServices) { | |
| const existingReferenceIds = await fetchExistingReferenceIds(service.name); | |
| if (existingReferenceIds.size > 0) { | |
| console.log(`Skipping service "${service.name}" - Reference ID already exists.`); | |
| } else { | |
| console.log(`Syncing service "${service.name}"...`); | |
| if (!debug) { | |
| await syncServices([ | |
| { | |
| name: service.name, | |
| dx_team_id: service.dx_team_id, | |
| }, | |
| ]); | |
| } else { | |
| console.log('Debug mode: Skipping API call.'); | |
| } | |
| } | |
| } | |
| } catch (error) { | |
| console.error('Error managing service catalog:', error); | |
| } finally { | |
| await client.end(); | |
| console.log('Database connection closed.'); | |
| } | |
| } | |
| // Run the service catalog management | |
| manageServiceCatalog(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment