Last active
May 7, 2018 21:19
-
-
Save kclinden/f1ec51a201419fcd86aa60ff5876672e to your computer and use it in GitHub Desktop.
Get Applicable Reservation Policies Custom Form Dropdown
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Description: This action is used with vRA to get applicable reservations policies using a custom form dropdown. | |
| //Inputs: | |
| //[STRING] blueprint -> bind with constant of blueprint id (Blueprint name with no spaces) | |
| //[STRING] component -> bind with constant of component such vSphere_Machine_1 | |
| //[STRING] user -> Requested For | |
| //[STRING] tenant -> Tenant reference | |
| //[STRING] subtenantId -> Subtenant reference | |
| //Return Type: Properties | |
| var IDS_PER_PAGE_LIMIT = 100; | |
| //var blueprint = System.getContext().getParameter("__asd_composition_blueprintId"); | |
| //var component = System.getContext().getParameter("__asd_composition_componentId"); | |
| //var user = System.getContext().getParameter("__asd_requestedFor"); | |
| //var tenant = System.getContext().getParameter("__asd_tenantRef"); | |
| //var subtenantId = System.getContext().getParameter("__asd_subtenantRef") | |
| var host = vCACCAFEHostManager.getDefaultHostForTenant(tenant , true); | |
| var reservationPolicyService = host.createReservationClient().getReservationReservationPolicyService(); | |
| var reservations = System.getModule("com.vmware.vra.reservations").getReservationsForUserAndComponent(user,tenant,host, blueprint, component, subtenantId); | |
| // get reservation filtered by reservations | |
| var reservationPolicies = []; | |
| if (reservations.length !== 0) { | |
| var totalPages = reservations.length / IDS_PER_PAGE_LIMIT + 1; | |
| for (var page = 0; page < totalPages; page++) { | |
| var from = page * IDS_PER_PAGE_LIMIT; | |
| var to = from + IDS_PER_PAGE_LIMIT; | |
| var reservationPolicyFilter = getReservationPolicyFilterForReservations(reservations.slice(from, to)); | |
| if (reservationPolicyFilter) { | |
| Array.prototype.push.apply(reservationPolicies, | |
| reservationPolicyService.getAllReservationPolicies(reservationPolicyFilter).getContent()); | |
| } | |
| } | |
| } | |
| var reservationPolicyProperties = new Properties(); | |
| for each(var reservationPolicy in reservationPolicies) { | |
| reservationPolicyProperties.put(reservationPolicy.getId(),reservationPolicy.getName()); | |
| } | |
| return reservationPolicyProperties; | |
| ////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
| function getReservationPolicyFilterForReservations(reservations) { | |
| var query = new vCACCAFEOdataQuery(); | |
| var queryParams = []; | |
| for each (var reservation in reservations) { | |
| queryParams.push(vCACCAFEFilterParam.equal("id" , vCACCAFEFilterParam.string(reservation.getReservationPolicyId()))); | |
| } | |
| if(queryParams.length > 1) { | |
| query.addFilter([vCACCAFEFilterParam.or(queryParams)]); | |
| } else if(queryParams.length == 1){ | |
| query.addFilter(queryParams); | |
| } else { | |
| return null; | |
| } | |
| return new vCACCAFEPageOdataRequest(1 , IDS_PER_PAGE_LIMIT, query); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment