UNPKG

@axway/axway-central-cli

Version:

Manage APIs, services and publish to the Amplify Marketplace

128 lines (124 loc) 6.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PlatformTeamMemberRole = exports.PlatformServiceAccountRole = exports.PlatformClient = exports.PlatformAuthMethod = void 0; var _snooplogg = _interopRequireDefault(require("snooplogg")); var _amplifyCliUtils = require("@axway/amplify-cli-utils"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function _classPrivateFieldInitSpec(e, t, a) { _checkPrivateRedeclaration(e, t), t.set(e, a); } function _checkPrivateRedeclaration(e, t) { if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object"); } function _classPrivateFieldGet(s, a) { return s.get(_assertClassBrand(s, a)); } function _classPrivateFieldSet(s, a, r) { return s.set(_assertClassBrand(s, a), r), r; } function _assertClassBrand(e, t, n) { if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n; throw new TypeError("Private element is not present on this object"); } const { log } = (0, _snooplogg.default)('central:class.PlatformClient'); let PlatformAuthMethod = exports.PlatformAuthMethod = /*#__PURE__*/function (PlatformAuthMethod) { PlatformAuthMethod["Certificate"] = "certificate"; PlatformAuthMethod["Secret"] = "secret"; return PlatformAuthMethod; }({}); let PlatformServiceAccountRole = exports.PlatformServiceAccountRole = /*#__PURE__*/function (PlatformServiceAccountRole) { PlatformServiceAccountRole["ApiCentralAdmin"] = "api_central_admin"; PlatformServiceAccountRole["FlowCentralAccessManager"] = "fc_access_manager"; PlatformServiceAccountRole["FlowCentralIntegration"] = "fc_integration"; PlatformServiceAccountRole["FlowCentralITAdmin"] = "fc_it_admin"; PlatformServiceAccountRole["FlowCentralProductsAdmin"] = "fc_products_admin"; PlatformServiceAccountRole["FlowCentralSpecOps"] = "fc_spec_ops"; PlatformServiceAccountRole["FlowCentralSubscriptionApprover"] = "fc_subscriptionapprover"; PlatformServiceAccountRole["FlowCentralSubscriptionSpecialist"] = "fc_subscriptionspecialist"; PlatformServiceAccountRole["FlowCentralTemplatePublisher"] = "fc_templatepublisher"; PlatformServiceAccountRole["FlowCentralCftAdmin"] = "fc_cft_admin"; return PlatformServiceAccountRole; }({}); let PlatformTeamMemberRole = exports.PlatformTeamMemberRole = /*#__PURE__*/function (PlatformTeamMemberRole) { PlatformTeamMemberRole["Admin"] = "administrator"; PlatformTeamMemberRole["Developer"] = "developer"; PlatformTeamMemberRole["Consumer"] = "consumer"; PlatformTeamMemberRole["CatalogManager"] = "catalog_manager"; return PlatformTeamMemberRole; }({}); /** Options used to create a service account via the PlatformClient.createServiceAccount() method. */ /** Provides information for a platform service account. */ var _baseUrl = /*#__PURE__*/new WeakMap(); var _accountName = /*#__PURE__*/new WeakMap(); var _amplifyConfig = /*#__PURE__*/new WeakMap(); var _amplifySdk = /*#__PURE__*/new WeakMap(); class PlatformClient { constructor({ baseUrl, region, account } = {}) { _classPrivateFieldInitSpec(this, _baseUrl, void 0); _classPrivateFieldInitSpec(this, _accountName, void 0); _classPrivateFieldInitSpec(this, _amplifyConfig, void 0); _classPrivateFieldInitSpec(this, _amplifySdk, void 0); log(`initializing client with params: baseUrl = ${baseUrl}, region = ${region}, account = ${account}`); _classPrivateFieldSet(_baseUrl, this, baseUrl); _classPrivateFieldSet(_accountName, this, account); const initResult = (0, _amplifyCliUtils.initSDK)({ baseUrl: _classPrivateFieldGet(_baseUrl, this), username: _classPrivateFieldGet(_accountName, this) }); _classPrivateFieldSet(_amplifyConfig, this, initResult.config); _classPrivateFieldSet(_amplifySdk, this, initResult.sdk); } async getAccountInfo() { var _classPrivateFieldGet2; // Get default teams from config. const defaultTeams = _classPrivateFieldGet(_amplifyConfig, this).get(`${(0, _amplifyCliUtils.getAuthConfigEnvSpecifier)((_classPrivateFieldGet2 = _classPrivateFieldGet(_amplifySdk, this).env) === null || _classPrivateFieldGet2 === void 0 ? void 0 : _classPrivateFieldGet2.name)}.defaultTeam`); // Fetch specified account or default account currently logged in. let accountInfo; if (_classPrivateFieldGet(_accountName, this)) { accountInfo = await _classPrivateFieldGet(_amplifySdk, this).auth.find(_classPrivateFieldGet(_accountName, this), defaultTeams); if (!accountInfo) { throw new Error(`Account "${_classPrivateFieldGet(_accountName, this)}" not found`); } } if (!accountInfo) { const accountArray = await _classPrivateFieldGet(_amplifySdk, this).auth.list({ defaultTeams, validate: true }); if (accountArray) { accountInfo = accountArray.find(nextAccount => nextAccount.default) || accountArray[0]; } } // Make sure "subscriptions" is defined since Amplify SDK requires it. (Will throw error if missing.) if (accountInfo && accountInfo.org && !accountInfo.org.subscriptions) { accountInfo.org.subscriptions = []; } // Return account info if found. return accountInfo; } async createServiceAccount(options) { const accountInfo = await this.getAccountInfo(); const result = await _classPrivateFieldGet(_amplifySdk, this).client.create(accountInfo, accountInfo === null || accountInfo === void 0 ? void 0 : accountInfo.org, options); return result.client; } async getServiceAccounts(filterRole) { const result = await _classPrivateFieldGet(_amplifySdk, this).client.list(await this.getAccountInfo()); let clients = result.clients || []; if (filterRole) { log('filter clients by ', filterRole); clients = clients.filter(client => client === null || client === void 0 ? void 0 : client.roles.find(r => r === filterRole)); } return clients; } async getTeams() { var _account$team, _teams$find; log(`getTeams`); const account = await this.getAccountInfo(); const { teams } = await _classPrivateFieldGet(_amplifySdk, this).team.list(account); const teamGuid = ((_account$team = account.team) === null || _account$team === void 0 ? void 0 : _account$team.guid) && ((_teams$find = teams.find(team => team.guid === account.team.guid)) === null || _teams$find === void 0 ? void 0 : _teams$find.guid); return teams.map(team => ({ ...team, default: teamGuid && team.guid === teamGuid || !teamGuid && team.default })); } } exports.PlatformClient = PlatformClient;