VnetBuilder
Class OverviewThe VnetBuilder
class is designed to build and configure an Azure Virtual Network (VNet) with specific configurations such as subnets, public IPs, NAT gateways, VPN gateways, firewalls, bastions, security rules, route rules, private DNS, and peering.
Initializes the VnetBuilder
with the provided arguments.
const vnetBuilder = new VnetBuilder({
name: 'myVnet',
group: { resourceGroupName: 'myResourceGroup' },
// other necessary arguments
});
asHub
Configures the VNet as a hub with specified properties.
vnetBuilder.asHub({
subnets: { subnet1: { addressPrefix: '10.0.0.0/24' } },
dnsServers: ['8.8.8.8'],
addressSpaces: ['10.0.0.0/16'],
});
asSpoke
Configures the VNet as a spoke with specified properties.
vnetBuilder.asSpoke({
subnets: { subnet1: { addressPrefix: '10.0.1.0/24' } },
dnsServers: ['8.8.8.8'],
addressSpaces: ['10.0.1.0/16'],
});
withPublicIP
Sets the type of public IP for the VNet.
vnetBuilder.withPublicIP('prefix');
withPublicIPFrom
Uses an existing public IP for the VNet.
vnetBuilder.withPublicIPFrom('existing-ip-id');
withNatGateway
Enables NAT gateway for the VNet.
vnetBuilder.withNatGateway();
withVpnGateway
Sets the VPN gateway properties for the VNet.
vnetBuilder.withVpnGateway({
subnetSpace: '10.0.2.0/24',
// other VpnGatewayCreationProps properties
});
withFirewall
Sets the firewall properties for the VNet.
vnetBuilder.withFirewall({
subnet: { addressPrefix: '10.0.3.0/24' },
// other FirewallCreationProps properties
});
withFirewallAndNatGateway
Sets the firewall properties and enables NAT gateway for the VNet.
vnetBuilder.withFirewallAndNatGateway({
subnet: { addressPrefix: '10.0.3.0/24' },
// other FirewallCreationProps properties
});
withBastion
Sets the bastion properties for the VNet.
vnetBuilder.withBastion({
subnet: { addressPrefix: '10.0.4.0/24' },
// other BastionCreationProps properties
});
withSecurityRules
Adds security rules to the VNet.
vnetBuilder.withSecurityRules({
name: 'AllowSSH',
priority: 100,
direction: 'Inbound',
access: 'Allow',
protocol: 'Tcp',
sourcePortRange: '*',
destinationPortRange: '22',
sourceAddressPrefix: '*',
destinationAddressPrefix: '*',
});
withRouteRules
Adds route rules to the VNet.
vnetBuilder.withRouteRules({
name: 'RouteToInternet',
addressPrefix: '0.0.0.0/0',
nextHopType: 'Internet',
});
withPrivateDns
Adds private DNS settings to the VNet.
vnetBuilder.withPrivateDns('mydomain.com', (builder) => {
builder.addRecordSet({
name: 'www',
type: 'A',
ttl: 300,
records: ['10.0.0.4'],
});
});
peeringTo
Adds peering to another VNet.
vnetBuilder.peeringTo({
vnetId: 'other-vnet-id',
direction: 'Bidirectional',
options: { allowForwardedTraffic: true },
});
buildIpAddress
Creates the public IP addresses for the VNet.
This method is called internally by the build
method and is not typically called directly.
buildNatGateway
Creates the NAT gateway for the VNet.
This method is called internally by the build
method and is not typically called directly.
buildVnet
Creates the VNet with the specified configurations.
This method is called internally by the build
method and is not typically called directly.
buildFirewall
Creates the firewall for the VNet.
This method is called internally by the build
method and is not typically called directly.
buildVpnGateway
Creates the VPN gateway for the VNet.
This method is called internally by the build
method and is not typically called directly.
buildBastion
Creates the bastion for the VNet.
This method is called internally by the build
method and is not typically called directly.
buildPrivateDns
Creates the private DNS settings for the VNet.
This method is called internally by the build
method and is not typically called directly.
buildPeering
Creates the peering connections for the VNet.
This method is called internally by the build
method and is not typically called directly.
build
Builds the VNet and returns the results.
const vnetResults = vnetBuilder.build();
console.log(vnetResults);
Here is a full example demonstrating how to use the VnetBuilder
class:
import VnetBuilder from './Builder/VnetBuilder';
import { VnetBuilderArgs } from './types';
const args: VnetBuilderArgs = {
name: 'myVnet',
group: { resourceGroupName: 'myResourceGroup' },
// other necessary arguments
};
const vnetBuilder = new VnetBuilder(args);
vnetBuilder
.asHub({
subnets: { subnet1: { addressPrefix: '10.0.0.0/24' } },
dnsServers: ['8.8.8.8'],
addressSpaces: ['10.0.0.0/16'],
})
.withPublicIP('prefix')
.withNatGateway()
.withVpnGateway({
subnetSpace: '10.0.2.0/24',
// other VpnGatewayCreationProps properties
})
.withFirewall({
subnet: { addressPrefix: '10.0.3.0/24' },
// other FirewallCreationProps properties
})
.withBastion({
subnet: { addressPrefix: '10.0.4.0/24' },
// other BastionCreationProps properties
})
.withSecurityRules({
name: 'AllowSSH',
priority: 100,
direction: 'Inbound',
access: 'Allow',
protocol: 'Tcp',
sourcePortRange: '*',
destinationPortRange: '22',
sourceAddressPrefix: '*',
destinationAddressPrefix: '*',
})
.withRouteRules({
name: 'RouteToInternet',
addressPrefix: '0.0.0.0/0',
nextHopType: 'Internet',
})
.withPrivateDns('mydomain.com', (builder) => {
builder.addRecordSet({
name: 'www',
type: 'A',
ttl: 300,
records: ['10.0.0.4'],
});
})
.peeringTo({
vnetId: 'other-vnet-id',
direction: 'Bidirectional',
options: { allowForwardedTraffic: true },
});
const vnetResults = vnetBuilder.build();
console.log(vnetResults);
This guideline should help developers understand and reuse the methods in the VnetBuilder
class effectively.