Skip to content

Problem with If, and getSecret function #1440

Closed

Description

@PoborcaMaciej

Hello,

I have a problem with implementing tests for Bicep where I am using if conditions together with getSecret function. Do you know how I can solve it or if it is something that is not possible to omit?

Example of code that generates error
image

Exception calling "GetBicepResources" with "3" argument(s): "Unable to expand resources because the source file 'd:\agents\agent_work\24\s/tests/environments/test.bicep' was not valid. The template parameter 'test' does not use the required format."

Activity

  1. github-actions commented on Feb 21, 2023

    @github-actions
    Contributor

    Thanks for raising your first issue, the team appreciates the time you have taken 😉

  2. ElinoraWhite commented on Feb 21, 2023

    @ElinoraWhite
    Member

    PoborcaMaciej Thanks for raising the issue.

    It may depend on what your goal is with the condition that either returning a secret or I assume a dummy value.

    If you goal is to provide a test with a dummy value because you don't want to expose a secret in code then you shouldn't need to add the condition, you can just reference kv.getSecret() as you normally would where kv is an existing reference. For example:

    // ----------
    // REFERENCES
    // ----------
    
    resource kv 'Microsoft.KeyVault/vaults@2021-06-01-preview' existing = {
      name: 'kv-001'
    }
    
    resource ag 'Microsoft.Insights/actionGroups@2021-09-01' existing = {
      name: 'ag-001'
    }
    
    resource vnet 'Microsoft.Network/virtualNetworks@2021-05-01' existing = {
      name: 'vnet-001'
    }
    
    resource subnet 'Microsoft.Network/virtualNetworks/subnets@2021-05-01' existing = {
      parent: vnet
      name: 'subnet-001'
    }
    
    // ---------
    // RESOURCES
    // ---------
    
    // Test a basic VM
    module test_vm_with_no_disks '../main.bicep' = {
      name: 'test_vm_with_no_disks'
      params: {
        name: 'vm001'
        adminUsername: kv.getSecret('vm-username')
        adminPassword: kv.getSecret('vm-password')
        subnetId: subnet.id
        size: 'Standard_D2s_v3'
        imageSKU: '2019-Datacenter-Core'
        actionGroupId: ag.id
      }
    }

    PSRule for Azure will automatically use a placeholder value during expansion.


    Since this is more a question, I'm going to move it to discussion so that it might help someone else in the community out.

    I hope that helps.

  3. locked and limited conversation to collaborators on Feb 21, 2023
  4. converted this issue into a discussion #1441 on Feb 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions