forked from influxdata/chronograf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
organizations.go
128 lines (116 loc) · 3.87 KB
/
organizations.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package multistore
import (
"context"
"fmt"
"strings"
"github.com/hws522/chronograf"
)
// Ensure OrganizationsStore implements chronograf.OrganizationsStore.
var _ chronograf.OrganizationsStore = &OrganizationsStore{}
// OrganizationsStore implements the chronograf.OrganizationsStore interface, and
// delegates to all contained OrganizationsStores
type OrganizationsStore struct {
Stores []chronograf.OrganizationsStore
}
// All concatenates the Organizations of all contained Stores
func (multi *OrganizationsStore) All(ctx context.Context) ([]chronograf.Organization, error) {
all := []chronograf.Organization{}
orgSet := map[string]struct{}{}
ok := false
var err error
for _, store := range multi.Stores {
var orgs []chronograf.Organization
orgs, err = store.All(ctx)
if err != nil {
// If this Store is unable to return an array of orgs, skip to the
// next Store.
continue
}
ok = true // We've received a response from at least one Store
for _, org := range orgs {
// Enforce that the org has a unique ID
// If the ID has been seen before, ignore the org
if _, okay := orgSet[org.ID]; !okay { // We have a new org
orgSet[org.ID] = struct{}{} // We just care that the ID is unique
all = append(all, org)
}
}
}
if !ok {
return nil, err
}
return all, nil
}
// Add the org to the first responsive Store
func (multi *OrganizationsStore) Add(ctx context.Context, org *chronograf.Organization) (*chronograf.Organization, error) {
errors := []string{}
for _, store := range multi.Stores {
var o *chronograf.Organization
o, err := store.Add(ctx, org)
if err == nil {
return o, nil
}
errors = append(errors, err.Error())
}
return nil, fmt.Errorf("Unknown error while adding organization: %s", strings.Join(errors, " "))
}
// Delete delegates to all Stores, returns success if one Store is successful
func (multi *OrganizationsStore) Delete(ctx context.Context, org *chronograf.Organization) error {
errors := []string{}
for _, store := range multi.Stores {
err := store.Delete(ctx, org)
if err == nil {
return nil
}
errors = append(errors, err.Error())
}
return fmt.Errorf("Unknown error while deleting organization: %s", strings.Join(errors, " "))
}
// Get finds the Organization by id among all contained Stores
func (multi *OrganizationsStore) Get(ctx context.Context, query chronograf.OrganizationQuery) (*chronograf.Organization, error) {
var err error
for _, store := range multi.Stores {
var o *chronograf.Organization
o, err = store.Get(ctx, query)
if err == nil {
return o, nil
}
}
return nil, chronograf.ErrOrganizationNotFound
}
// Update the first responsive Store
func (multi *OrganizationsStore) Update(ctx context.Context, org *chronograf.Organization) error {
errors := []string{}
for _, store := range multi.Stores {
err := store.Update(ctx, org)
if err == nil {
return nil
}
errors = append(errors, err.Error())
}
return fmt.Errorf("Unknown error while updating organization: %s", strings.Join(errors, " "))
}
// CreateDefault makes a default organization in the first responsive Store
func (multi *OrganizationsStore) CreateDefault(ctx context.Context) error {
errors := []string{}
for _, store := range multi.Stores {
err := store.CreateDefault(ctx)
if err == nil {
return nil
}
errors = append(errors, err.Error())
}
return fmt.Errorf("Unknown error while creating default organization: %s", strings.Join(errors, " "))
}
// DefaultOrganization returns the first successful DefaultOrganization
func (multi *OrganizationsStore) DefaultOrganization(ctx context.Context) (*chronograf.Organization, error) {
errors := []string{}
for _, store := range multi.Stores {
org, err := store.DefaultOrganization(ctx)
if err == nil {
return org, nil
}
errors = append(errors, err.Error())
}
return nil, fmt.Errorf("Unknown error while getting default organization: %s", strings.Join(errors, " "))
}