-
Notifications
You must be signed in to change notification settings - Fork 3
/
ovirtObjectMethods.template
100 lines (91 loc) · 2.49 KB
/
ovirtObjectMethods.template
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//go:generate genny -in=$GOFILE -out=gen-$GOFILE gen "OvirtObjectType=Vm,Cluster" -pkg ovirtapi
// Copyright (C) 2017 Battelle Memorial Institute
// All rights reserved.
//
// This software may be modified and distributed under the terms
// of the BSD-2 license. See the LICENSE file for details.
package ovirtapi
import (
"encoding/json"
"fmt"
"reflect"
"github.com/cheekybits/genny/generic"
)
type OvirtObjectType generic.Type
func (con *Connection) GetOvirtObjectType(id string) (*OvirtObjectType, error) {
body, err := con.GetLinkBody(reflect.TypeOf(OvirtObjectType{}).Name()+"s", id)
if err != nil {
return nil, err
}
object := con.NewOvirtObjectType()
err = json.Unmarshal(body, object)
if err != nil {
return nil, err
}
return object, err
}
func (object *OvirtObjectType) Update() error {
if object.OvirtObject.Href == "" {
return fmt.Errorf("Object has not been saved to the server")
}
body, err := object.Con.Request("GET", object.Con.ResolveLink(object.Href), nil)
if err != nil {
return err
}
tempObject := OvirtObjectType{OvirtObject: OvirtObject{Con: object.Con}}
err = json.Unmarshal(body, &tempObject)
if err != nil {
return err
}
*object = tempObject
return nil
}
func (con *Connection) GetAllOvirtObjectTypes() ([]*OvirtObjectType, error) {
body, err := con.GetLinkBody(reflect.TypeOf(OvirtObjectType{}).Name()+"s", "")
if err != nil {
return nil, err
}
objects := []*OvirtObjectType{}
err = json.Unmarshal(body, &struct {
OvirtObjectType *[]*OvirtObjectType
}{&objects})
if err != nil {
return nil, err
}
for _, object := range objects {
object.Con = con
}
return objects, err
}
func (con *Connection) NewOvirtObjectType() *OvirtObjectType {
return &OvirtObjectType{OvirtObject: OvirtObject{Con: con}}
}
func (object *OvirtObjectType) Save() error {
body, err := json.MarshalIndent(object, "", " ")
if err != nil {
return err
}
// If there is a link, it is an already saved object, we need to update it
if object.OvirtObject.Href != "" {
body, err = object.Con.Request("PUT", object.Con.ResolveLink(object.Href), body)
if err != nil {
return err
}
} else {
link, err := object.Con.GetLink(reflect.TypeOf(OvirtObjectType{}).Name() + "s")
if err != nil {
return err
}
body, err = object.Con.Request("POST", link, body)
if err != nil {
return err
}
}
tempObject := OvirtObjectType{OvirtObject: OvirtObject{Con: object.Con}}
err = json.Unmarshal(body, &tempObject)
if err != nil {
return err
}
*object = tempObject
return nil
}