/
lxd_profiles.go
94 lines (75 loc) · 2.23 KB
/
lxd_profiles.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
package lxd
import (
"fmt"
"net/url"
"github.com/canonical/lxd/shared/api"
)
// Profile handling functions
// GetProfileNames returns a list of available profile names.
func (r *ProtocolLXD) GetProfileNames() ([]string, error) {
// Fetch the raw URL values.
urls := []string{}
baseURL := "/profiles"
_, err := r.queryStruct("GET", baseURL, nil, "", &urls)
if err != nil {
return nil, err
}
// Parse it.
return urlsToResourceNames(baseURL, urls...)
}
// GetProfiles returns a list of available Profile structs.
func (r *ProtocolLXD) GetProfiles() ([]api.Profile, error) {
profiles := []api.Profile{}
// Fetch the raw value
_, err := r.queryStruct("GET", "/profiles?recursion=1", nil, "", &profiles)
if err != nil {
return nil, err
}
return profiles, nil
}
// GetProfile returns a Profile entry for the provided name.
func (r *ProtocolLXD) GetProfile(name string) (*api.Profile, string, error) {
profile := api.Profile{}
// Fetch the raw value
etag, err := r.queryStruct("GET", fmt.Sprintf("/profiles/%s", url.PathEscape(name)), nil, "", &profile)
if err != nil {
return nil, "", err
}
return &profile, etag, nil
}
// CreateProfile defines a new container profile.
func (r *ProtocolLXD) CreateProfile(profile api.ProfilesPost) error {
// Send the request
_, _, err := r.query("POST", "/profiles", profile, "")
if err != nil {
return err
}
return nil
}
// UpdateProfile updates the profile to match the provided Profile struct.
func (r *ProtocolLXD) UpdateProfile(name string, profile api.ProfilePut, ETag string) error {
// Send the request
_, _, err := r.query("PUT", fmt.Sprintf("/profiles/%s", url.PathEscape(name)), profile, ETag)
if err != nil {
return err
}
return nil
}
// RenameProfile renames an existing profile entry.
func (r *ProtocolLXD) RenameProfile(name string, profile api.ProfilePost) error {
// Send the request
_, _, err := r.query("POST", fmt.Sprintf("/profiles/%s", url.PathEscape(name)), profile, "")
if err != nil {
return err
}
return nil
}
// DeleteProfile deletes a profile.
func (r *ProtocolLXD) DeleteProfile(name string) error {
// Send the request
_, _, err := r.query("DELETE", fmt.Sprintf("/profiles/%s", url.PathEscape(name)), nil, "")
if err != nil {
return err
}
return nil
}