forked from Juniper/contrail
/
service.go
62 lines (53 loc) · 1.56 KB
/
service.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
package logic
import (
"context"
"github.com/pkg/errors"
"github.com/Juniper/contrail/pkg/compilation/dependencies"
"github.com/Juniper/contrail/pkg/compilation/intent"
"github.com/Juniper/contrail/pkg/services"
)
// Service implementing Intent Compiler's type-specific logic.
type Service struct {
services.BaseService
// WriteService is used to create/update/delete lower-level resources
WriteService services.WriteService
IntPoolAllocator services.IntPoolAllocator
ReadService services.ReadService
cache *intent.Cache
dependencyProcessor *dependencies.DependencyProcessor
}
// NewService creates a Service
func NewService(
apiClient services.WriteService,
readService services.ReadService,
allocator services.IntPoolAllocator,
cache *intent.Cache,
dependencyProcessor *dependencies.DependencyProcessor,
) *Service {
return &Service{
WriteService: apiClient,
ReadService: readService,
IntPoolAllocator: allocator,
cache: cache,
dependencyProcessor: dependencyProcessor,
}
}
func (s *Service) evaluateContext() *intent.EvaluateContext {
return &intent.EvaluateContext{
WriteService: s.WriteService,
ReadService: s.ReadService,
IntPoolAllocator: s.IntPoolAllocator,
IntentLoader: s.cache,
}
}
func (s *Service) storeAndEvaluate(
ctx context.Context,
i intent.Intent,
) error {
s.cache.Store(i)
ec := s.evaluateContext()
if err := s.EvaluateDependencies(ctx, ec, i); err != nil {
return errors.Wrapf(err, "failed to evaluate %s dependencies", i.Kind())
}
return nil
}