-
Notifications
You must be signed in to change notification settings - Fork 8
/
apps.go
271 lines (221 loc) · 7.44 KB
/
apps.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package shim
import (
"errors"
"fmt"
"github.com/fnproject/fn_go/clientv2/apps"
"github.com/fnproject/fn_go/modelsv2"
"github.com/fnproject/fn_go/provider/oracle/shim/client"
"github.com/go-openapi/runtime"
"github.com/go-openapi/strfmt"
"github.com/oracle/oci-go-sdk/v65/functions"
)
const (
annotationSubnet = "oracle.com/oci/subnetIds"
)
type appsShim struct {
ociClient client.FunctionsManagementClient
compartmentId string
}
var _ apps.ClientService = &appsShim{}
func NewAppsShim(ociClient client.FunctionsManagementClient, compartmentId string) apps.ClientService {
return &appsShim{ociClient: ociClient, compartmentId: compartmentId}
}
func (s *appsShim) CreateApp(params *apps.CreateAppParams) (*apps.CreateAppOK, error) {
subnetIds, err := parseSubnetIds(params.Body.Annotations)
if err != nil {
return nil, err
}
shape, err := v2ToOCICreateApplicationShape(params.Body.Shape)
if err != nil {
return nil, err
}
details := functions.CreateApplicationDetails{
CompartmentId: &s.compartmentId,
DisplayName: ¶ms.Body.Name,
SubnetIds: subnetIds,
Config: params.Body.Config,
SyslogUrl: params.Body.SyslogURL,
Shape: shape,
}
req := functions.CreateApplicationRequest{CreateApplicationDetails: details}
res, err := s.ociClient.CreateApplication(ctxOrBackground(params.Context), req)
if err != nil {
return nil, err
}
return &apps.CreateAppOK{
Payload: ociAppToV2(res.Application),
}, nil
}
func (s *appsShim) DeleteApp(params *apps.DeleteAppParams) (*apps.DeleteAppNoContent, error) {
req := functions.DeleteApplicationRequest{ApplicationId: ¶ms.AppID}
_, err := s.ociClient.DeleteApplication(ctxOrBackground(params.Context), req)
if err != nil {
return nil, err
}
return &apps.DeleteAppNoContent{}, nil
}
func (s *appsShim) GetApp(params *apps.GetAppParams) (*apps.GetAppOK, error) {
req := functions.GetApplicationRequest{ApplicationId: ¶ms.AppID}
res, err := s.ociClient.GetApplication(ctxOrBackground(params.Context), req)
if err != nil {
return nil, err
}
return &apps.GetAppOK{
Payload: ociAppToV2(res.Application),
}, nil
}
func (s *appsShim) ListApps(params *apps.ListAppsParams) (*apps.ListAppsOK, error) {
var limit *int
if params.PerPage != nil {
ppInt := int(*params.PerPage)
limit = &ppInt
}
req := functions.ListApplicationsRequest{
CompartmentId: &s.compartmentId,
Limit: limit,
Page: params.Cursor,
DisplayName: params.Name,
}
var applicationSummaries []functions.ApplicationSummary
for {
res, err := s.ociClient.ListApplications(ctxOrBackground(params.Context), req)
if err != nil {
return nil, err
}
applicationSummaries = append(applicationSummaries, res.Items...)
if res.OpcNextPage != nil {
req.Page = res.OpcNextPage
} else {
break
}
}
var items []*modelsv2.App
// Consumers such as Fn CLI expect to get 'config' and 'syslogUrl' when doing a filter-by-name
// Given ApplicationSummary doesn't have these fields, we do a follow-up GetApp to get the full Application entity
// We could possibly optimise Fn CLI usage of this somehow so it's only used where necessary (variable in ctx?)
if params.Name != nil && len(applicationSummaries) == 1 {
getAppOK, err := s.GetApp(&apps.GetAppParams{
AppID: *applicationSummaries[0].Id,
Context: ctxOrBackground(params.Context),
})
if err != nil {
return nil, err
}
items = append(items, getAppOK.Payload)
} else {
for _, a := range applicationSummaries {
items = append(items, ociAppSummaryToV2(a))
}
}
return &apps.ListAppsOK{
Payload: &modelsv2.AppList{
Items: items,
},
}, nil
}
func (s *appsShim) UpdateApp(params *apps.UpdateAppParams) (*apps.UpdateAppOK, error) {
var etag *string
// We can respect 'omitempty' here - only do get-and-merge on config if present
if params.Body.Config != nil && len(params.Body.Config) != 0 {
// Get the current version of the App so that we can merge config
req := functions.GetApplicationRequest{ApplicationId: ¶ms.AppID}
res, err := s.ociClient.GetApplication(ctxOrBackground(params.Context), req)
if err != nil {
return nil, err
}
params.Body.Config = mergeConfig(res.Config, params.Body.Config)
etag = res.Etag
}
details := functions.UpdateApplicationDetails{
Config: params.Body.Config,
SyslogUrl: params.Body.SyslogURL,
}
req := functions.UpdateApplicationRequest{
ApplicationId: ¶ms.AppID,
UpdateApplicationDetails: details,
IfMatch: etag,
}
res, err := s.ociClient.UpdateApplication(ctxOrBackground(params.Context), req)
if err != nil {
return nil, err
}
return &apps.UpdateAppOK{
Payload: ociAppToV2(res.Application),
}, nil
}
func (*appsShim) SetTransport(runtime.ClientTransport) {}
func parseSubnetIds(annotations map[string]interface{}) ([]string, error) {
if annotations == nil || len(annotations) == 0 {
return nil, fmt.Errorf("missing subnets annotation")
}
var subnets []string
subnetsInterface, ok := annotations[annotationSubnet]
if !ok {
return nil, fmt.Errorf("missing subnets annotation")
}
// Typecast to []interface{}
subnetsArray, success := subnetsInterface.([]interface{})
if !success {
return nil, fmt.Errorf("invalid subnets annotation")
}
for _, s := range subnetsArray {
// Typecast to string
subnetString, secondSuccess := s.(string)
if !secondSuccess {
return nil, fmt.Errorf("invalid subnets annotation")
}
subnets = append(subnets, subnetString)
}
return subnets, nil
}
func ociAppToV2(ociApp functions.Application) *modelsv2.App {
annotations := make(map[string]interface{})
annotations[annotationCompartmentId] = *ociApp.CompartmentId
annotations[annotationSubnet] = ociSubnetsToAnnotationValue(ociApp.SubnetIds)
return &modelsv2.App{
Annotations: annotations,
Config: ociApp.Config,
CreatedAt: strfmt.DateTime(ociApp.TimeCreated.Time),
ID: *ociApp.Id,
Name: *ociApp.DisplayName,
Shape: string(ociApp.Shape),
SyslogURL: ociApp.SyslogUrl,
UpdatedAt: strfmt.DateTime(ociApp.TimeUpdated.Time),
}
}
func ociAppSummaryToV2(ociAppSummary functions.ApplicationSummary) *modelsv2.App {
annotations := make(map[string]interface{})
annotations[annotationCompartmentId] = *ociAppSummary.CompartmentId
annotations[annotationSubnet] = ociSubnetsToAnnotationValue(ociAppSummary.SubnetIds)
return &modelsv2.App{
Annotations: annotations,
Shape: string(ociAppSummary.Shape),
CreatedAt: strfmt.DateTime(ociAppSummary.TimeCreated.Time),
ID: *ociAppSummary.Id,
Name: *ociAppSummary.DisplayName,
UpdatedAt: strfmt.DateTime(ociAppSummary.TimeUpdated.Time),
}
}
// Behaviour of go-swagger v2 client is to return slice of interfaces
func ociSubnetsToAnnotationValue(subnets []string) []interface{} {
ifs := make([]interface{}, len(subnets))
for i, s := range subnets {
ifs[i] = s
}
return ifs
}
func v2ToOCICreateApplicationShape(shape string) (functions.CreateApplicationDetailsShapeEnum, error) {
if shape == "" {
return "", nil
}
switch shape {
case string(functions.CreateApplicationDetailsShapeX86):
return functions.CreateApplicationDetailsShapeX86, nil
case string(functions.CreateApplicationDetailsShapeArm):
return functions.CreateApplicationDetailsShapeArm, nil
case string(functions.CreateApplicationDetailsShapeX86Arm):
return functions.CreateApplicationDetailsShapeX86Arm, nil
default:
return "", errors.New(fmt.Sprintf("invalid shape value %s", shape))
}
}