@twin.org/blob-storage-connector-gcp 0.0.1-next.10 → 0.0.1-next.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,6 +3,7 @@
3
3
  var storage = require('@google-cloud/storage');
4
4
  var core = require('@twin.org/core');
5
5
  var crypto = require('@twin.org/crypto');
6
+ var loggingModels = require('@twin.org/logging-models');
6
7
  var web = require('@twin.org/web');
7
8
 
8
9
  // Copyright 2024 IOTA Stiftung.
@@ -52,6 +53,60 @@ class GcpBlobStorageConnector {
52
53
  credentials
53
54
  });
54
55
  }
56
+ /**
57
+ * Bootstrap the component by creating and initializing any resources it needs.
58
+ * @param nodeLoggingConnectorType The node logging connector type, defaults to "node-logging".
59
+ * @returns True if the bootstrapping process was successful.
60
+ */
61
+ async bootstrap(nodeLoggingConnectorType) {
62
+ const nodeLogging = loggingModels.LoggingConnectorFactory.getIfExists(nodeLoggingConnectorType ?? "node-logging");
63
+ try {
64
+ await nodeLogging?.log({
65
+ level: "info",
66
+ source: this.CLASS_NAME,
67
+ message: "bucketCreating",
68
+ data: {
69
+ bucket: this._config.bucketName
70
+ }
71
+ });
72
+ const [buckets] = await this._storage.getBuckets();
73
+ const bucketExists = buckets.some(bucket => bucket.name === this._config.bucketName);
74
+ if (bucketExists) {
75
+ await nodeLogging?.log({
76
+ level: "info",
77
+ source: this.CLASS_NAME,
78
+ message: "bucketExists",
79
+ data: {
80
+ bucket: this._config.bucketName
81
+ }
82
+ });
83
+ }
84
+ else {
85
+ await this._storage.createBucket(this._config.bucketName);
86
+ await nodeLogging?.log({
87
+ level: "info",
88
+ source: this.CLASS_NAME,
89
+ message: "bucketCreated",
90
+ data: {
91
+ bucket: this._config.bucketName
92
+ }
93
+ });
94
+ }
95
+ }
96
+ catch (err) {
97
+ await nodeLogging?.log({
98
+ level: "error",
99
+ source: this.CLASS_NAME,
100
+ message: "bucketCreateFailed",
101
+ data: {
102
+ bucket: this._config.bucketName
103
+ },
104
+ error: core.BaseError.fromError(err)
105
+ });
106
+ return false;
107
+ }
108
+ return true;
109
+ }
55
110
  /**
56
111
  * Set the blob.
57
112
  * @param blob The data for the blob.
@@ -1,6 +1,7 @@
1
1
  import { Storage } from '@google-cloud/storage';
2
- import { Guards, Is, ObjectHelper, Converter, Urn, GeneralError } from '@twin.org/core';
2
+ import { Guards, Is, ObjectHelper, Converter, BaseError, Urn, GeneralError } from '@twin.org/core';
3
3
  import { Sha256 } from '@twin.org/crypto';
4
+ import { LoggingConnectorFactory } from '@twin.org/logging-models';
4
5
  import { MimeTypes } from '@twin.org/web';
5
6
 
6
7
  // Copyright 2024 IOTA Stiftung.
@@ -50,6 +51,60 @@ class GcpBlobStorageConnector {
50
51
  credentials
51
52
  });
52
53
  }
54
+ /**
55
+ * Bootstrap the component by creating and initializing any resources it needs.
56
+ * @param nodeLoggingConnectorType The node logging connector type, defaults to "node-logging".
57
+ * @returns True if the bootstrapping process was successful.
58
+ */
59
+ async bootstrap(nodeLoggingConnectorType) {
60
+ const nodeLogging = LoggingConnectorFactory.getIfExists(nodeLoggingConnectorType ?? "node-logging");
61
+ try {
62
+ await nodeLogging?.log({
63
+ level: "info",
64
+ source: this.CLASS_NAME,
65
+ message: "bucketCreating",
66
+ data: {
67
+ bucket: this._config.bucketName
68
+ }
69
+ });
70
+ const [buckets] = await this._storage.getBuckets();
71
+ const bucketExists = buckets.some(bucket => bucket.name === this._config.bucketName);
72
+ if (bucketExists) {
73
+ await nodeLogging?.log({
74
+ level: "info",
75
+ source: this.CLASS_NAME,
76
+ message: "bucketExists",
77
+ data: {
78
+ bucket: this._config.bucketName
79
+ }
80
+ });
81
+ }
82
+ else {
83
+ await this._storage.createBucket(this._config.bucketName);
84
+ await nodeLogging?.log({
85
+ level: "info",
86
+ source: this.CLASS_NAME,
87
+ message: "bucketCreated",
88
+ data: {
89
+ bucket: this._config.bucketName
90
+ }
91
+ });
92
+ }
93
+ }
94
+ catch (err) {
95
+ await nodeLogging?.log({
96
+ level: "error",
97
+ source: this.CLASS_NAME,
98
+ message: "bucketCreateFailed",
99
+ data: {
100
+ bucket: this._config.bucketName
101
+ },
102
+ error: BaseError.fromError(err)
103
+ });
104
+ return false;
105
+ }
106
+ return true;
107
+ }
53
108
  /**
54
109
  * Set the blob.
55
110
  * @param blob The data for the blob.
@@ -21,6 +21,12 @@ export declare class GcpBlobStorageConnector implements IBlobStorageConnector {
21
21
  constructor(options: {
22
22
  config: IGcpBlobStorageConnectorConfig;
23
23
  });
24
+ /**
25
+ * Bootstrap the component by creating and initializing any resources it needs.
26
+ * @param nodeLoggingConnectorType The node logging connector type, defaults to "node-logging".
27
+ * @returns True if the bootstrapping process was successful.
28
+ */
29
+ bootstrap(nodeLoggingConnectorType?: string): Promise<boolean>;
24
30
  /**
25
31
  * Set the blob.
26
32
  * @param blob The data for the blob.
package/docs/changelog.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # @twin.org/blob-storage-connector-gcp - Changelog
2
2
 
3
- ## v0.0.1-next.10
3
+ ## v0.0.1-next.12
4
4
 
5
5
  - Initial Release
@@ -51,6 +51,30 @@ Runtime name for the class.
51
51
 
52
52
  ## Methods
53
53
 
54
+ ### bootstrap()
55
+
56
+ > **bootstrap**(`nodeLoggingConnectorType`?): `Promise`\<`boolean`\>
57
+
58
+ Bootstrap the component by creating and initializing any resources it needs.
59
+
60
+ #### Parameters
61
+
62
+ • **nodeLoggingConnectorType?**: `string`
63
+
64
+ The node logging connector type, defaults to "node-logging".
65
+
66
+ #### Returns
67
+
68
+ `Promise`\<`boolean`\>
69
+
70
+ True if the bootstrapping process was successful.
71
+
72
+ #### Implementation of
73
+
74
+ `IBlobStorageConnector.bootstrap`
75
+
76
+ ***
77
+
54
78
  ### set()
55
79
 
56
80
  > **set**(`blob`): `Promise`\<`string`\>
package/locales/en.json CHANGED
@@ -1,6 +1,14 @@
1
1
  {
2
+ "info": {
3
+ "gcpBlobStorageConnector": {
4
+ "bucketCreating": "Creating bucket \"{bucket}\"",
5
+ "bucketCreated": "Created bucket \"{bucket}\"",
6
+ "bucketExists": "Skipping create bucket \"{bucket}\" as it already exists"
7
+ }
8
+ },
2
9
  "error": {
3
10
  "gcpBlobStorageConnector": {
11
+ "bucketCreateFailed": "Creating bucket \"{bucket}\" failed",
4
12
  "namespaceMismatch": "The namespace in the urn \"{id}\" does not match the namespace of the blob storage \"{namespace}\"",
5
13
  "getBlobFailed": "Failed to get blob in GCP",
6
14
  "setBlobFailed": "Failed to set blob in GCP",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/blob-storage-connector-gcp",
3
- "version": "0.0.1-next.10",
3
+ "version": "0.0.1-next.12",
4
4
  "description": "Blob Storage connector implementation using Google Cloud Storage",
5
5
  "repository": {
6
6
  "type": "git",
@@ -15,9 +15,10 @@
15
15
  },
16
16
  "dependencies": {
17
17
  "@google-cloud/storage": "^7.13.0",
18
- "@twin.org/blob-storage-models": "0.0.1-next.10",
18
+ "@twin.org/blob-storage-models": "0.0.1-next.12",
19
19
  "@twin.org/core": "next",
20
20
  "@twin.org/crypto": "next",
21
+ "@twin.org/logging-models": "next",
21
22
  "@twin.org/nameof": "next",
22
23
  "@twin.org/web": "next"
23
24
  },