Skip to content

Commit

Permalink
Merge pull request #62 from baoduy/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
baoduy authored Aug 9, 2024
2 parents d43eb88 + 8d387e7 commit 69cf992
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/Builder/DnsZoneBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BasicResourceArgs, ResourceInfo } from '../types';
import { DnsZoneARecordType, IDnsZoneBuilder } from './types';
import * as network from '@pulumi/azure-native/network';
import { globalKeyName } from '../Common/GlobalEnv';
import { globalKeyName } from '../Common';

class DnsZoneBuilder implements IDnsZoneBuilder {
private _aRecords: DnsZoneARecordType[] = [];
Expand Down
33 changes: 19 additions & 14 deletions src/Builder/PrivateDnsZoneBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from './types/privateDnsZoneBuilder';
import * as native from '@pulumi/azure-native';
import { output } from '@pulumi/pulumi';
import { getVnetIdFromSubnetId, globalKeyName } from '../Common';
import { rsInfo, globalKeyName } from '../Common';

class PrivateDnsZoneBuilder implements IPrivateDnsZoneBuilder {
private _aRecords: DnsZoneARecordType[] = [];
Expand Down Expand Up @@ -86,22 +86,27 @@ class PrivateDnsZoneBuilder implements IPrivateDnsZoneBuilder {
}

private buildVnetLinks() {
if (this._vnetLinks.length <= 0) return;
this._vnetLinks.forEach((lik, index) =>
[
//Link all subnets first
...(lik.subnetIds ?? []).map((s) =>
output(s).apply((i) => getVnetIdFromSubnetId(i)),
),
//Then link the extra Vnet
...(lik.vnetIds ?? []),
].map((v, i) => {
if (this._vnetLinks.length <= 0 || !this._dnsInfo) return;

const linkName = rsInfo.getNameFromId(this._dnsInfo.name);
const vnetIds = this._vnetLinks.flatMap((lik) => [
//Link all subnets first
...(lik.subnetIds ?? []).map((s) =>
output(s).apply((i) => rsInfo.getVnetIdFromSubnetId(i)),
),
//Then link the extra Vnet
...(lik.vnetIds ?? []),
]);

output(vnetIds).apply((vids) =>
vids.map((v) => {
const n = rsInfo.getNameFromId(v);
return new native.network.VirtualNetworkLink(
`${this.commonProps.name.split('.')[0]}-${index}-${i}-link`,
`${linkName}-${n}-link`,
{
privateZoneName: this._dnsInfo!.name,
...this._dnsInfo!.group,
registrationEnabled: Boolean(lik.registrationEnabled),
privateZoneName: this._dnsInfo!.name,
registrationEnabled: false,
virtualNetwork: { id: v },
},
{ dependsOn: this._zoneInstance, deleteBeforeReplace: true },
Expand Down
1 change: 0 additions & 1 deletion src/Builder/VnetBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,6 @@ class VnetBuilder
name: k,
}).linkTo({
vnetIds: [this._vnetInstance!.id],
registrationEnabled: false,
});

if (bFunc) bFunc(builder);
Expand Down
10 changes: 5 additions & 5 deletions src/Builder/types/privateDnsZoneBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Input } from "@pulumi/pulumi";
import { IBuilder } from "./genericBuilder";
import { ResourceInfo } from "../../types";
import { DnsZoneARecordType } from "./dnsZoneBuilder";
import { Input } from '@pulumi/pulumi';
import { IBuilder } from './genericBuilder';
import { ResourceInfo } from '../../types';
import { DnsZoneARecordType } from './dnsZoneBuilder';

export type PrivateDnsZoneVnetLinkingType = {
vnetIds?: Input<string>[];
//The vnetId will be calculated based on subnetId
subnetIds?: Input<string>[];
registrationEnabled?: boolean;
//registrationEnabled?: boolean;
};

export interface IPrivateDnsZoneBuilder {
Expand Down
15 changes: 14 additions & 1 deletion src/Common/RsInfo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const getResourceInfoFromId = (
): ResourceInfoWithSub | undefined => {
if (!id) return undefined;

const details = id.split('/');
const details = id.trim().split('/');
let name = '';
let groupName = '';
let subId = '';
Expand All @@ -41,6 +41,19 @@ export const getResourceInfoFromId = (
};
};

export const getNameFromId = (id: string) => {
id = id.trim();

//Resource ID
if (id.includes('/')) {
return id.split('/').pop();
}
//Domain
if (id.includes('.')) return id.split('.')[0];
//If not just get last 25 character
return id.slice(-25);
};

/** The method to get Resource group Name*/
export const getRGId = (group: ResourceGroupInfo) =>
interpolate`${defaultSubScope}/resourceGroups/${group.resourceGroupName}`;
Expand Down
33 changes: 20 additions & 13 deletions src/VNet/PrivateEndpoint.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as network from '@pulumi/azure-native/network';
import { output } from '@pulumi/pulumi';
import { OptsArgs, PrivateLinkPropsType, ResourceInfo } from '../types';
import { getPrivateEndpointName } from '../Common';
import { getPrivateEndpointName, rsInfo } from '../Common';
import { PrivateDnsZoneBuilder } from '../Builder';

export type PrivateEndpointProps = Omit<PrivateLinkPropsType, 'type'> &
Expand All @@ -22,13 +22,14 @@ export default ({
}: PrivateEndpointProps) => {
const name = getPrivateEndpointName(resourceInfo.name);

const endpoints = subnetIds.map(
(s, index) =>
new network.PrivateEndpoint(
`${name}-${index}`,
const endpoints = output(subnetIds).apply((ss) =>
ss.map((s) => {
const n = rsInfo.getNameFromId(s);
const ep = new network.PrivateEndpoint(
`${name}-${n}`,
{
...resourceInfo.group,
privateEndpointName: `${name}-${index}`,
privateEndpointName: `${name}-${n}`,
subnet: { id: s },
privateLinkServiceConnections: [
{
Expand All @@ -39,15 +40,21 @@ export default ({
],
},
{ dependsOn },
),
);

return {
instance: ep,
ipAddresses: ep.customDnsConfigs.apply((c) =>
c!.flatMap((i) => i!.ipAddresses!),
),
};
}),
);

//Get IpAddress in
const ipAddresses = output(
endpoints.map((e) =>
e.customDnsConfigs.apply((c) => c!.flatMap((i) => i.ipAddresses!)),
),
).apply((a) => a.flatMap((i) => i!));
const ipAddresses = output(endpoints).apply((a) =>
a.flatMap((i) => i.ipAddresses!),
);

output(ipAddresses).apply((ip) =>
PrivateDnsZoneBuilder({
Expand All @@ -56,7 +63,7 @@ export default ({
dependsOn,
})
.withARecord({ ipAddresses: ip, recordName: '@' })
.linkTo({ subnetIds, vnetIds: extraVnetIds, registrationEnabled: false })
.linkTo({ subnetIds, vnetIds: extraVnetIds })
.build(),
);

Expand Down

0 comments on commit 69cf992

Please sign in to comment.