VMware Cloud Community
BWinchell
Enthusiast
Enthusiast
Jump to solution

Embedded variable VS general attribute

Hello,

I have a weird situation where I am passing a variable into a module that is not getting interpreted correctly.  When the variable is passed into the module, I receive an error.  When the variable is embedded into the module, works as expected.

Orchestrator 7.0.1.17606 build 3571217

Code:

//var re = /(([^\-]*)\-(\d{2})(\d{2})(\d{2})(.)\-(GE|SR))/;

var re = datastoreFilterRegEx;

System.debug("DEBUG_LOG_Module=FindDatastore: var re= " + re);

var m;

var targetDatastore = [];

var len = datastores.length;

for (i = 0; i < len; i++) {

    var str = datastores[i].name;

    //Debug

        if (debugOutput == true) {

            System.debug("DEBUG_LOG_Module=FindDatastore: var str = " + str);

        }

    if ((m = re.exec(str)) !== null) {

            //Debug

                if (debugOutput == true) {

                    System.debug("DEBUG_LOG_Module=FindDatastore: var m = " + m);

                }

        var volumeName = m[0];

        var sanName = m[2];

        var arrayType = m[3];

        var diskType = m[4];

        var diskRaid = m[5];

        var volumeUse = m[6];

        var volumeAccess = m[7];

            //Debug

                if (debugOutput == true) {

                    System.debug("DEBUG_LOG_Module=FindDatastore: volumeName: " + volumeName);

                    System.debug("DEBUG_LOG_Module=FindDatastore: sanName: " + sanName);

                    System.debug("DEBUG_LOG_Module=FindDatastore: arrayType: " + arrayType);

                    System.debug("DEBUG_LOG_Module=FindDatastore: diskType: " + diskType);

                    System.debug("DEBUG_LOG_Module=FindDatastore: diskRaid: " + diskRaid);

                    System.debug("DEBUG_LOG_Module=FindDatastore: volumeUse: " + volumeUse);

                    System.debug("DEBUG_LOG_Module=FindDatastore: volumeAccess: " + volumeAccess);

                }

        if (volumeAccess === "SR") {

            //Debug

                if (debugOutput == true) {

                    System.debug("DEBUG_LOG_Module=FindDatastore: Datastore is not suitable: " + datastores[i].name);

                }

            re.lastIndex++;

        }

        if (volumeAccess === "GE") {

            System.log("This datastore is a possibility: " + datastores[i].name);

            targetDatastore.push(datastores[i]);

        }

    }

}

Log output:

[2016-08-23 13:45:01.045] [D] DEBUG_LOG_Module=FindDatastore: var re= /(([^\-]*)\-(\d{2})(\d{2})(\d{2})(.)\-(GE|SR))/

[2016-08-23 13:45:01.045] [D] DEBUG_LOG_Module=FindDatastore: var str = Datastore01

[2016-08-23 13:45:01.070] [E] Workfow execution stack:

***

item: 'TEMP_regExDeployVmSetup_v5/item2', state: 'failed', business state: 'null', exception: 'TypeError: Cannot find function exec in object /(([^\-]*)\-(\d{2})(\d{2})(\d{2})(.)\-(GE|SR))/. (Workflow:TEMP_regExDeployVmSetup_v5 / FindDatastore_Initial (item10)#27)'

workflow: 'TEMP_regExDeployVmSetup_v5' (6d64f977-589d-4203-8fcf-8c2deadc9a64)

|  'attribute': name=vm type=VC:VirtualMachine value=dunes://service.dunes.ch/CustomSDKObject?id='vcenter.thenewsgroup.com/vm-164091'&dunesName='VC:VirtualMachine'

|  'attribute': name=errorCode type=string value=TypeError: Cannot find function exec in object /(([^\-]*)\-(\d{2})(\d{2})(\d{2})(.)\-(GE|SR))/. (Workflow:TEMP_regExDeployVmSetup_v5 / FindDatastore_Initial (item10)#27)

Problem:

  • Line 01 has the variable as if it was embedded.  If I uncomment that line and comment out line 02, the module completes fine.
  • The log shows the variable as the same (text wise) as the embedded variable but it does not like it.
    • It is being interpreted as a function but cannot figure out why

Thanks

B

0 Kudos
1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

Sure, it can have it as a string which can be modified, passed around, etc. When you need to perform reg expression matching, you just need to instantiate a new RegExp object by passing the string to its constructor

// filter as plain string variable

// you can do all operations allowed on strings - modify it, pass it as attribute to another action, etc.

var datastoreFilterStr = "(([^\\-]*)\\-(\\d{2})(\\d{2})(\\d{2})(.)\\-(GE|SR))";

// in another action, where datastoreFilterStr is passed/accessed as a plain string

var datastoreFilterRegEx = new RegExp(datastoreFilterStr);

// call some method on the regexp object

View solution in original post

0 Kudos
4 Replies
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,

The error is expected, that's how JS regular expressions work.

/(([^\-]*)\-(\d{2})(\d{2})(\d{2})(.)\-(GE|SR))/ is so called RegExp literal. Note: it is not a string; it is a syntactic sugar for RegExp constructor.

So instead of passing datastoreFilterRegEx as a normal string, consider setting its value using RegExp constructor syntax; something like the following:

datastoreFilterRegEx = new RegExp("(([^\\-]*)\\-(\\d{2})(\\d{2})(\\d{2})(.)\\-(GE|SR))");

Note that in the constructor argument, comparing to RegExp literal, the leading and trailing slash characters / are gone, and each backslash character \ is escaped with another backslash character.

0 Kudos
BWinchell
Enthusiast
Enthusiast
Jump to solution

Is there a way I can pass that string in as a variable?

The reason I ask is like to keep any variable that might be modified by anyone, outside the module so you can find them all in the general attribute area.

I tried passing it as a string, RegExp, changed the syntax around but cannot get it.  Just don't fully understand it yet.

Thanks

B

0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

Sure, it can have it as a string which can be modified, passed around, etc. When you need to perform reg expression matching, you just need to instantiate a new RegExp object by passing the string to its constructor

// filter as plain string variable

// you can do all operations allowed on strings - modify it, pass it as attribute to another action, etc.

var datastoreFilterStr = "(([^\\-]*)\\-(\\d{2})(\\d{2})(\\d{2})(.)\\-(GE|SR))";

// in another action, where datastoreFilterStr is passed/accessed as a plain string

var datastoreFilterRegEx = new RegExp(datastoreFilterStr);

// call some method on the regexp object

0 Kudos
BWinchell
Enthusiast
Enthusiast
Jump to solution

I did not think of the simple answer of passing the variable in and then converting inside the module (was trying to do it in one step).

Thanks

B

0 Kudos