VMware Cloud Community
abassoonkid
Contributor
Contributor
Jump to solution

Setting Hostname for a VM

While instantiating vApps from a template in much the same way that the HellovCloud.java sample does it, I need to be able to change the hostname (called the Computer Name in vCD) of the single VM contained within the vApp. I can't seem to find any examples on how to do this. Any ideas?

Reply
0 Kudos
1 Solution

Accepted Solutions
cfor
Expert
Expert
Jump to solution

Hope this helps, I pulled it from some code we use (.NET SDK) -- This is not an exact copy of the working code, but should be what you need in order to get going.

string modifierText = '1234';

//With v1.5 full API may need to change this object type to Vm

Vapp vm = GETTHECHILDFROMTHEAPP( vapp.GetChildrenVms() );

if (vm.GetGuestCustomizationSection() != null)
{
    GuestCustomizationSectionType guestSection = vm.GetGuestCustomizationSection();
    int len = guestSection.ComputerName.Length;
    if (len > 10) len = 10;
    guestSection.ComputerName = guestSection.ComputerName.Substring(0, len) + modifierText;
    try
    {
        vm.UpdateSection(guestSection);
    }
    catch (VCloudException ex)
    {
        //Exception thrown by 1.5 hosts: VCloudException
        // (The parameter is not supported in the current context: AdminPassword)
        // bug workaround
        if (ex.Message.Contains("AdminPassword"))
        {
            //set AdminPassword to NULL and try again
            guestSection.AdminPassword = null;
            vm.UpdateSection(guestSection);
        } else {
            throw;
        }
    }
}
ChrisF (VCP4, VCP5, VCP-Cloud) - If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful

View solution in original post

Reply
0 Kudos
1 Reply
cfor
Expert
Expert
Jump to solution

Hope this helps, I pulled it from some code we use (.NET SDK) -- This is not an exact copy of the working code, but should be what you need in order to get going.

string modifierText = '1234';

//With v1.5 full API may need to change this object type to Vm

Vapp vm = GETTHECHILDFROMTHEAPP( vapp.GetChildrenVms() );

if (vm.GetGuestCustomizationSection() != null)
{
    GuestCustomizationSectionType guestSection = vm.GetGuestCustomizationSection();
    int len = guestSection.ComputerName.Length;
    if (len > 10) len = 10;
    guestSection.ComputerName = guestSection.ComputerName.Substring(0, len) + modifierText;
    try
    {
        vm.UpdateSection(guestSection);
    }
    catch (VCloudException ex)
    {
        //Exception thrown by 1.5 hosts: VCloudException
        // (The parameter is not supported in the current context: AdminPassword)
        // bug workaround
        if (ex.Message.Contains("AdminPassword"))
        {
            //set AdminPassword to NULL and try again
            guestSection.AdminPassword = null;
            vm.UpdateSection(guestSection);
        } else {
            throw;
        }
    }
}
ChrisF (VCP4, VCP5, VCP-Cloud) - If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful
Reply
0 Kudos