VMware Cloud Community
exm1110b
Contributor
Contributor

automate vapp property creation with api or ovftool?

hi..

i'm creating a vm via packer automation,  i was wondering if there's after creation to use ovftool or even a direct api call to vcenter to add vapps properties

i'm talking about version, vendor, vapp ip (static vs dhcp etc..)

Tags (3)
0 Kudos
1 Reply
dgold
Enthusiast
Enthusiast

yes it can be done via VirtualMachine API in SDK.   ovftool does the same thing when it deploys the VM, it then inserts all the vApp properties defined on the command line into the VM. ovftool requires the values to be there in OVF already and values passed in.  With SDK you can add it after.  

you need to insert key/value pairs in 

VirtualMachine's ConfigSpec.extraConfig 

push it into VM by reconfiguring the VM and once it powers on it all set

here is a very rough uncompilable c++ code but similar can be done in any language.

   Vm::ConfigSpec* configSpec = new Vm::ConfigSpec();

   DataArray<Option::OptionValue>* excs = new DataArray<Option::OptionValue>();

  // kvs is the array with all params you have in key value format

   for(int i = 0; i < kvs->GetLength(); i++) {

      KeyValue *kv = kvs->GetAt(i);

      Option::OptionValue* ov = new Option::OptionValue();

      ov->SetKey(string("guestinfo.") + kv->GetKey());

      ov->SetValue(NewPrimitive<string>(kv->GetValue()));

      excs->Append(ov);

   }

 

   // if target is Esx then extra parameter is required to get it into the VM

   string productName = siC->GetAbout()->GetProductLineId();

   if (IsTargetESXFamily(productName)) {

      Option::OptionValue* ov = new Option::OptionValue();

      ov->SetKey(string("guestinfo.ovfEnv"));

      // generateOvfEnv creates an xml for the ovf properties you have in ovfProperties shown below the code

      // moId is ManagedObject id

      string ovfEnv = GenerateOvfEnv(moId, ovfProperties);

      ov->SetValue(new <string>(ovfEnv));

 

      excs->Append(ov);

   }

 

   configSpec->SetExtraConfig(excs);

 

   MoRef recTaskMoRef;

   VirtualMachine* vm = get vm that you have;

   vm->Reconfigure(configSpec, recTaskMoRef);

 

   TaskInfo recTaskInfo;

   WaitForTask(taskMoRef, this, recTaskInfo /* out */);

   

   

   

<?xml version="1.0" encoding="UTF-8"?>

<Environment 

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:ovfenv="http://schemas.dmtf.org/ovf/environment/1"

    xmlns="http://schemas.dmtf.org/ovf/environment/1"

    xsi:schemaLocation="http://schemas.dmtf.org/ovf/environment/1 ../ovf-environment.xsd"

    ovfenv:id="vm1">

    

    <!-- Information about hypervisor platform -->

    <PlatformSection>        

        <Kind>ESX Server</Kind>

        <Version>3.0.1</Version>

        <Vendor>VMware, Inc.</Vendor>

        <Locale>en_US</Locale>

        <Timezone>-300</Timezone>

    </PlatformSection>

    

    <!--- Properties defined for this virtual machine -->

    <PropertySection>

        <Property ovfenv:key="ip" ovfenv:value="10.20.23.34"/>

        <Property ovfenv:key="debug" ovfenv:value="off"/>

    </PropertySection>

                        

</Environment>

 

 

   Vm::ConfigSpec configSpec = new Vm::ConfigSpec();
 
   DataArray<Option::OptionValue> excs = new DataArray<Option::OptionValue>();
   for(int i = 0; i < kvs->GetLength(); i++) {
      KeyValue *kv = kvs->GetAt(i);
      Option::OptionValue* ov = new Option::OptionValue();
      ov->SetKey(string("guestinfo.") + kv->GetKey());
      ov->SetValue(NewPrimitive<string>(kv->GetValue()));
      excs->Append(ov);
   }
 
   // if target is Esx then extra parameter is required to get it into the VM
   string productName = siC->GetAbout()->GetProductLineId();
   if (IsESXFamily(productName)) {
      Option::OptionValue* ov = new Option::OptionValue();
      ov->SetKey(string("guestinfo.ovfEnv"));
      // generateOvfEnv creates an xml for the ovf properties you have in ovfProperties shown below the code
      // moId is ManagedObject id
      string ovfEnv = GenerateOvfEnv(moId, ovfProperties);
      ov->SetValue(new <string>(ovfEnv));
 
      excs->Append(ov);
   }
 
   configSpec->SetExtraConfig(excs);
 
   MoRef taskMoRef;
   VirtualMachine* vm = get vm that you have;
   vm->Reconfigure(configSpec, taskMoRef);
 
   TaskInfo taskInfo;
   WaitForTask(taskMoRef, this, taskInfo /* out */);
   
   
   
<?xml version="1.0" encoding="UTF-8"?>
<Environment 
    xsi:schemaLocation="http://schemas.dmtf.org/ovf/environment/1 ../ovf-environment.xsd"
    ovfenv:id="vm1">
    
    <!-- Information about hypervisor platform -->
    <PlatformSection>        
        <Kind>ESX Server</Kind>
        <Version>3.0.1</Version>
        <Vendor>VMware, Inc.</Vendor>
        <Locale>en_US</Locale>
        <Timezone>-300</Timezone>
    </PlatformSection>
    
    <!--- Properties defined for this virtual machine -->
    <PropertySection>
        <Property ovfenv:key="ip" ovfenv:value="10.20.23.34"/>
        <Property ovfenv:key="debug" ovfenv:value="off"/>
    </PropertySection>
                        
</Environment>
0 Kudos