-
Notifications
You must be signed in to change notification settings - Fork 4
/
jsonRegistrationDS.go
57 lines (50 loc) · 1.33 KB
/
jsonRegistrationDS.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
package registrationDatastore
import (
"encoding/json"
"fmt"
"os"
)
type jsonRegistrationDatastore struct {
regMap map[string]Registration
}
func NewJsonRegistrationDatastore(jsonPath string) (RegistrationDatastore, error) {
var regs []Registration
file, err := os.Open(jsonPath)
if err != nil {
return nil, err
}
defer file.Close()
parser := json.NewDecoder(file)
if err := parser.Decode(®s); err != nil {
return nil, err
}
regMap := convertRegsToRegMap(regs)
return &jsonRegistrationDatastore{regMap: regMap}, nil
}
func (ds *jsonRegistrationDatastore) FindRegistration(issuer string) (*Registration, error) {
if reg, exists := ds.findRegistration(issuer); exists {
return ®, nil
}
return nil, fmt.Errorf("Issuer not found")
}
func (ds *jsonRegistrationDatastore) FindDeployment(issuer, deploymentID string) (*Deployment, error) {
if reg, exists := ds.findRegistration(issuer); exists {
for _, id := range reg.DeploymentIds {
if id == deploymentID {
return &Deployment{id}, nil
}
}
}
return nil, nil
}
func (ds *jsonRegistrationDatastore) findRegistration(issuer string) (Registration, bool) {
v, ok := ds.regMap[issuer]
return v, ok
}
func convertRegsToRegMap(regs []Registration) map[string]Registration {
m := make(map[string]Registration)
for _, reg := range regs {
m[reg.Issuer] = reg
}
return m
}