github.com/aws-cloudformation/cloudformation-cli-go-plugin@v1.2.0/python/rpdk/go/templates/main.go.tple (about) 1 // Code generated by 'cfn generate', changes will be undone by the next invocation. DO NOT EDIT. 2 package main 3 4 import ( 5 "errors" 6 "fmt" 7 "log" 8 9 "github.com/aws-cloudformation/cloudformation-cli-go-plugin/cfn" 10 "github.com/aws-cloudformation/cloudformation-cli-go-plugin/cfn/handler" 11 "{{ path }}" 12 ) 13 14 // Handler is a container for the CRUDL actions exported by resources 15 type Handler struct{} 16 17 {% for method in ("Create", "Read", "Update", "Delete", "List") %} 18 19 // {{ method }} wraps the related {{ method }} function exposed by the resource code 20 func (r *Handler) {{ method }}(req handler.Request) handler.ProgressEvent { 21 return wrap(req, resource.{{ method }}) 22 } 23 {% endfor %} 24 25 // main is the entry point of the application. 26 func main() { 27 cfn.Start(&Handler{}) 28 } 29 30 type handlerFunc func(handler.Request, *resource.Model, *resource.Model) (handler.ProgressEvent, error) 31 32 func wrap(req handler.Request, f handlerFunc) (response handler.ProgressEvent) { 33 defer func() { 34 // Catch any panics and return a failed ProgressEvent 35 if r := recover(); r != nil { 36 err, ok := r.(error) 37 if !ok { 38 err = errors.New(fmt.Sprint(r)) 39 } 40 41 log.Printf("Trapped error in handler: %v", err) 42 43 response = handler.NewFailedEvent(err) 44 } 45 }() 46 47 // Populate the previous model 48 prevModel := &resource.Model{} 49 if err := req.UnmarshalPrevious(prevModel); err != nil { 50 log.Printf("Error unmarshaling prev model: %v", err) 51 return handler.NewFailedEvent(err) 52 } 53 54 // Populate the current model 55 currentModel := &resource.Model{} 56 if err := req.Unmarshal(currentModel); err != nil { 57 log.Printf("Error unmarshaling model: %v", err) 58 return handler.NewFailedEvent(err) 59 } 60 61 response, err := f(req, prevModel, currentModel) 62 if err != nil { 63 log.Printf("Error returned from handler function: %v", err) 64 return handler.NewFailedEvent(err) 65 } 66 67 return response 68 }