|
| 1 | +import { fetchAndSaveData, prepareAndCleanup, RuntimeType, selectNamespace, writeCodefreshFiles } from '../deps.ts'; |
| 2 | + |
| 3 | +async function getAllAccounts(config: { headers: { Authorization: string }; baseUrl: string }) { |
| 4 | + const response = await fetch(`${config.baseUrl}/admin/accounts`, { |
| 5 | + method: 'GET', |
| 6 | + headers: config.headers, |
| 7 | + }); |
| 8 | + const accounts = await response.json(); |
| 9 | + await writeCodefreshFiles(accounts, 'onPrem-accounts'); |
| 10 | +} |
| 11 | + |
| 12 | +async function getAllRuntimes(config: { headers: { Authorization: string }; baseUrl: string }) { |
| 13 | + const response = await fetch(`${config.baseUrl}/admin/runtime-environments`, { |
| 14 | + method: 'GET', |
| 15 | + headers: config.headers, |
| 16 | + }); |
| 17 | + const onPremRuntimes = await response.json(); |
| 18 | + await writeCodefreshFiles(onPremRuntimes, 'onPrem-runtimes'); |
| 19 | +} |
| 20 | + |
| 21 | +async function getTotalUsers(config: { headers: { Authorization: string }; baseUrl: string }) { |
| 22 | + const response = await fetch(`${config.baseUrl}/admin/user?limit=1&page=1`, { |
| 23 | + method: 'GET', |
| 24 | + headers: config.headers, |
| 25 | + }); |
| 26 | + const users = await response.json(); |
| 27 | + await writeCodefreshFiles({ total: users.total }, 'onPrem-totalUsers'); |
| 28 | +} |
| 29 | + |
| 30 | +async function getSystemFeatureFlags(config: { headers: { Authorization: string }; baseUrl: string }) { |
| 31 | + const response = await fetch(`${config.baseUrl}/admin/features`, { |
| 32 | + method: 'GET', |
| 33 | + headers: config.headers, |
| 34 | + }); |
| 35 | + const onPremSystemFF = await response.json(); |
| 36 | + await writeCodefreshFiles(onPremSystemFF, 'onPrem-systemFeatureFlags'); |
| 37 | +} |
| 38 | + |
| 39 | +export async function onPrem(config: { headers: { Authorization: string }; baseUrl: string }) { |
| 40 | + if (config.baseUrl === 'https://g.codefresh.io/api') { |
| 41 | + console.error( |
| 42 | + `\nCannot gather On-Prem data for Codefresh SaaS. Please select either ${RuntimeType.pipelines} or ${RuntimeType.gitops}.`, |
| 43 | + ); |
| 44 | + console.error( |
| 45 | + 'If you need to gather data for Codefresh On-Prem, please update your ./cfconfig conext (or Envs) to point to an On-Prem instance.', |
| 46 | + ); |
| 47 | + Deno.exit(40); |
| 48 | + } |
| 49 | + try { |
| 50 | + const namespace = await selectNamespace(); |
| 51 | + console.log(`\nGathering data in "${namespace}" namespace for Codefresh On-Prem.`); |
| 52 | + await fetchAndSaveData(RuntimeType.onprem, namespace); |
| 53 | + await Promise.all([ |
| 54 | + getAllAccounts(config), |
| 55 | + getAllRuntimes(config), |
| 56 | + getTotalUsers(config), |
| 57 | + getSystemFeatureFlags(config), |
| 58 | + ]); |
| 59 | + console.log('\nData Gathered Successfully.'); |
| 60 | + await prepareAndCleanup(); |
| 61 | + } catch (error) { |
| 62 | + console.error(`Error gathering On-Prem data:`, error); |
| 63 | + } |
| 64 | +} |
0 commit comments