UNPKG

@pocinnovation/alumi

Version:

Dynamic pulumi provider for Aleph

161 lines (160 loc) 6.9 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Aggregate = void 0; const pulumi = __importStar(require("@pulumi/pulumi")); const client_1 = require("@aleph-sdk/client"); const utils_1 = require("./utils"); const propKey = 'key'; const propContent = 'content'; const propChannel = 'channel'; const propStorageEngine = 'storageEngine'; const propAccountEnvName = 'accountEnvName'; const AggregateProvider = { diff(id, olds, news) { return __awaiter(this, void 0, void 0, function* () { const replaces = []; const changes = []; if (JSON.stringify(olds[propContent]) !== JSON.stringify(news[propContent])) { changes.push(propContent); } if (olds[propChannel] !== news[propChannel]) { replaces.push(propChannel); } if (olds[propStorageEngine] !== news[propStorageEngine]) { replaces.push(propStorageEngine); } if (olds[propKey] !== news[propKey]) { replaces.push(propKey); } if (replaces.length > 0) { return { replaces: replaces }; } else if (changes.length > 0) { return { changes: true }; } else { return { changes: false }; } }); }, update(id, olds, news) { return __awaiter(this, void 0, void 0, function* () { const inputs = { key: news[propKey], content: news[propContent], channel: news[propChannel], storageEngine: news[propStorageEngine], accountEnvName: news[propAccountEnvName], }; return yield this.create(inputs); }); }, delete(id, props) { return __awaiter(this, void 0, void 0, function* () { const account = yield (0, utils_1.getAccount)(props[propAccountEnvName]); const client = new client_1.AuthenticatedAlephHttpClient(account); yield client.forget({ channel: props[propChannel], hashes: [props.item_hash], sync: true, }); }); }, create(inputs) { return __awaiter(this, void 0, void 0, function* () { const account = yield (0, utils_1.getAccount)(inputs[propAccountEnvName]); const client = new client_1.AuthenticatedAlephHttpClient(account); const res = yield client.createAggregate({ address: account.address, key: inputs[propKey], content: inputs[propContent], channel: inputs[propChannel], storageEngine: inputs[propStorageEngine], sync: true, }); const out = { // inputs key: inputs[propKey], content: inputs[propContent], storageEngine: inputs[propStorageEngine], accountEnvName: inputs[propAccountEnvName], // outputs chain: res.chain, channel: res.channel || '', sender: res.sender, type: `${res.type}`, signature: res.signature, time: res.time, item_type: res.item_type, item_hash: res.item_hash, // Created content_address: res.content.address, content_key: res.content.key.toString(), content_time: res.content.time, content_content: res.content.content, aleph_explorer_url: (0, utils_1.getAlephExplorerUrl)(res.chain, res.sender, 'AGGREGATE', res.item_hash), }; return { id: `${account.address}-${res.item_hash}`, outs: out, }; }); }, read(id, props) { return __awaiter(this, void 0, void 0, function* () { const account = yield (0, utils_1.getAccount)(props[propAccountEnvName]); const client = new client_1.AuthenticatedAlephHttpClient(account); const res = yield client.fetchAggregate(account.address, props[propKey]); if (res[props[propKey]] === null || res[props[propKey]] === undefined) { throw new Error('Aggregate not found'); } const out = Object.assign(Object.assign({}, props), { content_content: res }); return { id: id, outs: out, }; }); }, }; class Aggregate extends pulumi.dynamic.Resource { constructor(name, props, opts) { super(AggregateProvider, name, Object.assign({ // outputs chain: undefined, sender: undefined, type: undefined, signature: undefined, time: undefined, item_type: undefined, item_hash: undefined, // Created content_address: undefined, content_key: undefined, content_time: undefined, content_content: undefined, aleph_explorer_url: undefined }, props), opts); } } exports.Aggregate = Aggregate;