forked from raystack/entropy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.go
76 lines (65 loc) · 2.35 KB
/
core.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
package core
//go:generate mockery --name=ModuleService -r --case underscore --with-expecter --structname ModuleService --filename=module_service.go --output=./mocks
import (
"context"
"encoding/json"
"time"
"github.com/goto/entropy/core/module"
"github.com/goto/entropy/core/resource"
"github.com/goto/entropy/pkg/errors"
)
type Service struct {
clock func() time.Time
store resource.Store
moduleSvc ModuleService
syncBackoff time.Duration
maxSyncRetries int
secretMask string
}
type ModuleService interface {
PlanAction(ctx context.Context, res module.ExpandedResource, act module.ActionRequest) (*resource.Resource, error)
SyncState(ctx context.Context, res module.ExpandedResource) (*resource.State, error)
StreamLogs(ctx context.Context, res module.ExpandedResource, filter map[string]string) (<-chan module.LogChunk, error)
GetOutput(ctx context.Context, res module.ExpandedResource) (json.RawMessage, error)
MaskSecrets(ctx context.Context, res resource.Resource) (resource.Resource, error)
}
func New(repo resource.Store, moduleSvc ModuleService, clockFn func() time.Time, syncBackoffInterval time.Duration, maxRetries int) *Service {
if clockFn == nil {
clockFn = time.Now
}
return &Service{
clock: clockFn,
store: repo,
syncBackoff: syncBackoffInterval,
maxSyncRetries: maxRetries,
moduleSvc: moduleSvc,
}
}
func (svc *Service) generateModuleSpec(ctx context.Context, res resource.Resource) (*module.ExpandedResource, error) {
modSpec := module.ExpandedResource{
Resource: res,
Dependencies: map[string]module.ResolvedDependency{},
}
for key, resURN := range res.Spec.Dependencies {
d, err := svc.GetResource(ctx, resURN)
if err != nil {
if errors.Is(err, errors.ErrNotFound) {
return nil, errors.ErrInvalid.
WithMsgf("dependency '%s' not found", resURN)
}
return nil, err
} else if d.State.Status != resource.StatusCompleted {
return nil, errors.ErrInvalid.
WithMsgf("dependency '%s' is in incomplete state (%s)", resURN, d.State.Status)
} else if d.Project != res.Project {
return nil, errors.ErrInvalid.
WithMsgf("dependency '%s' not found", resURN).
WithCausef("cross-project references not allowed")
}
modSpec.Dependencies[key] = module.ResolvedDependency{
Kind: d.Kind,
Output: d.State.Output,
}
}
return &modSpec, nil
}