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
});
asHubConfigures 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'],
});
asSpokeConfigures 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'],
});
withPublicIPSets the type of public IP for the VNet.
vnetBuilder.withPublicIP('prefix');
withPublicIPFromUses an existing public IP for the VNet.
vnetBuilder.withPublicIPFrom('existing-ip-id');
withNatGatewayEnables NAT gateway for the VNet.
vnetBuilder.withNatGateway();
withVpnGatewaySets the VPN gateway properties for the VNet.
vnetBuilder.withVpnGateway({
subnetSpace: '10.0.2.0/24',
// other VpnGatewayCreationProps properties
});
withFirewallSets the firewall properties for the VNet.
vnetBuilder.withFirewall({
subnet: { addressPrefix: '10.0.3.0/24' },
// other FirewallCreationProps properties
});
withFirewallAndNatGatewaySets the firewall properties and enables NAT gateway for the VNet.
vnetBuilder.withFirewallAndNatGateway({
subnet: { addressPrefix: '10.0.3.0/24' },
// other FirewallCreationProps properties
});
withBastionSets the bastion properties for the VNet.
vnetBuilder.withBastion({
subnet: { addressPrefix: '10.0.4.0/24' },
// other BastionCreationProps properties
});
withSecurityRulesAdds security rules to the VNet.
vnetBuilder.withSecurityRules({
name: 'AllowSSH',
priority: 100,
direction: 'Inbound',
access: 'Allow',
protocol: 'Tcp',
sourcePortRange: '*',
destinationPortRange: '22',
sourceAddressPrefix: '*',
destinationAddressPrefix: '*',
});
withRouteRulesAdds route rules to the VNet.
vnetBuilder.withRouteRules({
name: 'RouteToInternet',
addressPrefix: '0.0.0.0/0',
nextHopType: 'Internet',
});
withPrivateDnsAdds private DNS settings to the VNet.
vnetBuilder.withPrivateDns('mydomain.com', (builder) => {
builder.addRecordSet({
name: 'www',
type: 'A',
ttl: 300,
records: ['10.0.0.4'],
});
});
peeringToAdds peering to another VNet.
vnetBuilder.peeringTo({
vnetId: 'other-vnet-id',
direction: 'Bidirectional',
options: { allowForwardedTraffic: true },
});
buildIpAddressCreates the public IP addresses for the VNet.
This method is called internally by the build method and is not typically called directly.
buildNatGatewayCreates the NAT gateway for the VNet.
This method is called internally by the build method and is not typically called directly.
buildVnetCreates the VNet with the specified configurations.
This method is called internally by the build method and is not typically called directly.
buildFirewallCreates the firewall for the VNet.
This method is called internally by the build method and is not typically called directly.
buildVpnGatewayCreates the VPN gateway for the VNet.
This method is called internally by the build method and is not typically called directly.
buildBastionCreates the bastion for the VNet.
This method is called internally by the build method and is not typically called directly.
buildPrivateDnsCreates the private DNS settings for the VNet.
This method is called internally by the build method and is not typically called directly.
buildPeeringCreates the peering connections for the VNet.
This method is called internally by the build method and is not typically called directly.
buildBuilds 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.