1 Star 0 Fork 0

powerpaas/machine

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
vapp.go 15.40 KB
一键复制 编辑 原始数据 按行查看 历史
Fabio Rapposelli 提交于 2014-12-11 10:13 . Godeps save and enable commands
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
/*
* Copyright 2014 VMware, Inc. All rights reserved. Licensed under the Apache v2 License.
*/
package govcloudair
import (
"bytes"
"encoding/xml"
"fmt"
"net/url"
"os"
"strconv"
types "github.com/vmware/govcloudair/types/v56"
)
type VApp struct {
VApp *types.VApp
c *Client
}
func NewVApp(c *Client) *VApp {
return &VApp{
VApp: new(types.VApp),
c: c,
}
}
func (v *VApp) Refresh() error {
if v.VApp.HREF == "" {
return fmt.Errorf("cannot refresh, Object is empty")
}
u, _ := url.ParseRequestURI(v.VApp.HREF)
req := v.c.NewRequest(map[string]string{}, "GET", *u, nil)
resp, err := checkResp(v.c.Http.Do(req))
if err != nil {
return fmt.Errorf("error retrieving task: %s", err)
}
// Empty struct before a new unmarshal, otherwise we end up with duplicate
// elements in slices.
v.VApp = &types.VApp{}
if err = decodeBody(resp, v.VApp); err != nil {
return fmt.Errorf("error decoding task response: %s", err)
}
// The request was successful
return nil
}
func (v *VApp) ComposeVApp(orgvdcnetwork OrgVDCNetwork, vapptemplate VAppTemplate, name string, description string) (Task, error) {
if vapptemplate.VAppTemplate.Children == nil || orgvdcnetwork.OrgVDCNetwork == nil {
return Task{}, fmt.Errorf("can't compose a new vApp, objects passed are not valid")
}
// Build request XML
vcomp := &types.ComposeVAppParams{
Ovf: "http://schemas.dmtf.org/ovf/envelope/1",
Xsi: "http://www.w3.org/2001/XMLSchema-instance",
Xmlns: "http://www.vmware.com/vcloud/v1.5",
Deploy: false,
Name: name,
PowerOn: false,
Description: description,
InstantiationParams: &types.InstantiationParams{
NetworkConfigSection: &types.NetworkConfigSection{
Info: "Configuration parameters for logical networks",
NetworkConfig: &types.VAppNetworkConfiguration{
NetworkName: orgvdcnetwork.OrgVDCNetwork.Name,
Configuration: &types.NetworkConfiguration{
FenceMode: "bridged",
ParentNetwork: &types.Reference{
HREF: orgvdcnetwork.OrgVDCNetwork.HREF,
Name: orgvdcnetwork.OrgVDCNetwork.Name,
Type: orgvdcnetwork.OrgVDCNetwork.Type,
},
},
},
},
},
SourcedItem: &types.SourcedCompositionItemParam{
Source: &types.Reference{
HREF: vapptemplate.VAppTemplate.Children.VM[0].HREF,
Name: vapptemplate.VAppTemplate.Children.VM[0].Name,
},
InstantiationParams: &types.InstantiationParams{
NetworkConnectionSection: &types.NetworkConnectionSection{
Type: vapptemplate.VAppTemplate.Children.VM[0].NetworkConnectionSection.Type,
HREF: vapptemplate.VAppTemplate.Children.VM[0].NetworkConnectionSection.HREF,
Info: "Network config for sourced item",
PrimaryNetworkConnectionIndex: vapptemplate.VAppTemplate.Children.VM[0].NetworkConnectionSection.PrimaryNetworkConnectionIndex,
NetworkConnection: &types.NetworkConnection{
Network: orgvdcnetwork.OrgVDCNetwork.Name,
NetworkConnectionIndex: vapptemplate.VAppTemplate.Children.VM[0].NetworkConnectionSection.NetworkConnection.NetworkConnectionIndex,
IsConnected: true,
IPAddressAllocationMode: "POOL",
},
},
},
NetworkAssignment: &types.NetworkAssignment{
InnerNetwork: orgvdcnetwork.OrgVDCNetwork.Name,
ContainerNetwork: orgvdcnetwork.OrgVDCNetwork.Name,
},
},
}
output, err := xml.MarshalIndent(vcomp, " ", " ")
if err != nil {
return Task{}, fmt.Errorf("error marshaling vapp compose: %s", err)
}
debug := os.Getenv("GOVCLOUDAIR_DEBUG")
if debug == "true" {
fmt.Printf("\n\nXML DEBUG: %s\n\n", string(output))
}
b := bytes.NewBufferString(xml.Header + string(output))
s := v.c.VCDVDCHREF
s.Path += "/action/composeVApp"
req := v.c.NewRequest(map[string]string{}, "POST", s, b)
req.Header.Add("Content-Type", "application/vnd.vmware.vcloud.composeVAppParams+xml")
resp, err := checkResp(v.c.Http.Do(req))
if err != nil {
return Task{}, fmt.Errorf("error instantiating a new vApp: %s", err)
}
if err = decodeBody(resp, v.VApp); err != nil {
return Task{}, fmt.Errorf("error decoding vApp response: %s", err)
}
task := NewTask(v.c)
task.Task = v.VApp.Tasks.Task[0]
// The request was successful
return *task, nil
}
func (v *VApp) PowerOn() (Task, error) {
s, _ := url.ParseRequestURI(v.VApp.HREF)
s.Path += "/power/action/powerOn"
req := v.c.NewRequest(map[string]string{}, "POST", *s, nil)
resp, err := checkResp(v.c.Http.Do(req))
if err != nil {
return Task{}, fmt.Errorf("error powering on vApp: %s", err)
}
task := NewTask(v.c)
if err = decodeBody(resp, task.Task); err != nil {
return Task{}, fmt.Errorf("error decoding Task response: %s", err)
}
// The request was successful
return *task, nil
}
func (v *VApp) PowerOff() (Task, error) {
s, _ := url.ParseRequestURI(v.VApp.HREF)
s.Path += "/power/action/powerOff"
req := v.c.NewRequest(map[string]string{}, "POST", *s, nil)
resp, err := checkResp(v.c.Http.Do(req))
if err != nil {
return Task{}, fmt.Errorf("error powering off vApp: %s", err)
}
task := NewTask(v.c)
if err = decodeBody(resp, task.Task); err != nil {
return Task{}, fmt.Errorf("error decoding Task response: %s", err)
}
// The request was successful
return *task, nil
}
func (v *VApp) Reboot() (Task, error) {
s, _ := url.ParseRequestURI(v.VApp.HREF)
s.Path += "/power/action/reboot"
req := v.c.NewRequest(map[string]string{}, "POST", *s, nil)
resp, err := checkResp(v.c.Http.Do(req))
if err != nil {
return Task{}, fmt.Errorf("error rebooting vApp: %s", err)
}
task := NewTask(v.c)
if err = decodeBody(resp, task.Task); err != nil {
return Task{}, fmt.Errorf("error decoding Task response: %s", err)
}
// The request was successful
return *task, nil
}
func (v *VApp) Reset() (Task, error) {
s, _ := url.ParseRequestURI(v.VApp.HREF)
s.Path += "/power/action/reset"
req := v.c.NewRequest(map[string]string{}, "POST", *s, nil)
resp, err := checkResp(v.c.Http.Do(req))
if err != nil {
return Task{}, fmt.Errorf("error resetting vApp: %s", err)
}
task := NewTask(v.c)
if err = decodeBody(resp, task.Task); err != nil {
return Task{}, fmt.Errorf("error decoding Task response: %s", err)
}
// The request was successful
return *task, nil
}
func (v *VApp) Suspend() (Task, error) {
s, _ := url.ParseRequestURI(v.VApp.HREF)
s.Path += "/power/action/suspend"
req := v.c.NewRequest(map[string]string{}, "POST", *s, nil)
resp, err := checkResp(v.c.Http.Do(req))
if err != nil {
return Task{}, fmt.Errorf("error suspending vApp: %s", err)
}
task := NewTask(v.c)
if err = decodeBody(resp, task.Task); err != nil {
return Task{}, fmt.Errorf("error decoding Task response: %s", err)
}
// The request was successful
return *task, nil
}
func (v *VApp) Shutdown() (Task, error) {
s, _ := url.ParseRequestURI(v.VApp.HREF)
s.Path += "/power/action/shutdown"
req := v.c.NewRequest(map[string]string{}, "POST", *s, nil)
resp, err := checkResp(v.c.Http.Do(req))
if err != nil {
return Task{}, fmt.Errorf("error shutting down vApp: %s", err)
}
task := NewTask(v.c)
if err = decodeBody(resp, task.Task); err != nil {
return Task{}, fmt.Errorf("error decoding Task response: %s", err)
}
// The request was successful
return *task, nil
}
func (v *VApp) Undeploy() (Task, error) {
vu := &types.UndeployVAppParams{
Xmlns: "http://www.vmware.com/vcloud/v1.5",
UndeployPowerAction: "powerOff",
}
output, err := xml.MarshalIndent(vu, " ", " ")
if err != nil {
fmt.Printf("error: %v\n", err)
}
debug := os.Getenv("GOVCLOUDAIR_DEBUG")
if debug == "true" {
fmt.Printf("\n\nXML DEBUG: %s\n\n", string(output))
}
b := bytes.NewBufferString(xml.Header + string(output))
s, _ := url.ParseRequestURI(v.VApp.HREF)
s.Path += "/action/undeploy"
req := v.c.NewRequest(map[string]string{}, "POST", *s, b)
req.Header.Add("Content-Type", "application/vnd.vmware.vcloud.undeployVAppParams+xml")
resp, err := checkResp(v.c.Http.Do(req))
if err != nil {
return Task{}, fmt.Errorf("error undeploy vApp: %s", err)
}
task := NewTask(v.c)
if err = decodeBody(resp, task.Task); err != nil {
return Task{}, fmt.Errorf("error decoding Task response: %s", err)
}
// The request was successful
return *task, nil
}
func (v *VApp) Deploy() (Task, error) {
vu := &types.DeployVAppParams{
Xmlns: "http://www.vmware.com/vcloud/v1.5",
PowerOn: false,
}
output, err := xml.MarshalIndent(vu, " ", " ")
if err != nil {
fmt.Printf("error: %v\n", err)
}
debug := os.Getenv("GOVCLOUDAIR_DEBUG")
if debug == "true" {
fmt.Printf("\n\nXML DEBUG: %s\n\n", string(output))
}
b := bytes.NewBufferString(xml.Header + string(output))
s, _ := url.ParseRequestURI(v.VApp.HREF)
s.Path += "/action/deploy"
req := v.c.NewRequest(map[string]string{}, "POST", *s, b)
req.Header.Add("Content-Type", "application/vnd.vmware.vcloud.deployVAppParams+xml")
resp, err := checkResp(v.c.Http.Do(req))
if err != nil {
return Task{}, fmt.Errorf("error undeploy vApp: %s", err)
}
task := NewTask(v.c)
if err = decodeBody(resp, task.Task); err != nil {
return Task{}, fmt.Errorf("error decoding Task response: %s", err)
}
// The request was successful
return *task, nil
}
func (v *VApp) Delete() (Task, error) {
s, _ := url.ParseRequestURI(v.VApp.HREF)
req := v.c.NewRequest(map[string]string{}, "DELETE", *s, nil)
resp, err := checkResp(v.c.Http.Do(req))
if err != nil {
return Task{}, fmt.Errorf("error deleting vApp: %s", err)
}
task := NewTask(v.c)
if err = decodeBody(resp, task.Task); err != nil {
return Task{}, fmt.Errorf("error decoding Task response: %s", err)
}
// The request was successful
return *task, nil
}
func (v *VApp) RunCustomizationScript(computername, script string) (Task, error) {
err := v.Refresh()
if err != nil {
return Task{}, fmt.Errorf("error refreshing vapp before running customization: %v", err)
}
// Check if VApp Children is populated
if v.VApp.Children == nil {
return Task{}, fmt.Errorf("vApp doesn't contain any children, aborting customization")
}
vu := &types.GuestCustomizationSection{
Ovf: "http://schemas.dmtf.org/ovf/envelope/1",
Xsi: "http://www.w3.org/2001/XMLSchema-instance",
Xmlns: "http://www.vmware.com/vcloud/v1.5",
HREF: v.VApp.Children.VM[0].HREF,
Type: "application/vnd.vmware.vcloud.guestCustomizationSection+xml",
Info: "Specifies Guest OS Customization Settings",
Enabled: true,
ComputerName: computername,
CustomizationScript: script,
}
output, err := xml.MarshalIndent(vu, " ", " ")
if err != nil {
fmt.Printf("error: %v\n", err)
}
debug := os.Getenv("GOVCLOUDAIR_DEBUG")
if debug == "true" {
fmt.Printf("\n\nXML DEBUG: %s\n\n", string(output))
}
b := bytes.NewBufferString(xml.Header + string(output))
s, _ := url.ParseRequestURI(v.VApp.Children.VM[0].HREF)
s.Path += "/guestCustomizationSection/"
req := v.c.NewRequest(map[string]string{}, "PUT", *s, b)
req.Header.Add("Content-Type", "application/vnd.vmware.vcloud.guestCustomizationSection+xml")
resp, err := checkResp(v.c.Http.Do(req))
if err != nil {
return Task{}, fmt.Errorf("error customizing VM: %s", err)
}
task := NewTask(v.c)
if err = decodeBody(resp, task.Task); err != nil {
return Task{}, fmt.Errorf("error decoding Task response: %s", err)
}
// The request was successful
return *task, nil
}
func (v *VApp) GetStatus() (string, error) {
err := v.Refresh()
if err != nil {
return "", fmt.Errorf("error refreshing vapp: %v", err)
}
return types.VAppStatuses[v.VApp.Status], nil
}
func (v *VApp) ChangeCPUcount(size int) (Task, error) {
err := v.Refresh()
if err != nil {
return Task{}, fmt.Errorf("error refreshing vapp before running customization: %v", err)
}
// Check if VApp Children is populated
if v.VApp.Children == nil {
return Task{}, fmt.Errorf("vApp doesn't contain any children, aborting customization")
}
newcpu := &types.OVFItem{
XmlnsRasd: "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData",
XmlnsVCloud: "http://www.vmware.com/vcloud/v1.5",
XmlnsXsi: "http://www.w3.org/2001/XMLSchema-instance",
VCloudHREF: v.VApp.Children.VM[0].HREF + "/virtualHardwareSection/cpu",
VCloudType: "application/vnd.vmware.vcloud.rasdItem+xml",
AllocationUnits: "hertz * 10^6",
Description: "Number of Virtual CPUs",
ElementName: strconv.Itoa(size) + " virtual CPU(s)",
InstanceID: 4,
Reservation: 0,
ResourceType: 3,
VirtualQuantity: size,
Weight: 0,
Link: &types.Link{
HREF: v.VApp.Children.VM[0].HREF + "/virtualHardwareSection/cpu",
Rel: "edit",
Type: "application/vnd.vmware.vcloud.rasdItem+xml",
},
}
output, err := xml.MarshalIndent(newcpu, " ", " ")
if err != nil {
fmt.Printf("error: %v\n", err)
}
debug := os.Getenv("GOVCLOUDAIR_DEBUG")
if debug == "true" {
fmt.Printf("\n\nXML DEBUG: %s\n\n", string(output))
}
b := bytes.NewBufferString(xml.Header + string(output))
s, _ := url.ParseRequestURI(v.VApp.Children.VM[0].HREF)
s.Path += "/virtualHardwareSection/cpu"
req := v.c.NewRequest(map[string]string{}, "PUT", *s, b)
req.Header.Add("Content-Type", "application/vnd.vmware.vcloud.rasdItem+xml")
resp, err := checkResp(v.c.Http.Do(req))
if err != nil {
return Task{}, fmt.Errorf("error customizing VM: %s", err)
}
task := NewTask(v.c)
if err = decodeBody(resp, task.Task); err != nil {
return Task{}, fmt.Errorf("error decoding Task response: %s", err)
}
// The request was successful
return *task, nil
}
func (v *VApp) ChangeMemorySize(size int) (Task, error) {
err := v.Refresh()
if err != nil {
return Task{}, fmt.Errorf("error refreshing vapp before running customization: %v", err)
}
// Check if VApp Children is populated
if v.VApp.Children == nil {
return Task{}, fmt.Errorf("vApp doesn't contain any children, aborting customization")
}
newmem := &types.OVFItem{
XmlnsRasd: "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData",
XmlnsVCloud: "http://www.vmware.com/vcloud/v1.5",
XmlnsXsi: "http://www.w3.org/2001/XMLSchema-instance",
VCloudHREF: v.VApp.Children.VM[0].HREF + "/virtualHardwareSection/memory",
VCloudType: "application/vnd.vmware.vcloud.rasdItem+xml",
AllocationUnits: "byte * 2^20",
Description: "Memory Size",
ElementName: strconv.Itoa(size) + " MB of memory",
InstanceID: 5,
Reservation: 0,
ResourceType: 4,
VirtualQuantity: size,
Weight: 0,
Link: &types.Link{
HREF: v.VApp.Children.VM[0].HREF + "/virtualHardwareSection/memory",
Rel: "edit",
Type: "application/vnd.vmware.vcloud.rasdItem+xml",
},
}
output, err := xml.MarshalIndent(newmem, " ", " ")
if err != nil {
fmt.Printf("error: %v\n", err)
}
debug := os.Getenv("GOVCLOUDAIR_DEBUG")
if debug == "true" {
fmt.Printf("\n\nXML DEBUG: %s\n\n", string(output))
}
b := bytes.NewBufferString(xml.Header + string(output))
s, _ := url.ParseRequestURI(v.VApp.Children.VM[0].HREF)
s.Path += "/virtualHardwareSection/memory"
req := v.c.NewRequest(map[string]string{}, "PUT", *s, b)
req.Header.Add("Content-Type", "application/vnd.vmware.vcloud.rasdItem+xml")
resp, err := checkResp(v.c.Http.Do(req))
if err != nil {
return Task{}, fmt.Errorf("error customizing VM: %s", err)
}
task := NewTask(v.c)
if err = decodeBody(resp, task.Task); err != nil {
return Task{}, fmt.Errorf("error decoding Task response: %s", err)
}
// The request was successful
return *task, nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/powerpaas/machine.git
git@gitee.com:powerpaas/machine.git
powerpaas
machine
machine
v0.2.0-rc1

搜索帮助