-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathproject.go
More file actions
166 lines (146 loc) · 4.49 KB
/
project.go
File metadata and controls
166 lines (146 loc) · 4.49 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package model
import (
"context"
"time"
"github.com/pkg/errors"
"github.com/launchdarkly/go-sdk-common/v3/ldcontext"
"github.com/launchdarkly/go-sdk-common/v3/ldvalue"
"github.com/launchdarkly/ldcli/internal/dev_server/adapters"
)
type Project struct {
Key string
SourceEnvironmentKey string
Context ldcontext.Context
LastSyncTime time.Time
AllFlagsState FlagsState
AvailableVariations []FlagVariation
PayloadVersion int
}
// CreateProject creates a project and adds it to the database.
func CreateProject(ctx context.Context, projectKey, sourceEnvironmentKey string, ldCtx *ldcontext.Context) (Project, error) {
project := Project{
Key: projectKey,
SourceEnvironmentKey: sourceEnvironmentKey,
PayloadVersion: 1,
}
if ldCtx == nil {
project.Context = ldcontext.NewBuilder("user").Key("dev-environment").Build()
} else {
project.Context = *ldCtx
}
err := project.refreshExternalState(ctx)
if err != nil {
return Project{}, err
}
store := StoreFromContext(ctx)
err = store.InsertProject(ctx, project)
if err != nil {
return Project{}, err
}
return project, nil
}
func (project *Project) refreshExternalState(ctx context.Context) error {
flagsState, err := project.fetchFlagState(ctx)
if err != nil {
return err
}
project.AllFlagsState = flagsState
project.LastSyncTime = time.Now()
availableVariations, err := project.fetchAvailableVariations(ctx)
if err != nil {
return err
}
project.AvailableVariations = availableVariations
return nil
}
func UpdateProject(ctx context.Context, projectKey string, context *ldcontext.Context, sourceEnvironmentKey *string) (Project, error) {
store := StoreFromContext(ctx)
project, err := store.GetDevProject(ctx, projectKey)
if err != nil {
return Project{}, err
}
if context != nil {
project.Context = *context
}
if sourceEnvironmentKey != nil {
project.SourceEnvironmentKey = *sourceEnvironmentKey
}
err = project.refreshExternalState(ctx)
if err != nil {
return Project{}, err
}
updated, err := store.UpdateProject(ctx, *project)
if err != nil {
return Project{}, err
}
if !updated {
return Project{}, errors.New("Project not updated")
}
newPayloadVersion, err := store.IncrementProjectPayloadVersion(ctx, projectKey)
if err != nil {
return Project{}, errors.Wrap(err, "unable to increment payload version")
}
project.PayloadVersion = newPayloadVersion
allFlagsWithOverrides, err := project.GetFlagStateWithOverridesForProject(ctx)
if err != nil {
return Project{}, errors.Wrapf(err, "unable to get overrides for project, %s", projectKey)
}
GetObserversFromContext(ctx).Notify(SyncEvent{
ProjectKey: project.Key,
AllFlagsState: allFlagsWithOverrides,
})
return *project, nil
}
func (project Project) GetFlagStateWithOverridesForProject(ctx context.Context) (FlagsState, error) {
store := StoreFromContext(ctx)
overrides, err := store.GetOverridesForProject(ctx, project.Key)
if err != nil {
return FlagsState{}, errors.Wrapf(err, "unable to fetch overrides for project %s", project.Key)
}
withOverrides := make(FlagsState, len(project.AllFlagsState))
for flagKey, flagState := range project.AllFlagsState {
if override, ok := overrides.GetFlag(flagKey); ok {
flagState = override.Apply(flagState)
}
withOverrides[flagKey] = flagState
}
return withOverrides, nil
}
func (project Project) fetchAvailableVariations(ctx context.Context) ([]FlagVariation, error) {
apiAdapter := adapters.GetApi(ctx)
flags, err := apiAdapter.GetAllFlags(ctx, project.Key)
if err != nil {
return nil, err
}
var allVariations []FlagVariation
for _, flag := range flags {
flagKey := flag.Key
for _, variation := range flag.Variations {
allVariations = append(allVariations, FlagVariation{
FlagKey: flagKey,
Variation: Variation{
Id: *variation.Id,
Description: variation.Description,
Name: variation.Name,
Value: ldvalue.CopyArbitraryValue(variation.Value),
},
})
}
}
return allVariations, nil
}
func (project Project) fetchFlagState(ctx context.Context) (FlagsState, error) {
apiAdapter := adapters.GetApi(ctx)
sdkKey, err := apiAdapter.GetSdkKey(ctx, project.Key, project.SourceEnvironmentKey)
flagsState := make(FlagsState)
if err != nil {
return flagsState, err
}
sdkAdapter := adapters.GetSdk(ctx)
sdkFlags, err := sdkAdapter.GetAllFlagsState(ctx, project.Context, sdkKey)
if err != nil {
return flagsState, err
}
flagsState = FromAllFlags(sdkFlags)
return flagsState, nil
}