UNPKG

3.42 kBJavaScriptView Raw
1"use strict";
2const errors_1 = require("@cumulus/errors");
3const BucketsConfigError = errors_1.createErrorType('BucketsConfigError');
4/**
5 * Class representing cumulus bucket configuration.
6 */
7class BucketsConfig {
8 constructor(bucketsJsonObject) {
9 this.buckets = bucketsJsonObject;
10 }
11 /**
12 * returns key into this.buckets whose object has `name` bucketName
13 * @param {string} bucketName
14 * @returns {string|undefined} desired bucket's key value.
15 */
16 key(bucketName) {
17 return Object.keys(this.buckets)
18 .find((bucketKey) => bucketName === this.buckets[bucketKey].name);
19 }
20 /**
21 * Return bucket type for bucketName
22 * @param {string} bucketName
23 * @returns {string} matching bucket's type
24 */
25 type(bucketName) {
26 const key = this.key(bucketName);
27 if (!key) {
28 throw new BucketsConfigError(`bucketName ${bucketName} not found in config ${JSON.stringify(this.buckets)}`);
29 }
30 return this.buckets[key].type;
31 }
32 /**
33 * returns bucket object who's name field matches bucketName
34 * @param {string} bucketName
35 * @returns {Object} bucket object
36 */
37 bucket(bucketName) {
38 const key = this.key(bucketName);
39 if (!key) {
40 throw new BucketsConfigError(`bucketName ${bucketName} not found in config ${JSON.stringify(this.buckets)}`);
41 }
42 return this.buckets[key];
43 }
44 /**
45 * returns true if bucketName is found in any attatched bucket objects.
46 * @param {string} bucketName
47 * @returns {boolean} truthyness of this bucket existing in the configuration
48 */
49 exists(bucketName) {
50 return this.key(bucketName) !== undefined;
51 }
52 /**
53 * returns true if configKey is found on the top-level config.
54 * @param {string} configKey
55 * @returns {boolean} truthyness of this key existing in the configuration
56 */
57 keyExists(configKey) {
58 return Object.keys(this.buckets).includes(configKey);
59 }
60 /**
61 * returns name of bucket attatched to top-level config at configKey.
62 * @param {string} configKey
63 * @returns {string} name of bucket at key.
64 */
65 nameByKey(configKey) {
66 return this.buckets[configKey].name;
67 }
68 /**
69 * return a list of configured buckets of desired type.
70 *
71 * @param {string/Array} types - types of buckets to return
72 * @returns {Array<Object>} - array of buckets that are of desired types
73 */
74 bucketsOfType(types) {
75 const checkTypes = typeof types === 'string' ? [types] : types;
76 return Object.values(this.buckets)
77 .filter(({ type }) => checkTypes.includes(type));
78 }
79 /** @returns {Array} list of private buckets */
80 privateBuckets() {
81 return this.bucketsOfType('private');
82 }
83 /** @returns {Array} list of protected buckets */
84 protectedBuckets() {
85 return this.bucketsOfType('protected');
86 }
87 /** @returns {Array} list of public buckets */
88 publicBuckets() {
89 return this.bucketsOfType('public');
90 }
91 /** @returns {Array} list of shared buckets */
92 sharedBuckets() {
93 return this.bucketsOfType('shared');
94 }
95 /** @returns {Array} list of internal buckets */
96 internalBuckets() {
97 return this.bucketsOfType('internal');
98 }
99}
100module.exports = BucketsConfig;
101//# sourceMappingURL=BucketsConfig.js.map
\No newline at end of file