Skip to content
This repository has been archived by the owner on Jun 25, 2024. It is now read-only.

Commit

Permalink
Added host/network APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
xenserverarmy committed Mar 20, 2015
1 parent f34ed59 commit 8829753
Showing 1 changed file with 159 additions and 0 deletions.
159 changes: 159 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type XenAPIObject struct {
Client *XenAPIClient
}

type Host XenAPIObject
type VM XenAPIObject
type SR XenAPIObject
type VDI XenAPIObject
Expand Down Expand Up @@ -75,6 +76,12 @@ func (client *XenAPIClient) Login() (err error) {
params[1] = client.Password

err = client.RPCCall(&result, "session.login_with_password", params)
if err == nil {
// err might not be set properly, so check the reference
if result["Value"] == nil {
return errors.New ("Invalid credentials supplied")
}
}
client.Session = result["Value"]
return err
}
Expand Down Expand Up @@ -316,6 +323,40 @@ func (client *XenAPIClient) CreateTask() (task *Task, err error) {
return
}

func (client *XenAPIClient) CreateNetwork(name_label string, name_description string) (network *Network, err error) {
network = new(Network)

net_rec := make(xmlrpc.Struct)
net_rec["name_label"] = name_label
net_rec["name_description"] = name_description
net_rec["bridge"] = ""
net_rec["other_config"] = make(xmlrpc.Struct)

fmt.Println ("foo")
result := APIResult{}
err = client.APICall(&result, "network.create", net_rec)
if err != nil {
return nil, err
}
network.Ref = result.Value.(string)
network.Client = client

return network, nil
}

// Host associated function

func (self *Host) GetAddress() (address string, err error) {
result := APIResult{}
err = self.Client.APICall(&result, "host.get_address", self.Ref)
if err != nil {
return "", err
}
address = result.Value.(string)
return address, nil
}


// VM associated functions

func (self *VM) Clone(label string) (new_instance *VM, err error) {
Expand All @@ -340,6 +381,32 @@ func (self *VM) Provision() (err error) {
return
}

func (self *VM) Copy (newName string, targetSr *SR) (new_instance *VM, err error) {
new_instance = new(VM)

result := APIResult{}
err = self.Client.APICall(&result, "VM.copy", self.Ref, newName, targetSr.Ref)
if err != nil {
return nil, err
}
new_instance.Ref = result.Value.(string)
new_instance.Client = self.Client
return
}

func (self *VM) Snapshot(label string) (snapshot *VM, err error) {
snapshot = new(VM)

result := APIResult{}
err = self.Client.APICall(&result, "VM.snapshot", self.Ref, label)
if err != nil {
return nil, err
}
snapshot.Ref = result.Value.(string)
snapshot.Client = self.Client
return
}

func (self *VM) Destroy() (err error) {
result := APIResult{}
err = self.Client.APICall(&result, "VM.destroy", self.Ref)
Expand Down Expand Up @@ -385,6 +452,15 @@ func (self *VM) Unpause() (err error) {
return
}

func (self *VM) Resume(paused, force bool) (err error) {
result := APIResult{}
err = self.Client.APICall(&result, "VM.resume", self.Ref, paused, force)
if err != nil {
return err
}
return
}

func (self *VM) SetHVMBoot(policy, bootOrder string) (err error) {
result := APIResult{}
err = self.Client.APICall(&result, "VM.set_HVM_boot_policy", self.Ref, policy)
Expand Down Expand Up @@ -425,6 +501,21 @@ func (self *VM) GetDomainId() (domid string, err error) {
return domid, nil
}

func (self *VM) GetResidentOn() (host *Host, err error) {
result := APIResult{}
err = self.Client.APICall(&result, "VM.get_resident_on", self.Ref)
if err != nil {
return nil, err
}

host = new(Host)
host.Ref = result.Value.(string)
host.Client = self.Client

return host, nil
}


func (self *VM) GetPowerState() (state string, err error) {
result := APIResult{}
err = self.Client.APICall(&result, "VM.get_power_state", self.Ref)
Expand Down Expand Up @@ -646,6 +737,30 @@ func (self *VM) SetPlatform(params map[string]string) (err error) {
return
}

func (self *VM) SetVCpuMax( vcpus uint) (err error) {
result := APIResult{}
strVcpu := fmt.Sprintf("%d", vcpus)

err = self.Client.APICall(&result, "VM.set_VCPUs_max", self.Ref, strVcpu)

if err != nil {
return err
}
return
}

func (self *VM) SetVCpuAtStartup( vcpus uint) (err error) {
result := APIResult{}
strVcpu := fmt.Sprintf("%d", vcpus)

err = self.Client.APICall(&result, "VM.set_VCPUs_at_startup", self.Ref, strVcpu)

if err != nil {
return err
}
return
}

func (self *VM) ConnectNetwork(network *Network, device string) (vif *VIF, err error) {
// Create the VIF

Expand Down Expand Up @@ -684,6 +799,15 @@ func (self *VM) SetIsATemplate(is_a_template bool) (err error) {
return
}

func (self *VM) SetHaAlwaysRun(ha_always_run bool) (err error) {
result := APIResult{}
err = self.Client.APICall(&result, "VM.set_ha_always_run", self.Ref, ha_always_run)
if err != nil {
return err
}
return
}

// SR associated functions

func (self *SR) CreateVdi(name_label string, size int64) (vdi *VDI, err error) {
Expand Down Expand Up @@ -713,6 +837,16 @@ func (self *SR) CreateVdi(name_label string, size int64) (vdi *VDI, err error) {
return
}

func (self *SR) GetUuid() (sr_uuid string, err error) {
result := APIResult{}
err = self.Client.APICall(&result, "SR.get_uuid", self.Ref)
if err != nil {
return "", err
}
sr_uuid = result.Value.(string)
return sr_uuid, nil
}

// Network associated functions

func (self *Network) GetAssignedIPs() (ip_map map[string]string, err error) {
Expand All @@ -728,6 +862,16 @@ func (self *Network) GetAssignedIPs() (ip_map map[string]string, err error) {
return ip_map, nil
}

func (self *Network) Destroy() (err error) {
result := APIResult{}
err = self.Client.APICall(&result, "network.destroy", self.Ref)
if err != nil {
return err
}
return
}


// PIF associated functions

func (self *PIF) GetRecord() (record map[string]interface{}, err error) {
Expand Down Expand Up @@ -823,6 +967,21 @@ func (self *VIF) Destroy() (err error) {
return nil
}

func (self *VIF) GetNetwork() (network *Network, err error) {

network = new(Network)
result := APIResult{}
err = self.Client.APICall(&result, "VIF.get_network", self.Ref)

if err != nil {
return nil, err
}
network.Ref = result.Value.(string)
network.Client = self.Client
return

}

// VDI associated functions

func (self *VDI) GetUuid() (vdi_uuid string, err error) {
Expand Down

0 comments on commit 8829753

Please sign in to comment.