This repository has been archived by the owner on May 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
spec.go
97 lines (83 loc) · 2.02 KB
/
spec.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
package aws
import (
"errors"
"fmt"
"github.com/gogo/protobuf/types"
"github.com/solo-io/gloo-api/pkg/api/types/v1"
"github.com/solo-io/gloo/pkg/protoutil"
)
var (
validRegions = map[string]bool{
"us-east-2": true,
"us-east-1": true,
"us-west-1": true,
"us-west-2": true,
"ap-northeast-1": true,
"ap-northeast-2": true,
"ap-south-1": true,
"ap-southeast-1": true,
"ap-southeast-2": true,
"ca-central-1": true,
"cn-north-1": true,
"eu-central-1": true,
"eu-west-1": true,
"eu-west-2": true,
"eu-west-3": true,
"sa-east-1": true,
}
)
type UpstreamSpec struct {
Region string `json:"region"`
SecretRef string `json:"secret_ref"`
}
func EncodeUpstreamSpec(spec UpstreamSpec) *types.Struct {
v1Spec, err := protoutil.MarshalStruct(spec)
if err != nil {
panic(err)
}
return v1Spec
}
func DecodeUpstreamSpec(generic v1.UpstreamSpec) (*UpstreamSpec, error) {
s := new(UpstreamSpec)
if err := protoutil.UnmarshalStruct(generic, s); err != nil {
return nil, err
}
return s, s.validateLambda()
}
func (s *UpstreamSpec) validateLambda() error {
_, exists := validRegions[s.Region]
if !exists {
return errors.New("no such region")
}
if s.SecretRef == "" {
return errors.New("missing secret reference")
}
return nil
}
func (s *UpstreamSpec) GetLambdaHostname() string {
return fmt.Sprintf("lambda.%s.amazonaws.com", s.Region)
}
type FunctionSpec struct {
FunctionName string `json:"function_name"`
Qualifier string `json:"qualifier"`
}
func DecodeFunctionSpec(generic v1.FunctionSpec) (*FunctionSpec, error) {
s := new(FunctionSpec)
if err := protoutil.UnmarshalStruct(generic, s); err != nil {
return nil, err
}
return s, s.ValidateLambda()
}
func EncodeFunctionSpec(spec FunctionSpec) *types.Struct {
v1Spec, err := protoutil.MarshalStruct(spec)
if err != nil {
panic(err)
}
return v1Spec
}
func (s *FunctionSpec) ValidateLambda() error {
if s.FunctionName == "" {
return errors.New("invalid function name")
}
return nil
}