Created
June 2, 2017 09:45
-
-
Save thebsdbox/d20801fdac8d8adf5bc6e6e2585a7f07 to your computer and use it in GitHub Desktop.
XM Hell
This file contains 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
package main | |
import ( | |
"encoding/xml" | |
"fmt" | |
) | |
// Envelope : is the parent XML and holds all information about a VM | |
type Envelope struct { | |
XMLName xml.Name `xml:"Envelope"` | |
BuildID string `xml:"vmwbuildId,attr"` | |
XMLNS string `xml:"xmlns,attr"` | |
CIM string `xml:"xmlns:cim,attr"` | |
OVF string `xml:"xmlns:ovf,attr"` | |
RASD string `xml:"xmlns:rasd,attr"` | |
VMW string `xml:"xmlns:vmw,attr"` | |
VSSD string `xml:"xmlns:vssd,attr"` | |
XSI string `xml:"xmlns:xsi,attr"` | |
File []References `xml:"References>File"` | |
Disk []DiskSection `xml:"DiskSection"` | |
Net NetSection `xml:"NetworkSection"` | |
VM VirtualSystem `xml:"VirtualSystem"` | |
} | |
// References : Contains the references to additional files | |
type References struct { | |
OVFHREF string `xml:"ovf:href,attr"` | |
OVFID string `xml:"ovf:id,attr"` | |
OVFSIZE string `xml:"ovf:size,attr"` | |
} | |
// DiskSection : Defines the disks attached to the VM | |
type DiskSection struct { | |
DiskInfo string `xml:"Info"` | |
Disk []DiskDetails | |
} | |
// DiskDetails : Details all of the parts of a disk | |
type DiskDetails struct { | |
OVFCAP string `xml:"ovf:capacity,attr"` | |
OVCAPUNITS string `xml:"ovf:capacityAllocationUnits,attr"` | |
OVFDISKID string `xml:"ovf:diskId,attr"` | |
OVFFILEREF string `xml:"ovf:fileRef,attr"` | |
OVFFORMAT string `xml:"ovf:format,attr"` | |
OVFPOPSIZE string `xml:"ovf:populatedSize,attr"` | |
} | |
// NetSection : Details the networking configuration | |
type NetSection struct { | |
NetInfo string `xml:"Info"` | |
Network struct { | |
NetName string `xml:"ovf:name,attr"` | |
NetDesc string `xml:"Description"` | |
} `xml:"Network"` | |
} | |
// VirtualSystem : The overall struct that details the Virtual Machine | |
type VirtualSystem struct { | |
VID string `xml:"ovf:id,attr"` | |
VInfo string `xml:"Info"` | |
VName string `xml:"Name"` | |
VOSSection struct { | |
VOSID string `xml:"ovf:id,attr"` | |
VOSType string `xml:"ovf:osType,attr"` | |
VOSInfo string `xml:"Info"` | |
VOSDesc string `xml:"Description"` | |
} `xml:"OperatingSystemSection"` | |
VHardware struct { | |
Hardware []VirtualHardware | |
} `xml:"VirtualHardwareSection"` | |
} | |
// VirtualHardware : The overall struct that details the Virtual Machine | |
type VirtualHardware struct { | |
} | |
func main() { | |
var envelope Envelope | |
envelope.BuildID = "build123" | |
var newDiskSection DiskSection | |
appendFilesToReferences(&envelope, "Other_Linux_3.x_kernel_64-bit-disk1.vmdk", "file1", "100004") | |
appdendDiskToDiskSection(&newDiskSection, "8", "byte * 2^30", "file1", "vmdisk1", "http://format", "0") | |
appdendDiskToDiskSection(&newDiskSection, "8", "byte * 2^30", "file1", "vmdisk1", "http://format", "0") | |
appdendDiskToDiskSection(&newDiskSection, "8", "byte * 2^30", "file1", "vmdisk1", "http://format", "0") | |
newDiskSection.DiskInfo = "Awesome New Disk" | |
envelope.Disk = append(envelope.Disk, newDiskSection) | |
var net NetSection | |
net.NetInfo = "List of Networks" | |
net.Network.NetName = "VM Network" | |
net.Network.NetDesc = "Default Network" | |
envelope.Net = net | |
output, err := xml.MarshalIndent(envelope, " ", " ") | |
if err != nil { | |
fmt.Printf("error: %v\n", err) | |
} | |
var xmlOutput string | |
xmlOutput = xml.Header + string(output) | |
fmt.Println(xmlOutput) | |
} | |
func appendFilesToReferences(references *Envelope, ovfHref string, ovfID string, ovfSize string) { | |
var newFile References | |
newFile.OVFHREF = ovfHref | |
newFile.OVFID = ovfID | |
newFile.OVFSIZE = ovfSize | |
references.File = append(references.File, newFile) | |
} | |
func appdendDiskToDiskSection(disksection *DiskSection, ovfCapacity string, ovfCapUnits string, ovfDiskID string, ovfFileRef string, ovfFormat string, ovfPopSize string) { | |
var newDisk DiskDetails | |
newDisk.OVFCAP = ovfCapacity | |
newDisk.OVCAPUNITS = ovfCapUnits | |
newDisk.OVFDISKID = ovfDiskID | |
newDisk.OVFFILEREF = ovfFileRef | |
newDisk.OVFFORMAT = ovfFormat | |
newDisk.OVFPOPSIZE = ovfPopSize | |
disksection.Disk = append(disksection.Disk, newDisk) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment