4 Replies Latest reply on May 30, 2018 9:59 PM by kumar6384

    [vRA 7.4] - Update existing RESERVATION

    ymichalak Novice

      Yep team,

       

      We need yet your help.

      We have an existing RESERVATION with Networks & Networks profiles associated.

       

      network.png

       

       

      Now from vRO we want to UNcheck (remove) some networks of this reservation.

       

      We don't find any TOPIC or DOC about this management from vRO....

       

      We have only found one post with POWERvra :

      remove a network profile you can pass an empty string to the NetworkProfile parameter.

      Get-vRAReservation -Name "Reservation01" | Set-vRAReservationNetwork -NetworkPath "VM Network" -NetworkProfile ""

       

      How can we update vRA reservation from vRO ?

       

      Thx for your help.

        • 1. Re: [vRA 7.4] - Update existing RESERVATION
          eservent Enthusiast

          Hello ymichalak,

           

          I found a way to remove networks from a reservation.

          I was helped by this really usefull post : How to create a new reservation in vRO .

           

          Here is the code. Tell me if it's working.

           

          var reservationNameInput = "MyReservation";
          var uncheckNetworkLabelListInput = ["VM Network"];
          
          System.log("Search for reservation with name " + reservationNameInput);
          
          var vcaccafeHost = Server.findAllForType("vCACCAFE:VCACHost")[0];
          var reservationClient = vcaccafeHost.createReservationClient();
          var reservationService = reservationClient.getReservationReservationService();
          var tenantReservationPagedResources = reservationService.getAllReservations(null);
          
          var reservationToUpdate = null;
          for each(var reservation in tenantReservationPagedResources.getContent()){
              System.log("Reservation " + reservation.getName())
              if(reservation.getName().toLowerCase() == reservationNameInput.toLowerCase()) {
                  reservationToUpdate = reservation;
                  break;
              }
          }
          
          System.log("Found Reservation " + reservationToUpdate.getName());
          
          var extData = reservationToUpdate.getExtensionData();
          var networkLiteral = extData.get("reservationNetworks");
          
          var complexArray =  new Array();
          for each(var network in networkLiteral.getValue()) {
              var networkLabel = network.getValue().get("networkPath").getLabel();
              System.warn(networkLabel);
              var keepNetwork = true;
              for each(var uncheckNetworkLabel in uncheckNetworkLabelListInput) {
                  if(uncheckNetworkLabel == networkLabel) {
                      keepNetwork = false;
                      break;
                  }
              }
              if(keepNetwork) {
                  complexArray.push(network);
              }
          }
          
          var typeID = vCACCAFEDataTypeId.valueOf("COMPLEX") ;
          var newNetworkLiteral = new vCACCAFEMultipleLiteral(complexArray, typeID);
          
          extData.put("reservationNetworks", newNetworkLiteral)
          
          reservationService.updateReservation(reservationToUpdate);
          

           

           

          Emmanuel.

          1 person found this helpful
          • 3. Re: [vRA 7.4] - Update existing RESERVATION
            eservent Enthusiast

            Your're welcome.

            Could you mark the answer as correct to "solve" the question, please ?

             

            Emmanuel.

            • 4. Re: [vRA 7.4] - Update existing RESERVATION
              kumar6384 Enthusiast

              Hi,  This is very useful to you, please refer the following code. Here is the action to Update a reservation:

               

               

              var reservations = new Array();

              var portGroupName=new Array();

              var dvPG = [];

              try{

              var tenantName = cafeHost.tenant;

              var allReservations = getAllReservations(cafeHost);

               

              //System.debug("\n\n---------------------- Tenant ------------------------------");

              //System.debug("Name: "+tenantName);

              //System.debug("----------------------------------------------------");

               

              /// Metro Cluster function

              var metroclus = compartment + "_ESX_CL_METRO_DBL01";

              System.log("Reservation Name: "+metroclus+"\n");

               

              //Get applicable  Reservations details

              for each (cafeReservation in allReservations){

              //System.log("Reservation Name: "+cafeReservation.name+"\n");

              if((cafeReservation.name.indexOf(compartment) != -1) && (cafeReservation.name.indexOf("ReservationName") != -1)){

              System.log("Reservation Name: "+cafeReservation.name+"\n");

              reservations.push(cafeReservation);

              }else if(String(metroclus) == String(cafeReservation.name)){

              System.log("Reservation Name: "+cafeReservation.name+"\n");

              reservations.push(cafeReservation);

              }

              }

               

              //System.debug("\n\n---------------------- Reservations ------------------------------");

              //System.debug("reservations: \n "+reservations);

              //System.debug("----------------------------------------------------");

               

              var vCenter;

              //Get Logical Swithes details

              var vCenter = System.getModule("org.company.VMwarePSO").getVCenter(vCenterConfig,compartment) ;

              System.debug("[vCenter]:\t" + vCenter + "\n\n");

              dvPG = vCenter.getAllDistributedVirtualPortgroups();

              for each (var pg in dvPG){

              pgName=pg.name;

              //System.log("Before split: "+pgName+"\n");

              pgName = pgName.split('(')[0].replace(' ','');

              //System.log("After split: "+pgName+"\n");

              if((pgName.indexOf(systemId) > -1) && (pgName.indexOf(geoTag) > -1)){

              //System.log("Inside Loop: "+pgName+"\n");

              portGroupName.push(pgName);

              }

                   

              }

              System.debug("\n\n---------------------- Portgroupname ------------------------------");

              System.warn("\n portGroupName to be updated with network is: \n"+portGroupName);

              System.debug("\n\n----------------------------------------------------");

               

              for each (cafeReservation in reservations){

              // System.log("************ START FOR "+cafeReservation.name);

              var computeResource = getComputeResourceOfRes(cafeReservation); // compute resource name

               

               

              // System.debug("\n\n---------------------- computeResource ------------------------------");

              // System.warn("\n computeResource is: \n"+computeResource);

              // System.warn("\n computeResource is: \n"+computeResource.split("(")[0].replace(' ',''));

              // System.debug("\n\n----------------------------------------------------");

              computeResource = computeResource.split("(")[0].replace(' ','');

               

              //System.log("computeResource: "+ computeResource);

              //System.debug("\n \n [computeResource]:\t "+ computeResource);

               

              var newNetworkLiteralArr = new Array();

              for each (var networkName in portGroupName) {

               

              var networkProfileName = compartment+"-"+networkName.substring(networkName.indexOf(""+systemId+"")).trim();

              System.warn("computeResource: "+computeResource+" networkName: "+networkName.replace(' ','')+" networkProfileName: "+networkProfileName);

              if(!isNetworkConfiguredForReservation(cafeReservation, networkName.replace(' ',''))){

              System.debug("computeResource: "+computeResource+" networkName: "+networkName.replace(' ','')+" networkProfileName: "+networkProfileName);

              var newNetworkLiteral = buildComplexNetworkLiteral(iaasHost, computeResource, networkName.replace(' ',''),networkProfileName)

              System.debug("\n Network Literal:" +newNetworkLiteral);

              newNetworkLiteralArr.push(newNetworkLiteral);

              }

              }

               

               

              System.warn("Before updating Reservation !!!");

              updateReservation(cafeHost, cafeReservation, newNetworkLiteralArr);

              System.log("*********** updated networkName: "+networkName)

              System.log("*********** END reservation: "+cafeReservation.name);

              //System.sleep(2*1000);

              }

              //output = "true";

              } //for each (res in allReservations)

              //trty

              catch(error){

              System.log("----------------------------------------");

              //System.log(error);

              //throw error;

              output = error;

              System.error(error);

              System.log("----------------------------------------");

              }

               

               

               

               

              //--------------------------------------------------

              function isNetworkConfiguredForReservation(reservation, network){

              var extData = reservation.getExtensionData();

              var networksMultiLiteral = extData.get("reservationNetworks");

              var result = false;

               

               

              for each(var networkLiteral in networksMultiLiteral.getValue()){

              var literalMap = networkLiteral.getValue();

              var networkLabel = literalMap.get("networkPath").getLabel();

               

               

              if(networkLabel.toLowerCase() == network.toLowerCase()){

              result = true;

              break;

              }

              }

              return result;

              }

              function updateReservation(tenantCafeHost, cafeReservation, newNetworkLiteralArr){

              var networksLiteralArray = new Array();

              var reservationClient = tenantCafeHost.createReservationClient();

              var reservationService = reservationClient.getReservationReservationService();

               

              var extData = cafeReservation.getExtensionData();

              //System.log("extData before: "+extData.get("reservationNetworks"));

               

               

              if(newNetworkLiteralArr != null){

              System.log("newNetworkLiteral != null");

              var reservationNetworksMultiLiteral = extData.get("reservationNetworks");

              var networksLiteralArray = reservationNetworksMultiLiteral.getValue();

              for each(n in newNetworkLiteralArr)

              networksLiteralArray.push(n);

               

              //networksLiteralArray.push(newNetworkLiteral);

              //System.log("New Networks in Reservation: "+networksLiteralArray);

              var typeID = reservationNetworksMultiLiteral.getTypeId();

              var newReservationNetworkMultiLiteral = new vCACCAFEMultipleLiteral(networksLiteralArray, typeID);

               

               

              extData.put("reservationNetworks", newReservationNetworkMultiLiteral);

              //System.log("extData after: "+extData);

              }

               

               

              cafeReservation.setExtensionData(extData);

              reservationService.updateReservation(cafeReservation);

              }

               

               

               

               

               

               

              function getReservationByName(tenantCafeHost, name){

              System.log("Search for reservation with name " + name);

              var reservationClient = tenantCafeHost.createReservationClient();

              var reservationService = reservationClient.getReservationReservationService();

              var tenantReservationPagedResources = reservationService.getAllReservations(null);

               

              for each(var reservation in tenantReservationPagedResources.getContent()){

              //System.log("Reservation " + reservation.getName())

              if(reservation.getName().toLowerCase() == name.toLowerCase()){

              return reservation;

              }

              }

              return null;

              }

               

               

              function buildComplexNetworkLiteral(vcacHost, computeResourceHostName, netLabel, netProfileLabel){

              var literalMap =  new vCACCAFELiteralMap();

              literalMap.put("networkPath", getNetworkPathEntityReference(vcacHost, computeResourceHostName, netLabel));

              literalMap.put("networkProfile", getNetworkProfileEntityReference(vcacHost, netProfileLabel));

               

               

              var componentTypeId = "com.vmware.csp.iaas.blueprint.service";

              var componentId = null;

              var classId = "Infrastructure.Reservation.Network";

              var typeFilter = null;

               

               

              return new vCACCAFEComplexLiteral(componentTypeId, componentId, classId, typeFilter, literalMap);

              }

               

               

               

               

               

               

              function getNetworkPathEntityReference(vcacHost, computeResourceHostName, netLabel){

              var id = getNetworkId(vcacHost, netLabel, computeResourceHostName);

              System.log("Network ["+netLabel+"] id = " + id);

              var componentId = null;

              var classId = "Network";

              var entityReference = new vCACCAFEEntityReference(componentId, classId, id, netLabel);

               

              return entityReference;

              }

               

               

              function getNetworkId(vcacHost, netLabel, computeResourceHostName){

               

               

              System.log("********************* inside getNetworkId");

              //System.log();

              var networks = getInfrastructureManagementModelEntitiesByProperty(vcacHost, "HostNics", "HostNicName", netLabel);

              //System.log("******* networks: "+networks);

              var resultNetwork = null;

               

              for each(var network in networks){

              var host = network.getLink(vcacHost, "Host")[0];

              var hostName = host.getProperty("HostName");

              //System.log("hostname: "+hostName);

              //System.log("computeResourceHostName: "+computeResourceHostName);

              if(hostName.toLowerCase() == computeResourceHostName.toLowerCase()){

              resultNetwork = network;

              break;

              }

              }

               

               

              if(resultNetwork == null){

              throw("No network with name [" + netLabel + "] was found for compute resource [" + computeResourceHostName +"]");

              }

               

               

              return resultNetwork.getProperty("HostNicID");

              }

               

               

              function getNetworkProfileEntityReference(vcacHost, netProfileLabel){

              var id = getNetworkProfileIdByLabel(vcacHost, netProfileLabel);

              var entityReference =null;

              if(id){

              var componentId = null;

              var classId = "NetworkProfile";

              entityReference = new vCACCAFEEntityReference(componentId, classId, id, netProfileLabel);

              }

               

               

              return entityReference;

              }

               

               

              function getNetworkProfileIdByLabel(vcacHost, netProfileLabel){

              var networkProfile = getInfrastructureManagementModelEntityByProperty(vcacHost, "StaticIPv4NetworkProfiles", "StaticIPv4NetworkProfileName", netProfileLabel);

              var id = null;

              if(networkProfile != null){

              System.log("Network profile with name " + netProfileLabel + " found.");

              id = networkProfile.getProperty("ID");

              }else{

              System.log("Network profile with name " + netProfileLabel + " not found.");

              }

               

               

              return id;

              }

               

               

              function getInfrastructureManagementModelEntityByProperty(vcacHost, entityType, propertyName, propertyValue){

              var modelName = 'ManagementModelEntities.svc';

              var entitySetName = entityType;

              var inputProperties = new Properties();

              inputProperties.put(propertyName, propertyValue);

               

               

              return getInfrastructureManagementModelEntitiesByProperty(vcacHost, entityType, propertyName, propertyValue)[0];

              }

               

               

              function getInfrastructureManagementModelEntitiesByProperty(vcacHost, entityType, propertyName, propertyValue){

              var modelName = 'ManagementModelEntities.svc';

              var entitySetName = entityType;

              var inputProperties = new Properties();

              inputProperties.put(propertyName, propertyValue );

               

               

              return vCACEntityManager.readModelEntitiesByCustomFilter(vcacHost.id, modelName, entitySetName, inputProperties, null);

              }

               

               

              function getAllReservations(tenantCafeHost){

              System.log("Get all reservations for host: "+tenantCafeHost);

               

              var reservationClient = tenantCafeHost.createReservationClient();

              var reservationService = reservationClient.getReservationReservationService();

              var tenantReservationPagedResources = reservationService.getAllReservations(null);

              var allReservations = new Array();

               

              for each(reservation in tenantReservationPagedResources.getContent()){

              //System.log("reservation.name: "+reservation.name);

              allReservations.push(reservation);

              }

               

               

              return allReservations;

              }

               

               

              function getComputeResourceOfRes(reservation){

              System.log("inside get compute resource for reservation");

               

              var extensionData = reservation.getExtensionData();

              var reservationComputeResource = extensionData.get("computeResource").getLabel();

              System.log("reservation " + reservation.name);

              System.log("computeResource name: " + reservationComputeResource);

               

               

              return reservationComputeResource;

              }