UNPKG

@axway/axway-central-cli

Version:

Manage APIs, services and publish to the Amplify Marketplace

155 lines (152 loc) 5.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getListByResource = exports.getLatestAgentVersion = exports.getIngestionHost = exports.getEnvironmentId = exports.getCurrentUserOrgId = void 0; var _snooplogg = _interopRequireDefault(require("snooplogg")); var _CoreConfigController = require("../../../common/CoreConfigController"); var _dataService = require("../../../common/dataService"); var _types = require("../../../common/types"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } const { log } = (0, _snooplogg.default)('central: install: agents: helpers'); const getListByResource = async input => { // NOTE: only a first found set is used const defs = input.defsManager.findDefsByWord(input.resourceShortName); if (!defs) { throw Error(`the server doesn't have a resource type "${input.resourceType}"`); } return input.client.getResourcesList({ resourceDef: defs[0].resource, scopeDef: defs[0].scope ? defs[0].scope : undefined, scopeName: input.scopeName, query: input.query }); }; // Note: forcing it to use apicentral client id exports.getListByResource = getListByResource; const getCurrentUserOrgId = async () => { const authData = await new _CoreConfigController.CoreConfigController().getAuthInfo(); if (!authData.orgId) throw Error(`Can't find org ID`); return authData.orgId; }; exports.getCurrentUserOrgId = getCurrentUserOrgId; const getEnvironmentId = async (client, defsManager, environmentName, scopeName) => { var _resource$data; const defs = defsManager.findDefsByWord('env'); if (!defs) { throw Error(`the server doesn't have a resource type "Environment"`); } const resource = await client.getResourceByName({ resourceDef: defs[0].resource, resourceName: environmentName, scopeDef: defs[0].scope ? defs[0].scope : undefined, scopeName: scopeName ? scopeName : undefined }); return (_resource$data = resource.data) !== null && _resource$data !== void 0 && _resource$data.metadata ? resource.data.metadata.id || '' : ''; }; exports.getEnvironmentId = getEnvironmentId; const getIngestionHost = (region, env, apicDeployment, protocol = _types.IngestionProtocol.Lumberjack) => { const hosts = { [_types.IngestionProtocol.Lumberjack]: { [_types.Regions.US]: { [_types.Platforms.prod]: { [_types.APICDeployments.US]: _types.IngestionHosts.US }, [_types.Platforms.staging]: { [_types.APICDeployments.USStaging]: _types.IngestionHosts.USStaging, [_types.APICDeployments.QA]: _types.IngestionHosts.QA, [_types.APICDeployments.TEAMS]: _types.IngestionHosts.QA } }, [_types.Regions.EU]: { [_types.Platforms.prod]: { [_types.APICDeployments.EU]: _types.IngestionHosts.EU }, [_types.Platforms.staging]: { [_types.APICDeployments.EUStaging]: _types.IngestionHosts.EUStaging } }, [_types.Regions.AP]: { [_types.Platforms.prod]: { [_types.APICDeployments.AP]: _types.IngestionHosts.AP }, [_types.Platforms.staging]: { [_types.APICDeployments.APStaging]: _types.IngestionHosts.APStaging } } }, [_types.IngestionProtocol.HTTPS]: { [_types.Regions.US]: { [_types.Platforms.prod]: { [_types.APICDeployments.US]: _types.IngestionHTTPHosts.US }, [_types.Platforms.staging]: { [_types.APICDeployments.US]: _types.IngestionHTTPHosts.USStaging } }, [_types.Regions.EU]: { [_types.Platforms.prod]: { [_types.APICDeployments.EU]: _types.IngestionHTTPHosts.EU }, [_types.Platforms.staging]: { [_types.APICDeployments.EU]: _types.IngestionHTTPHosts.EUStaging } }, [_types.Regions.AP]: { [_types.Platforms.prod]: { [_types.APICDeployments.AP]: _types.IngestionHTTPHosts.AP }, [_types.Platforms.staging]: { [_types.APICDeployments.AP]: _types.IngestionHTTPHosts.APStaging } } } }; return hosts[protocol][region][env][apicDeployment] || _types.IngestionHosts.US; }; exports.getIngestionHost = getIngestionHost; const getLatestAgentVersion = async agent => { try { const service = await (0, _dataService.dataService)({ baseUrl: _types.PublicRepoUrl, basePath: _types.BasePaths.DockerAgentAPIRepoPath }); const response = await service.get(`/${agent}/tags/list`, { // docker api requires auth, even if its just anonymous Authorization: `Basic ${Buffer.from('anonymous:').toString('base64')}` }, true); if (response.tags.length === 0) { return 'latest'; } const latestVersion = response.tags.reduce((prev, current) => { // skip any tags that are latest if (prev === 'latest') { return current; } else if (current === 'latest') { return prev; } // find the largest tag const [pMajor, pMinor, pPatch] = prev.split('.').map(Number); const [cMajor, cMinor, cPatch] = current.split('.').map(Number); if (cMajor > pMajor) { return current; } if (cMajor == pMajor && cMinor > pMinor) { return current; } if (cMajor == pMajor && cMinor == pMinor && cPatch > pPatch) { return current; } return prev; }); log(`Latest Version (${agent}): ${latestVersion}`); return latestVersion; } catch (e) { log('Error hit retrieving latest version of agent, setting tag to latest'); log(e); return 'latest'; } }; exports.getLatestAgentVersion = getLatestAgentVersion;