-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathrequest.go
More file actions
117 lines (92 loc) · 3.5 KB
/
request.go
File metadata and controls
117 lines (92 loc) · 3.5 KB
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
package handler
import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws-cloudformation/cloudformation-cli-go-plugin/cfn/cfnerr"
"github.com/aws-cloudformation/cloudformation-cli-go-plugin/cfn/encoding"
)
const (
// marshalingError occurs when we can't marshal data from one format into another.
marshalingError = "Marshaling"
// bodyEmptyError happens when the resource body is empty
bodyEmptyError = "BodyEmpty"
)
// Request is passed to actions with customer related data
// such as resource states
type Request struct {
// The logical ID of the resource in the CloudFormation stack
LogicalResourceID string
// The callback context is an arbitrary datum which the handler can return in an
// IN_PROGRESS event to allow the passing through of additional state or
// metadata between subsequent retries; for example to pass through a Resource
// identifier which can be used to continue polling for stabilization
CallbackContext map[string]interface{}
// The RequestContext is information about the current
// invocation.
RequestContext RequestContext
// An authenticated AWS session that can be used with the AWS Go SDK
Session *session.Session
previousResourcePropertiesBody []byte
resourcePropertiesBody []byte
typeConfiguration []byte
}
// RequestContext represents information about the current
// invocation request of the handler.
type RequestContext struct {
// The stack ID of the CloudFormation stack
StackID string
// The Region of the requester
Region string
// The Account ID of the requester
AccountID string
// The stack tags associated with the cloudformation stack
StackTags map[string]string
// The SystemTags associated with the request
SystemTags map[string]string
// The NextToken provided in the request
NextToken string
}
// NewRequest returns a new Request based on the provided parameters
func NewRequest(id string, ctx map[string]interface{}, requestCTX RequestContext, sess *session.Session, previousBody, body []byte, config []byte) Request {
return Request{
LogicalResourceID: id,
CallbackContext: ctx,
Session: sess,
previousResourcePropertiesBody: previousBody,
resourcePropertiesBody: body,
RequestContext: requestCTX,
typeConfiguration: config,
}
}
// UnmarshalPrevious populates the provided interface
// with the previous properties of the resource
func (r *Request) UnmarshalPrevious(v interface{}) error {
if len(r.previousResourcePropertiesBody) == 0 {
return nil
}
if err := encoding.Unmarshal(r.previousResourcePropertiesBody, v); err != nil {
return cfnerr.New(marshalingError, "Unable to convert type", err)
}
return nil
}
// Unmarshal populates the provided interface
// with the current properties of the resource
func (r *Request) Unmarshal(v interface{}) error {
if len(r.resourcePropertiesBody) == 0 {
return cfnerr.New(bodyEmptyError, "Body is empty", nil)
}
if err := encoding.Unmarshal(r.resourcePropertiesBody, v); err != nil {
return cfnerr.New(marshalingError, "Unable to convert type", err)
}
return nil
}
// UnmarshalType populates the provided interface
// with the current resource type configuration
func (r *Request) UnmarshalType(v interface{}) error {
if len(r.resourcePropertiesBody) == 0 {
return cfnerr.New(bodyEmptyError, "Body is empty", nil)
}
if err := encoding.Unmarshal(r.typeConfiguration, v); err != nil {
return cfnerr.New(marshalingError, "Unable to convert type", err)
}
return nil
}