Skip to content

Commit

Permalink
Refactor subnet creation (#1521)
Browse files Browse the repository at this point in the history
* Refactor subnet creation

* rush change
  • Loading branch information
gonzalogarciajaubert authored Feb 1, 2024
1 parent bb9ddcd commit 2206386
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@boostercloud/framework-core",
"comment": "refactor subnet creation for Azure Provider",
"type": "minor"
}
],
"packageName": "@boostercloud/framework-core"
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import { TerraformNetworkSecurityGroup } from './gateway/terraform-network-secur
import { TerraformVirtualNetwork } from './gateway/terraform-virtual-network'
import { TerraformPublicIp } from './gateway/terraform-public-ip'
import { TerraformPublicIpData } from './gateway/terraform-public-ip-data'
import { TerraformSubnet } from './gateway/terraform-subnet'
import { TerraformSubnetSecurity } from './gateway/terraform-subnet-security'

export class ApplicationSynth {
readonly config: BoosterConfig
Expand Down Expand Up @@ -78,6 +80,8 @@ export class ApplicationSynth {
`${this.stackNames.resourceGroupName}rjsg`
)
stack.virtualNetwork = TerraformVirtualNetwork.build(stack)
stack.subnet = TerraformSubnet.build(stack)
stack.subnetNetworkSecurityGroupAssociation = TerraformSubnetSecurity.build(stack)
stack.publicIP = TerraformPublicIp.build(stack, 'pip')
stack.appGateway = TerraformApplicationGateway.build(stack)
stack.publicIPData = TerraformPublicIpData.build(stack)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ export class TerraformApplicationGateway {
resourceGroupName,
functionAppName,
virtualNetwork,
subnet,
publicIP,
}: ApplicationSynthStack): applicationGateway.ApplicationGateway {
const environment = process.env.BOOSTER_ENV ?? 'azure'
if (!virtualNetwork) {
throw new Error('Undefined virtualNetwork resource')
}
if (!subnet) {
throw new Error('Undefined subnet resource')
}
if (!publicIP) {
throw new Error('Undefined publicIP resource')
}
Expand All @@ -36,7 +40,7 @@ export class TerraformApplicationGateway {
gatewayIpConfiguration: [
{
name: 'myGatewayIpConfiguration',
subnetId: virtualNetwork.subnet.get(0).id,
subnetId: subnet.id,
},
],
frontendIpConfiguration: [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { toTerraformName } from '../../helper/utils'
import { ApplicationSynthStack } from '../../types/application-synth-stack'
import { subnetNetworkSecurityGroupAssociation } from '@cdktf/provider-azurerm'

export class TerraformSubnetSecurity {
static build({
terraformStack,
appPrefix,
networkSecurityGroup,
subnet,
}: ApplicationSynthStack): subnetNetworkSecurityGroupAssociation.SubnetNetworkSecurityGroupAssociation {
if (!networkSecurityGroup) {
throw new Error('Undefined networkSecurityGroup resource')
}
if (!subnet) {
throw new Error('Undefined subnet resource')
}

return new subnetNetworkSecurityGroupAssociation.SubnetNetworkSecurityGroupAssociation(
terraformStack,
toTerraformName(appPrefix, 'snsg'),
{
subnetId: subnet.id,
networkSecurityGroupId: networkSecurityGroup.id,
}
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { toTerraformName } from '../../helper/utils'
import { ApplicationSynthStack } from '../../types/application-synth-stack'
import { subnet } from '@cdktf/provider-azurerm'

export class TerraformSubnet {
static build({
terraformStack,
appPrefix,
virtualNetwork,
resourceGroupName,
networkSecurityGroup,
}: ApplicationSynthStack): subnet.Subnet {
if (!networkSecurityGroup) {
throw new Error('Undefined networkSecurityGroup resource')
}
if (!virtualNetwork) {
throw new Error('Undefined virtualNetwork resource')
}

return new subnet.Subnet(terraformStack, toTerraformName(appPrefix, 'vsn'), {
name: `${resourceGroupName}vsn`,
resourceGroupName: resourceGroupName,
virtualNetworkName: virtualNetwork.name,
addressPrefixes: ['10.0.1.0/24'],
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ export class TerraformVirtualNetwork {
location: resourceGroup.location,
resourceGroupName: resourceGroupName,
addressSpace: ['10.0.0.0/16'],
subnet: [
{
name: 'gatewaySubnet' + Math.floor(Math.random() * 100),
addressPrefix: '10.0.1.0/24',
securityGroup: networkSecurityGroup.id,
},
],
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
storageAccount,
virtualNetwork,
webPubsub,
subnet,
subnetNetworkSecurityGroupAssociation,
webPubsubHub,
windowsFunctionApp,
} from '@cdktf/provider-azurerm'
Expand Down Expand Up @@ -45,6 +47,8 @@ export interface ApplicationSynthStack extends StackNames {
dataFunctionAppHostKeys?: dataAzurermFunctionAppHostKeys.DataAzurermFunctionAppHostKeys
appGateway?: applicationGateway.ApplicationGateway
networkSecurityGroup?: networkSecurityGroup.NetworkSecurityGroup
subnet?: subnet.Subnet
subnetNetworkSecurityGroupAssociation?: subnetNetworkSecurityGroupAssociation.SubnetNetworkSecurityGroupAssociation
virtualNetwork?: virtualNetwork.VirtualNetwork
publicIP?: publicIp.PublicIp
publicIPData?: dataAzurermPublicIp.DataAzurermPublicIp
Expand Down

0 comments on commit 2206386

Please sign in to comment.