-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimages.go
177 lines (133 loc) · 3.91 KB
/
images.go
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
Copyright Contributors to the Feilong Project.
SPDX-License-Identifier: Apache-2.0
**/
package feilong
import (
"encoding/json"
)
// https://cloudlib4zvm.readthedocs.io/en/latest/restapi.html#list-images
type ListImagesImage struct {
ImageName string `json:"imagename"`
ImageOSDistro string `json:"imageosdistro"`
MD5Sum string `json:"md5sum"`
DiskSizeUnits string `json:"disk_size_units"`
ImageSizeInBytes string `json:"image_size_in_bytes"`
Type string `json:"type"`
Comments string `json:"comments"`
LastAccessTime float64 `json:"last_access_time"`
}
type ListImagesResult struct {
OverallRC int `json:"overallRC"`
ReturnCode int `json:"rc"`
Reason int `json:"rs"`
ErrorMsg string `json:"errmsg"`
ModuleId int `json:"modID"`
Output []ListImagesImage `json:"output"`
}
func (c *Client) ListImages(imageName *string) (*ListImagesResult, error) {
var result ListImagesResult
path := "/images"
if imageName != nil {
path = path + "/" + *imageName
}
body, err := c.doRequest("GET", path, nil)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &result)
if err != nil {
return nil, err
}
return &result, nil
}
// https://cloudlib4zvm.readthedocs.io/en/latest/restapi.html#create-image
type CreateImageMeta struct {
OSVersion string `json:"os_version"`
MD5Sum string `json:"md5sum,omitempty"`
DiskType string `json:"disk_type,omitempty"`
}
type CreateImageParams struct {
ImageName string `json:"image_name"`
URL string `json:"url"`
ImageMeta CreateImageMeta `json:"image_meta"`
RemoteHost string `json:"remote_host,omitempty"`
}
func (c *Client) CreateImage(params *CreateImageParams) error {
wrapper := createImageWrapper { Image: *params }
body, err := json.Marshal(&wrapper)
if err != nil {
return err
}
_, err = c.doRequest("POST", "/images", body)
return err
}
// https://cloudlib4zvm.readthedocs.io/en/latest/restapi.html#export-image
type ExportImageParams struct {
DestURL string `json:"dest_url"`
RemoteHost string `json:"remote_host,omitempty"`
}
type ExportImageOutput struct {
ImageName string `json:"image_name"`
ImagePath string `json:"image_path"`
OSVersion string `json:"os_version"`
MD5Sum string `json:"md5sum"`
}
type ExportImageResult struct {
OverallRC int `json:"overallRC"`
ReturnCode int `json:"rc"`
Reason int `json:"rs"`
ErrorMsg string `json:"errmsg"`
ModuleId int `json:"modID"`
Output ExportImageOutput `json:"output"`
}
func (c *Client) ExportImage(name string, params *ExportImageParams) (*ExportImageResult, error) {
wrapper := exportImageWrapper { Location: *params }
var result ExportImageResult
body, err := json.Marshal(&wrapper)
if err != nil {
return nil, err
}
body, err = c.doRequest("PUT", "/images/" + name, body)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &result)
if err != nil {
return nil, err
}
return &result, err
}
// https://cloudlib4zvm.readthedocs.io/en/latest/restapi.html#get-root-disk-size-of-image
type GetImageRootDiskSizeResult struct {
OverallRC int `json:"overallRC"`
ReturnCode int `json:"rc"`
Reason int `json:"rs"`
ErrorMsg string `json:"errmsg"`
ModuleId int `json:"modID"`
Output string `json:"output"`
}
func (c *Client) GetImageRootDiskSize(name string) (*GetImageRootDiskSizeResult, error) {
var result GetImageRootDiskSizeResult
body, err := c.doRequest("GET", "/images/" + name + "/root_disk_size", nil)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &result)
if err != nil {
return nil, err
}
return &result, nil
}
// https://cloudlib4zvm.readthedocs.io/en/latest/restapi.html#delete-image
func (c *Client) DeleteImage(name string) error {
_, err := c.doRequest("DELETE", "/images/" + name, nil)
return err
}
// For internal use
type createImageWrapper struct {
Image CreateImageParams `json:"image"`
}
type exportImageWrapper struct {
Location ExportImageParams `json:"location"`
}