/
template_app.go
47 lines (37 loc) · 1.05 KB
/
template_app.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
package esx
import (
"fmt"
)
// TempExist 判断模板是否存在
// true => 存在, false => 不存在
func TempExist(client *Client, name string) bool {
uri := fmt.Sprintf("_template/%s", name)
code, _ := client.GetResponse("GET", uri, "", map[string]string{})
return code < 400
}
// TempCreate 创建模板
func TempCreate(client *Client, name string, qdata []byte) error {
uri := fmt.Sprintf("_template/%s", name)
code, rdata := client.GetResponse("PUT", uri, string(qdata), map[string]string{})
if code >= 400 {
return fmt.Errorf("创建模板[%s]失败, 返回的响应是: %s", name, string(rdata))
}
return nil
}
// TempExistWithPool 判断模板是否存在
// true => 存在, false => 不存在
func TempExistWithPool(name string) bool {
client := Pool.Get()
if client != nil {
defer Pool.Put(client)
}
return TempExist(client, name)
}
// TempCreateWithPool 创建模板
func TempCreateWithPool(name string, qdata []byte) error {
client := Pool.Get()
if client != nil {
defer Pool.Put(client)
}
return TempCreate(client, name, qdata)
}