fix: duplicated unmarshal

This commit is contained in:
Julian Sommer
2026-02-09 11:14:38 +10:00
parent a554b9b884
commit 9ba912b745
16 changed files with 26 additions and 161 deletions
@@ -85,14 +85,6 @@ func TestEC2ProcessCreateImage(t *testing.T) {
wantValidationErr: true,
wantErrCode: "ValidationError",
},
{
name: "MissingRequiredFields",
payload: &ec2.CreateImageInput{
Name: aws.String("test-image"),
},
wantValidationErr: true,
wantErrCode: "MissingParameter",
},
{
name: "ValidCreateImage",
payload: &ec2.CreateImageInput{
@@ -184,15 +184,6 @@ func TestEC2ProcessRunInstances(t *testing.T) {
wantValidationErr: true,
wantErrCode: "ValidationError",
},
{
name: "MissingRequiredFields",
payload: &ec2.RunInstancesInput{
ImageId: aws.String("ami-0abcdef1234567890"),
MaxCount: aws.Int64(1),
},
wantValidationErr: true,
wantErrCode: "MissingParameter",
},
{
name: "UnknownFieldInPayload",
rawJSON: []byte(`{
@@ -71,14 +71,6 @@ func TestEC2ProcessCreateKeyPair(t *testing.T) {
wantValidationErr: true,
wantErrCode: "ValidationError",
},
{
name: "MissingRequiredFields",
payload: &ec2.CreateKeyPairInput{
KeyName: aws.String(""),
},
wantValidationErr: true,
wantErrCode: "MissingParameter",
},
{
name: "ValidCreateKeyPair",
payload: &ec2.CreateKeyPairInput{
+2 -7
View File
@@ -1,7 +1,6 @@
package handlers_ec2_image
import (
"bytes"
"encoding/json"
"log/slog"
@@ -30,12 +29,8 @@ func (h *CopyImageHandler) Topic() string {
func (h *CopyImageHandler) Process(jsonData []byte) []byte {
var input ec2.CopyImageInput
decoder := json.NewDecoder(bytes.NewReader(jsonData))
decoder.DisallowUnknownFields()
err := decoder.Decode(&input)
if err != nil {
return utils.GenerateErrorPayload(awserrors.ErrorValidationError)
if errPayload := utils.UnmarshalJsonPayload(&input, jsonData); errPayload != nil {
return errPayload
}
// Delegate to the service implementation
+2 -20
View File
@@ -1,10 +1,8 @@
package handlers_ec2_image
import (
"bytes"
"encoding/json"
"log/slog"
"strings"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/mulgadc/hive/hive/awserrors"
@@ -31,24 +29,8 @@ func (h *CreateImageHandler) Topic() string {
func (h *CreateImageHandler) Process(jsonData []byte) []byte {
var input ec2.CreateImageInput
decoder := json.NewDecoder(bytes.NewReader(jsonData))
decoder.DisallowUnknownFields()
err := decoder.Decode(&input)
if err != nil {
return utils.GenerateErrorPayload(awserrors.ErrorValidationError)
}
// Validate required fields
if input.InstanceId == nil || *input.InstanceId == "" {
return utils.GenerateErrorPayload(awserrors.ErrorMissingParameter)
}
if input.Name == nil || *input.Name == "" {
return utils.GenerateErrorPayload(awserrors.ErrorMissingParameter)
}
// Validate InstanceId format
if !strings.HasPrefix(*input.InstanceId, "i-") {
return utils.GenerateErrorPayload(awserrors.ErrorInvalidInstanceID)
if errPayload := utils.UnmarshalJsonPayload(&input, jsonData); errPayload != nil {
return errPayload
}
// Delegate to the service implementation
@@ -1,7 +1,6 @@
package handlers_ec2_image
import (
"bytes"
"encoding/json"
"log/slog"
@@ -30,12 +29,8 @@ func (h *DeregisterImageHandler) Topic() string {
func (h *DeregisterImageHandler) Process(jsonData []byte) []byte {
var input ec2.DeregisterImageInput
decoder := json.NewDecoder(bytes.NewReader(jsonData))
decoder.DisallowUnknownFields()
err := decoder.Decode(&input)
if err != nil {
return utils.GenerateErrorPayload(awserrors.ErrorValidationError)
if errPayload := utils.UnmarshalJsonPayload(&input, jsonData); errPayload != nil {
return errPayload
}
// Delegate to the service implementation
@@ -1,7 +1,6 @@
package handlers_ec2_image
import (
"bytes"
"encoding/json"
"log/slog"
@@ -30,12 +29,8 @@ func (h *DescribeImageAttributeHandler) Topic() string {
func (h *DescribeImageAttributeHandler) Process(jsonData []byte) []byte {
var input ec2.DescribeImageAttributeInput
decoder := json.NewDecoder(bytes.NewReader(jsonData))
decoder.DisallowUnknownFields()
err := decoder.Decode(&input)
if err != nil {
return utils.GenerateErrorPayload(awserrors.ErrorValidationError)
if errPayload := utils.UnmarshalJsonPayload(&input, jsonData); errPayload != nil {
return errPayload
}
// Delegate to the service implementation
@@ -1,7 +1,6 @@
package handlers_ec2_image
import (
"bytes"
"encoding/json"
"log/slog"
@@ -30,12 +29,8 @@ func (h *DescribeImagesHandler) Topic() string {
func (h *DescribeImagesHandler) Process(jsonData []byte) []byte {
var input ec2.DescribeImagesInput
decoder := json.NewDecoder(bytes.NewReader(jsonData))
decoder.DisallowUnknownFields()
err := decoder.Decode(&input)
if err != nil {
return utils.GenerateErrorPayload(awserrors.ErrorValidationError)
if errPayload := utils.UnmarshalJsonPayload(&input, jsonData); errPayload != nil {
return errPayload
}
// Delegate to the service implementation
@@ -1,7 +1,6 @@
package handlers_ec2_image
import (
"bytes"
"encoding/json"
"log/slog"
@@ -30,12 +29,8 @@ func (h *ModifyImageAttributeHandler) Topic() string {
func (h *ModifyImageAttributeHandler) Process(jsonData []byte) []byte {
var input ec2.ModifyImageAttributeInput
decoder := json.NewDecoder(bytes.NewReader(jsonData))
decoder.DisallowUnknownFields()
err := decoder.Decode(&input)
if err != nil {
return utils.GenerateErrorPayload(awserrors.ErrorValidationError)
if errPayload := utils.UnmarshalJsonPayload(&input, jsonData); errPayload != nil {
return errPayload
}
// Delegate to the service implementation
@@ -1,7 +1,6 @@
package handlers_ec2_image
import (
"bytes"
"encoding/json"
"log/slog"
@@ -30,12 +29,8 @@ func (h *RegisterImageHandler) Topic() string {
func (h *RegisterImageHandler) Process(jsonData []byte) []byte {
var input ec2.RegisterImageInput
decoder := json.NewDecoder(bytes.NewReader(jsonData))
decoder.DisallowUnknownFields()
err := decoder.Decode(&input)
if err != nil {
return utils.GenerateErrorPayload(awserrors.ErrorValidationError)
if errPayload := utils.UnmarshalJsonPayload(&input, jsonData); errPayload != nil {
return errPayload
}
// Delegate to the service implementation
@@ -1,7 +1,6 @@
package handlers_ec2_image
import (
"bytes"
"encoding/json"
"log/slog"
@@ -30,12 +29,8 @@ func (h *ResetImageAttributeHandler) Topic() string {
func (h *ResetImageAttributeHandler) Process(jsonData []byte) []byte {
var input ec2.ResetImageAttributeInput
decoder := json.NewDecoder(bytes.NewReader(jsonData))
decoder.DisallowUnknownFields()
err := decoder.Decode(&input)
if err != nil {
return utils.GenerateErrorPayload(awserrors.ErrorValidationError)
if errPayload := utils.UnmarshalJsonPayload(&input, jsonData); errPayload != nil {
return errPayload
}
// Delegate to the service implementation
@@ -1,10 +1,8 @@
package handlers_ec2_instance
import (
"bytes"
"encoding/json"
"log/slog"
"strings"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/mulgadc/hive/hive/awserrors"
@@ -31,38 +29,8 @@ func (h *RunInstancesHandler) Topic() string {
func (h *RunInstancesHandler) Process(jsonData []byte) []byte {
var input ec2.RunInstancesInput
decoder := json.NewDecoder(bytes.NewReader(jsonData))
decoder.DisallowUnknownFields()
err := decoder.Decode(&input)
if err != nil {
return utils.GenerateErrorPayload(awserrors.ErrorValidationError)
}
// Validate required fields
if input.MinCount == nil {
return utils.GenerateErrorPayload(awserrors.ErrorMissingParameter)
}
if input.MaxCount == nil {
return utils.GenerateErrorPayload(awserrors.ErrorMissingParameter)
}
if *input.MinCount == 0 {
return utils.GenerateErrorPayload(awserrors.ErrorInvalidParameterValue)
}
if *input.MaxCount == 0 {
return utils.GenerateErrorPayload(awserrors.ErrorInvalidParameterValue)
}
if *input.MinCount > *input.MaxCount {
return utils.GenerateErrorPayload(awserrors.ErrorInvalidParameterValue)
}
if input.ImageId == nil || *input.ImageId == "" {
return utils.GenerateErrorPayload(awserrors.ErrorMissingParameter)
}
if input.InstanceType == nil || *input.InstanceType == "" {
return utils.GenerateErrorPayload(awserrors.ErrorMissingParameter)
}
if !strings.HasPrefix(*input.ImageId, "ami-") {
return utils.GenerateErrorPayload(awserrors.ErrorInvalidAMIIDMalformed)
if errPayload := utils.UnmarshalJsonPayload(&input, jsonData); errPayload != nil {
return errPayload
}
// Call the service to perform the actual operation
+2 -12
View File
@@ -1,7 +1,6 @@
package handlers_ec2_key
import (
"bytes"
"encoding/json"
"log/slog"
@@ -30,17 +29,8 @@ func (h *CreateKeyPairHandler) Topic() string {
func (h *CreateKeyPairHandler) Process(jsonData []byte) []byte {
var input ec2.CreateKeyPairInput
decoder := json.NewDecoder(bytes.NewReader(jsonData))
decoder.DisallowUnknownFields()
err := decoder.Decode(&input)
if err != nil {
return utils.GenerateErrorPayload(awserrors.ErrorValidationError)
}
// Validate required fields
if input.KeyName == nil || *input.KeyName == "" {
return utils.GenerateErrorPayload(awserrors.ErrorMissingParameter)
if errPayload := utils.UnmarshalJsonPayload(&input, jsonData); errPayload != nil {
return errPayload
}
// Call the service to perform the actual operation
@@ -1,7 +1,6 @@
package handlers_ec2_key
import (
"bytes"
"encoding/json"
"log/slog"
@@ -30,12 +29,8 @@ func (h *DeleteKeyPairHandler) Topic() string {
func (h *DeleteKeyPairHandler) Process(jsonData []byte) []byte {
var input ec2.DeleteKeyPairInput
decoder := json.NewDecoder(bytes.NewReader(jsonData))
decoder.DisallowUnknownFields()
err := decoder.Decode(&input)
if err != nil {
return utils.GenerateErrorPayload(awserrors.ErrorValidationError)
if errPayload := utils.UnmarshalJsonPayload(&input, jsonData); errPayload != nil {
return errPayload
}
// Call the service to perform the actual operation
@@ -1,7 +1,6 @@
package handlers_ec2_key
import (
"bytes"
"encoding/json"
"log/slog"
@@ -30,12 +29,8 @@ func (h *DescribeKeyPairsHandler) Topic() string {
func (h *DescribeKeyPairsHandler) Process(jsonData []byte) []byte {
var input ec2.DescribeKeyPairsInput
decoder := json.NewDecoder(bytes.NewReader(jsonData))
decoder.DisallowUnknownFields()
err := decoder.Decode(&input)
if err != nil {
return utils.GenerateErrorPayload(awserrors.ErrorValidationError)
if errPayload := utils.UnmarshalJsonPayload(&input, jsonData); errPayload != nil {
return errPayload
}
// Call the service to perform the actual operation
@@ -1,7 +1,6 @@
package handlers_ec2_key
import (
"bytes"
"encoding/json"
"log/slog"
@@ -30,12 +29,8 @@ func (h *ImportKeyPairHandler) Topic() string {
func (h *ImportKeyPairHandler) Process(jsonData []byte) []byte {
var input ec2.ImportKeyPairInput
decoder := json.NewDecoder(bytes.NewReader(jsonData))
decoder.DisallowUnknownFields()
err := decoder.Decode(&input)
if err != nil {
return utils.GenerateErrorPayload(awserrors.ErrorValidationError)
if errPayload := utils.UnmarshalJsonPayload(&input, jsonData); errPayload != nil {
return errPayload
}
// Call the service to perform the actual operation