@pocinnovation/alumi
Version:
Dynamic pulumi provider for Aleph
152 lines (151 loc) • 6.56 kB
JavaScript
;
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.Instance = exports.getDefaultImage = void 0;
const pulumi = __importStar(require("@pulumi/pulumi"));
const client_1 = require("@aleph-sdk/client");
const utils_1 = require("./utils");
const propChannel = 'channel';
const propAuthorizedKeys = 'authorizedKeys';
const propMemory = 'memory';
const propImage = 'image';
const propVolumes = 'volumes';
const propStorageEngine = 'storageEngine';
const propAccountEnvName = 'accountEnvName';
const getDefaultImage = () => {
return '549ec451d9b099cad112d4aaa2c00ac40fb6729a92ff252ff22eef0b5c3cb613';
};
exports.getDefaultImage = getDefaultImage;
const InstanceProvider = {
diff(id, olds, news) {
return __awaiter(this, void 0, void 0, function* () {
const replaces = [];
if (olds[propChannel] !== news[propChannel]) {
replaces.push(propChannel);
}
if (olds[propAuthorizedKeys] !== news[propAuthorizedKeys]) {
replaces.push(propAuthorizedKeys);
}
if (olds[propMemory] !== news[propMemory]) {
replaces.push(propMemory);
}
if (olds[propImage] !== news[propImage]) {
replaces.push(propImage);
}
if (JSON.stringify(olds[propVolumes]) !== JSON.stringify(news[propVolumes])) {
replaces.push(propVolumes);
}
if (olds[propStorageEngine] !== news[propStorageEngine]) {
replaces.push(propStorageEngine);
}
if (replaces.length === 0) {
return { changes: false };
}
return { replaces: replaces };
});
},
update(id, olds, news) {
return __awaiter(this, void 0, void 0, function* () {
throw new Error('Update not implemented; Only Delete and Create');
});
},
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.createInstance({
channel: inputs[propChannel],
authorized_keys: inputs[propAuthorizedKeys],
resources: {
memory: inputs[propMemory],
},
image: inputs[propImage],
volumes: inputs[propVolumes].map((volume) => {
if (volume._type === 'immutable') {
return Object.assign(Object.assign({}, volume), { is_read_only: () => true });
}
else if (volume._type === 'ephemeral') {
return Object.assign(Object.assign({}, volume), { is_read_only: () => false });
}
else {
throw new Error('Invalid volume type: This should never happen.');
}
}),
storageEngine: inputs[propStorageEngine],
});
const out = {
// inputs
channel: inputs[propChannel],
authorizedKeys: inputs[propAuthorizedKeys],
memory: inputs[propMemory],
image: inputs[propImage],
volumes: inputs[propVolumes],
storageEngine: inputs[propStorageEngine],
accountEnvName: inputs[propAccountEnvName],
// outputs
chain: res.chain,
sender: res.sender,
type: `${res.type}`,
item_hash: res.item_hash,
// Created
aleph_explorer_url: (0, utils_1.getAlephExplorerUrl)(res.chain, res.sender, 'INSTANCE', res.item_hash),
};
return {
id: `${account.address}-${res.item_hash}`,
outs: out,
};
});
},
};
class Instance extends pulumi.dynamic.Resource {
constructor(name, args, opts) {
super(InstanceProvider, name, Object.assign({
// outputs
chain: undefined, sender: undefined, type: undefined, item_hash: undefined,
// Created
aleph_explorer_url: undefined }, args), opts);
}
}
exports.Instance = Instance;