UNPKG

@altostra/core

Version:

Core library for shared types and logic

1 lines • 8.14 kB
"use strict";var _DeploymentsManager_instances,_DeploymentsManager_endpoint,_DeploymentsManager_getAuthenticationHeaders,__awaiter=this&&this.__awaiter||function(e,t,r,i){return new(r||(r=Promise))((function(a,n){function o(e){try{l(i.next(e))}catch(e){n(e)}}function s(e){try{l(i.throw(e))}catch(e){n(e)}}function l(e){var t;e.done?a(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(o,s)}l((i=i.apply(e,t||[])).next())}))},__classPrivateFieldSet=this&&this.__classPrivateFieldSet||function(e,t,r,i,a){if("m"===i)throw new TypeError("Private method is not writable");if("a"===i&&!a)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof t?e!==t||!a:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===i?a.call(e,r):a?a.value=r:t.set(e,r),r},__classPrivateFieldGet=this&&this.__classPrivateFieldGet||function(e,t,r,i){if("a"===r&&!i)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!i:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===r?i:"a"===r?i.call(e):i?i.value:t.get(e)};Object.defineProperty(exports,"__esModule",{value:!0}),exports.filterHistoryBy=exports.DeploymentsManager=void 0;const EntityId_1=require("../../../common/CustomTypes/EntityId"),AltoError_1=require("../../../common/Errors/AltoError"),Url_1=require("../../../common/Utils/Url"),primitives_1=require("@altostra/type-validations/lib/primitives"),first_1=require("@reactivex/ix-es2015-cjs/iterable/first"),from_1=require("@reactivex/ix-es2015-cjs/iterable/from"),defaultifempty_1=require("@reactivex/ix-es2015-cjs/iterable/operators/defaultifempty"),filter_1=require("@reactivex/ix-es2015-cjs/iterable/operators/filter"),maxby_1=require("@reactivex/ix-es2015-cjs/iterable/operators/maxby"),common_1=require("../common"),ItemSelection_1=require("../ItemSelection"),ServiceClientBase_1=require("../ServiceClientBase"),ProjectValidatingServiceClient_1=require("../ServiceClientBase/ProjectValidatingServiceClient"),DeploymentResponse_1=require("./DeploymentResponse"),Types_1=require("./Types"),ServiceClientBase=(0,ProjectValidatingServiceClient_1.ProjectValidatingServiceClient)(ServiceClientBase_1.ServiceClientBase);class DeploymentsManager extends ServiceClientBase{constructor({axios:e,endpoint:t,getToken:r,logger:i,errorMapping:a={}}){super({axios:e,getToken:r,logger:i,supportedFeatures:[Types_1.FEATURE_RICH_EXTRADATA],errorMapping:a,serviceName:"DeploymentsManager",[ProjectValidatingServiceClient_1.backofficeUrlSym]:t}),_DeploymentsManager_instances.add(this),_DeploymentsManager_endpoint.set(this,void 0),__classPrivateFieldSet(this,_DeploymentsManager_endpoint,t,"f")}get(e,t){return this._request({request:r=>__awaiter(this,void 0,void 0,(function*(){const i="string"==typeof e?{id:(0,EntityId_1.entityId)(e)}:e;return yield r.get((0,ItemSelection_1.appendUrlQuery)(getPath(__classPrivateFieldGet(this,_DeploymentsManager_endpoint,"f"),(0,ItemSelection_1.selectionValue)(i),t),(0,ItemSelection_1.selectionQueryParam)(i)),{headers:yield this._getHeaders()})})),dataValidation:Types_1.isDeploymentEntityResponse,errorWrapping:{message:"An error occurred while getting deployment for a project.\nPlease try again or contact support.",data:{project:e,deploymentName:t}}})}list(e,t){return __awaiter(this,void 0,void 0,(function*(){return yield this._request({request:r=>__awaiter(this,void 0,void 0,(function*(){const i="string"==typeof e?{id:(0,EntityId_1.entityId)(e)}:e;return yield r.get((0,ItemSelection_1.appendUrlQuery)(listPath(__classPrivateFieldGet(this,_DeploymentsManager_endpoint,"f"),(0,ItemSelection_1.selectionValue)(i)),(0,ItemSelection_1.selectionQueryParam)(i)),(0,common_1.paginate)(t,{headers:yield this._getHeaders()}))})),dataValidation:Types_1.isDeploymentsArray,errorWrapping:{message:"An error occurred while listing deployments for a project.\nPlease try again or contact support.",data:{project:e}}})}))}getHistory(e,t,r){return __awaiter(this,void 0,void 0,(function*(){return yield this._request({request:i=>__awaiter(this,void 0,void 0,(function*(){const a="string"==typeof e?{id:(0,EntityId_1.entityId)(e)}:e;return yield i.get((0,ItemSelection_1.appendUrlQuery)(historyPath(__classPrivateFieldGet(this,_DeploymentsManager_endpoint,"f"),(0,ItemSelection_1.selectionValue)(a),t),(0,ItemSelection_1.selectionQueryParam)(a)),(0,common_1.paginate)(r,{headers:Object.assign({},yield __classPrivateFieldGet(this,_DeploymentsManager_instances,"m",_DeploymentsManager_getAuthenticationHeaders).call(this))}))})),dataValidation:Types_1.isCombinedDeploymentHistoryActionArray,errorWrapping:{message:"An error occurred while getting the deployment history for a project.\nPlease try again or contact support.",data:{project:e,deploymentName:t}}})}))}getLatestMetadata(e,t,r){var i;return __awaiter(this,void 0,void 0,(function*(){const a=(0,from_1.from)(yield this.getHistory(e,t)).pipe(filterHistoryBy({successfulOnly:!r,hasExtraData:!r}),(0,defaultifempty_1.defaultIfEmpty)(void 0),(0,maxby_1.maxBy)(e=>null==e?void 0:e.version)),n=(0,first_1.first)(a);return null!==(i=null==n?void 0:n.extraData)&&void 0!==i?i:void 0}))}deploy({projectId:e,tag:t,deployment:r,deploymentType:i,environment:a,configSet:n,configMap:o,deleteStaleStack:s,deploymentParams:l,isSourcesRequired:c}){return __awaiter(this,arguments,void 0,(function*(){if("update"!==(i=null!=i?i:"update")&&void 0===a)throw AltoError_1.AltoError.create("Please specify the target environment for the new deployment.",{data:{args:[...arguments]}});const d=yield this._request({request:d=>__awaiter(this,void 0,void 0,(function*(){return yield d.post(deployPath(__classPrivateFieldGet(this,_DeploymentsManager_endpoint,"f"),e,r),Object.assign(Object.assign(Object.assign({type:"deploy"},"createOrUpdate"===i&&{force:!0}),"create"===i&&{create:!0}),{tag:t,environment:a,configSet:n,configMap:o,deleteStaleStack:s,deploymentParams:l,isSourcesRequired:c}),{headers:Object.assign({},yield __classPrivateFieldGet(this,_DeploymentsManager_instances,"m",_DeploymentsManager_getAuthenticationHeaders).call(this))})})),dataValidation:DeploymentResponse_1.isBossDeploymentResponse,errorWrapping:{message:"Deployment of the project has failed.\nPlease try again or contact support.",data:{args:[...arguments]}}});return d}))}deleteDeployment(e,t,{bucketsToEmpty:r}={}){const i=new URL(getPath(__classPrivateFieldGet(this,_DeploymentsManager_endpoint,"f"),e,t));return(null==r?void 0:r.length)&&i.searchParams.append(Types_1.QUERY_EMPTIED_BUCKETS,r.join(" ")),this._request({request:e=>__awaiter(this,void 0,void 0,(function*(){return yield e.delete(i.toString(),{headers:yield this._getHeaders()})})),dataValidation:primitives_1.any,errorWrapping:{message:"Deployment deletion has failed.\nPlease try again or contact support.",data:{projectId:e,deploymentName:t}}})}getImageUsage(e,t,r){return this._request({request:i=>__awaiter(this,void 0,void 0,(function*(){return yield i.get(getImageUsagePath(__classPrivateFieldGet(this,_DeploymentsManager_endpoint,"f"),e,t),(0,common_1.paginate)(r,{headers:yield this._getHeaders()}))})),dataValidation:Types_1.isVersionUsageHistoryResponse,errorWrapping:{}})}}function filterHistoryBy({successfulOnly:e,hasExtraData:t}){return r=>{let i=(0,from_1.from)(r);return e||t?(e&&(i=i.pipe((0,filter_1.filter)(({status:e})=>"Deployed"===e))),t&&(i=i.pipe((0,filter_1.filter)(({extraData:e})=>!!e))),i):i}}function getPath(e,t,r){return listPath(e,t)+Url_1.asUrl`/${r}`}function listPath(e,t){return e+Url_1.asUrl`/projects/${t}/deployments`}function deployPath(e,t,r){return listPath(e,t)+Url_1.asUrl`/${r}`}function historyPath(e,t,r){return deployPath(e,t,r)+"/history"}function getImageUsagePath(e,t,r){return e+Url_1.asUrl`/projects/${t}/images/${r}/history`}exports.DeploymentsManager=DeploymentsManager,_DeploymentsManager_endpoint=new WeakMap,_DeploymentsManager_instances=new WeakSet,_DeploymentsManager_getAuthenticationHeaders=function(){return this._getHeaders()},exports.filterHistoryBy=filterHistoryBy;