-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
85 lines (68 loc) · 2.49 KB
/
main.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
// Code generated by 'cfn generate', changes will be undone by the next invocation. DO NOT EDIT.
package main
import (
"errors"
"fmt"
"log"
"github.com/422158/mongodbstpatlas-cloudformation-resources/cfn-resources/V1/project/cmd/resource"
"github.com/aws-cloudformation/cloudformation-cli-go-plugin/cfn"
"github.com/aws-cloudformation/cloudformation-cli-go-plugin/cfn/handler"
)
// Handler is a container for the CRUDL actions exported by resources
type Handler struct{}
// Create wraps the related Create function exposed by the resource code
func (r *Handler) Create(req handler.Request) handler.ProgressEvent {
return wrap(req, resource.Create)
}
// Read wraps the related Read function exposed by the resource code
func (r *Handler) Read(req handler.Request) handler.ProgressEvent {
return wrap(req, resource.Read)
}
// Update wraps the related Update function exposed by the resource code
func (r *Handler) Update(req handler.Request) handler.ProgressEvent {
return wrap(req, resource.Update)
}
// Delete wraps the related Delete function exposed by the resource code
func (r *Handler) Delete(req handler.Request) handler.ProgressEvent {
return wrap(req, resource.Delete)
}
// List wraps the related List function exposed by the resource code
func (r *Handler) List(req handler.Request) handler.ProgressEvent {
return wrap(req, resource.List)
}
// main is the entry point of the application.
func main() {
cfn.Start(&Handler{})
}
type handlerFunc func(handler.Request, *resource.Model, *resource.Model) (handler.ProgressEvent, error)
func wrap(req handler.Request, f handlerFunc) (response handler.ProgressEvent) {
defer func() {
// Catch any panics and return a failed ProgressEvent
if r := recover(); r != nil {
err, ok := r.(error)
if !ok {
err = errors.New(fmt.Sprint(r))
}
log.Printf("Trapped error in handler: %v", err)
response = handler.NewFailedEvent(err)
}
}()
// Populate the previous model
prevModel := &resource.Model{}
if err := req.UnmarshalPrevious(prevModel); err != nil {
log.Printf("Error unmarshaling prev model: %v", err)
return handler.NewFailedEvent(err)
}
// Populate the current model
currentModel := &resource.Model{}
if err := req.Unmarshal(currentModel); err != nil {
log.Printf("Error unmarshaling model: %v", err)
return handler.NewFailedEvent(err)
}
response, err := f(req, prevModel, currentModel)
if err != nil {
log.Printf("Error returned from handler function: %v", err)
return handler.NewFailedEvent(err)
}
return response
}