-
Notifications
You must be signed in to change notification settings - Fork 7
/
deviceCertificateFetcher.go
89 lines (74 loc) · 2.06 KB
/
deviceCertificateFetcher.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
package c8yfetcher
import (
"context"
"strings"
"github.com/pkg/errors"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/cmdutil"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/matcher"
"github.com/reubenmiller/go-c8y/pkg/c8y"
)
type DeviceCertificateFetcher struct {
*CumulocityFetcher
*IDNameFetcher
}
func NewDeviceCertificateFetcher(factory *cmdutil.Factory) *DeviceCertificateFetcher {
return &DeviceCertificateFetcher{
CumulocityFetcher: &CumulocityFetcher{
factory: factory,
},
}
}
func (f *DeviceCertificateFetcher) IsID(id string) bool {
isFingerprint := true
for _, c := range id {
if !strings.Contains("0123456789abcdef", string(c)) {
isFingerprint = false
break
}
}
return isFingerprint && len(id) > 30
}
func (f *DeviceCertificateFetcher) getByID(id string) ([]fetcherResultSet, error) {
cert, resp, err := f.Client().DeviceCertificate.GetCertificate(
c8y.WithDisabledDryRunContext(context.Background()),
id,
)
if err != nil {
return nil, errors.Wrap(err, "Could not fetch by id")
}
results := make([]fetcherResultSet, 1)
results[0] = fetcherResultSet{
ID: cert.Fingerprint,
Name: cert.Name,
Self: cert.Self,
Value: resp.JSON(),
}
return results, nil
}
func (f *DeviceCertificateFetcher) getByName(name string) ([]fetcherResultSet, error) {
// check if already resolved, so we can save a lookup
col, _, err := f.Client().DeviceCertificate.GetCertificates(
c8y.WithDisabledDryRunContext(context.Background()),
&c8y.DeviceCertificateCollectionOptions{
PaginationOptions: *c8y.NewPaginationOptions(100),
},
)
if err != nil {
return nil, errors.Wrap(err, "Could not fetch by name")
}
results := make([]fetcherResultSet, 0)
for _, cert := range col.Certificates {
nameMatch, _ := matcher.MatchWithWildcards(cert.Name, name)
fingerprintMatch, _ := matcher.MatchWithWildcards(cert.Fingerprint, name)
if !nameMatch && !fingerprintMatch {
continue
}
results = append(results, fetcherResultSet{
ID: cert.Fingerprint,
Name: cert.Name,
Self: cert.Self,
Value: cert,
})
}
return results, nil
}