From 433b0944c295e3944614e179e90ee3c54ddc2ae4 Mon Sep 17 00:00:00 2001 From: Mattias Holm Date: Mon, 16 Oct 2023 15:47:36 +0200 Subject: [PATCH] Added cidrSubnet function --- azure/arm/main.json | 44 +++++++++++++++++++++++---------- azure/arm/parameters.json | 6 +++++ azure/bicep/main.bicep | 20 ++++++--------- azure/bicep/main.bicepparam | 2 ++ azure/bicep/modules/pdnsz.bicep | 2 +- bash/.bashrc | 4 +++ fish/config.fish | 8 ++++++ macOS/brew/InstallPackages.sh | 1 + pulumi/azure-python/__main__.py | 4 +-- pwsh/Azure/activate.ps1 | 2 +- terraform/azure/providers.tf | 5 ++-- terraform/github/providers.tf | 5 ++-- terraform/sp/providers.tf | 5 ++-- 13 files changed, 72 insertions(+), 36 deletions(-) diff --git a/azure/arm/main.json b/azure/arm/main.json index 89c9f09..012e25d 100644 --- a/azure/arm/main.json +++ b/azure/arm/main.json @@ -12,8 +12,9 @@ }, "appiKind": { "type": "string", - "defaultValue": "web", + "defaultValue": "", "allowedValues": [ + "", "web", "java", "store", @@ -113,8 +114,13 @@ ] }, "vnetAddressPrefix": { - "type": "string", - "defaultValue": "" + "type": "string" + }, + "vnetSubnetSize": { + "type": "int" + }, + "vnetSubnetCount": { + "type": "int" } }, "variables": { @@ -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", @@ -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')]", @@ -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')]", @@ -230,7 +246,6 @@ "tags": "[parameters('tags')]", "resources": [ { - "condition": "[not(empty(parameters('vnetAddressPrefix')))]", "type": "virtualNetworkLinks", "apiVersion": "2020-06-01", "name": "[variables('vnetName')]", @@ -321,7 +336,7 @@ { "type": "containers", "apiVersion": "2023-01-01", - "name": "[variables('containerName')]", + "name": "container-01", "dependsOn": [ "[variables('blobId')[copyIndex()]]" ] @@ -332,7 +347,6 @@ ] }, { - "condition": "[not(empty(parameters('vnetAddressPrefix')))]", "type": "Microsoft.Network/virtualNetworks", "apiVersion": "2023-05-01", "name": "[variables('vnetName')]", @@ -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')]]" + } } } ] diff --git a/azure/arm/parameters.json b/azure/arm/parameters.json index 2016223..7732eeb 100644 --- a/azure/arm/parameters.json +++ b/azure/arm/parameters.json @@ -102,6 +102,12 @@ }, "vnetAddressPrefix": { "value": "10.0.0.0/24" + }, + "vnetSubnetSize": { + "value": 26 + }, + "vnetSubnetCount": { + "value": 4 } } } \ No newline at end of file diff --git a/azure/bicep/main.bicep b/azure/bicep/main.bicep index d88a9c5..c0ccb48 100644 --- a/azure/bicep/main.bicep +++ b/azure/bicep/main.bicep @@ -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: { @@ -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: { @@ -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: { @@ -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) + }] } } diff --git a/azure/bicep/main.bicepparam b/azure/bicep/main.bicepparam index 97c4a34..540e983 100644 --- a/azure/bicep/main.bicepparam +++ b/azure/bicep/main.bicepparam @@ -85,5 +85,7 @@ param config = { } vnet: { addressPrefix: '10.0.0.0/24' + subnetSize: 26 + subnetCount: 4 } } diff --git a/azure/bicep/modules/pdnsz.bicep b/azure/bicep/modules/pdnsz.bicep index 3c7db94..439c00e 100644 --- a/azure/bicep/modules/pdnsz.bicep +++ b/azure/bicep/modules/pdnsz.bicep @@ -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: { diff --git a/bash/.bashrc b/bash/.bashrc index 11ce4cb..d2b590a 100644 --- a/bash/.bashrc +++ b/bash/.bashrc @@ -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' diff --git a/fish/config.fish b/fish/config.fish index e5cf5bf..b5f5b06 100644 --- a/fish/config.fish +++ b/fish/config.fish @@ -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 diff --git a/macOS/brew/InstallPackages.sh b/macOS/brew/InstallPackages.sh index c1969ba..aa44cf3 100755 --- a/macOS/brew/InstallPackages.sh +++ b/macOS/brew/InstallPackages.sh @@ -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 diff --git a/pulumi/azure-python/__main__.py b/pulumi/azure-python/__main__.py index 4f6d3ca..49dc841 100644 --- a/pulumi/azure-python/__main__.py +++ b/pulumi/azure-python/__main__.py @@ -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 ) @@ -136,7 +136,7 @@ ), subnets=[ network.SubnetArgs( - name=f'snet-01', + name='snet-01', address_prefix=config.vnet_address_prefix ) ] diff --git a/pwsh/Azure/activate.ps1 b/pwsh/Azure/activate.ps1 index eaa8afb..068b956 100755 --- a/pwsh/Azure/activate.ps1 +++ b/pwsh/Azure/activate.ps1 @@ -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 diff --git a/terraform/azure/providers.tf b/terraform/azure/providers.tf index 2c35502..97a5ac4 100644 --- a/terraform/azure/providers.tf +++ b/terraform/azure/providers.tf @@ -1,7 +1,8 @@ terraform { - required_version = "~> 1.5.0" + required_version = "~> 1.6.0" cloud { + hostname = "app.terraform.io" organization = "mattiasholm" workspaces { @@ -17,7 +18,7 @@ terraform { azurerm = { source = "hashicorp/azurerm" - version = "~> 3.74.0" + version = "~> 3.75.0" } } } diff --git a/terraform/github/providers.tf b/terraform/github/providers.tf index cce3417..9546941 100644 --- a/terraform/github/providers.tf +++ b/terraform/github/providers.tf @@ -1,7 +1,8 @@ terraform { - required_version = "~> 1.5.0" + required_version = "~> 1.6.0" cloud { + hostname = "app.terraform.io" organization = "mattiasholm" workspaces { @@ -12,7 +13,7 @@ terraform { required_providers { github = { source = "integrations/github" - version = "~> 5.38.0" + version = "~> 5.39.0" } } } diff --git a/terraform/sp/providers.tf b/terraform/sp/providers.tf index 2c3c947..9144563 100644 --- a/terraform/sp/providers.tf +++ b/terraform/sp/providers.tf @@ -1,7 +1,8 @@ terraform { - required_version = "~> 1.5.0" + required_version = "~> 1.6.0" cloud { + hostname = "app.terraform.io" organization = "mattiasholm" workspaces { @@ -17,7 +18,7 @@ terraform { azurerm = { source = "hashicorp/azurerm" - version = "~> 3.74.0" + version = "~> 3.75.0" } time = {