Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AVM Module Issue]: How to conditionally define network security group in subnets #1681

Closed
1 task done
teemukom opened this issue Apr 15, 2024 · 8 comments · Fixed by #1772
Closed
1 task done

[AVM Module Issue]: How to conditionally define network security group in subnets #1681

teemukom opened this issue Apr 15, 2024 · 8 comments · Fixed by #1772
Assignees
Labels
Type: AVM 🅰️ ✌️ Ⓜ️ This is an AVM related issue

Comments

@teemukom
Copy link

teemukom commented Apr 15, 2024

Check for previous/existing GitHub issues

  • I have checked for previous/existing GitHub issues

Issue Type?

I'm not sure

Module Name

avm/res/network/virtual-network

(Optional) Module Version

0.1.1

Description

I'm unable to find a way to conditionally define network security group for a subnet. For example I would like to define NSG id if the subnet's name is AzureBastionSubnet but not for others. The most obvious approach would be:

subnets: [for subnet in subnets: {
  name: subnet.name
  addressPrefix: subnet.addressPrefix
  networkSecurityGroupResourceId: (subnet.name == 'AzureBastionSubnet') ? nsg.id : null
}]

But it seems that the value if set can't be null:

     | InvalidRequestFormat)    - Value for the id property is invalid.
     | Expecting a string. Actual value is Null. Path
     | properties.subnets[0].properties.networkSecurityGroup.
     | (Code:InvalidJsonPropertyType)

### (Optional) Correlation Id

_No response_
@teemukom teemukom added Needs: Triage 🔍 Maintainers need to triage still Type: AVM 🅰️ ✌️ Ⓜ️ This is an AVM related issue labels Apr 15, 2024

Important

The "Needs: Triage 🔍" label must be removed once the triage process is complete!

Tip

For additional guidance on how to triage this issue/PR, see the BRM Issue Triage documentation.

Note

This label was added as per ITA06.

@elbatane
Copy link
Contributor

Hi @teemukom, thank you very much for opening the issue. We will com back to you with an answer as soon as possible

@elbatane elbatane removed the Needs: Triage 🔍 Maintainers need to triage still label Apr 19, 2024
@cloudchristoph
Copy link
Contributor

I have checked various options and found that as soon as the parameter networkSecurityGroupResourceId is specified, there is no chance to leave it empty (empty string, null, false, ...)

The submodule for subnets has handled this better. It checks whether the parameter is empty, not whether it is included.

From subnet module:

    networkSecurityGroup: !empty(networkSecurityGroupResourceId)
      ? {
          id: networkSecurityGroupResourceId
        }
      : null

I suggest that the main module also checks for “empty”. In addition to “contains”.

From virtual network module:

      networkSecurityGroup: contains(subnet, 'networkSecurityGroupResourceId')
        ? {
            id: subnet.networkSecurityGroupResourceId
          }
        : null

My proposal for networkSecurityGroups.

image

And at the same time we should also adapt it for natGateway and routeTable. For all other parameters I can either pass “null” directly or an empty array, so that's fine.

Any objections?

@elbatane
Copy link
Contributor

elbatane commented Apr 29, 2024

HIi @cloudchristoph , could you please create a PR with the changes you are proposing? Thank you very much

@cloudchristoph
Copy link
Contributor

Sure! Will send a PR tomorrow.

@AlexanderSehr
Copy link
Contributor

I'm a bit late to the part, but one option to achieve this with the module today would be to only add the property to the subnets if the condition is true in the first place. This can be done using the union() function that merges the main chunk of the object with a conditional block that's only added if your condition is true.

This would look like this

var test = [
  for subnet in subnets: union(
    {
      name: subnet.name
      addressPrefix: subnet.addressPrefix
    },
    (subnet.name == 'AzureBastionSubnet' ? { networkSecurityGroupResourceId : nsg.id } : {})
  )
]

@teemukom
Copy link
Author

I'm a bit late to the part, but one option to achieve this with the module today would be to only add the property to the subnets if the condition is true in the first place. This can be done using the union() function that merges the main chunk of the object with a conditional block that's only added if your condition is true.

This would look like this

var test = [
  for subnet in subnets: union(
    {
      name: subnet.name
      addressPrefix: subnet.addressPrefix
    },
    (subnet.name == 'AzureBastionSubnet' ? { networkSecurityGroupResourceId : nsg.id } : {})
  )
]

@AlexanderSehr thanks for the reply. Actually I also figured this earlier. Not nice but doable.

@AlexanderSehr
Copy link
Contributor

I'm a bit late to the part, but one option to achieve this with the module today would be to only add the property to the subnets if the condition is true in the first place. This can be done using the union() function that merges the main chunk of the object with a conditional block that's only added if your condition is true.
This would look like this

var test = [
  for subnet in subnets: union(
    {
      name: subnet.name
      addressPrefix: subnet.addressPrefix
    },
    (subnet.name == 'AzureBastionSubnet' ? { networkSecurityGroupResourceId : nsg.id } : {})
  )
]

@AlexanderSehr thanks for the reply. Actually I also figured this earlier. Not nice but doable.

We continue to discuss the best solution in @cloudchristoph's PR 😉

elbatane pushed a commit that referenced this issue May 3, 2024
…k` (#1772)

## Description

You cannot provide the following parameters in your subnet parameter, if
they are empty:

- networkSecurityGroup
- natGateway
- routeTable

As soon as they are set, they have to have a correct resource id. 

This PR implements additional checks for given, but empty, parameters.
I also added a new subnet definition in the `max` test to check for the
correct behaviour.

Fixes #1681
Closes #1681

## Pipeline Reference

| Pipeline |
| -------- |
|
[![avm.res.network.virtual-network](https://github.com/cloudchristoph/bicep-registry-modules/actions/workflows/avm.res.network.virtual-network.yml/badge.svg?branch=1681_vnet_subnet_conditional_params)](https://github.com/cloudchristoph/bicep-registry-modules/actions/workflows/avm.res.network.virtual-network.yml)
|

## Type of Change

- [ ] Update to CI Environment or utilities (Non-module effecting
changes)
- [x] Azure Verified Module updates:
- [x] Bugfix containing backwards compatible bug fixes, and I have NOT
bumped the MAJOR or MINOR version in `version.json`:
- [x] Someone has opened a bug report issue, and I have included "Closes
#{bug_report_issue_number}" in the PR description.
- [ ] The bug was found by the module author, and no one has opened an
issue to report it yet.
- [ ] Feature update backwards compatible feature updates, and I have
bumped the MINOR version in `version.json`.
- [ ] Breaking changes and I have bumped the MAJOR version in
`version.json`.
  - [x] Update to documentation

## Checklist

- [x] I'm sure there are no other open Pull Requests for the same
update/change
- [x] I have run `Set-AVMModule` locally to generate the supporting
module files.
- [x] My corresponding pipelines / checks run clean and green without
any errors or warnings

<!-- Please keep up to day with the contribution guide at
https://aka.ms/avm/contribute/bicep -->
hundredacres pushed a commit to hundredacres/bicep-registry-modules that referenced this issue Jun 19, 2024
…k` (Azure#1772)

## Description

You cannot provide the following parameters in your subnet parameter, if
they are empty:

- networkSecurityGroup
- natGateway
- routeTable

As soon as they are set, they have to have a correct resource id. 

This PR implements additional checks for given, but empty, parameters.
I also added a new subnet definition in the `max` test to check for the
correct behaviour.

Fixes Azure#1681
Closes Azure#1681

## Pipeline Reference

| Pipeline |
| -------- |
|
[![avm.res.network.virtual-network](https://github.com/cloudchristoph/bicep-registry-modules/actions/workflows/avm.res.network.virtual-network.yml/badge.svg?branch=1681_vnet_subnet_conditional_params)](https://github.com/cloudchristoph/bicep-registry-modules/actions/workflows/avm.res.network.virtual-network.yml)
|

## Type of Change

- [ ] Update to CI Environment or utilities (Non-module effecting
changes)
- [x] Azure Verified Module updates:
- [x] Bugfix containing backwards compatible bug fixes, and I have NOT
bumped the MAJOR or MINOR version in `version.json`:
- [x] Someone has opened a bug report issue, and I have included "Closes
#{bug_report_issue_number}" in the PR description.
- [ ] The bug was found by the module author, and no one has opened an
issue to report it yet.
- [ ] Feature update backwards compatible feature updates, and I have
bumped the MINOR version in `version.json`.
- [ ] Breaking changes and I have bumped the MAJOR version in
`version.json`.
  - [x] Update to documentation

## Checklist

- [x] I'm sure there are no other open Pull Requests for the same
update/change
- [x] I have run `Set-AVMModule` locally to generate the supporting
module files.
- [x] My corresponding pipelines / checks run clean and green without
any errors or warnings

<!-- Please keep up to day with the contribution guide at
https://aka.ms/avm/contribute/bicep -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: AVM 🅰️ ✌️ Ⓜ️ This is an AVM related issue
Projects
None yet
4 participants