/
function.go
75 lines (65 loc) · 2.38 KB
/
function.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
package service
import (
"fmt"
"github.com/baetyl/baetyl-cloud/common"
"github.com/baetyl/baetyl-cloud/config"
"github.com/baetyl/baetyl-cloud/models"
"github.com/baetyl/baetyl-cloud/plugin"
)
//go:generate mockgen -destination=../mock/service/function.go -package=plugin github.com/baetyl/baetyl-cloud/service FunctionService
type FunctionService interface {
List(userID, source string) ([]models.Function, error)
ListFunctionVersions(userID, name, source string) ([]models.Function, error)
ListSources() []models.FunctionSource
GetFunction(userID, name, version, source string) (*models.Function, error)
}
type functionService struct {
functions map[string]plugin.Function
}
// NewFunctionService NewFunctionService
func NewFunctionService(config *config.CloudConfig) (FunctionService, error) {
functions := make(map[string]plugin.Function)
for _, v := range config.Plugin.Functions {
cs, err := plugin.GetPlugin(v)
if err != nil {
return nil, err
}
functions[v] = cs.(plugin.Function)
}
return &functionService{
functions: functions,
}, nil
}
// List list functions
func (c *functionService) List(userID string, source string) ([]models.Function, error) {
functionPlugin, ok := c.functions[source]
if !ok {
return nil, common.Error(common.ErrRequestParamInvalid, common.Field("error", fmt.Sprintf("the source (%s) is not supported", source)))
}
return functionPlugin.List(userID)
}
//ListVersions List all versions of a function
func (c *functionService) ListFunctionVersions(userID, name string, source string) ([]models.Function, error) {
functionPlugin, ok := c.functions[source]
if !ok {
return nil, common.Error(common.ErrRequestParamInvalid, common.Field("error", fmt.Sprintf("the source (%s) is not supported", source)))
}
return functionPlugin.ListFunctionVersions(userID, name)
}
func (c *functionService) ListSources() []models.FunctionSource {
sources := []models.FunctionSource{}
for name := range c.functions {
source := models.FunctionSource{
Name: name,
}
sources = append(sources, source)
}
return sources
}
func (c *functionService) GetFunction(userID, name, version, source string) (*models.Function, error) {
functionPlugin, ok := c.functions[source]
if !ok {
return nil, common.Error(common.ErrRequestParamInvalid, common.Field("error", fmt.Sprintf("the source (%s) is not supported", source)))
}
return functionPlugin.Get(userID, name, version)
}