Skip to content

Commit

Permalink
Merge pull request #76 from baoduy/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
baoduy authored Aug 23, 2024
2 parents dcfc96b + abcb8cd commit c60a3d0
Show file tree
Hide file tree
Showing 14 changed files with 209 additions and 207 deletions.
15 changes: 10 additions & 5 deletions src/Builder/AppConfigBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import {
Builder,
IAppConfigBuilder,
} from './types';
import env from '../env';
import { ResourceInfo } from '../types';
import { isPrd, naming } from '../Common';
import * as appConfig from '@pulumi/azure-native/appconfiguration/v20230901preview';
import { addEncryptKey } from '../KeyVault/Helper';
import { addCustomSecret } from '../KeyVault/CustomHelper';
import { addCustomSecret, addCustomSecrets } from '../KeyVault/CustomHelper';
import { AppConfigPrivateLink } from '../VNet';

class AppConfigBuilder
Expand Down Expand Up @@ -122,10 +123,14 @@ class AppConfigBuilder
});

if (keys.value) {
const readPrimaryConnectionStringKey = `${this._instanceName}-read-primary-connection-string`;
const readSecondaryConnectionStringKey = `${this._instanceName}-read-secondary-connection-string`;

keys.value.map((key) => {
const readPrimaryConnectionStringKey = env.DPA_CONN_ENABLE_SECONDARY
? `${this._instanceName}-read-conn-primary`
: `${this._instanceName}-read-conn`;
const readSecondaryConnectionStringKey = `${this._instanceName}-read-conn-secondary`;

keys.value.forEach((key) => {
if (!env.DPA_CONN_ENABLE_SECONDARY && !key.name.includes('Primary'))
return;
//Only Read Connection String here
if (key.readOnly) {
addCustomSecret({
Expand Down
31 changes: 20 additions & 11 deletions src/Builder/RedisCacheBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
RedisCacheBuilderArgs,
RedisCacheSkuBuilder,
} from './types';
import env from '../env';
import { NetworkPropsType, ResourceInfo } from '../types';
import { isPrd, naming } from '../Common';
import * as cache from '@pulumi/azure-native/cache';
Expand Down Expand Up @@ -113,17 +114,25 @@ class RedisCacheBuilder
vaultInfo,
contentType: 'Redis Cache',
dependsOn: this._redisInstance,
items: [
{ name: `${this._instanceName}-host`, value: h },
{
name: `${this._instanceName}-primary-conn`,
value: `${h}:6380,password=${keys.primaryKey},ssl=True,abortConnect=False`,
},
{
name: `${this._instanceName}-secondary-conn`,
value: `${h}:6380,password=${keys.secondaryKey},ssl=True,abortConnect=False`,
},
],
items: env.DPA_CONN_ENABLE_SECONDARY
? [
{ name: `${this._instanceName}-host`, value: h },
{
name: `${this._instanceName}-conn-primary`,
value: `${h}:6380,password=${keys.primaryKey},ssl=True,abortConnect=False`,
},
{
name: `${this._instanceName}-conn-secondary`,
value: `${h}:6380,password=${keys.secondaryKey},ssl=True,abortConnect=False`,
},
]
: [
{ name: `${this._instanceName}-host`, value: h },
{
name: `${this._instanceName}-conn`,
value: `${h}:6380,password=${keys.primaryKey},ssl=True,abortConnect=False`,
},
],
});
});
}
Expand Down
102 changes: 50 additions & 52 deletions src/Builder/ServiceBusBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
import {
IServiceBusBuilder,
IServiceBusSkuBuilder,
ServiceBusBuilderArgs,
ServiceBusOptions,
ServiceBusQueueArgs,
ServiceBusSkuTypes,
ServiceBusSubArgs,
ServiceBusTopicArgs,
Builder,
} from './types';

import * as types from './types';
import env from '../env';
import {
NetworkPropsType,
ResourceInfo,
Expand All @@ -22,7 +12,7 @@ import { addEncryptKey } from '../KeyVault/Helper';
import { addCustomSecrets } from '../KeyVault/CustomHelper';
import { ServiceBusPrivateLink } from '../VNet';

const defaultQueueOptions: ServiceBusQueueArgs = {
const defaultQueueOptions: types.ServiceBusQueueArgs = {
//duplicateDetectionHistoryTimeWindow: 'P10M',
//maxMessageSizeInKilobytes: 1024,
//autoDeleteOnIdle: isPrd ? 'P180D' : 'P90D',
Expand All @@ -36,7 +26,7 @@ const defaultQueueOptions: ServiceBusQueueArgs = {
deadLetteringOnMessageExpiration: true,
};

const defaultTopicOptions: ServiceBusTopicArgs = {
const defaultTopicOptions: types.ServiceBusTopicArgs = {
//duplicateDetectionHistoryTimeWindow: 'P10M',
//maxMessageSizeInKilobytes: 1024,
//autoDeleteOnIdle: isPrd ? 'P180D' : 'P90D',
Expand All @@ -46,7 +36,7 @@ const defaultTopicOptions: ServiceBusTopicArgs = {
enableBatchedOperations: true,
};

const defaultSubOptions: ServiceBusSubArgs = {
const defaultSubOptions: types.ServiceBusSubArgs = {
duplicateDetectionHistoryTimeWindow: 'P10M',
autoDeleteOnIdle: isPrd ? 'P180D' : 'P90D',
defaultMessageTimeToLive: isPrd ? 'P30D' : 'P5D',
Expand All @@ -57,33 +47,33 @@ const defaultSubOptions: ServiceBusSubArgs = {
};

class ServiceBusBuilder
extends Builder<ResourceInfo>
implements IServiceBusBuilder, IServiceBusSkuBuilder
extends types.Builder<ResourceInfo>
implements types.IServiceBusBuilder, types.IServiceBusSkuBuilder
{
private readonly _instanceName: string;
private _sbInstance: bus.Namespace | undefined = undefined;
private _networkInstance: bus.NamespaceNetworkRuleSet | undefined = undefined;

private _sku: ServiceBusSkuTypes = 'Basic';
private _sku: types.ServiceBusSkuTypes = 'Basic';
private _network: NetworkPropsType | undefined = undefined;
private _queues: Record<string, ServiceBusQueueArgs> = {};
private _topics: Record<string, ServiceBusTopicArgs> = {};
private _options: ServiceBusOptions = {};
private _queues: Record<string, types.ServiceBusQueueArgs> = {};
private _topics: Record<string, types.ServiceBusTopicArgs> = {};
private _options: types.ServiceBusOptions = {};

constructor(private args: ServiceBusBuilderArgs) {
constructor(private args: types.ServiceBusBuilderArgs) {
super(args);
this._instanceName = naming.getServiceBusName(args.name);
}

public withSku(sku: ServiceBusSkuTypes): IServiceBusBuilder {
public withSku(sku: types.ServiceBusSkuTypes): types.IServiceBusBuilder {
this._sku = sku;
return this;
}
public withOptions(props: ServiceBusOptions): IServiceBusBuilder {
public withOptions(props: types.ServiceBusOptions): types.IServiceBusBuilder {
this._options = props;
return this;
}
public withNetwork(props: NetworkPropsType): IServiceBusBuilder {
public withNetwork(props: NetworkPropsType): types.IServiceBusBuilder {
if (this._sku !== 'Premium')
throw new Error(
"The network only support for Service Bus with 'Premium' tier.",
Expand All @@ -95,19 +85,19 @@ class ServiceBusBuilder
public withNetworkIf(
condition: boolean,
props: NetworkPropsType,
): IServiceBusBuilder {
): types.IServiceBusBuilder {
if (condition) return this.withNetwork(props);
return this;
}
public withQueues(
props: Record<string, ServiceBusQueueArgs>,
): IServiceBusBuilder {
props: Record<string, types.ServiceBusQueueArgs>,
): types.IServiceBusBuilder {
this._queues = { ...this._queues, ...props };
return this;
}
public withTopics(
props: Record<string, ServiceBusTopicArgs>,
): IServiceBusBuilder {
props: Record<string, types.ServiceBusTopicArgs>,
): types.IServiceBusBuilder {
this._topics = { ...this._topics, ...props };
return this;
}
Expand Down Expand Up @@ -139,7 +129,7 @@ class ServiceBusBuilder
type: this.args.envUIDInfo
? bus.ManagedServiceIdentityType.SystemAssigned_UserAssigned
: bus.ManagedServiceIdentityType.SystemAssigned,
//all uuid must assigned here before use
//all uuid must assign here before use
userAssignedIdentities: this.args.envUIDInfo
? [this.args.envUIDInfo.id]
: undefined,
Expand Down Expand Up @@ -235,7 +225,7 @@ class ServiceBusBuilder
['both', 'send', 'listen'].map((type) =>
this.buildConnectionString({
type,
level: 'topic',
level: 'queue',
name: queueName,
dependsOn: queue,
}),
Expand All @@ -258,7 +248,7 @@ class ServiceBusBuilder
{ dependsOn: this._sbInstance },
);

['both', 'send', 'listen'].map((type) =>
['manage', 'both', 'send', 'listen'].map((type) =>
this.buildConnectionString({
type,
level: 'topic',
Expand All @@ -284,7 +274,7 @@ class ServiceBusBuilder
subs,
}: {
topic: bus.Topic;
subs?: Record<string, ServiceBusSubArgs>;
subs?: Record<string, types.ServiceBusSubArgs>;
} & WithNamedType) {
if (!subs) return;

Expand Down Expand Up @@ -326,20 +316,26 @@ class ServiceBusBuilder
name,
dependsOn,
}: {
type: 'send' | 'listen' | 'both' | string;
type: 'send' | 'listen' | 'both' | 'manage' | string;
level: 'queue' | 'topic' | string;
} & WithDependsOn &
WithNamedType) {
if (this._options?.disableLocalAuth || !this.args.vaultInfo) return;
const authorizationRuleName = `${level}-${name}-${type}`;
const n = `${this._instanceName}-${authorizationRuleName}`;

const permissions =
type == 'both'
? [bus.AccessRights.Send, bus.AccessRights.Listen]
: type === 'send'
? [bus.AccessRights.Send]
: [bus.AccessRights.Listen];
const rights =
type === 'manage'
? [
bus.AccessRights.Manage,
bus.AccessRights.Send,
bus.AccessRights.Listen,
]
: type == 'both'
? [bus.AccessRights.Send, bus.AccessRights.Listen]
: type === 'send'
? [bus.AccessRights.Send]
: [bus.AccessRights.Listen];

const rule =
level === 'topic'
Expand All @@ -350,7 +346,7 @@ class ServiceBusBuilder
authorizationRuleName,
topicName: name,
namespaceName: this._instanceName,
rights: permissions,
rights,
},
{ dependsOn },
)
Expand All @@ -361,7 +357,7 @@ class ServiceBusBuilder
authorizationRuleName,
queueName: name,
namespaceName: this._instanceName,
rights: permissions,
rights,
},
{ dependsOn },
);
Expand All @@ -386,13 +382,15 @@ class ServiceBusBuilder
vaultInfo: this.args.vaultInfo!,
contentType: `ServiceBus ${n}`,
dependsOn: rule,
items: [
{ name: `${n}-primary`, value: keys.primaryConnectionString },
{
name: `${n}-secondary`,
value: keys.secondaryConnectionString,
},
],
items: env.DPA_CONN_ENABLE_SECONDARY
? [
{ name: `${n}-primary`, value: keys.primaryConnectionString },
{
name: `${n}-secondary`,
value: keys.secondaryConnectionString,
},
]
: [{ name: n, value: keys.primaryConnectionString }],
});
});
}
Expand All @@ -411,5 +409,5 @@ class ServiceBusBuilder
}
}

export default (props: ServiceBusBuilderArgs) =>
new ServiceBusBuilder(props) as IServiceBusSkuBuilder;
export default (props: types.ServiceBusBuilderArgs) =>
new ServiceBusBuilder(props) as types.IServiceBusSkuBuilder;
31 changes: 20 additions & 11 deletions src/Builder/SignalRBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
SignalROptionsBuilder,
SignalRSkuBuilderType,
} from './types';
import env from '../env';
import { PrivateLinkPropsType, ResourceInfo } from '../types';
import { naming } from '../Common';
import { Input } from '@pulumi/pulumi';
Expand Down Expand Up @@ -143,17 +144,25 @@ class SignalRBuilder
vaultInfo,
contentType: 'SignalR',
dependsOn: this._signalRInstance,
items: [
{ name: `${this._instanceName}-host`, value: h },
{
name: `${this._instanceName}-primaryConnection`,
value: keys.primaryConnectionString!,
},
{
name: `${this._instanceName}-secondaryConnection`,
value: keys.secondaryConnectionString!,
},
],
items: env.DPA_CONN_ENABLE_SECONDARY
? [
{ name: `${this._instanceName}-host`, value: h },
{
name: `${this._instanceName}-conn-primary`,
value: keys.primaryConnectionString!,
},
{
name: `${this._instanceName}-conn-secondary`,
value: keys.secondaryConnectionString!,
},
]
: [
{ name: `${this._instanceName}-host`, value: h },
{
name: `${this._instanceName}-conn`,
value: keys.primaryConnectionString!,
},
],
});
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Naming.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ConventionProps, ReplacePattern, NamingType } from '../types';
import * as env from '../envHelper';
import env from '../env';
import { currentCountryCode } from './AzureEnv';
import { organization, stack } from './StackEnv';

Expand Down
2 changes: 1 addition & 1 deletion src/KeyVault/CustomHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { VaultSecretResource } from '@drunk-pulumi/azure-providers/VaultSecret';
import { KeyVaultInfo, NamedBasicArgs, NamedWithVaultType } from '../types';
import { getSecret } from '../Common/ConfigHelper';
import { getVaultItemName } from './Helper';
import * as env from '../envHelper';
import env from '../env';

interface Props extends Required<NamedWithVaultType> {
/** The value of the secret. If Value is not provided the secret will be got from config*/
Expand Down
7 changes: 5 additions & 2 deletions src/KeyVault/Helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import { KeyVaultInfo, NamedWithVaultType, WithVaultInfo } from '../types';
import getKeyVaultBase from '@drunk-pulumi/azure-providers/AzBase/KeyVaultBase';
import { VaultKeyResource } from '@drunk-pulumi/azure-providers';
import { stack, removeLeadingAndTrailingDash } from '../Common';
import * as env from '../envHelper';
import env from '../env';

/** Get Vault Secret Name. Remove the stack name and replace all _ with - then lower cases. */
export const getVaultItemName = (name: string, currentStack: string = stack) => {
export const getVaultItemName = (
name: string,
currentStack: string = stack,
) => {
name = name
.replace(new RegExp(currentStack, 'g'), '') // Replace occurrences of "stack" variable with "-"
.replace(/\.|_|\s/g, '-') // Replace ".", "_", and spaces with "-"
Expand Down
Loading

0 comments on commit c60a3d0

Please sign in to comment.