forked from canonical/lxd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lxd_certificates.go
94 lines (74 loc) · 2.46 KB
/
lxd_certificates.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"
"strings"
"github.com/lxc/lxd/shared/api"
)
// Certificate handling functions
// GetCertificateFingerprints returns a list of certificate fingerprints
func (r *ProtocolLXD) GetCertificateFingerprints() ([]string, error) {
certificates := []string{}
// Fetch the raw value
_, err := r.queryStruct("GET", "/certificates", nil, "", &certificates)
if err != nil {
return nil, err
}
// Parse it
fingerprints := []string{}
for _, fingerprint := range fingerprints {
fields := strings.Split(fingerprint, "/certificates/")
fingerprints = append(fingerprints, fields[len(fields)-1])
}
return fingerprints, nil
}
// GetCertificates returns a list of certificates
func (r *ProtocolLXD) GetCertificates() ([]api.Certificate, error) {
certificates := []api.Certificate{}
// Fetch the raw value
_, err := r.queryStruct("GET", "/certificates?recursion=1", nil, "", &certificates)
if err != nil {
return nil, err
}
return certificates, nil
}
// GetCertificate returns the certificate entry for the provided fingerprint
func (r *ProtocolLXD) GetCertificate(fingerprint string) (*api.Certificate, string, error) {
certificate := api.Certificate{}
// Fetch the raw value
etag, err := r.queryStruct("GET", fmt.Sprintf("/certificates/%s", url.QueryEscape(fingerprint)), nil, "", &certificate)
if err != nil {
return nil, "", err
}
return &certificate, etag, nil
}
// CreateCertificate adds a new certificate to the LXD trust store
func (r *ProtocolLXD) CreateCertificate(certificate api.CertificatesPost) error {
// Send the request
_, _, err := r.query("POST", "/certificates", certificate, "")
if err != nil {
return err
}
return nil
}
// UpdateCertificate updates the certificate definition
func (r *ProtocolLXD) UpdateCertificate(fingerprint string, certificate api.CertificatePut, ETag string) error {
if !r.HasExtension("certificate_update") {
return fmt.Errorf("The server is missing the required \"certificate_update\" API extension")
}
// Send the request
_, _, err := r.query("PUT", fmt.Sprintf("/certificates/%s", url.QueryEscape(fingerprint)), certificate, ETag)
if err != nil {
return err
}
return nil
}
// DeleteCertificate removes a certificate from the LXD trust store
func (r *ProtocolLXD) DeleteCertificate(fingerprint string) error {
// Send the request
_, _, err := r.query("DELETE", fmt.Sprintf("/certificates/%s", url.QueryEscape(fingerprint)), nil, "")
if err != nil {
return err
}
return nil
}