Skip to content

Commit

Permalink
Added cidrSubnet function
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiasholm committed Oct 16, 2023
1 parent 9fac7e4 commit 433b094
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 36 deletions.
44 changes: 31 additions & 13 deletions azure/arm/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
},
"appiKind": {
"type": "string",
"defaultValue": "web",
"defaultValue": "",
"allowedValues": [
"",
"web",
"java",
"store",
Expand Down Expand Up @@ -113,8 +114,13 @@
]
},
"vnetAddressPrefix": {
"type": "string",
"defaultValue": ""
"type": "string"
},
"vnetSubnetSize": {
"type": "int"
},
"vnetSubnetCount": {
"type": "int"
}
},
"variables": {
Expand All @@ -127,10 +133,8 @@
"kvId": "[resourceId('Microsoft.KeyVault/vaults', variables('kvName'))]",
"pdnszId": "[resourceId('Microsoft.Network/privateDnsZones', parameters('pdnszName'))]",
"pipCount": "[length(parameters('pipLabels'))]",
"containerName": "container-01",
"vnetName": "[format('vnet-{0}-01', variables('prefix'))]",
"vnetId": "[resourceId('Microsoft.Network/virtualNetworks', variables('vnetName'))]",
"snetName": "snet-01",
"copy": [
{
"name": "cnameName",
Expand Down Expand Up @@ -171,11 +175,22 @@
"name": "blobId",
"count": "[parameters('stCount')]",
"input": "[resourceId('Microsoft.Storage/storageAccounts/blobServices', variables('stName')[copyIndex('blobId')], 'default')]"
},
{
"name": "snetName",
"count": "[parameters('vnetSubnetCount')]",
"input": "[format('snet-{0}', padLeft(copyIndex('snetName', 1), 2, '0'))]"
},
{
"name": "snetAddressPrefix",
"count": "[parameters('vnetSubnetCount')]",
"input": "[cidrSubnet(parameters('vnetAddressPrefix'), parameters('vnetSubnetSize'), copyIndex('snetAddressPrefix'))]"
}
]
},
"resources": [
{
"condition": "[not(empty(parameters('appiKind')))]",
"type": "Microsoft.Insights/components",
"apiVersion": "2020-02-02",
"name": "[variables('appiName')]",
Expand Down Expand Up @@ -208,12 +223,13 @@
},
"resources": [
{
"condition": "[not(empty(parameters('appiKind')))]",
"type": "secrets",
"apiVersion": "2023-02-01",
"name": "APPLICATIONINSIGHTS-CONNECTION-STRING",
"tags": "[parameters('tags')]",
"properties": {
"value": "[reference(variables('appiId')).connectionString]"
"value": "[if(empty(parameters('appiKind')), '', reference(variables('appiId')).connectionString)]"
},
"dependsOn": [
"[variables('kvId')]",
Expand All @@ -230,7 +246,6 @@
"tags": "[parameters('tags')]",
"resources": [
{
"condition": "[not(empty(parameters('vnetAddressPrefix')))]",
"type": "virtualNetworkLinks",
"apiVersion": "2020-06-01",
"name": "[variables('vnetName')]",
Expand Down Expand Up @@ -321,7 +336,7 @@
{
"type": "containers",
"apiVersion": "2023-01-01",
"name": "[variables('containerName')]",
"name": "container-01",
"dependsOn": [
"[variables('blobId')[copyIndex()]]"
]
Expand All @@ -332,7 +347,6 @@
]
},
{
"condition": "[not(empty(parameters('vnetAddressPrefix')))]",
"type": "Microsoft.Network/virtualNetworks",
"apiVersion": "2023-05-01",
"name": "[variables('vnetName')]",
Expand All @@ -344,11 +358,15 @@
"[parameters('vnetAddressPrefix')]"
]
},
"subnets": [
"copy": [
{
"name": "[variables('snetName')]",
"properties": {
"addressPrefix": "[parameters('vnetAddressPrefix')]"
"name": "subnets",
"count": "[parameters('vnetSubnetCount')]",
"input": {
"name": "[variables('snetName')[copyIndex('subnets')]]",
"properties": {
"addressPrefix": "[variables('snetAddressPrefix')[copyIndex('subnets')]]"
}
}
}
]
Expand Down
6 changes: 6 additions & 0 deletions azure/arm/parameters.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@
},
"vnetAddressPrefix": {
"value": "10.0.0.0/24"
},
"vnetSubnetSize": {
"value": 26
},
"vnetSubnetCount": {
"value": 4
}
}
}
20 changes: 7 additions & 13 deletions azure/bicep/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ resource rg 'Microsoft.Resources/resourceGroups@2023-07-01' = {
tags: config.tags
}

module appi 'modules/appi.bicep' = {
module appi 'modules/appi.bicep' = if (contains(config, 'appi')) {
name: 'appi'
scope: rg
params: {
Expand Down Expand Up @@ -47,7 +47,7 @@ module pdnsz 'modules/pdnsz.bicep' = {
params: {
name: config.pdnsz.name
vnetName: vnet.outputs.name
vnetId: contains(config, 'vnet') ? vnet.outputs.id : ''
vnetId: vnet.outputs.id
registrationEnabled: config.pdnsz.registration
ttl: config.pdnsz.ttl
cnameRecords: [for (label, i) in config.pip.labels: {
Expand Down Expand Up @@ -86,7 +86,7 @@ module st 'modules/st.bicep' = [for i in range(0, config.st.count): {
}
}]

module vnet 'modules/vnet.bicep' = if (contains(config, 'vnet')) {
module vnet 'modules/vnet.bicep' = {
name: 'vnet'
scope: rg
params: {
Expand All @@ -95,16 +95,10 @@ module vnet 'modules/vnet.bicep' = if (contains(config, 'vnet')) {
addressPrefixes: [
config.vnet.addressPrefix
]
subnets: [
{
name: 'snet-01'
addressPrefix: cidrSubnet(config.vnet.addressPrefix, 25, 0)
}
{
name: 'snet-02'
addressPrefix: cidrSubnet(config.vnet.addressPrefix, 25, 1)
}
]
subnets: [for i in range(0, config.vnet.subnetCount): {
name: 'snet-${padLeft(i + 1, 2, '0')}'
addressPrefix: cidrSubnet(config.vnet.addressPrefix, config.vnet.subnetSize, i)
}]
}
}

Expand Down
2 changes: 2 additions & 0 deletions azure/bicep/main.bicepparam
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,7 @@ param config = {
}
vnet: {
addressPrefix: '10.0.0.0/24'
subnetSize: 26
subnetCount: 4
}
}
2 changes: 1 addition & 1 deletion azure/bicep/modules/pdnsz.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ resource pdnsz 'Microsoft.Network/privateDnsZones@2020-06-01' = {
location: location
tags: tags

resource link 'virtualNetworkLinks' = if (!empty(vnetId)) {
resource link 'virtualNetworkLinks' = {
name: vnetName
location: location
properties: {
Expand Down
4 changes: 4 additions & 0 deletions bash/.bashrc
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ function ip() {
echo -n "$ip" | pbcopy
}

function ipi() {
curl --silent ipinfo.io/$1
}

function pw() {
if [[ -z "$1" ]]; then
length='16'
Expand Down
8 changes: 8 additions & 0 deletions fish/config.fish
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ function ip
echo -n $ip | pbcopy
end

function ipi --argument-names ip
if not test $ip
set ip ''
end

curl --silent ipinfo.io/$ip
end

function pw --argument-names length count
if not test $length
set length 16
Expand Down
1 change: 1 addition & 0 deletions macOS/brew/InstallPackages.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ brew install pwgen
brew install gh
brew install azure/bicep/bicep
brew install terraform
brew install opentofu
brew install tflint
brew install tfsec
brew install terrascan
Expand Down
4 changes: 2 additions & 2 deletions pulumi/azure-python/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@

storage.BlobContainer(
f'container{i}',
container_name=f'container-01',
container_name='container-01',
account_name=st.name,
resource_group_name=rg.name
)
Expand All @@ -136,7 +136,7 @@
),
subnets=[
network.SubnetArgs(
name=f'snet-01',
name='snet-01',
address_prefix=config.vnet_address_prefix
)
]
Expand Down
2 changes: 1 addition & 1 deletion pwsh/Azure/activate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ switch ($Role) {

$Method = 'PUT'
$Uri = '/providers/Microsoft.Subscription/subscriptions/{0}/providers/Microsoft.Authorization/roleAssignmentScheduleRequests/{1}?api-version=2020-10-01' -f $Subscription, (New-Guid)
$Body = ConvertTo-Json -Depth 100 @{
$Body = ConvertTo-Json -Depth 10 @{
properties = @{
principalId = az ad signed-in-user show --query id --output tsv
roleDefinitionId = az role definition list --name $Role --subscription $Subscription --query [].id --output tsv
Expand Down
5 changes: 3 additions & 2 deletions terraform/azure/providers.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
terraform {
required_version = "~> 1.5.0"
required_version = "~> 1.6.0"

cloud {
hostname = "app.terraform.io"
organization = "mattiasholm"

workspaces {
Expand All @@ -17,7 +18,7 @@ terraform {

azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.74.0"
version = "~> 3.75.0"
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions terraform/github/providers.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
terraform {
required_version = "~> 1.5.0"
required_version = "~> 1.6.0"

cloud {
hostname = "app.terraform.io"
organization = "mattiasholm"

workspaces {
Expand All @@ -12,7 +13,7 @@ terraform {
required_providers {
github = {
source = "integrations/github"
version = "~> 5.38.0"
version = "~> 5.39.0"
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions terraform/sp/providers.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
terraform {
required_version = "~> 1.5.0"
required_version = "~> 1.6.0"

cloud {
hostname = "app.terraform.io"
organization = "mattiasholm"

workspaces {
Expand All @@ -17,7 +18,7 @@ terraform {

azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.74.0"
version = "~> 3.75.0"
}

time = {
Expand Down

0 comments on commit 433b094

Please sign in to comment.