forked from raystack/optimus
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tenant.go
102 lines (82 loc) · 2.05 KB
/
tenant.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
package tenant
import (
"github.com/goto/optimus/internal/errors"
"github.com/goto/optimus/internal/utils"
)
const EntityTenant = "tenant"
type Tenant struct {
projName ProjectName
nsName NamespaceName
}
func (t Tenant) ProjectName() ProjectName {
return t.projName
}
func (t Tenant) NamespaceName() NamespaceName {
return t.nsName
}
func (t Tenant) IsInvalid() bool {
return t.projName.String() == ""
}
func NewTenant(projectName string, namespaceName string) (Tenant, error) {
projName, err := ProjectNameFrom(projectName)
if err != nil {
return Tenant{}, err
}
nsName, err := NamespaceNameFrom(namespaceName)
if err != nil {
return Tenant{}, err
}
return Tenant{
projName: projName,
nsName: nsName,
}, nil
}
type WithDetails struct {
project Project
namespace Namespace
secretsMap map[string]string
}
func NewTenantDetails(proj *Project, namespace *Namespace, secrets PlainTextSecrets) (*WithDetails, error) {
if proj == nil {
return nil, errors.InvalidArgument(EntityTenant, "project is nil")
}
if namespace == nil {
return nil, errors.InvalidArgument(EntityTenant, "namespace is nil")
}
return &WithDetails{
project: *proj,
namespace: *namespace,
secretsMap: secrets.ToMap(),
}, nil
}
func (w *WithDetails) ToTenant() Tenant {
return Tenant{
projName: w.project.Name(),
nsName: w.namespace.Name(),
}
}
func (w *WithDetails) GetConfig(key string) (string, error) {
config, err := w.namespace.GetConfig(key)
if err == nil {
return config, nil
}
// key not present in namespace, check project
config, err = w.project.GetConfig(key)
if err == nil {
return config, nil
}
return "", errors.NotFound(EntityTenant, "config not present in tenant "+key)
}
func (w *WithDetails) GetConfigs() map[string]string {
m1 := w.namespace.GetConfigs()
return utils.MergeMaps(w.project.GetConfigs(), m1)
}
func (w *WithDetails) Project() *Project {
return &w.project
}
func (w *WithDetails) Namespace() *Namespace {
return &w.namespace
}
func (w *WithDetails) SecretsMap() map[string]string {
return w.secretsMap
}