UNPKG

@nasriya/orchestriq

Version:

A package to generate Docker files

61 lines (60 loc) 2.35 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const StackNetwork_1 = __importDefault(require("../containers/templates/assets/networks/StackNetwork")); class NetworksManager { #_socket; constructor(socket) { this.#_socket = socket; } /** * Retrieves a list of all networks. * * @returns {Promise<Network[]>} A promise that resolves to an array of Network objects. * @throws {Error} If unable to list networks. */ async list() { try { const networks = await this.#_socket.fetch('/networks'); return networks; } catch (error) { if (error instanceof Error) { { error.message = `Unable to list networks: ${error.message}`; } } throw error; } } /** * Creates a new network and returns its ID. * @param data The data for the network to create. * @param options An object with options for creating the network. * The object must have a `checkDuplicates` property that is a boolean. * If `checkDuplicates` is true, the method will check if a network with the same name already exists. * If `checkDuplicates` is false, the method will create a new network with the same name if it already exists. * @returns The ID of the created network. * @throws {Error} If the network could not be created. */ async create(data, options = { checkDuplicates: true }) { try { const stackNetwork = data instanceof StackNetwork_1.default ? data : new StackNetwork_1.default(data); const json = stackNetwork.toJSON('api'); json.CheckDuplicate = options.checkDuplicates; const network = await this.#_socket.fetch('/networks/create', { method: 'POST', body: JSON.stringify(json) }); return network.Id; } catch (error) { if (error instanceof Error) { { error.message = `Unable to create network: ${error.message}`; } } throw error; } } } exports.default = NetworksManager;