Thursday, January 6, 2011

How to create a new vApp

There are many ways one can create a vApp in vCloud, and they vary depending on where one starts. Here are some of the options. As promised we will use the SDK to illustrate the example(s).

  • Create one form a template (POST .../api/v1.0/vdc/<vdc-id>/action/instantiateVAppTemplate)
  • Start from OVF package.
  • Assorted vApp:
    • Compose new vApp from templates and/or existing vApp
      (POST .../api/v1.0/vdc/<vdc-id>/action/composeVAp)
    • Recompose vApp to add/delete VM in it.
      POST .../api/v1.0/vApp/<vapp-id>/action/recomposeVApp (This is not really creating a new vApp so we will visit it next time).
  • Clone from existing vApp POST .../api/v1.0/vdc/<vdc-id>/action/cloneVApp
  • Importing from vSphere 
Let’s visit the above one by one:
1.    Start from OVF Package:
This is the most ground up operation one can think of for creating a vApp. You are basically loading the vApp from the OVF as a vApp Template in a catalog and then its matter of details for instantiating vApp from template as in #2. So let’s look at the first thing first: uploading OVF as vApp Template. The OVF file essentially describes how the vApp is configured using various ‘Sections’ and bundles with the  disk files of the virtual machine. Even before we upload this vApp description we need to create a ‘vApp Template’ object in the vCloud. The SDK provides a method on Vdc object ‘createVappTemplate’.

UploadVAppTemplateParamsType vappTemplParams = new
UploadVAppTemplateParamsType();

vappTemplParams.setDescription("New Template Description");
vappTemplParams.setName("NewVAppTemplate");

VappTemplate vappTemplate = vdc.createVappTemplate(vappTemplParams);

It goes without saying that this template needs to be later added to a Catalog. Once the object is created we upload the OVF descriptor using method on newly created vApp template ‘uploadOVFFile’.

File ovfFile = new File(ovfFileLocation);

FileInputStream ovfFileInputStream = new FileInputStream(ovfFile);

vappTemplate.uploadOVFFile(ovfFileInputStream, ovfFile.length());

 As a result of resolving the OVF descriptor, the Template object knows how many virtual machines are part of the vApp and how many disk every machine has. We are yet to upload the disk files so as a pre-requisite for that it creates a place holder link for those disk files and adds details to the vApp Template object. Make sure that you refresh the vApp Template object on the client side so as to get this newly available configuration data. Now you can upload the disk files to the cloud and take a coffee break.

// wait to make sure OVF descriptor is uploaded.
// Refresh the vApp Template object.
File vmdkFile = new File(vmdkFileLocation);
FileInputStream vmdkFileInputStream = new FileInputStream(vmdkFile);

vappTemplate.uploadFile(vmdkfileName, vmdkFileInputStream, vmdkFile.length());

 Once done go on to the next step of creating a vApp from template as follows. All this available in the sample provided as part of the vCloud SDK.


2.    Create a vApp from vApp Template:
This is a very straight forward way of creating a vApp from a Template. In other words we are instantiating a vApp Template into a vApp. Here vApp Template works as a blue print or a mold for creating a vApp. Of course there are certain instantiation parameters (some required, some optional) that needs to be set for every new vApp that gets created.
Although networking warrants a separate detailed discussion let me just get it started here as many users may do this right/wrong unknowingly.
One of the important aspects of the instantiation is Network Settings for the vApp.  If you have already looked at the ‘Hello vCloud’ sample distributed with the vCloud Java SDK, it creates a new vApp Network by creating a completely new ‘Network Config Section’ and correspondingly new ‘Network Config’ in it and subsequently ‘Configuration’. The actual type definitions for them are as follows:

          Java Type                                     XML Element                      Description
1. NetworkConfigSectionType             NetworkConfigSection         This is an extension of ovf:SectionType
2. VAppNetworkConfigurationType     NetworkConfig                     vApp Network
3. NetworkConfigurationType              Configuration                      Configuration info for the above vApp 
                                                                                                       Network configuration. E.g. Parent
                                                                                                       Network, Fence Mode, etc.

The above table shows how the three are related and play part in defining the vApp network. Following shows how a vApp Network is configured using the Data Types in above table.
NetworkConfigurationType networkConfigurationType = new NetworkConfigurationType();

networkConfigurationType.setParentNetwork(
vdc.getAvailableNetworkRefs().iterator().next());

networkConfigurationType.setFenceMode(FenceModeValuesType.BRIDGED);

VAppNetworkConfigurationType vAppNetworkConfigurationType =
new VAppNetworkConfigurationType();

vAppNetworkConfigurationType.setConfiguration(networkConfigurationType);
vAppNetworkConfigurationType.setNetworkName(
      vdc.getAvailableNetworkRefs().iterator().next().getName());

NetworkConfigSectionType networkConfigSectionType = new NetworkConfigSectionType();
MsgType networkInfo = new MsgType();
networkConfigSectionType.setInfo(networkInfo);
List<VAppNetworkConfigurationType> vAppNetworkConfigs = networkConfigSectionType.getNetworkConfig();
vAppNetworkConfigs.add(vAppNetworkConfigurationType);

It is possible that the vApp Template already has the vApp network details configured in such case it is not necessary to go through the steps of vApp Network configuration as in the sample and the instantiation step (Instantiation Params) can ignore it completely.

 InstantiationParamsType instantiationParamsType = new InstantiationParamsType();

List<JAXBElement<? extends SectionType>> sections = instantiationParamsType.getSection();
sections.add(new ObjectFactory().createNetworkConfigSection(networkConfigSectionType));

InstantiateVAppTemplateParamsType instVappTemplParamsType = new InstantiateVAppTemplateParamsType();
instVappTemplParamsType.setName("NewAppp");
instVappTemplParamsType.setSource(vAppTemplateReference);
instVappTemplParamsType.setInstantiationParams(instantiationParamsType);

Vapp vapp = vdc.instantiateVappTemplate(instVappTemplParamsType);

 Most importantly, just by doing above will not get the VM in the vApp connected to the network automatically, unless of course it is already configured in the template. Hence there is a separate step after the instantiation of vApp to configure the VM network.

The following code snippet shows configuration of VM network and its association with vApp network.
Every VM consists of Network Connection section that includes all the network interfaces for the VM as ‘Network Connection’.  Every ‘Network Connection’ in turn provides details such as IP Address, MAC Address, IP Address Allocation Mode etc. for that particular network interface of the VM.  One VM can have multiple such interfaces. It’s necessary that one of them is identified as a ‘Primary Network connection’. User can chose to add a new interface or change the existing one as needed.

NetworkConnectionSectionType networkConnectionSectionType = childVm
                    .getNetworkConnectionSection();
List<NetworkConnectionType> networkConnections = networkConnectionSectionType.getNetworkConnection();
 For given NetworkConnectionSection
 {
    networkConnection.setIpAddressAllocationMode(
             IpAddressAllocationModeType.POOL);
                networkConnection.setNetwork(vdc.getAvailableNetworkRefs()
                        .iterator().next().getName());
  }
  waitForTaskCompletion(
           childVm.updateSection(networkConnectionSectionType));

Something that needs to be noted that the VM configuration step is performed only after instantiating the vApp from template.

We will visit the other 'Assorted vApp' creation using the compose/re-compose API in next post.

Don't forget to check out the 'Hello vCloud' sample bundled with the SDK for above working example.

No comments:

Post a Comment