diff --git a/hive/gateway/ec2/image/CreateImage_test.go b/hive/gateway/ec2/image/CreateImage_test.go index 1135ff05c..5daa3a464 100644 --- a/hive/gateway/ec2/image/CreateImage_test.go +++ b/hive/gateway/ec2/image/CreateImage_test.go @@ -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{ diff --git a/hive/gateway/ec2/instance/RunInstances_test.go b/hive/gateway/ec2/instance/RunInstances_test.go index a7ad6f2d1..60325868a 100644 --- a/hive/gateway/ec2/instance/RunInstances_test.go +++ b/hive/gateway/ec2/instance/RunInstances_test.go @@ -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(`{ diff --git a/hive/gateway/ec2/key/CreateKeyPair_test.go b/hive/gateway/ec2/key/CreateKeyPair_test.go index c547bee89..7176c0395 100644 --- a/hive/gateway/ec2/key/CreateKeyPair_test.go +++ b/hive/gateway/ec2/key/CreateKeyPair_test.go @@ -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{ diff --git a/hive/handlers/ec2/image/CopyImageHandler.go b/hive/handlers/ec2/image/CopyImageHandler.go index 34305d332..0de730480 100644 --- a/hive/handlers/ec2/image/CopyImageHandler.go +++ b/hive/handlers/ec2/image/CopyImageHandler.go @@ -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 diff --git a/hive/handlers/ec2/image/CreateImageHandler.go b/hive/handlers/ec2/image/CreateImageHandler.go index 99f9dd39a..647467074 100644 --- a/hive/handlers/ec2/image/CreateImageHandler.go +++ b/hive/handlers/ec2/image/CreateImageHandler.go @@ -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 diff --git a/hive/handlers/ec2/image/DeregisterImageHandler.go b/hive/handlers/ec2/image/DeregisterImageHandler.go index ec896239b..f44926d80 100644 --- a/hive/handlers/ec2/image/DeregisterImageHandler.go +++ b/hive/handlers/ec2/image/DeregisterImageHandler.go @@ -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 diff --git a/hive/handlers/ec2/image/DescribeImageAttributeHandler.go b/hive/handlers/ec2/image/DescribeImageAttributeHandler.go index 4ba3bd425..82a823888 100644 --- a/hive/handlers/ec2/image/DescribeImageAttributeHandler.go +++ b/hive/handlers/ec2/image/DescribeImageAttributeHandler.go @@ -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 diff --git a/hive/handlers/ec2/image/DescribeImagesHandler.go b/hive/handlers/ec2/image/DescribeImagesHandler.go index 9e21980b7..9e6198986 100644 --- a/hive/handlers/ec2/image/DescribeImagesHandler.go +++ b/hive/handlers/ec2/image/DescribeImagesHandler.go @@ -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 diff --git a/hive/handlers/ec2/image/ModifyImageAttributeHandler.go b/hive/handlers/ec2/image/ModifyImageAttributeHandler.go index 2272cc007..28c89950e 100644 --- a/hive/handlers/ec2/image/ModifyImageAttributeHandler.go +++ b/hive/handlers/ec2/image/ModifyImageAttributeHandler.go @@ -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 diff --git a/hive/handlers/ec2/image/RegisterImageHandler.go b/hive/handlers/ec2/image/RegisterImageHandler.go index 66d4901ed..c58eff301 100644 --- a/hive/handlers/ec2/image/RegisterImageHandler.go +++ b/hive/handlers/ec2/image/RegisterImageHandler.go @@ -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 diff --git a/hive/handlers/ec2/image/ResetImageAttributeHandler.go b/hive/handlers/ec2/image/ResetImageAttributeHandler.go index d5807786f..5a3875b57 100644 --- a/hive/handlers/ec2/image/ResetImageAttributeHandler.go +++ b/hive/handlers/ec2/image/ResetImageAttributeHandler.go @@ -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 diff --git a/hive/handlers/ec2/instance/RunInstancesHandler.go b/hive/handlers/ec2/instance/RunInstancesHandler.go index 465710a00..4cee122d7 100644 --- a/hive/handlers/ec2/instance/RunInstancesHandler.go +++ b/hive/handlers/ec2/instance/RunInstancesHandler.go @@ -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 diff --git a/hive/handlers/ec2/key/CreateKeyPairHandler.go b/hive/handlers/ec2/key/CreateKeyPairHandler.go index 77a6b8519..5fdfc5720 100644 --- a/hive/handlers/ec2/key/CreateKeyPairHandler.go +++ b/hive/handlers/ec2/key/CreateKeyPairHandler.go @@ -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 diff --git a/hive/handlers/ec2/key/DeleteKeyPairHandler.go b/hive/handlers/ec2/key/DeleteKeyPairHandler.go index a0ba14c8e..5791c2f78 100644 --- a/hive/handlers/ec2/key/DeleteKeyPairHandler.go +++ b/hive/handlers/ec2/key/DeleteKeyPairHandler.go @@ -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 diff --git a/hive/handlers/ec2/key/DescribeKeyPairsHandler.go b/hive/handlers/ec2/key/DescribeKeyPairsHandler.go index 1854b6f9e..59e973c18 100644 --- a/hive/handlers/ec2/key/DescribeKeyPairsHandler.go +++ b/hive/handlers/ec2/key/DescribeKeyPairsHandler.go @@ -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 diff --git a/hive/handlers/ec2/key/ImportKeyPairHandler.go b/hive/handlers/ec2/key/ImportKeyPairHandler.go index e09869d0f..5ad4bd083 100644 --- a/hive/handlers/ec2/key/ImportKeyPairHandler.go +++ b/hive/handlers/ec2/key/ImportKeyPairHandler.go @@ -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