Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose previous and current resource properties #185

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cfn/handler/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ type Request struct {
resourcePropertiesBody []byte
}

func (r *Request) PreviousResourcePropertiesBody() []byte {
return r.previousResourcePropertiesBody
}

func (r *Request) ResourcePropertiesBody() []byte {
return r.resourcePropertiesBody
}

// RequestContext represents information about the current
// invocation request of the handler.
type RequestContext struct {
Expand Down
56 changes: 56 additions & 0 deletions cfn/handler/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,59 @@ func TestUnmarshal(t *testing.T) {
t.Errorf(diff)
}
}

func TestNestedUnmarshal(t *testing.T) {
type Model struct {
Name *string
Version *float64
Detail map[string]interface{}
}

req := Request{
LogicalResourceID: "foo",
previousResourcePropertiesBody: []byte(`{"Name":"bar","Version":"0.1","Detail":{"Nested":{"Build":"57","IsProduction":"false"}}}`),
resourcePropertiesBody: []byte(`{"Name":"baz","Version":"2.3","Detail":{"Nested":{"Build":"69","IsProduction":"true"}}}`),
}

expectedPrevious := Model{
Name: aws.String("bar"),
Version: aws.Float64(0.1),
Detail: map[string]interface{}{
"Nested": map[string]interface{}{
"Build": aws.Int(57),
"IsProduction": aws.Bool(false),
},
},
}

expectedCurrent := Model{
Name: aws.String("baz"),
Version: aws.Float64(2.3),
Detail: map[string]interface{}{
"Nested": map[string]interface{}{
"Build": aws.Int(69),
"IsProduction": aws.Bool(true),
},
},
}

actual := Model{}

// Previous body
err := req.UnmarshalPrevious(&actual)
if err != nil {
t.Error(err)
}
if diff := cmp.Diff(actual, expectedPrevious); diff != "" {
t.Errorf(diff)
}

// Current body
err = req.Unmarshal(&actual)
if err != nil {
t.Error(err)
}
if diff := cmp.Diff(actual, expectedCurrent); diff != "" {
t.Errorf(diff)
}
}