Skip to content

Commit

Permalink
Merge pull request #36 from baoduy/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
baoduy authored Jul 2, 2024
2 parents 486a54b + 64b8cff commit 3f5de24
Show file tree
Hide file tree
Showing 26 changed files with 432 additions and 348 deletions.
43 changes: 27 additions & 16 deletions src/Apps/LogicApp.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
import * as logic from '@pulumi/azure-native/logic';
import { BasicResourceArgs, DefaultResourceArgs } from '../types';
import creator from '../Core/ResourceCreator';
import { global } from '../Common';
import { getCertOrderName } from '../Common/Naming';
import * as logic from "@pulumi/azure-native/logic";
import { BasicResourceArgs, DefaultResourceArgs } from "../types";
import { getWorkflowName } from "../Common/Naming";

interface Props extends BasicResourceArgs, DefaultResourceArgs {}
export type WorkflowProps = BasicResourceArgs &
DefaultResourceArgs &
Pick<logic.WorkflowArgs, "accessControl">;

export default ({ name, ...others }: Props) => {
const n = getCertOrderName(name);
export default ({
name,
group,
dependsOn,
ignoreChanges,
importUri,
...others
}: WorkflowProps) => {
const n = getWorkflowName(name);

const order = creator(logic.Workflow, {
workflowName: n,
...global.groupInfo,
...others,
sku: '',
const workFlow = new logic.Workflow(
name,
{
workflowName: n,
...group,
...others,
identity: { type: logic.ManagedServiceIdentityType.SystemAssigned },
accessControl: { actions: {} },
},
{ dependsOn, ignoreChanges, import: importUri },
);

} as logic.WorkflowArgs & DefaultResourceArgs);

return order;
return workFlow;
};
23 changes: 13 additions & 10 deletions src/AzAd/RoleAssignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ export const roleAssignment = ({
dependsOn,
}: RoleAssignmentProps) => {
const role = getRoleDefinitionByName({ roleName });
return new native.authorization.RoleAssignment(
`${name}-${roleName.split(" ").join("")}`,
{
principalId,
principalType,
roleDefinitionId: role.id,
scope,
},
{ dependsOn },
);
return pulumi.output(principalId).apply((id) => {
if (!id) return undefined;
return new native.authorization.RoleAssignment(
`${name}-${roleName.split(" ").join("")}`,
{
principalId,
principalType,
roleDefinitionId: role.id,
scope,
},
{ dependsOn },
);
});
};
13 changes: 9 additions & 4 deletions src/Builder/ApimBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,12 +336,17 @@ class ApimBuilder
private buildPrivateLink() {
if (!this._privateLink) return;
PrivateEndpoint({
...this.commonProps,
name: this._instanceName!,
resourceId: this._apimInstance!.id,
resourceInfo: {
resourceName: this._instanceName!,
group: this.commonProps.group,
id: this._apimInstance!.id,
},

privateDnsZoneName: "privatelink.azure-api.net",
subnetIds: this._privateLink.subnetIds,
linkServiceGroupIds: ["Gateway"],
linkServiceGroupIds: this._privateLink.type
? [this._privateLink.type]
: ["Gateway"],
dependsOn: this._apimInstance,
});
}
Expand Down
1 change: 0 additions & 1 deletion src/Builder/ResourceBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ class ResourceBuilder

if (asPrivateLink && subIds.length > 0) {
createVaultPrivateLink({
name: `${this.name}-vault`,
vaultInfo: this._vaultInfo!.info(),
subnetIds: subIds,
});
Expand Down
1 change: 0 additions & 1 deletion src/Builder/VaultBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export class VaultBuilderResults implements IVaultBuilderResults {

public privateLinkTo(subnetIds: Input<string>[]): IVaultBuilderResults {
createVaultPrivateLink({
name: `${this.vaultInfo.name}-vault`,
vaultInfo: this.vaultInfo,
subnetIds,
});
Expand Down
44 changes: 30 additions & 14 deletions src/Builder/VnetBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ResourceInfo } from "../types";
import IpAddressPrefix, {
PublicIpAddressPrefixResult,
} from "../VNet/IpAddressPrefix";
Expand All @@ -12,6 +13,7 @@ import { input as inputs } from "@pulumi/azure-native/types";
import NetworkPeering from "../VNet/NetworkPeering";
import { LogInfoResults } from "../Logs/Helpers";
import VPNGateway from "../VNet/VPNGateway";
import PrivateDnsZoneBuilder from "./PrivateDnsZoneBuilder";
import {
BastionCreationProps,
FirewallCreationProps,
Expand All @@ -27,6 +29,7 @@ import {
VnetBuilderResults,
VpnGatewayCreationProps,
BuilderProps,
VnetPrivateDnsBuilderFunc,
} from "./types";
import { getVnetInfo, parseVnetInfoFromId } from "../VNet/Helper";
import Bastion from "../VNet/Bastion";
Expand All @@ -51,6 +54,8 @@ class VnetBuilder
private _peeringProps: PeeringProps[] = [];
private _logInfo: LogInfoResults | undefined = undefined;
private _ipType: "prefix" | "individual" = "prefix";
private _privateDns: Record<string, VnetPrivateDnsBuilderFunc | undefined> =
{};

/** The Instances */
private _ipAddressInstance: PublicIpAddressPrefixResult | undefined =
Expand All @@ -60,6 +65,7 @@ class VnetBuilder
private _natGatewayInstance: network.NatGateway | undefined = undefined;
private _vnpGatewayInstance: network.VirtualNetworkGateway | undefined =
undefined;
private _privateDnsInstances: Record<string, ResourceInfo> = {};

constructor(commonProps: BuilderProps) {
super(commonProps);
Expand Down Expand Up @@ -122,6 +128,13 @@ class VnetBuilder
this._enableRoute = true;
return this;
}
public withPrivateDns(
domain: string,
builder?: VnetPrivateDnsBuilderFunc,
): IVnetBuilder {
this._privateDns[domain] = builder;
return this;
}

public peeringTo(props: PeeringProps): IVnetBuilder {
this._peeringProps.push(props);
Expand All @@ -134,20 +147,6 @@ class VnetBuilder
}

/** Builders methods */
// private validate() {
// if (this._firewallProps) {
// if (!this._firewallProps.sku)
// this._firewallProps.sku = this._natGatewayEnabled
// ? { tier: "Basic", name: "AZFW_VNet" }
// : { tier: "Basic", name: "AZFW_VNet" };
//
// // if (this._natGatewayEnabled && this._firewallProps.sku.tier === "Basic")
// // throw new Error(
// // 'The Firewall tier "Basic" is not support Nat Gateway.',
// // );
// }
// }

private buildIpAddress() {
const ipNames = [];

Expand Down Expand Up @@ -316,6 +315,22 @@ class VnetBuilder
});
}

private buildPrivateDns() {
Object.keys(this._privateDns).forEach((k) => {
const bFunc = this._privateDns[k];
const builder = PrivateDnsZoneBuilder({
...this.commonProps,
name: k,
}).linkTo({
vnetIds: [this._vnetInstance!.id],
registrationEnabled: false,
});

if (bFunc) bFunc(builder);
this._privateDnsInstances[k] = builder.build();
});
}

private buildPeering() {
if (!this._peeringProps || !this._vnetInstance) return;

Expand Down Expand Up @@ -348,6 +363,7 @@ class VnetBuilder
this.buildFirewall();
this.buildVpnGateway();
this.buildBastion();
this.buildPrivateDns();
this.buildPeering();

return {
Expand Down
5 changes: 2 additions & 3 deletions src/Builder/types/apimBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SkuType } from "@pulumi/azure-native/apimanagement";
import { Input } from "@pulumi/pulumi";
import { IBuilder } from "./genericBuilder";
import { ResourceInfo } from "../../types";
import { PrivateLinkPropsType, ResourceInfo } from "../../types";
import { AppInsightInfo } from "../../Logs/Helpers";

export type ApimSkuBuilderType = {
Expand Down Expand Up @@ -40,8 +40,7 @@ export type ApimVnetType = {
* */
type: "External" | "Internal";
};
export type ApimPrivateLinkType = {
subnetIds: Input<string>[];
export type ApimPrivateLinkType = PrivateLinkPropsType & {
disablePublicAccess?: boolean;
};
export type ApimAuthType = {
Expand Down
11 changes: 10 additions & 1 deletion src/Builder/types/vnetBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { VpnGatewayProps } from "../../VNet/VPNGateway";
import { CustomSecurityRuleArgs, RouteArgs } from "../../VNet/types";
import { LogInfoResults } from "../../Logs/Helpers";
import { PublicIpAddressPrefixResult } from "../../VNet/IpAddressPrefix";
import { IPrivateDnsZoneBuilder } from "./privateDnsZoneBuilder";

//VNet Builder Types
export type VnetBuilderResults = VnetResult & {
Expand Down Expand Up @@ -45,6 +46,10 @@ export type VpnGatewayCreationProps = Pick<
"sku" | "vpnClientAddressPools"
> & { subnetSpace: string };

export type VnetPrivateDnsBuilderFunc = (
builder: IPrivateDnsZoneBuilder,
) => IPrivateDnsZoneBuilder;

//Starting Interface
export interface IVnetBuilderStart {
asHub(props?: VnetBuilderProps): IPublicIpBuilder;
Expand All @@ -64,9 +69,13 @@ export interface IGatewayFireWallBuilder extends IFireWallOrVnetBuilder {

export interface IVnetBuilder extends IBuilder<VnetBuilderResults> {
withBastion(props: BastionCreationProps): IVnetBuilder;
peeringTo(props: PeeringProps): IVnetBuilder;
withPrivateDns(
domain: string,
builder?: VnetPrivateDnsBuilderFunc,
): IVnetBuilder;
withSecurityRules(rules: CustomSecurityRuleArgs[]): IVnetBuilder;
withRouteRules(rules: RouteArgs[]): IVnetBuilder;
withLogInfo(info: LogInfoResults): IVnetBuilder;
withVpnGateway(props: VpnGatewayCreationProps): IVnetBuilder;
peeringTo(props: PeeringProps): IVnetBuilder;
}
3 changes: 1 addition & 2 deletions src/Common/AzureEnv.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import * as pulumi from "@pulumi/pulumi";
import { authorization } from "@pulumi/azure-native";
import { registerAutoTags } from "./AutoTags";
import { KeyVaultInfo, ResourceGroupInfo } from "../types";
import { KeyVaultInfo, ResourceGroupInfo, ResourceInfoArg } from "../types";
import { getKeyVaultName, getResourceGroupName } from "./Naming";
import { ResourceInfoArg } from "./ResourceEnv";
import { organization, projectName, stack } from "./StackEnv";
import { getCountryCode, getRegionCode } from "./Location";

Expand Down
47 changes: 43 additions & 4 deletions src/Common/Helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,61 @@ export function replaceAll(value: string, search: string, replace: string) {
}

export const toBase64 = (value: string) =>
Buffer.from(value).toString('base64');
Buffer.from(value).toString("base64");

export const shallowEquals = (obj1: any, obj2: any) =>
Object.keys(obj1).length === Object.keys(obj2).length &&
Object.keys(obj1).every((key) => obj1[key] === obj2[key]);

/** Get Domain from Url*/
export const getDomainFromUrl = (url: string) =>
url.replace('https://', '').replace('http://', '').split('/')[0];
url.replace("https://", "").replace("http://", "").split("/")[0];

/** Get Root Domain from Url or Sub domain*/
export const getRootDomainFromUrl = (url: string) => {
const array = getDomainFromUrl(url).split('.');
return array.slice(Math.max(array.length - 2, 0)).join('.');
const array = getDomainFromUrl(url).split(".");
return array.slice(Math.max(array.length - 2, 0)).join(".");
};

/** Create Range*/
export const RangeOf = (length: number) =>
Array.from({ length: length }, (v, k) => k);

/**
* Simple object check.
* @param item
* @returns {boolean}
*/
export function isObject(item: any): boolean {
return item !== null && typeof item === "object" && !Array.isArray(item);
}

/**
* Deep merge two or more objects.
* @param target
* @param sources
* @returns {T}
*/
export function mergeDeep<T>(target: T, ...sources: any[]): T {
if (!sources.length) return target;

for (const source of sources) {
if (isObject(source)) {
for (const key of Object.keys(source)) {
const sourceValue = source[key];
const targetValue = (target as any)[key];

if (isObject(sourceValue)) {
if (!targetValue) {
(target as any)[key] = {};
}
mergeDeep((target as any)[key], sourceValue);
} else {
(target as any)[key] = sourceValue;
}
}
}
}

return target;
}
17 changes: 12 additions & 5 deletions src/Common/Naming/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import { getResourceName } from "../ResourceEnv";
import { organization, stack } from "../StackEnv";

/** The method to get Resource group Name*/
export const getResourceGroupName = (name: string): string =>
export const getResourceGroupName = (
name: string,
convention: ConventionProps = {},
): string =>
getResourceName(name, {
...convention,
suffix: organization ? `grp-${organization}` : "grp",
});

Expand All @@ -19,8 +23,8 @@ export const getStorageName = (name: string): string => {

/** Get Vault Secret Name. Remove the stack name and replace all _ with - then lower cases. */
export const getSecretName = (name: string) => {
name = name.replace(`${stack}-`, "");
name = name.replace(stack, "");
name = replaceAll(name, `${stack}-`, "");
name = replaceAll(name, stack, "");
name = replaceAll(name, " ", "-");
name = replaceAll(name, ".", "-");
return replaceAll(name, "_", "-").toLowerCase();
Expand Down Expand Up @@ -142,8 +146,8 @@ export const getNICName = (name: string) =>
export const getVpnName = (name: string) =>
getResourceName(name, { suffix: "vpn" });

export const getVnetName = (name: string) =>
getResourceName(name, { suffix: "vnt" });
export const getVnetName = (name: string, convention: ConventionProps = {}) =>
getResourceName(name, { ...convention, suffix: "vnt" });

export const getWanName = (name: string) =>
getResourceName(name, { suffix: "wan" });
Expand All @@ -160,6 +164,9 @@ export const getRouteName = (name: string) =>
export const getRouteItemName = (name: string) =>
getResourceName(name, { suffix: "", includeOrgName: false });

export const getWorkflowName = (name: string) =>
getResourceName(name, { suffix: "wkp" });

export const getNetworkSecurityGroupName = (name: string) =>
getResourceName(name, { suffix: "nsg" });

Expand Down
Loading

0 comments on commit 3f5de24

Please sign in to comment.