@controlplane/cli
Version:
Control Plane Corporation CLI
113 lines • 3.52 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.dismantleSelfLink = exports.deploymentPath = exports.resolveToLink = exports.kindResolver = void 0;
const links_1 = require("../util/links");
const kindResolver = (kind) => {
return {
resourceLink(ref, ctx) {
return resolveToLink(kind, ref, ctx);
},
parentLink(ctx) {
let res = resolveToLink(kind, 'item', ctx);
return (0, links_1.parentLink)(res);
},
homeLink(ctx) {
if (kind == 'org') {
return '/org';
}
if (kind == 'account') {
return '/account';
}
let res = resolveToLink('org', undefined, ctx);
return res + '/' + kind;
},
kind: kind,
};
};
exports.kindResolver = kindResolver;
function resolveToLink(kind, ref, ctx) {
let orgLink;
if (kind == 'account') {
if (ref) {
return '/account/' + ref;
}
else {
throw new Error('Cannot guess the account id');
}
}
if (ctx.org) {
orgLink = '/org/' + ctx.org;
}
if (kind == 'org') {
if (ref) {
return '/org/' + ref;
}
else if (ctx.org) {
return '/org/' + ctx.org;
}
else {
throw new Error('Cannot guess the organization: either provide --org or set one in your profile');
}
}
if (!orgLink) {
throw new Error('Cannot guess the organization: either provide --org or set one in your profile');
}
let gvcLink;
if (kind == 'gvc') {
if (ref) {
return orgLink + '/gvc/' + ref;
}
else if (ctx.gvc) {
return orgLink + '/gvc/' + ctx.gvc;
}
else {
throw new Error('Cannot guess the gvc: either provide --gvc or set one in your profile');
}
}
if (!ctx.gvc) {
gvcLink = undefined;
}
else {
gvcLink = orgLink + '/gvc/' + ctx.gvc;
}
if (!ref) {
throw new Error(`Cannot guess the ${kind}: provide a name`);
}
if (['workload', 'identity', 'dbcluster', 'volumeset'].includes(kind)) {
if (!gvcLink) {
throw new Error(`Cannot guess the ${kind} without a gvc: either provide --gvc or set one in your profile`);
}
else {
return gvcLink + '/' + kind + '/' + ref;
}
}
return orgLink + '/' + kind + '/' + ref;
}
exports.resolveToLink = resolveToLink;
function deploymentPath(workload, context) {
return (0, exports.kindResolver)('workload').parentLink(context) + `/${workload.name}/deployment`;
}
exports.deploymentPath = deploymentPath;
/**
* Dismantles the given string to extract details about the obj.
* @param org Org name that is associated with the resource.
* @param selfLink Object's self link to dismantle.
* @returns An object with details of the self link.
*/
function dismantleSelfLink(org, selfLink) {
const parts = selfLink.split('/').filter((part) => part.trim() !== '');
const result = {
kind: parts[parts.length - 2],
name: parts[parts.length - 1],
ctx: { org },
};
if (parts.length > 4 && parts[parts.length - 4] === 'gvc') {
result.ctx = {
...result.ctx,
gvc: parts[parts.length - 3],
};
}
return result;
}
exports.dismantleSelfLink = dismantleSelfLink;
//# sourceMappingURL=resolver.js.map
;