UNPKG

34 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.AnalyticsIndexManager = exports.AzureExternalAnalyticsLink = exports.S3ExternalAnalyticsLink = exports.CouchbaseRemoteAnalyticsLink = exports.AnalyticsLink = exports.CouchbaseAnalyticsEncryptionSettings = exports.AnalyticsIndex = exports.AnalyticsDataset = exports.AnalyticsEncryptionLevel = exports.AnalyticsLinkType = void 0;
4const errors_1 = require("./errors");
5const httpexecutor_1 = require("./httpexecutor");
6const utilities_1 = require("./utilities");
7/**
8 * Represents the type of an analytics link.
9 *
10 * @category Analytics
11 */
12var AnalyticsLinkType;
13(function (AnalyticsLinkType) {
14 /**
15 * Indicates that the link is for S3.
16 */
17 AnalyticsLinkType["S3External"] = "s3";
18 /**
19 * Indicates that the link is for Azure.
20 */
21 AnalyticsLinkType["AzureBlobExternal"] = "azureblob";
22 /**
23 * Indicates that the link is for a remote Couchbase cluster.
24 */
25 AnalyticsLinkType["CouchbaseRemote"] = "couchbase";
26})(AnalyticsLinkType || (exports.AnalyticsLinkType = AnalyticsLinkType = {}));
27/**
28 * Represents what level of encryption to use for analytics remote links.
29 *
30 * @category Analytics
31 */
32var AnalyticsEncryptionLevel;
33(function (AnalyticsEncryptionLevel) {
34 /**
35 * Indicates that no encryption should be used.
36 */
37 AnalyticsEncryptionLevel["None"] = "none";
38 /**
39 * Indicates that half encryption should be used.
40 */
41 AnalyticsEncryptionLevel["Half"] = "half";
42 /**
43 * Indicates that full encryption should be used.
44 */
45 AnalyticsEncryptionLevel["Full"] = "full";
46})(AnalyticsEncryptionLevel || (exports.AnalyticsEncryptionLevel = AnalyticsEncryptionLevel = {}));
47/**
48 * Contains a specific dataset configuration for the analytics service.
49 *
50 * @category Management
51 */
52class AnalyticsDataset {
53 /**
54 * @internal
55 */
56 constructor(data) {
57 this.name = data.name;
58 this.dataverseName = data.dataverseName;
59 this.linkName = data.linkName;
60 this.bucketName = data.bucketName;
61 }
62}
63exports.AnalyticsDataset = AnalyticsDataset;
64/**
65 * Contains a specific index configuration for the analytics service.
66 *
67 * @category Management
68 */
69class AnalyticsIndex {
70 /**
71 * @internal
72 */
73 constructor(data) {
74 this.name = data.name;
75 this.datasetName = data.datasetName;
76 this.dataverseName = data.dataverseName;
77 this.isPrimary = data.isPrimary;
78 }
79}
80exports.AnalyticsIndex = AnalyticsIndex;
81/**
82 * Includes information about an analytics remote links encryption.
83 */
84class CouchbaseAnalyticsEncryptionSettings {
85 /**
86 * @internal
87 */
88 constructor(data) {
89 this.encryptionLevel = data.encryptionLevel;
90 this.certificate = data.certificate;
91 this.clientCertificate = data.clientCertificate;
92 this.clientKey = data.clientKey;
93 }
94}
95exports.CouchbaseAnalyticsEncryptionSettings = CouchbaseAnalyticsEncryptionSettings;
96/**
97 * This is a base class for specific link configurations for the analytics service.
98 */
99class AnalyticsLink {
100 /**
101 * @internal
102 */
103 constructor() {
104 this.linkType = '';
105 }
106 /**
107 * @internal
108 */
109 static _toHttpData(data) {
110 if (data.linkType === AnalyticsLinkType.CouchbaseRemote) {
111 return CouchbaseRemoteAnalyticsLink._toHttpData(data);
112 }
113 else if (data.linkType === AnalyticsLinkType.S3External) {
114 return S3ExternalAnalyticsLink._toHttpData(data);
115 }
116 else if (data.linkType === AnalyticsLinkType.AzureBlobExternal) {
117 return AzureExternalAnalyticsLink._toHttpData(data);
118 }
119 else {
120 throw new Error('invalid link type');
121 }
122 }
123 /**
124 * @internal
125 */
126 static _fromHttpData(data) {
127 if (data.type === 'couchbase') {
128 return CouchbaseRemoteAnalyticsLink._fromHttpData(data);
129 }
130 else if (data.type === 's3') {
131 return S3ExternalAnalyticsLink._fromHttpData(data);
132 }
133 else if (data.type === 'azure') {
134 return AzureExternalAnalyticsLink._fromHttpData(data);
135 }
136 else {
137 throw new Error('invalid link type');
138 }
139 }
140}
141exports.AnalyticsLink = AnalyticsLink;
142/**
143 * Provides information about a analytics remote Couchbase link.
144 */
145class CouchbaseRemoteAnalyticsLink extends AnalyticsLink {
146 /**
147 * @internal
148 */
149 constructor(data) {
150 super();
151 this.linkType = AnalyticsLinkType.CouchbaseRemote;
152 this.dataverse = data.dataverse;
153 this.name = data.name;
154 this.hostname = data.hostname;
155 this.encryption = data.encryption;
156 this.username = data.username;
157 this.password = data.password;
158 }
159 /**
160 * @internal
161 */
162 static _toHttpData(data) {
163 let dvSpecific;
164 if (data.dataverse.indexOf('/') !== -1) {
165 const encDataverse = encodeURIComponent(data.dataverse);
166 const encName = encodeURIComponent(data.name);
167 dvSpecific = {
168 httpPath: `/analytics/link/${encDataverse}/${encName}`,
169 };
170 }
171 else {
172 dvSpecific = {
173 httpPath: `/analytics/link`,
174 dataverse: data.dataverse,
175 name: data.name,
176 };
177 }
178 return {
179 type: 'couchbase',
180 ...dvSpecific,
181 hostname: data.hostname,
182 username: data.username,
183 password: data.password,
184 encryption: data.encryption ? data.encryption.encryptionLevel : undefined,
185 certificate: data.encryption
186 ? data.encryption.certificate
187 ? data.encryption.certificate.toString()
188 : undefined
189 : undefined,
190 clientCertificate: data.encryption
191 ? data.encryption.clientCertificate
192 ? data.encryption.clientCertificate.toString()
193 : undefined
194 : undefined,
195 clientKey: data.encryption
196 ? data.encryption.clientKey
197 ? data.encryption.clientKey.toString()
198 : undefined
199 : undefined,
200 };
201 }
202 /**
203 * @internal
204 */
205 static _fromHttpData(data) {
206 return new CouchbaseRemoteAnalyticsLink({
207 linkType: AnalyticsLinkType.CouchbaseRemote,
208 dataverse: data.dataverse || data.scope,
209 name: data.name,
210 hostname: data.activeHostname,
211 encryption: new CouchbaseAnalyticsEncryptionSettings({
212 encryptionLevel: data.encryption,
213 certificate: Buffer.from(data.certificate),
214 clientCertificate: Buffer.from(data.clientCertificate),
215 clientKey: Buffer.from(data.clientKey),
216 }),
217 username: data.username,
218 password: undefined,
219 });
220 }
221}
222exports.CouchbaseRemoteAnalyticsLink = CouchbaseRemoteAnalyticsLink;
223/**
224 * Provides information about a analytics remote S3 link.
225 */
226class S3ExternalAnalyticsLink extends AnalyticsLink {
227 /**
228 * @internal
229 */
230 constructor(data) {
231 super();
232 this.linkType = AnalyticsLinkType.S3External;
233 this.dataverse = data.dataverse;
234 this.name = data.name;
235 this.accessKeyId = data.accessKeyId;
236 this.secretAccessKey = data.secretAccessKey;
237 this.sessionToken = data.sessionToken;
238 this.region = data.region;
239 this.serviceEndpoint = data.serviceEndpoint;
240 }
241 /**
242 * @internal
243 */
244 static _toHttpData(data) {
245 let dvSpecific;
246 if (data.dataverse.indexOf('/') !== -1) {
247 const encDataverse = encodeURIComponent(data.dataverse);
248 const encName = encodeURIComponent(data.name);
249 dvSpecific = {
250 httpPath: `/analytics/link/${encDataverse}/${encName}`,
251 };
252 }
253 else {
254 dvSpecific = {
255 httpPath: `/analytics/link`,
256 dataverse: data.dataverse,
257 name: data.name,
258 };
259 }
260 return {
261 type: 's3',
262 ...dvSpecific,
263 accessKeyId: data.accessKeyId,
264 secretAccessKey: data.secretAccessKey,
265 region: data.region,
266 sessionToken: data.sessionToken,
267 serviceEndpoint: data.serviceEndpoint,
268 };
269 }
270 /**
271 * @internal
272 */
273 static _fromHttpData(data) {
274 return new S3ExternalAnalyticsLink({
275 linkType: AnalyticsLinkType.S3External,
276 dataverse: data.dataverse || data.scope,
277 name: data.name,
278 accessKeyId: data.accessKeyId,
279 secretAccessKey: undefined,
280 region: data.region,
281 sessionToken: undefined,
282 serviceEndpoint: data.serviceEndpoint,
283 });
284 }
285}
286exports.S3ExternalAnalyticsLink = S3ExternalAnalyticsLink;
287/**
288 * Provides information about a analytics remote S3 link.
289 */
290class AzureExternalAnalyticsLink extends AnalyticsLink {
291 /**
292 * @internal
293 */
294 constructor(data) {
295 super();
296 this.linkType = AnalyticsLinkType.AzureBlobExternal;
297 this.dataverse = data.dataverse;
298 this.name = data.name;
299 this.connectionString = data.connectionString;
300 this.accountName = data.accountName;
301 this.accountKey = data.accountKey;
302 this.sharedAccessSignature = data.sharedAccessSignature;
303 this.blobEndpoint = data.blobEndpoint;
304 this.endpointSuffix = data.endpointSuffix;
305 }
306 /**
307 * @internal
308 */
309 static _toHttpData(data) {
310 let dvSpecific;
311 if (data.dataverse.indexOf('/') !== -1) {
312 const encDataverse = encodeURIComponent(data.dataverse);
313 const encName = encodeURIComponent(data.name);
314 dvSpecific = {
315 httpPath: `/analytics/link/${encDataverse}/${encName}`,
316 };
317 }
318 else {
319 dvSpecific = {
320 httpPath: `/analytics/link`,
321 dataverse: data.dataverse,
322 name: data.name,
323 };
324 }
325 return {
326 type: 'azure',
327 ...dvSpecific,
328 connectionString: data.connectionString,
329 accountName: data.accountName,
330 accountKey: data.accountKey,
331 sharedAccessSignature: data.sharedAccessSignature,
332 blobEndpoint: data.blobEndpoint,
333 endpointSuffix: data.endpointSuffix,
334 };
335 }
336 /**
337 * @internal
338 */
339 static _fromHttpData(data) {
340 return new AzureExternalAnalyticsLink({
341 linkType: AnalyticsLinkType.AzureBlobExternal,
342 dataverse: data.dataverse || data.scope,
343 name: data.name,
344 connectionString: undefined,
345 accountName: data.accountName,
346 accountKey: undefined,
347 sharedAccessSignature: undefined,
348 blobEndpoint: data.blobEndpoint,
349 endpointSuffix: data.endpointSuffix,
350 });
351 }
352}
353exports.AzureExternalAnalyticsLink = AzureExternalAnalyticsLink;
354/**
355 * AnalyticsIndexManager provides an interface for performing management
356 * operations against the analytics service of the cluster.
357 *
358 * @category Management
359 */
360class AnalyticsIndexManager {
361 /**
362 * @internal
363 */
364 constructor(cluster) {
365 this._cluster = cluster;
366 }
367 get _http() {
368 return new httpexecutor_1.HttpExecutor(this._cluster.conn);
369 }
370 /**
371 * Creates a new dataverse.
372 *
373 * @param dataverseName The name of the dataverse to create.
374 * @param options Optional parameters for this operation.
375 * @param callback A node-style callback to be invoked after execution.
376 */
377 async createDataverse(dataverseName, options, callback) {
378 if (options instanceof Function) {
379 callback = arguments[1];
380 options = undefined;
381 }
382 if (!options) {
383 options = {};
384 }
385 let qs = '';
386 qs += 'CREATE DATAVERSE';
387 qs += ' `' + dataverseName.split('/').join('`.`') + '`';
388 if (options.ignoreIfExists) {
389 qs += ' IF NOT EXISTS';
390 }
391 const timeout = options.timeout || this._cluster.managementTimeout;
392 return utilities_1.PromiseHelper.wrapAsync(async () => {
393 try {
394 await this._cluster.analyticsQuery(qs, {
395 timeout: timeout,
396 });
397 }
398 catch (err) {
399 if (err instanceof errors_1.DataverseExistsError) {
400 throw err;
401 }
402 throw new errors_1.CouchbaseError('failed to create dataverse', err);
403 }
404 }, callback);
405 }
406 /**
407 * Drops a previously created dataverse.
408 *
409 * @param dataverseName The name of the dataverse to drop.
410 * @param options Optional parameters for this operation.
411 * @param callback A node-style callback to be invoked after execution.
412 */
413 async dropDataverse(dataverseName, options, callback) {
414 if (options instanceof Function) {
415 callback = arguments[1];
416 options = undefined;
417 }
418 if (!options) {
419 options = {};
420 }
421 let qs = '';
422 qs += 'DROP DATAVERSE';
423 qs += ' `' + dataverseName.split('/').join('`.`') + '`';
424 if (options.ignoreIfNotExists) {
425 qs += ' IF EXISTS';
426 }
427 const timeout = options.timeout || this._cluster.managementTimeout;
428 return utilities_1.PromiseHelper.wrapAsync(async () => {
429 try {
430 await this._cluster.analyticsQuery(qs, {
431 timeout: timeout,
432 });
433 }
434 catch (err) {
435 if (err instanceof errors_1.DataverseNotFoundError) {
436 throw err;
437 }
438 throw new errors_1.CouchbaseError('failed to drop dataverse', err);
439 }
440 }, callback);
441 }
442 /**
443 * Creates a new dataset.
444 *
445 * @param bucketName The name of the bucket to create this dataset of.
446 * @param datasetName The name of the new dataset.
447 * @param options Optional parameters for this operation.
448 * @param callback A node-style callback to be invoked after execution.
449 */
450 async createDataset(bucketName, datasetName, options, callback) {
451 if (options instanceof Function) {
452 callback = arguments[2];
453 options = undefined;
454 }
455 if (!options) {
456 options = {};
457 }
458 let qs = '';
459 qs += 'CREATE DATASET';
460 if (options.ignoreIfExists) {
461 qs += ' IF NOT EXISTS';
462 }
463 if (options.dataverseName) {
464 qs +=
465 ' `' +
466 options.dataverseName.split('/').join('`.`') +
467 '`.`' +
468 datasetName +
469 '`';
470 }
471 else {
472 qs += ' `' + datasetName + '`';
473 }
474 qs += ' ON `' + bucketName + '`';
475 if (options.condition) {
476 qs += ' WHERE ' + options.condition;
477 }
478 const timeout = options.timeout || this._cluster.managementTimeout;
479 return utilities_1.PromiseHelper.wrapAsync(async () => {
480 try {
481 await this._cluster.analyticsQuery(qs, {
482 timeout: timeout,
483 });
484 }
485 catch (err) {
486 if (err instanceof errors_1.DatasetExistsError) {
487 throw err;
488 }
489 throw new errors_1.CouchbaseError('failed to create dataset', err);
490 }
491 }, callback);
492 }
493 /**
494 * Drops a previously created dataset.
495 *
496 * @param datasetName The name of the dataset to drop.
497 * @param options Optional parameters for this operation.
498 * @param callback A node-style callback to be invoked after execution.
499 */
500 async dropDataset(datasetName, options, callback) {
501 if (options instanceof Function) {
502 callback = arguments[1];
503 options = undefined;
504 }
505 if (!options) {
506 options = {};
507 }
508 let qs = '';
509 qs += 'DROP DATASET';
510 if (options.dataverseName) {
511 qs +=
512 ' `' +
513 options.dataverseName.split('/').join('`.`') +
514 '`.`' +
515 datasetName +
516 '`';
517 }
518 else {
519 qs += ' `' + datasetName + '`';
520 }
521 if (options.ignoreIfNotExists) {
522 qs += ' IF EXISTS';
523 }
524 const timeout = options.timeout || this._cluster.managementTimeout;
525 return utilities_1.PromiseHelper.wrapAsync(async () => {
526 try {
527 await this._cluster.analyticsQuery(qs, {
528 timeout: timeout,
529 });
530 }
531 catch (err) {
532 if (err instanceof errors_1.DatasetNotFoundError) {
533 throw err;
534 }
535 throw new errors_1.CouchbaseError('failed to drop dataset', err);
536 }
537 }, callback);
538 }
539 /**
540 * Returns a list of all existing datasets.
541 *
542 * @param options Optional parameters for this operation.
543 * @param callback A node-style callback to be invoked after execution.
544 */
545 async getAllDatasets(options, callback) {
546 if (options instanceof Function) {
547 callback = arguments[0];
548 options = undefined;
549 }
550 if (!options) {
551 options = {};
552 }
553 const qs = 'SELECT d.* FROM `Metadata`.`Dataset` d WHERE d.DataverseName <> "Metadata"';
554 const timeout = options.timeout || this._cluster.managementTimeout;
555 return utilities_1.PromiseHelper.wrapAsync(async () => {
556 const res = await this._cluster.analyticsQuery(qs, {
557 timeout: timeout,
558 });
559 const datasets = res.rows.map((row) => new AnalyticsDataset({
560 name: row.DatasetName,
561 dataverseName: row.DataverseName,
562 linkName: row.LinkName,
563 bucketName: row.BucketName,
564 }));
565 return datasets;
566 }, callback);
567 }
568 /**
569 * Creates a new index.
570 *
571 * @param datasetName The name of the dataset to create this index on.
572 * @param indexName The name of index to create.
573 * @param fields A map of fields that the index should contain.
574 * @param options Optional parameters for this operation.
575 * @param callback A node-style callback to be invoked after execution.
576 */
577 async createIndex(datasetName, indexName, fields, options, callback) {
578 if (options instanceof Function) {
579 callback = arguments[3];
580 options = undefined;
581 }
582 if (!options) {
583 options = {};
584 }
585 let qs = '';
586 qs += 'CREATE INDEX';
587 qs += ' `' + indexName + '`';
588 if (options.ignoreIfExists) {
589 qs += ' IF NOT EXISTS';
590 }
591 if (options.dataverseName) {
592 qs +=
593 ' ON `' +
594 options.dataverseName.split('/').join('`.`') +
595 '`.`' +
596 datasetName +
597 '`';
598 }
599 else {
600 qs += ' ON `' + datasetName + '`';
601 }
602 qs += ' (';
603 qs += Object.keys(fields)
604 .map((i) => '`' + i + '`: ' + fields[i])
605 .join(', ');
606 qs += ')';
607 const timeout = options.timeout || this._cluster.managementTimeout;
608 return utilities_1.PromiseHelper.wrapAsync(async () => {
609 await this._cluster.analyticsQuery(qs, {
610 timeout: timeout,
611 });
612 }, callback);
613 }
614 /**
615 * Drops a previously created index.
616 *
617 * @param datasetName The name of the dataset containing the index to drop.
618 * @param indexName The name of the index to drop.
619 * @param options Optional parameters for this operation.
620 * @param callback A node-style callback to be invoked after execution.
621 */
622 async dropIndex(datasetName, indexName, options, callback) {
623 if (options instanceof Function) {
624 callback = arguments[2];
625 options = undefined;
626 }
627 if (!options) {
628 options = {};
629 }
630 let qs = '';
631 qs += 'DROP INDEX';
632 if (options.dataverseName) {
633 qs +=
634 ' `' +
635 options.dataverseName.split('/').join('`.`') +
636 '`.`' +
637 datasetName +
638 '`';
639 }
640 else {
641 qs += ' `' + datasetName + '`';
642 }
643 qs += '.`' + indexName + '`';
644 if (options.ignoreIfNotExists) {
645 qs += ' IF EXISTS';
646 }
647 const timeout = options.timeout || this._cluster.managementTimeout;
648 return utilities_1.PromiseHelper.wrapAsync(async () => {
649 await this._cluster.analyticsQuery(qs, {
650 timeout: timeout,
651 });
652 }, callback);
653 }
654 /**
655 * Returns a list of all existing indexes.
656 *
657 * @param options Optional parameters for this operation.
658 * @param callback A node-style callback to be invoked after execution.
659 */
660 async getAllIndexes(options, callback) {
661 if (options instanceof Function) {
662 callback = arguments[0];
663 options = undefined;
664 }
665 if (!options) {
666 options = {};
667 }
668 const qs = 'SELECT d.* FROM `Metadata`.`Index` d WHERE d.DataverseName <> "Metadata"';
669 const timeout = options.timeout || this._cluster.managementTimeout;
670 return utilities_1.PromiseHelper.wrapAsync(async () => {
671 const res = await this._cluster.analyticsQuery(qs, {
672 timeout: timeout,
673 });
674 const indexes = res.rows.map((row) => new AnalyticsIndex({
675 name: row.IndexName,
676 datasetName: row.DatasetName,
677 dataverseName: row.DataverseName,
678 isPrimary: row.IsPrimary,
679 }));
680 return indexes;
681 }, callback);
682 }
683 /**
684 * Connects a not yet connected link.
685 *
686 * @param linkName The name of the link to connect.
687 * @param options Optional parameters for this operation.
688 * @param callback A node-style callback to be invoked after execution.
689 */
690 async connectLink(linkName, options, callback) {
691 if (options instanceof Function) {
692 callback = arguments[1];
693 options = undefined;
694 }
695 if (!options) {
696 options = {};
697 }
698 const qs = 'CONNECT LINK ' + linkName;
699 const timeout = options.timeout || this._cluster.managementTimeout;
700 return utilities_1.PromiseHelper.wrapAsync(async () => {
701 await this._cluster.analyticsQuery(qs, {
702 timeout: timeout,
703 });
704 }, callback);
705 }
706 /**
707 * Disconnects a previously connected link.
708 *
709 * @param linkName The name of the link to disconnect.
710 * @param options Optional parameters for this operation.
711 * @param callback A node-style callback to be invoked after execution.
712 */
713 async disconnectLink(linkName, options, callback) {
714 if (options instanceof Function) {
715 callback = arguments[1];
716 options = undefined;
717 }
718 if (!options) {
719 options = {};
720 }
721 const qs = 'DISCONNECT LINK ' + linkName;
722 const timeout = options.timeout || this._cluster.managementTimeout;
723 return utilities_1.PromiseHelper.wrapAsync(async () => {
724 await this._cluster.analyticsQuery(qs, {
725 timeout: timeout,
726 });
727 }, callback);
728 }
729 /**
730 * Returns a list of all pending mutations.
731 *
732 * @param options Optional parameters for this operation.
733 * @param callback A node-style callback to be invoked after execution.
734 */
735 async getPendingMutations(options, callback) {
736 if (options instanceof Function) {
737 callback = arguments[0];
738 options = undefined;
739 }
740 if (!options) {
741 options = {};
742 }
743 const timeout = options.timeout || this._cluster.managementTimeout;
744 return utilities_1.PromiseHelper.wrapAsync(async () => {
745 const res = await this._http.request({
746 type: httpexecutor_1.HttpServiceType.Analytics,
747 method: httpexecutor_1.HttpMethod.Get,
748 path: `/analytics/node/agg/stats/remaining`,
749 timeout: timeout,
750 });
751 if (res.statusCode !== 200) {
752 const errCtx = httpexecutor_1.HttpExecutor.errorContextFromResponse(res);
753 throw new errors_1.CouchbaseError('failed to get pending mutations', undefined, errCtx);
754 }
755 return JSON.parse(res.body.toString());
756 }, callback);
757 }
758 /**
759 * Creates a new analytics remote link.
760 *
761 * @param link The settings for the link to create.
762 * @param options Optional parameters for this operation.
763 * @param callback A node-style callback to be invoked after execution.
764 */
765 async createLink(link, options, callback) {
766 if (options instanceof Function) {
767 callback = arguments[1];
768 options = undefined;
769 }
770 if (!options) {
771 options = {};
772 }
773 const timeout = options.timeout || this._cluster.managementTimeout;
774 return utilities_1.PromiseHelper.wrapAsync(async () => {
775 const linkData = AnalyticsLink._toHttpData(link);
776 const res = await this._http.request({
777 type: httpexecutor_1.HttpServiceType.Analytics,
778 method: httpexecutor_1.HttpMethod.Post,
779 path: linkData.httpPath,
780 contentType: 'application/x-www-form-urlencoded',
781 body: (0, utilities_1.cbQsStringify)({
782 linkData,
783 httpPath: undefined,
784 }),
785 timeout: timeout,
786 });
787 if (res.statusCode !== 200) {
788 const errCtx = httpexecutor_1.HttpExecutor.errorContextFromResponse(res);
789 const errText = res.body.toString().toLowerCase();
790 if (errText.includes('24055')) {
791 throw new errors_1.LinkExistsError(undefined, errCtx);
792 }
793 else if (errText.includes('24034')) {
794 throw new errors_1.DataverseNotFoundError(undefined, errCtx);
795 }
796 throw new errors_1.CouchbaseError('failed to create link', undefined, errCtx);
797 }
798 }, callback);
799 }
800 /**
801 * Replaces an existing analytics remote link.
802 *
803 * @param link The settings for the updated link.
804 * @param options Optional parameters for this operation.
805 * @param callback A node-style callback to be invoked after execution.
806 */
807 async replaceLink(link, options, callback) {
808 if (options instanceof Function) {
809 callback = arguments[1];
810 options = undefined;
811 }
812 if (!options) {
813 options = {};
814 }
815 const timeout = options.timeout || this._cluster.managementTimeout;
816 return utilities_1.PromiseHelper.wrapAsync(async () => {
817 const linkData = AnalyticsLink._toHttpData(link);
818 const res = await this._http.request({
819 type: httpexecutor_1.HttpServiceType.Analytics,
820 method: httpexecutor_1.HttpMethod.Put,
821 path: linkData.httpPath,
822 contentType: 'application/x-www-form-urlencoded',
823 body: (0, utilities_1.cbQsStringify)({
824 linkData,
825 httpPath: undefined,
826 }),
827 timeout: timeout,
828 });
829 if (res.statusCode !== 200) {
830 const errCtx = httpexecutor_1.HttpExecutor.errorContextFromResponse(res);
831 const errText = res.body.toString().toLowerCase();
832 if (errText.includes('24055')) {
833 throw new errors_1.LinkExistsError(undefined, errCtx);
834 }
835 else if (errText.includes('24034')) {
836 throw new errors_1.DataverseNotFoundError(undefined, errCtx);
837 }
838 throw new errors_1.CouchbaseError('failed to replace link', undefined, errCtx);
839 }
840 }, callback);
841 }
842 /**
843 * Drops an existing analytics remote link.
844 *
845 * @param linkName The name of the link to drop.
846 * @param dataverseName The dataverse containing the link to drop.
847 * @param options Optional parameters for this operation.
848 * @param callback A node-style callback to be invoked after execution.
849 */
850 async dropLink(linkName, dataverseName, options, callback) {
851 if (options instanceof Function) {
852 callback = arguments[2];
853 options = undefined;
854 }
855 if (!options) {
856 options = {};
857 }
858 const timeout = options.timeout || this._cluster.managementTimeout;
859 return utilities_1.PromiseHelper.wrapAsync(async () => {
860 let httpPath;
861 let httpParams;
862 if (dataverseName.indexOf('/') !== -1) {
863 const encDataverse = encodeURIComponent(dataverseName);
864 const encName = encodeURIComponent(linkName);
865 httpPath = `/analytics/link/${encDataverse}/${encName}`;
866 httpParams = {};
867 }
868 else {
869 httpPath = `/analytics/link`;
870 httpParams = {
871 dataverse: dataverseName,
872 name: linkName,
873 };
874 }
875 const res = await this._http.request({
876 type: httpexecutor_1.HttpServiceType.Analytics,
877 method: httpexecutor_1.HttpMethod.Delete,
878 path: httpPath,
879 contentType: 'application/x-www-form-urlencoded',
880 body: (0, utilities_1.cbQsStringify)(httpParams),
881 timeout: timeout,
882 });
883 if (res.statusCode !== 200) {
884 const errCtx = httpexecutor_1.HttpExecutor.errorContextFromResponse(res);
885 const errText = res.body.toString().toLowerCase();
886 if (errText.includes('24055')) {
887 throw new errors_1.LinkExistsError(undefined, errCtx);
888 }
889 else if (errText.includes('24034')) {
890 throw new errors_1.DataverseNotFoundError(undefined, errCtx);
891 }
892 throw new errors_1.CouchbaseError('failed to delete link', undefined, errCtx);
893 }
894 }, callback);
895 }
896 /**
897 * Returns a list of existing analytics remote links.
898 *
899 * @param options Optional parameters for this operation.
900 * @param callback A node-style callback to be invoked after execution.
901 */
902 async getAllLinks(options, callback) {
903 if (options instanceof Function) {
904 callback = arguments[0];
905 options = undefined;
906 }
907 if (!options) {
908 options = {};
909 }
910 const dataverseName = options.dataverse;
911 const linkName = options.name;
912 const linkType = options.linkType;
913 const timeout = options.timeout || this._cluster.managementTimeout;
914 return utilities_1.PromiseHelper.wrapAsync(async () => {
915 let httpPath;
916 if (dataverseName && dataverseName.indexOf('/') !== -1) {
917 const encDataverse = encodeURIComponent(dataverseName);
918 httpPath = `/analytics/link/${encDataverse}`;
919 if (linkName) {
920 const encName = encodeURIComponent(linkName);
921 httpPath += `/${encName}`;
922 }
923 httpPath += '?';
924 if (linkType) {
925 httpPath += `type=${linkType}&`;
926 }
927 }
928 else {
929 httpPath = `/analytics/link?`;
930 if (dataverseName) {
931 httpPath += `dataverse=${dataverseName}&`;
932 if (linkName) {
933 httpPath += `dataverse=${linkName}&`;
934 }
935 }
936 if (linkType) {
937 httpPath += `type=${linkType}&`;
938 }
939 }
940 const res = await this._http.request({
941 type: httpexecutor_1.HttpServiceType.Analytics,
942 method: httpexecutor_1.HttpMethod.Get,
943 path: httpPath,
944 timeout: timeout,
945 });
946 if (res.statusCode !== 200) {
947 const errCtx = httpexecutor_1.HttpExecutor.errorContextFromResponse(res);
948 const errText = res.body.toString().toLowerCase();
949 if (errText.includes('24034')) {
950 throw new errors_1.DataverseNotFoundError(undefined, errCtx);
951 }
952 throw new errors_1.CouchbaseError('failed to get links', undefined, errCtx);
953 }
954 const linksData = JSON.parse(res.body.toString());
955 const links = linksData.map((linkData) => AnalyticsLink._fromHttpData(linkData));
956 return links;
957 }, callback);
958 }
959}
960exports.AnalyticsIndexManager = AnalyticsIndexManager;