Layer7 API Management

  • 1.  Using context variables from parent policy when using included policy fragments

    Posted Jun 26, 2017 06:25 AM

    I am trying to make my policies more generic and in that I have found that policy fragments are a nice tool. It seems however that I can not reach context variables of the policy that include a fragment from within the child fragment.

     

    How can this be a accomplished?

     

    I.e. I need to pass variables from the parent policy to the logic of an included policy fragment.

     

    (In my case I want the included fragment to use an xpath that is passed down from the parent, and is different for each parent that utilize the policy fragment).

     

    Thank you!



  • 2.  Re: Using context variables from parent policy when using included policy fragments
    Best Answer

    Broadcom Employee
    Posted Jun 26, 2017 08:23 AM

    Hi David,

     

    The assertion "Export Variable From Policy Fragment" can help here. In my example I set the variable 'first' to John in my main policy. In the included fragment I set 'last' to Doe and concatenate the two in the fragment:

     

     

     

     

    More details can be found here:

    Export Variables from Fragment Assertion - CA API Gateway - 9.2 - CA Technologies Documentation 

     

    Regards,

    Joe



  • 3.  Re: Using context variables from parent policy when using included policy fragments

    Posted Jun 26, 2017 08:58 AM

    Aha, so export works both from child to parent and parent to child by introducing a shared scope...



  • 4.  Re: Using context variables from parent policy when using included policy fragments

    Posted Jun 26, 2017 09:54 AM

    I Still have some issues, trying both setContext Variable using request.shared and lookup contextvariable. Both fails!

    In the service debugger i see %%SHARED%%.request.shared.SOAPRequest. Which is what I try to use from within the child...

    What am I missing?

     

       



  • 5.  Re: Using context variables from parent policy when using included policy fragments

    Broadcom Employee
    Posted Jun 26, 2017 04:28 PM

    When using the Look Up Context Variable assertion you should leave off the usual variable formatting ${}

    i.e: To get the value of the exported variable LAST we should use request.shared.last rather than ${request.shared.last}

     

    ${lookup.found}               //Returns True
    ${lookup.output}               //Returns DOE

     



  • 6.  Re: Using context variables from parent policy when using included policy fragments

    Posted Jun 29, 2017 01:20 PM

    that is not the proper use of 'Export variables from fragment'

    1. it should be used within a policy fragment

    2. the policy you've shown does not require that assertion in order to send the response you have shown here



  • 7.  Re: Using context variables from parent policy when using included policy fragments

    Posted Jun 29, 2017 01:16 PM

    All of the variables declared in the calling policy will be available in the called fragment, however because the policy manager does not know what policy will be calling the fragment it cannot prototype them for you, but they will work at run time.  The assertion 'Export Variables from Fragment' will return variables to the calling policy but is not required to import variables, that is automatic.  Also, there are certain variables that exist by reference and so modifications to them will be returned reguardeless of whether exporting (i.e. the request, response, and error template objects).  You should also note that the error template (unlike request and response) acts like a just in time object, so variables refrenced within it that are declared within a fragment must be exported in order for their values to be available at the end of the parent policy execution.

    If you want more regimented varable declarations (and to be able to reference them in the policy fragment without a validation warning) then you should wrap your fragment as an encapsulated assertion, which can import variables implicetly (by leaving the 'Show in assertions properties dialog' unchecked) so you don't have to type them in.