Skip to content

Commit

Permalink
[#175059122] Centralise config read (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
balanza authored Oct 6, 2020
1 parent 77067d9 commit 3918cfd
Show file tree
Hide file tree
Showing 29 changed files with 513 additions and 166 deletions.
6 changes: 3 additions & 3 deletions CreateDevelopmentProfile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ import {
ProfileModel
} from "io-functions-commons/dist/src/models/profile";

import { getRequiredStringEnv } from "io-functions-commons/dist/src/utils/env";
import { secureExpressApp } from "io-functions-commons/dist/src/utils/express";
import { AzureContextTransport } from "io-functions-commons/dist/src/utils/logging";
import { setAppContext } from "io-functions-commons/dist/src/utils/middlewares/context_middleware";

import createAzureFunctionHandler from "io-functions-express/dist/src/createAzureFunctionsHandler";

import { getConfigOrThrow } from "../utils/config";
import { cosmosdbClient } from "../utils/cosmosdb";
import { CreateDevelopmentProfile } from "./handler";

// Setup Express
const app = express();
secureExpressApp(app);

const cosmosDbName = getRequiredStringEnv("COSMOSDB_NAME");
const config = getConfigOrThrow();

const profilesContainer = cosmosdbClient
.database(cosmosDbName)
.database(config.COSMOSDB_NAME)
.container(PROFILE_COLLECTION_NAME);

const profileModel = new ProfileModel(profilesContainer);
Expand Down
6 changes: 3 additions & 3 deletions CreateService/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
ServiceModel
} from "io-functions-commons/dist/src/models/service";

import { getRequiredStringEnv } from "io-functions-commons/dist/src/utils/env";
import { secureExpressApp } from "io-functions-commons/dist/src/utils/express";
import { AzureContextTransport } from "io-functions-commons/dist/src/utils/logging";
import { setAppContext } from "io-functions-commons/dist/src/utils/middlewares/context_middleware";
Expand All @@ -17,12 +16,13 @@ import createAzureFunctionHandler from "io-functions-express/dist/src/createAzur

import { cosmosdbClient } from "../utils/cosmosdb";

import { getConfigOrThrow } from "../utils/config";
import { CreateService } from "./handler";

const cosmosDbName = getRequiredStringEnv("COSMOSDB_NAME");
const config = getConfigOrThrow();

const servicesContainer = cosmosdbClient
.database(cosmosDbName)
.database(config.COSMOSDB_NAME)
.container(SERVICE_COLLECTION_NAME);

const serviceModel = new ServiceModel(servicesContainer);
Expand Down
16 changes: 9 additions & 7 deletions CreateSubscription/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@ import { Context } from "@azure/functions";
import * as express from "express";
import * as winston from "winston";

import { getRequiredStringEnv } from "io-functions-commons/dist/src/utils/env";
import { secureExpressApp } from "io-functions-commons/dist/src/utils/express";
import { AzureContextTransport } from "io-functions-commons/dist/src/utils/logging";
import { setAppContext } from "io-functions-commons/dist/src/utils/middlewares/context_middleware";

import createAzureFunctionHandler from "io-functions-express/dist/src/createAzureFunctionsHandler";

import { getConfigOrThrow } from "../utils/config";
import { CreateSubscription } from "./handler";

const config = getConfigOrThrow();

const servicePrincipalCreds = {
clientId: getRequiredStringEnv("SERVICE_PRINCIPAL_CLIENT_ID"),
secret: getRequiredStringEnv("SERVICE_PRINCIPAL_SECRET"),
tenantId: getRequiredStringEnv("SERVICE_PRINCIPAL_TENANT_ID")
clientId: config.SERVICE_PRINCIPAL_CLIENT_ID,
secret: config.SERVICE_PRINCIPAL_SECRET,
tenantId: config.SERVICE_PRINCIPAL_TENANT_ID
};
const azureApimConfig = {
apim: getRequiredStringEnv("AZURE_APIM"),
apimResourceGroup: getRequiredStringEnv("AZURE_APIM_RESOURCE_GROUP"),
subscriptionId: getRequiredStringEnv("AZURE_SUBSCRIPTION_ID")
apim: config.AZURE_APIM,
apimResourceGroup: config.AZURE_APIM_RESOURCE_GROUP,
subscriptionId: config.AZURE_SUBSCRIPTION_ID
};

// tslint:disable-next-line: no-let
Expand Down
14 changes: 8 additions & 6 deletions CreateUser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,26 @@ import { setAppContext } from "io-functions-commons/dist/src/utils/middlewares/c

import createAzureFunctionHandler from "io-functions-express/dist/src/createAzureFunctionsHandler";

import { getConfigOrThrow } from "../utils/config";
import { CreateUser } from "./handler";

const config = getConfigOrThrow();
const adb2cCreds = {
clientId: getRequiredStringEnv("ADB2C_CLIENT_ID"),
secret: getRequiredStringEnv("ADB2C_CLIENT_KEY"),
tenantId: getRequiredStringEnv("ADB2C_TENANT_ID")
};

const servicePrincipalCreds = {
clientId: getRequiredStringEnv("SERVICE_PRINCIPAL_CLIENT_ID"),
secret: getRequiredStringEnv("SERVICE_PRINCIPAL_SECRET"),
tenantId: getRequiredStringEnv("SERVICE_PRINCIPAL_TENANT_ID")
clientId: config.SERVICE_PRINCIPAL_CLIENT_ID,
secret: config.SERVICE_PRINCIPAL_SECRET,
tenantId: config.SERVICE_PRINCIPAL_TENANT_ID
};

const azureApimConfig = {
apim: getRequiredStringEnv("AZURE_APIM"),
apimResourceGroup: getRequiredStringEnv("AZURE_APIM_RESOURCE_GROUP"),
subscriptionId: getRequiredStringEnv("AZURE_SUBSCRIPTION_ID")
apim: config.AZURE_APIM,
apimResourceGroup: config.AZURE_APIM_RESOURCE_GROUP,
subscriptionId: config.AZURE_SUBSCRIPTION_ID
};

// tslint:disable-next-line: no-let
Expand Down
27 changes: 11 additions & 16 deletions DeleteUserDataActivity/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { getRequiredStringEnv } from "io-functions-commons/dist/src/utils/env";

import { cosmosdbClient } from "../utils/cosmosdb";

import { createDeleteUserDataActivityHandler } from "./handler";
Expand All @@ -10,64 +8,61 @@ import { MESSAGE_STATUS_COLLECTION_NAME } from "io-functions-commons/dist/src/mo
import { NOTIFICATION_COLLECTION_NAME } from "io-functions-commons/dist/src/models/notification";
import { NOTIFICATION_STATUS_COLLECTION_NAME } from "io-functions-commons/dist/src/models/notification_status";
import { PROFILE_COLLECTION_NAME } from "io-functions-commons/dist/src/models/profile";
import { getConfigOrThrow } from "../utils/config";
import { MessageDeletableModel } from "../utils/extensions/models/message";
import { MessageStatusDeletableModel } from "../utils/extensions/models/message_status";
import { NotificationDeletableModel } from "../utils/extensions/models/notification";
import { NotificationStatusDeletableModel } from "../utils/extensions/models/notification_status";
import { ProfileDeletableModel } from "../utils/extensions/models/profile";

const cosmosDbName = getRequiredStringEnv("COSMOSDB_NAME");
const config = getConfigOrThrow();

const messagesContainer = cosmosdbClient
.database(cosmosDbName)
.database(config.COSMOSDB_NAME)
.container(MESSAGE_COLLECTION_NAME);

const messageModel = new MessageDeletableModel(
messagesContainer,
getRequiredStringEnv("MESSAGE_CONTAINER_NAME")
config.MESSAGE_CONTAINER_NAME
);

const messageStatusesContainer = cosmosdbClient
.database(cosmosDbName)
.database(config.COSMOSDB_NAME)
.container(MESSAGE_STATUS_COLLECTION_NAME);

const messageStatusModel = new MessageStatusDeletableModel(
messageStatusesContainer
);

const notificationsContainer = cosmosdbClient
.database(cosmosDbName)
.database(config.COSMOSDB_NAME)
.container(NOTIFICATION_COLLECTION_NAME);

const notificationModel = new NotificationDeletableModel(
notificationsContainer
);

const notificationStatusesContainer = cosmosdbClient
.database(cosmosDbName)
.database(config.COSMOSDB_NAME)
.container(NOTIFICATION_STATUS_COLLECTION_NAME);

const notificationStatusModel = new NotificationStatusDeletableModel(
notificationStatusesContainer
);

const profilesContainer = cosmosdbClient
.database(cosmosDbName)
.database(config.COSMOSDB_NAME)
.container(PROFILE_COLLECTION_NAME);

const profileModel = new ProfileDeletableModel(profilesContainer);

const userDataBackupBlobService = createBlobService(
getRequiredStringEnv("UserDataBackupStorageConnection")
config.UserDataBackupStorageConnection
);

const messageContentBlobService = createBlobService(
getRequiredStringEnv("StorageConnection")
);
const messageContentBlobService = createBlobService(config.StorageConnection);

const userDataBackupContainerName = getRequiredStringEnv(
"USER_DATA_BACKUP_CONTAINER_NAME"
);
const userDataBackupContainerName = config.USER_DATA_BACKUP_CONTAINER_NAME;

const activityFunctionHandler = createDeleteUserDataActivityHandler({
messageContentBlobService,
Expand Down
18 changes: 8 additions & 10 deletions ExtractUserDataActivity/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { getRequiredStringEnv } from "io-functions-commons/dist/src/utils/env";

import { cosmosdbClient } from "../utils/cosmosdb";

import { createExtractUserDataActivityHandler } from "./handler";
Expand All @@ -25,13 +23,15 @@ import {
PROFILE_COLLECTION_NAME,
ProfileModel
} from "io-functions-commons/dist/src/models/profile";
import { getConfigOrThrow } from "../utils/config";

const config = getConfigOrThrow();

const cosmosDbName = getRequiredStringEnv("COSMOSDB_NAME");
const database = cosmosdbClient.database(cosmosDbName);
const database = cosmosdbClient.database(config.COSMOSDB_NAME);

const messageModel = new MessageModel(
database.container(MESSAGE_COLLECTION_NAME),
getRequiredStringEnv("MESSAGE_CONTAINER_NAME")
config.MESSAGE_CONTAINER_NAME
);

const messageStatusModel = new MessageStatusModel(
Expand All @@ -51,14 +51,12 @@ const profileModel = new ProfileModel(
);

const userDataBlobService = createBlobService(
getRequiredStringEnv("UserDataArchiveStorageConnection")
config.UserDataArchiveStorageConnection
);

const messageContentBlobService = createBlobService(
getRequiredStringEnv("StorageConnection")
);
const messageContentBlobService = createBlobService(config.StorageConnection);

const userDataContainerName = getRequiredStringEnv("USER_DATA_CONTAINER_NAME");
const userDataContainerName = config.USER_DATA_CONTAINER_NAME;

const activityFunctionHandler = createExtractUserDataActivityHandler({
messageContentBlobService,
Expand Down
8 changes: 4 additions & 4 deletions GetProfileActivity/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { getRequiredStringEnv } from "io-functions-commons/dist/src/utils/env";

import { cosmosdbClient } from "../utils/cosmosdb";

import {
PROFILE_COLLECTION_NAME,
ProfileModel
} from "io-functions-commons/dist/src/models/profile";
import { getConfigOrThrow } from "../utils/config";
import { createGetProfileActivityHandler } from "./handler";

const cosmosDbName = getRequiredStringEnv("COSMOSDB_NAME");
const database = cosmosdbClient.database(cosmosDbName);
const config = getConfigOrThrow();

const database = cosmosdbClient.database(config.COSMOSDB_NAME);

const profileModel = new ProfileModel(
database.container(PROFILE_COLLECTION_NAME)
Expand Down
6 changes: 3 additions & 3 deletions GetService/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
ServiceModel
} from "io-functions-commons/dist/src/models/service";

import { getRequiredStringEnv } from "io-functions-commons/dist/src/utils/env";
import { secureExpressApp } from "io-functions-commons/dist/src/utils/express";
import { AzureContextTransport } from "io-functions-commons/dist/src/utils/logging";
import { setAppContext } from "io-functions-commons/dist/src/utils/middlewares/context_middleware";
Expand All @@ -17,12 +16,13 @@ import createAzureFunctionHandler from "io-functions-express/dist/src/createAzur

import { cosmosdbClient } from "../utils/cosmosdb";

import { getConfigOrThrow } from "../utils/config";
import { GetService } from "./handler";

const cosmosDbName = getRequiredStringEnv("COSMOSDB_NAME");
const config = getConfigOrThrow();

const servicesContainer = cosmosdbClient
.database(cosmosDbName)
.database(config.COSMOSDB_NAME)
.container(SERVICE_COLLECTION_NAME);

const serviceModel = new ServiceModel(servicesContainer);
Expand Down
6 changes: 3 additions & 3 deletions GetServices/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
ServiceModel
} from "io-functions-commons/dist/src/models/service";

import { getRequiredStringEnv } from "io-functions-commons/dist/src/utils/env";
import { secureExpressApp } from "io-functions-commons/dist/src/utils/express";
import { AzureContextTransport } from "io-functions-commons/dist/src/utils/logging";
import { setAppContext } from "io-functions-commons/dist/src/utils/middlewares/context_middleware";
Expand All @@ -17,12 +16,13 @@ import createAzureFunctionHandler from "io-functions-express/dist/src/createAzur

import { cosmosdbClient } from "../utils/cosmosdb";

import { getConfigOrThrow } from "../utils/config";
import { GetServices } from "./handler";

const cosmosDbName = getRequiredStringEnv("COSMOSDB_NAME");
const config = getConfigOrThrow();

const servicesContainer = cosmosdbClient
.database(cosmosDbName)
.database(config.COSMOSDB_NAME)
.container(SERVICE_COLLECTION_NAME);

const serviceModel = new ServiceModel(servicesContainer);
Expand Down
16 changes: 9 additions & 7 deletions GetSubscriptionKeys/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@ import { Context } from "@azure/functions";
import * as express from "express";
import * as winston from "winston";

import { getRequiredStringEnv } from "io-functions-commons/dist/src/utils/env";
import { secureExpressApp } from "io-functions-commons/dist/src/utils/express";
import { AzureContextTransport } from "io-functions-commons/dist/src/utils/logging";
import { setAppContext } from "io-functions-commons/dist/src/utils/middlewares/context_middleware";

import createAzureFunctionHandler from "io-functions-express/dist/src/createAzureFunctionsHandler";

import { getConfigOrThrow } from "../utils/config";
import { GetSubscriptionKeys } from "./handler";

const config = getConfigOrThrow();

const servicePrincipalCreds = {
clientId: getRequiredStringEnv("SERVICE_PRINCIPAL_CLIENT_ID"),
secret: getRequiredStringEnv("SERVICE_PRINCIPAL_SECRET"),
tenantId: getRequiredStringEnv("SERVICE_PRINCIPAL_TENANT_ID")
clientId: config.SERVICE_PRINCIPAL_CLIENT_ID,
secret: config.SERVICE_PRINCIPAL_SECRET,
tenantId: config.SERVICE_PRINCIPAL_TENANT_ID
};
const azureApimConfig = {
apim: getRequiredStringEnv("AZURE_APIM"),
apimResourceGroup: getRequiredStringEnv("AZURE_APIM_RESOURCE_GROUP"),
subscriptionId: getRequiredStringEnv("AZURE_SUBSCRIPTION_ID")
apim: config.AZURE_APIM,
apimResourceGroup: config.AZURE_APIM_RESOURCE_GROUP,
subscriptionId: config.AZURE_SUBSCRIPTION_ID
};

// tslint:disable-next-line: no-let
Expand Down
15 changes: 9 additions & 6 deletions GetUser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,25 @@ import { setAppContext } from "io-functions-commons/dist/src/utils/middlewares/c

import createAzureFunctionHandler from "io-functions-express/dist/src/createAzureFunctionsHandler";

import { getConfigOrThrow } from "../utils/config";
import { GetUser } from "./handler";

const config = getConfigOrThrow();

const adb2cCreds = {
clientId: getRequiredStringEnv("ADB2C_CLIENT_ID"),
secret: getRequiredStringEnv("ADB2C_CLIENT_KEY"),
tenantId: getRequiredStringEnv("ADB2C_TENANT_ID")
};
const servicePrincipalCreds = {
clientId: getRequiredStringEnv("SERVICE_PRINCIPAL_CLIENT_ID"),
secret: getRequiredStringEnv("SERVICE_PRINCIPAL_SECRET"),
tenantId: getRequiredStringEnv("SERVICE_PRINCIPAL_TENANT_ID")
clientId: config.SERVICE_PRINCIPAL_CLIENT_ID,
secret: config.SERVICE_PRINCIPAL_SECRET,
tenantId: config.SERVICE_PRINCIPAL_TENANT_ID
};
const azureApimConfig = {
apim: getRequiredStringEnv("AZURE_APIM"),
apimResourceGroup: getRequiredStringEnv("AZURE_APIM_RESOURCE_GROUP"),
subscriptionId: getRequiredStringEnv("AZURE_SUBSCRIPTION_ID")
apim: config.AZURE_APIM,
apimResourceGroup: config.AZURE_APIM_RESOURCE_GROUP,
subscriptionId: config.AZURE_SUBSCRIPTION_ID
};

const adb2cTokenAttributeName = getRequiredStringEnv(
Expand Down
8 changes: 4 additions & 4 deletions GetUserDataProcessingActivity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import {
UserDataProcessingModel
} from "io-functions-commons/dist/src/models/user_data_processing";

import { getRequiredStringEnv } from "io-functions-commons/dist/src/utils/env";

import { cosmosdbClient } from "../utils/cosmosdb";

import { getConfigOrThrow } from "../utils/config";
import { createSetUserDataProcessingStatusActivityHandler } from "./handler";

const cosmosDbName = getRequiredStringEnv("COSMOSDB_NAME");
const database = cosmosdbClient.database(cosmosDbName);
const config = getConfigOrThrow();

const database = cosmosdbClient.database(config.COSMOSDB_NAME);

const userDataProcessingModel = new UserDataProcessingModel(
database.container(USER_DATA_PROCESSING_COLLECTION_NAME)
Expand Down
Loading

0 comments on commit 3918cfd

Please sign in to comment.