Skip to content

Commit

Permalink
clean up and updating docs
Browse files Browse the repository at this point in the history
  • Loading branch information
brett-onions committed Dec 10, 2024
1 parent ac92d06 commit 96fef7e
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 42 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,27 @@ MINIO_SECRETE_KEY:
``` bash
MINIO_BUCKET=climate-mediator,
```

## Creating Buckets

### Validation Rules for Creating a Bucket

When creating a bucket the name must follow these following rules:
> Bucket names must be between 3 (min) and 63 (max) characters long.
> Bucket names can consist only of lowercase letters, numbers, dots (.), and hyphens (-).
> Bucket names must not start with the prefix xn--.
> Bucket names must not end with the suffix -s3alias. This suffix is reserved for access point alias names.
### Enabling Automatic Bucket Creation Through API

To allow automatic creation of the bucket if it does not exist, include the createBucketIfNotExists query parameter and set it to true. This will ensure the bucket is created with the specified name if it is not already present.

```bash
/upload?bucket=:name&createBucketIfNotExists=true
```

This optional parameter simplifies the process by eliminating the need to manually create buckets beforehand.

### Enabling Automatic Bucket Creation Through OpenHIM Console

Navigate to `/mediators/urn:mediator:climate-mediator` and click the gear icon, next click the green button that states `Minio Buckets Registry` and add the bucket name and region to the two form elements that appear. Finally click `Save Changes`, the newly entered buckets should appear withing minio momentary.
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getConfig } from './config/config';
import logger from './logger';
import routes from './routes/index';
import { getMediatorConfig, initializeBuckets, setupMediator } from './openhim/openhim';
import { MinioBucketsRegistry } from './types/mediatorConfig';

const app = express();

Expand All @@ -16,9 +17,11 @@ app.listen(getConfig().port, async () => {

const mediatorConfig = await getMediatorConfig();
if (mediatorConfig) {
await initializeBuckets(mediatorConfig);
await initializeBuckets(
mediatorConfig.config?.minio_buckets_registry as MinioBucketsRegistry[]
);
} else {
logger.error('Failed to fetch mediator config');
logger.warn('Failed to fetch mediator config, skipping bucket initialization');
}
} else {
logger.info('Running in testing mode, skipping mediator setup');
Expand Down
2 changes: 1 addition & 1 deletion src/openhim/mediatorConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
{
"param": "minio_buckets_registry",
"displayName": "Minio Buckets Registry",
"description": "The available Minio buckets and their configurations",
"description": "The available Minio buckets and their configurations (Note: The names provided must be between 3 and 63 characters long, and can only contain lowercase letters, numbers, dots (.), and hyphens (-))",
"type": "struct",
"array": true,
"template": [
Expand Down
46 changes: 9 additions & 37 deletions src/openhim/openhim.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logger from '../logger';
import { MediatorConfig, MinioBucketsRegistry } from '../types/mediatorConfig';
import { RequestOptions } from '../types/request';
import { getConfig } from '../config/config';
import { Config, getConfig } from '../config/config';
import axios, { AxiosError } from 'axios';
import https from 'https';
import { activateHeartbeat, fetchConfig, registerMediator } from 'openhim-mediator-utils';
Expand Down Expand Up @@ -65,18 +65,7 @@ export const setupMediator = async () => {

emitter.on('config', async (config: any) => {
logger.debug('Received new configs from OpenHIM');
const mediatorConfig = {
config: {
minio_buckets_registry: config.minio_buckets_registry,
},
defaultChannelConfig: [],
endpoints: [],
urn: config.urn,
version: config.version,
name: config.name,
description: config.description,
};
await initializeBuckets(mediatorConfig);
await initializeBuckets(config.minio_buckets_registry);
});
});
});
Expand All @@ -93,12 +82,16 @@ export const setupMediator = async () => {
*
* @param mediatorConfig - The mediator config
*/
export async function initializeBuckets(mediatorConfig: MediatorConfig) {
const bucketsFromOpenhimConfig = mediatorConfig.config?.minio_buckets_registry as Bucket[];
export async function initializeBuckets(buckets: MinioBucketsRegistry[]) {
if (!buckets) {
logger.error('No buckets found in mediator config');
return;
}

const validBuckets: string[] = [];
const invalidBuckets: string[] = [];

for await (const { bucket, region } of bucketsFromOpenhimConfig) {
for await (const { bucket, region } of buckets) {
if (!validateBucketName(bucket)) {
logger.error(`Invalid bucket name ${bucket}, skipping`);
invalidBuckets.push(bucket);
Expand Down Expand Up @@ -188,27 +181,6 @@ async function putMediatorConfig(mediatorUrn: string, mediatorConfig: MinioBucke
}
}

export async function getRegisteredBuckets(): Promise<Bucket[]> {
if (runningMode === 'testing') {
logger.info('Running in testing mode, reading buckets from ENV');
const buckets = getConfig().minio.buckets.split(',');
return buckets.map((bucket) => ({ bucket, region: '' }));
}

logger.info('Fetching registered buckets from OpenHIM');
const mediatorConfig = await getMediatorConfig();

if (!mediatorConfig) {
return [];
}

if (mediatorConfig) {
await initializeBuckets(mediatorConfig);
return mediatorConfig.config?.minio_buckets_registry as Bucket[];
}
return [];
}

export async function registerBucket(bucket: string, region?: string) {
// If we are in testing mode, we don't need to have the registered buckets persisted
if (runningMode === 'testing') {
Expand Down
4 changes: 3 additions & 1 deletion src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ routes.post('/upload', upload.single('file'), async (req, res) => {
? await handleCsvFile(file, bucket, region)
: handleJsonFile(file);

createBucketIfNotExists && (await registerBucket(bucket, region));
if (createBucketIfNotExists && getConfig().runningMode !== 'testing') {
await registerBucket(bucket, region);
}

const statusCode = response.status === 'success' ? 201 : 400;
return res.status(statusCode).json(response);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/minioClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export async function uploadToMinio(
export async function createMinioBucketListeners(listOfBuckets: string[]) {
for (const bucket of listOfBuckets) {
if (registeredBuckets.has(bucket)) {
logger.info(`Bucket ${bucket} already registered`);
logger.debug(`Bucket ${bucket} already registered`);
continue;
}

Expand Down

0 comments on commit 96fef7e

Please sign in to comment.