vRO Code – Calculate CIDR notation

Recently I ran into situations where I needed to supply a network address in CIDR notation to external systems (infoblox and the oracle GRID installer in my case) from a vRealize Orchestrator workflow.

CIDR notation looks like this: 192.168.1.20/24. So instead of specifying the subnet mask using the 4 bytes separated by dots it just tells how many bits are used for the network number.

The thing is, all you can get from vRA is the regular subnet mask (255.255.255.0 in this example). Sometimes you can get away with a solution as simple as a few ifs or a switch/case but that’s not really the way to properly fix this.  I wanted to solve this once and for all so I wrote some JavaScript code for vRealize Orchestrator that calculates the number of bits in the mask and creates the CIDR network notation for you. Here it is:

  • Inputs for the script object or action:
    • gateway (String)
    • subnetMask (String)
  • Output:
    • cidr (String)

This script generates the network number but it can easily be adapted to return the ip address itself in CIDR notation.

 

 

2 thoughts on “vRO Code – Calculate CIDR notation”

  1. Line 6 should be “var numberOfBits = parseInt(maskPart, 10).toString(2).replace(/0/g,””).length;”

    1. I’m pretty sure it works fine as it is. But if you insist on using correct regex then your version is better 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *