From e6bea9f50a815c79aae2c294ad3114df91964fe3 Mon Sep 17 00:00:00 2001 From: benduncan <5178243+benduncan@users.noreply.github.co> Date: Thu, 16 Oct 2025 17:54:57 +1100 Subject: [PATCH] Logic cleanup --- hive/gateway/ec2/image/CopyImage.go | 45 ++------------ hive/gateway/ec2/image/CreateImage.go | 45 ++------------ hive/gateway/ec2/image/CreateImage_test.go | 6 +- hive/gateway/ec2/image/DeregisterImage.go | 44 ++------------ .../ec2/image/DescribeImageAttribute.go | 48 ++------------- hive/gateway/ec2/image/DescribeImages.go | 55 ++--------------- .../gateway/ec2/image/ModifyImageAttribute.go | 44 ++------------ hive/gateway/ec2/image/RegisterImage.go | 45 ++------------ hive/gateway/ec2/image/ResetImageAttribute.go | 44 ++------------ hive/gateway/ec2/instance/RunInstances.go | 60 ++----------------- .../gateway/ec2/instance/RunInstances_test.go | 6 +- hive/gateway/ec2/key/CreateKeyPair.go | 48 ++------------- hive/gateway/ec2/key/CreateKeyPair_test.go | 6 +- hive/gateway/ec2/key/DeleteKeyPair.go | 46 ++------------ hive/gateway/ec2/key/DescribeKeyPairs.go | 52 ++-------------- hive/gateway/ec2/key/ImportKeyPair.go | 47 ++------------- hive/utils/utils.go | 6 +- 17 files changed, 68 insertions(+), 579 deletions(-) diff --git a/hive/gateway/ec2/image/CopyImage.go b/hive/gateway/ec2/image/CopyImage.go index 6581971b9..a6efd6eb4 100644 --- a/hive/gateway/ec2/image/CopyImage.go +++ b/hive/gateway/ec2/image/CopyImage.go @@ -1,16 +1,14 @@ package gateway_ec2_image import ( - "bytes" "encoding/json" "errors" "fmt" "log/slog" "strings" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" - "github.com/mulgadc/hive/hive/awserrors" + "github.com/mulgadc/hive/hive/handlers/ec2/image" "github.com/mulgadc/hive/hive/utils" ) @@ -120,8 +118,9 @@ func CopyImage(input *ec2.CopyImageInput) (output ec2.CopyImageOutput, err error return output, fmt.Errorf("failed to marshal input to JSON: %w", err) } - // Run the simulated JSON request via NATS, which will return a JSON response - jsonResp := EC2_Process_CopyImage(jsonData) + // Run the simulated JSON request via handler, which will return a JSON response + handler := handlers_ec2_image.NewCopyImageHandler(handlers_ec2_image.NewMockImageService()) + jsonResp := handler.Process(jsonData) // Validate if the response is successful or an error responseError, err := utils.ValidateErrorPayload(jsonResp) @@ -142,39 +141,3 @@ func CopyImage(input *ec2.CopyImageInput) (output ec2.CopyImageOutput, err error return output, nil } - -func EC2_Process_CopyImage(jsonData []byte) (output []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) - } - - // Ensure the payload provided the fields that EC2 expects before proceeding. - err = ValidateCopyImageInput(&input) - - if err != nil { - return utils.GenerateErrorPayload(awserrors.ErrorValidationError) - } - - // TODO: Add the logic to actually copy the image. - // This is a placeholder response for testing the framework. - - result := &ec2.CopyImageOutput{ - ImageId: aws.String("ami-0987654321fedcba0"), - } - - // Return as JSON, to simulate the NATS response - jsonResponse, err := json.Marshal(result) - if err != nil { - slog.Error("EC2_Process_CopyImage could not marshal output", "err", err) - return nil - } - - return jsonResponse -} diff --git a/hive/gateway/ec2/image/CreateImage.go b/hive/gateway/ec2/image/CreateImage.go index 0ff95abb3..57336b66b 100644 --- a/hive/gateway/ec2/image/CreateImage.go +++ b/hive/gateway/ec2/image/CreateImage.go @@ -1,16 +1,14 @@ package gateway_ec2_image import ( - "bytes" "encoding/json" "errors" "fmt" "log/slog" "strings" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" - "github.com/mulgadc/hive/hive/awserrors" + "github.com/mulgadc/hive/hive/handlers/ec2/image" "github.com/mulgadc/hive/hive/utils" ) @@ -119,8 +117,9 @@ func CreateImage(input *ec2.CreateImageInput) (output ec2.CreateImageOutput, err return output, fmt.Errorf("failed to marshal input to JSON: %w", err) } - // Run the simulated JSON request via NATS, which will return a JSON response - jsonResp := EC2_Process_CreateImage(jsonData) + // Run the simulated JSON request via handler, which will return a JSON response + handler := handlers_ec2_image.NewCreateImageHandler(handlers_ec2_image.NewMockImageService()) + jsonResp := handler.Process(jsonData) // Validate if the response is successful or an error responseError, err := utils.ValidateErrorPayload(jsonResp) @@ -141,39 +140,3 @@ func CreateImage(input *ec2.CreateImageInput) (output ec2.CreateImageOutput, err return output, nil } - -func EC2_Process_CreateImage(jsonData []byte) (output []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) - } - - // Ensure the payload provided the fields that EC2 expects before proceeding. - err = ValidateCreateImageInput(&input) - - if err != nil { - return utils.GenerateErrorPayload(awserrors.ErrorValidationError) - } - - // TODO: Add the logic to actually create the image from the instance. - // This is a placeholder response for testing the framework. - - result := &ec2.CreateImageOutput{ - ImageId: aws.String("ami-0123456789abcdef0"), - } - - // Return as JSON, to simulate the NATS response - jsonResponse, err := json.Marshal(result) - if err != nil { - slog.Error("EC2_Process_CreateImage could not marshal output", "err", err) - return nil - } - - return jsonResponse -} diff --git a/hive/gateway/ec2/image/CreateImage_test.go b/hive/gateway/ec2/image/CreateImage_test.go index 7b3728cb3..76056940d 100644 --- a/hive/gateway/ec2/image/CreateImage_test.go +++ b/hive/gateway/ec2/image/CreateImage_test.go @@ -8,6 +8,7 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" + "github.com/mulgadc/hive/hive/handlers/ec2/image" "github.com/mulgadc/hive/hive/utils" "github.com/stretchr/testify/assert" ) @@ -90,7 +91,7 @@ func TestEC2ProcessCreateImage(t *testing.T) { Name: aws.String("test-image"), }, wantValidationErr: true, - wantErrCode: "ValidationError", + wantErrCode: "MissingParameter", }, { name: "ValidCreateImage", @@ -117,7 +118,8 @@ func TestEC2ProcessCreateImage(t *testing.T) { assert.NoError(t, err) } - jsonResp := EC2_Process_CreateImage(jsonData) + handler := handlers_ec2_image.NewCreateImageHandler(handlers_ec2_image.NewMockImageService()) + jsonResp := handler.Process(jsonData) responseError, err := utils.ValidateErrorPayload(jsonResp) if tt.wantValidationErr { diff --git a/hive/gateway/ec2/image/DeregisterImage.go b/hive/gateway/ec2/image/DeregisterImage.go index cc77637f5..c6d1cc873 100644 --- a/hive/gateway/ec2/image/DeregisterImage.go +++ b/hive/gateway/ec2/image/DeregisterImage.go @@ -1,7 +1,6 @@ package gateway_ec2_image import ( - "bytes" "encoding/json" "errors" "fmt" @@ -9,7 +8,7 @@ import ( "strings" "github.com/aws/aws-sdk-go/service/ec2" - "github.com/mulgadc/hive/hive/awserrors" + "github.com/mulgadc/hive/hive/handlers/ec2/image" "github.com/mulgadc/hive/hive/utils" ) @@ -74,8 +73,9 @@ func DeregisterImage(input *ec2.DeregisterImageInput) (output ec2.DeregisterImag return output, fmt.Errorf("failed to marshal input to JSON: %w", err) } - // Run the simulated JSON request via NATS, which will return a JSON response - jsonResp := EC2_Process_DeregisterImage(jsonData) + // Run the simulated JSON request via handler, which will return a JSON response + handler := handlers_ec2_image.NewDeregisterImageHandler(handlers_ec2_image.NewMockImageService()) + jsonResp := handler.Process(jsonData) // Validate if the response is successful or an error responseError, err := utils.ValidateErrorPayload(jsonResp) @@ -96,39 +96,3 @@ func DeregisterImage(input *ec2.DeregisterImageInput) (output ec2.DeregisterImag return output, nil } - -func EC2_Process_DeregisterImage(jsonData []byte) (output []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) - } - - // Ensure the payload provided the fields that EC2 expects before proceeding. - err = ValidateDeregisterImageInput(&input) - - if err != nil { - return utils.GenerateErrorPayload(awserrors.ErrorValidationError) - } - - // TODO: Add the logic to actually deregister the image. - // This is a placeholder response for testing the framework. - - // DeregisterImageOutput has no exported fields, so we return a simple success indicator - // to distinguish from error responses - result := map[string]bool{"Return": true} - - // Return as JSON, to simulate the NATS response - jsonResponse, err := json.Marshal(result) - if err != nil { - slog.Error("EC2_Process_DeregisterImage could not marshal output", "err", err) - return nil - } - - return jsonResponse -} diff --git a/hive/gateway/ec2/image/DescribeImageAttribute.go b/hive/gateway/ec2/image/DescribeImageAttribute.go index bcf2102e6..5819e41f4 100644 --- a/hive/gateway/ec2/image/DescribeImageAttribute.go +++ b/hive/gateway/ec2/image/DescribeImageAttribute.go @@ -1,16 +1,14 @@ package gateway_ec2_image import ( - "bytes" "encoding/json" "errors" "fmt" "log/slog" "strings" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" - "github.com/mulgadc/hive/hive/awserrors" + "github.com/mulgadc/hive/hive/handlers/ec2/image" "github.com/mulgadc/hive/hive/utils" ) @@ -119,8 +117,9 @@ func DescribeImageAttribute(input *ec2.DescribeImageAttributeInput) (output ec2. return output, fmt.Errorf("failed to marshal input to JSON: %w", err) } - // Run the simulated JSON request via NATS, which will return a JSON response - jsonResp := EC2_Process_DescribeImageAttribute(jsonData) + // Run the simulated JSON request via handler, which will return a JSON response + handler := handlers_ec2_image.NewDescribeImageAttributeHandler(handlers_ec2_image.NewMockImageService()) + jsonResp := handler.Process(jsonData) // Validate if the response is successful or an error responseError, err := utils.ValidateErrorPayload(jsonResp) @@ -141,42 +140,3 @@ func DescribeImageAttribute(input *ec2.DescribeImageAttributeInput) (output ec2. return output, nil } - -func EC2_Process_DescribeImageAttribute(jsonData []byte) (output []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) - } - - // Ensure the payload provided the fields that EC2 expects before proceeding. - err = ValidateDescribeImageAttributeInput(&input) - - if err != nil { - return utils.GenerateErrorPayload(awserrors.ErrorValidationError) - } - - // TODO: Add the logic to actually describe image attributes. - // This is a placeholder response for testing the framework. - - result := &ec2.DescribeImageAttributeOutput{ - ImageId: input.ImageId, - Description: &ec2.AttributeValue{ - Value: aws.String("Test image description"), - }, - } - - // Return as JSON, to simulate the NATS response - jsonResponse, err := json.Marshal(result) - if err != nil { - slog.Error("EC2_Process_DescribeImageAttribute could not marshal output", "err", err) - return nil - } - - return jsonResponse -} diff --git a/hive/gateway/ec2/image/DescribeImages.go b/hive/gateway/ec2/image/DescribeImages.go index cf9fe57da..8d544a3f6 100644 --- a/hive/gateway/ec2/image/DescribeImages.go +++ b/hive/gateway/ec2/image/DescribeImages.go @@ -1,16 +1,14 @@ package gateway_ec2_image import ( - "bytes" "encoding/json" "errors" "fmt" "log/slog" "strings" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" - "github.com/mulgadc/hive/hive/awserrors" + "github.com/mulgadc/hive/hive/handlers/ec2/image" "github.com/mulgadc/hive/hive/utils" ) @@ -108,8 +106,9 @@ func DescribeImages(input *ec2.DescribeImagesInput) (output ec2.DescribeImagesOu return output, fmt.Errorf("failed to marshal input to JSON: %w", err) } - // Run the simulated JSON request via NATS, which will return a JSON response - jsonResp := EC2_Process_DescribeImages(jsonData) + // Run the simulated JSON request via handler, which will return a JSON response + handler := handlers_ec2_image.NewDescribeImagesHandler(handlers_ec2_image.NewMockImageService()) + jsonResp := handler.Process(jsonData) // Validate if the response is successful or an error responseError, err := utils.ValidateErrorPayload(jsonResp) @@ -130,49 +129,3 @@ func DescribeImages(input *ec2.DescribeImagesInput) (output ec2.DescribeImagesOu return output, nil } - -func EC2_Process_DescribeImages(jsonData []byte) (output []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) - } - - // Ensure the payload provided the fields that EC2 expects before proceeding. - err = ValidateDescribeImagesInput(&input) - - if err != nil { - return utils.GenerateErrorPayload(awserrors.ErrorValidationError) - } - - // TODO: Add the logic to actually query images from the system. - // This is a placeholder response for testing the framework. - - result := &ec2.DescribeImagesOutput{ - Images: []*ec2.Image{ - { - ImageId: aws.String("ami-0123456789abcdef0"), - Name: aws.String("test-image"), - State: aws.String("available"), - Architecture: aws.String("x86_64"), - ImageType: aws.String("machine"), - OwnerId: aws.String("123456789012"), - Public: aws.Bool(false), - }, - }, - } - - // Return as JSON, to simulate the NATS response - jsonResponse, err := json.Marshal(result) - if err != nil { - slog.Error("EC2_Process_DescribeImages could not marshal output", "err", err) - return nil - } - - return jsonResponse -} diff --git a/hive/gateway/ec2/image/ModifyImageAttribute.go b/hive/gateway/ec2/image/ModifyImageAttribute.go index ee9b7d936..9508a7456 100644 --- a/hive/gateway/ec2/image/ModifyImageAttribute.go +++ b/hive/gateway/ec2/image/ModifyImageAttribute.go @@ -1,7 +1,6 @@ package gateway_ec2_image import ( - "bytes" "encoding/json" "errors" "fmt" @@ -9,7 +8,7 @@ import ( "strings" "github.com/aws/aws-sdk-go/service/ec2" - "github.com/mulgadc/hive/hive/awserrors" + "github.com/mulgadc/hive/hive/handlers/ec2/image" "github.com/mulgadc/hive/hive/utils" ) @@ -100,8 +99,9 @@ func ModifyImageAttribute(input *ec2.ModifyImageAttributeInput) (output ec2.Modi return output, fmt.Errorf("failed to marshal input to JSON: %w", err) } - // Run the simulated JSON request via NATS, which will return a JSON response - jsonResp := EC2_Process_ModifyImageAttribute(jsonData) + // Run the simulated JSON request via handler, which will return a JSON response + handler := handlers_ec2_image.NewModifyImageAttributeHandler(handlers_ec2_image.NewMockImageService()) + jsonResp := handler.Process(jsonData) // Validate if the response is successful or an error responseError, err := utils.ValidateErrorPayload(jsonResp) @@ -122,39 +122,3 @@ func ModifyImageAttribute(input *ec2.ModifyImageAttributeInput) (output ec2.Modi return output, nil } - -func EC2_Process_ModifyImageAttribute(jsonData []byte) (output []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) - } - - // Ensure the payload provided the fields that EC2 expects before proceeding. - err = ValidateModifyImageAttributeInput(&input) - - if err != nil { - return utils.GenerateErrorPayload(awserrors.ErrorValidationError) - } - - // TODO: Add the logic to actually modify image attributes. - // This is a placeholder response for testing the framework. - - // ModifyImageAttributeOutput has no exported fields, so we return a simple success indicator - // to distinguish from error responses - result := map[string]bool{"Return": true} - - // Return as JSON, to simulate the NATS response - jsonResponse, err := json.Marshal(result) - if err != nil { - slog.Error("EC2_Process_ModifyImageAttribute could not marshal output", "err", err) - return nil - } - - return jsonResponse -} diff --git a/hive/gateway/ec2/image/RegisterImage.go b/hive/gateway/ec2/image/RegisterImage.go index d1fd25340..58324623d 100644 --- a/hive/gateway/ec2/image/RegisterImage.go +++ b/hive/gateway/ec2/image/RegisterImage.go @@ -1,15 +1,13 @@ package gateway_ec2_image import ( - "bytes" "encoding/json" "errors" "fmt" "log/slog" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" - "github.com/mulgadc/hive/hive/awserrors" + "github.com/mulgadc/hive/hive/handlers/ec2/image" "github.com/mulgadc/hive/hive/utils" ) @@ -139,8 +137,9 @@ func RegisterImage(input *ec2.RegisterImageInput) (output ec2.RegisterImageOutpu return output, fmt.Errorf("failed to marshal input to JSON: %w", err) } - // Run the simulated JSON request via NATS, which will return a JSON response - jsonResp := EC2_Process_RegisterImage(jsonData) + // Run the simulated JSON request via handler, which will return a JSON response + handler := handlers_ec2_image.NewRegisterImageHandler(handlers_ec2_image.NewMockImageService()) + jsonResp := handler.Process(jsonData) // Validate if the response is successful or an error responseError, err := utils.ValidateErrorPayload(jsonResp) @@ -161,39 +160,3 @@ func RegisterImage(input *ec2.RegisterImageInput) (output ec2.RegisterImageOutpu return output, nil } - -func EC2_Process_RegisterImage(jsonData []byte) (output []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) - } - - // Ensure the payload provided the fields that EC2 expects before proceeding. - err = ValidateRegisterImageInput(&input) - - if err != nil { - return utils.GenerateErrorPayload(awserrors.ErrorValidationError) - } - - // TODO: Add the logic to actually register the image. - // This is a placeholder response for testing the framework. - - result := &ec2.RegisterImageOutput{ - ImageId: aws.String("ami-0123456789abcdef0"), - } - - // Return as JSON, to simulate the NATS response - jsonResponse, err := json.Marshal(result) - if err != nil { - slog.Error("EC2_Process_RegisterImage could not marshal output", "err", err) - return nil - } - - return jsonResponse -} diff --git a/hive/gateway/ec2/image/ResetImageAttribute.go b/hive/gateway/ec2/image/ResetImageAttribute.go index 3a4ab2f9f..fcd90e3f9 100644 --- a/hive/gateway/ec2/image/ResetImageAttribute.go +++ b/hive/gateway/ec2/image/ResetImageAttribute.go @@ -1,7 +1,6 @@ package gateway_ec2_image import ( - "bytes" "encoding/json" "errors" "fmt" @@ -9,7 +8,7 @@ import ( "strings" "github.com/aws/aws-sdk-go/service/ec2" - "github.com/mulgadc/hive/hive/awserrors" + "github.com/mulgadc/hive/hive/handlers/ec2/image" "github.com/mulgadc/hive/hive/utils" ) @@ -85,8 +84,9 @@ func ResetImageAttribute(input *ec2.ResetImageAttributeInput) (output ec2.ResetI return output, fmt.Errorf("failed to marshal input to JSON: %w", err) } - // Run the simulated JSON request via NATS, which will return a JSON response - jsonResp := EC2_Process_ResetImageAttribute(jsonData) + // Run the simulated JSON request via handler, which will return a JSON response + handler := handlers_ec2_image.NewResetImageAttributeHandler(handlers_ec2_image.NewMockImageService()) + jsonResp := handler.Process(jsonData) // Validate if the response is successful or an error responseError, err := utils.ValidateErrorPayload(jsonResp) @@ -107,39 +107,3 @@ func ResetImageAttribute(input *ec2.ResetImageAttributeInput) (output ec2.ResetI return output, nil } - -func EC2_Process_ResetImageAttribute(jsonData []byte) (output []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) - } - - // Ensure the payload provided the fields that EC2 expects before proceeding. - err = ValidateResetImageAttributeInput(&input) - - if err != nil { - return utils.GenerateErrorPayload(awserrors.ErrorValidationError) - } - - // TODO: Add the logic to actually reset image attributes. - // This is a placeholder response for testing the framework. - - // ResetImageAttributeOutput has no exported fields, so we return a simple success indicator - // to distinguish from error responses - result := map[string]bool{"Return": true} - - // Return as JSON, to simulate the NATS response - jsonResponse, err := json.Marshal(result) - if err != nil { - slog.Error("EC2_Process_ResetImageAttribute could not marshal output", "err", err) - return nil - } - - return jsonResponse -} diff --git a/hive/gateway/ec2/instance/RunInstances.go b/hive/gateway/ec2/instance/RunInstances.go index 1076357e2..5d4426b62 100644 --- a/hive/gateway/ec2/instance/RunInstances.go +++ b/hive/gateway/ec2/instance/RunInstances.go @@ -1,16 +1,14 @@ package gateway_ec2_instance import ( - "bytes" "encoding/json" "errors" "fmt" "log/slog" "strings" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" - "github.com/mulgadc/hive/hive/awserrors" + "github.com/mulgadc/hive/hive/handlers/ec2/instance" "github.com/mulgadc/hive/hive/utils" ) @@ -193,8 +191,9 @@ func RunInstances(input *ec2.RunInstancesInput) (reservation ec2.Reservation, er return reservation, fmt.Errorf("failed to marshal input to JSON: %w", err) } - // Run the simulated JSON request via NATS, which will return a JSON response - jsonResp := EC2_Process_RunInstances(jsonData) + // Run the simulated JSON request via handler, which will return a JSON response + handler := handlers_ec2_instance.NewRunInstancesHandler(handlers_ec2_instance.NewMockInstanceService()) + jsonResp := handler.Process(jsonData) // Validate if the response is successful or an error @@ -214,54 +213,3 @@ func RunInstances(input *ec2.RunInstancesInput) (reservation ec2.Reservation, er return reservation, nil } - -func EC2_Process_RunInstances(jsonData []byte) (output []byte) { - - var input ec2.RunInstancesInput - - decoder := json.NewDecoder(bytes.NewReader(jsonData)) - decoder.DisallowUnknownFields() - - err := decoder.Decode(&input) - if err != nil { - // TODO: Move error codes with vars to errors.go - return utils.GenerateErrorPayload(awserrors.ErrorValidationError) - } - - // Ensure the payload provided the fields that EC2 expects before proceeding. - err = ValidateRunInstancesInput(&input) - - if err != nil { - return utils.GenerateErrorPayload(awserrors.ErrorValidationError) - } - - // Here you would add the logic to actually create the instance in your system. - // For this example, we'll just create a dummy response. - - instance := &ec2.Instance{ - InstanceId: aws.String("i-0123456789abcdef0"), - State: &ec2.InstanceState{ - Code: aws.Int64(16), - Name: aws.String("running"), - }, - ImageId: input.ImageId, - InstanceType: input.InstanceType, - KeyName: input.KeyName, - SubnetId: input.SubnetId, - } - - reservation := &ec2.Reservation{ - Instances: []*ec2.Instance{instance}, - OwnerId: aws.String("123456789012"), - } - - // Return as JSON, to simulate the NATS response - jsonResponse, err := json.Marshal(reservation) - if err != nil { - slog.Error("EC2_Process_RunInstances could not marshal reservation", "err", err) - return nil - } - - return jsonResponse - -} diff --git a/hive/gateway/ec2/instance/RunInstances_test.go b/hive/gateway/ec2/instance/RunInstances_test.go index 68b66f103..d4d99f81f 100644 --- a/hive/gateway/ec2/instance/RunInstances_test.go +++ b/hive/gateway/ec2/instance/RunInstances_test.go @@ -8,6 +8,7 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" + "github.com/mulgadc/hive/hive/handlers/ec2/instance" "github.com/mulgadc/hive/hive/utils" "github.com/stretchr/testify/assert" ) @@ -216,7 +217,7 @@ func TestEC2ProcessRunInstances(t *testing.T) { MaxCount: aws.Int64(1), }, wantValidationErr: true, - wantErrCode: "ValidationError", + wantErrCode: "MissingParameter", }, { name: "UnknownFieldInPayload", @@ -263,7 +264,8 @@ func TestEC2ProcessRunInstances(t *testing.T) { assert.NoError(t, err) } - jsonResp := EC2_Process_RunInstances(jsonData) + handler := handlers_ec2_instance.NewRunInstancesHandler(handlers_ec2_instance.NewMockInstanceService()) + jsonResp := handler.Process(jsonData) responseError, err := utils.ValidateErrorPayload(jsonResp) diff --git a/hive/gateway/ec2/key/CreateKeyPair.go b/hive/gateway/ec2/key/CreateKeyPair.go index cdc0a2571..54e02b5bf 100644 --- a/hive/gateway/ec2/key/CreateKeyPair.go +++ b/hive/gateway/ec2/key/CreateKeyPair.go @@ -1,15 +1,13 @@ package gateway_ec2_key import ( - "bytes" "encoding/json" "errors" "fmt" "log/slog" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" - "github.com/mulgadc/hive/hive/awserrors" + "github.com/mulgadc/hive/hive/handlers/ec2/key" "github.com/mulgadc/hive/hive/utils" ) @@ -76,8 +74,9 @@ func CreateKeyPair(input *ec2.CreateKeyPairInput) (output ec2.CreateKeyPairOutpu return output, fmt.Errorf("failed to marshal input to JSON: %w", err) } - // Run the simulated JSON request via NATS, which will return a JSON response - jsonResp := EC2_Process_CreateKeyPair(jsonData) + // Run the simulated JSON request via handler, which will return a JSON response + handler := handlers_ec2_key.NewCreateKeyPairHandler(handlers_ec2_key.NewMockKeyService()) + jsonResp := handler.Process(jsonData) // Validate if the response is successful or an error responseError, err := utils.ValidateErrorPayload(jsonResp) @@ -98,42 +97,3 @@ func CreateKeyPair(input *ec2.CreateKeyPairInput) (output ec2.CreateKeyPairOutpu return output, nil } - -func EC2_Process_CreateKeyPair(jsonData []byte) (output []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) - } - - // Ensure the payload provided the fields that EC2 expects before proceeding. - err = ValidateCreateKeyPairInput(&input) - - if err != nil { - return utils.GenerateErrorPayload(awserrors.ErrorValidationError) - } - - // TODO: Add the logic to actually create the key pair. - // This is a placeholder response for testing the framework. - - result := &ec2.CreateKeyPairOutput{ - KeyFingerprint: aws.String("1f:51:ae:28:bf:89:e9:d8:1f:25:5d:37:2d:7d:b8:ca:9f:f5:f1:6f"), - KeyMaterial: aws.String("-----BEGIN RSA PRIVATE KEY-----\nMIIEpQIBAAKCAQEA...\n-----END RSA PRIVATE KEY-----"), - KeyName: input.KeyName, - KeyPairId: aws.String("key-0123456789abcdef0"), - } - - // Return as JSON, to simulate the NATS response - jsonResponse, err := json.Marshal(result) - if err != nil { - slog.Error("EC2_Process_CreateKeyPair could not marshal output", "err", err) - return nil - } - - return jsonResponse -} diff --git a/hive/gateway/ec2/key/CreateKeyPair_test.go b/hive/gateway/ec2/key/CreateKeyPair_test.go index 49dca904b..5fccf7e6c 100644 --- a/hive/gateway/ec2/key/CreateKeyPair_test.go +++ b/hive/gateway/ec2/key/CreateKeyPair_test.go @@ -7,6 +7,7 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" + "github.com/mulgadc/hive/hive/handlers/ec2/key" "github.com/mulgadc/hive/hive/utils" "github.com/stretchr/testify/assert" ) @@ -83,7 +84,7 @@ func TestEC2ProcessCreateKeyPair(t *testing.T) { KeyName: aws.String(""), }, wantValidationErr: true, - wantErrCode: "ValidationError", + wantErrCode: "MissingParameter", }, { name: "ValidCreateKeyPair", @@ -112,7 +113,8 @@ func TestEC2ProcessCreateKeyPair(t *testing.T) { assert.NoError(t, err) } - jsonResp := EC2_Process_CreateKeyPair(jsonData) + handler := handlers_ec2_key.NewCreateKeyPairHandler(handlers_ec2_key.NewMockKeyService()) + jsonResp := handler.Process(jsonData) responseError, err := utils.ValidateErrorPayload(jsonResp) if tt.wantValidationErr { diff --git a/hive/gateway/ec2/key/DeleteKeyPair.go b/hive/gateway/ec2/key/DeleteKeyPair.go index 268fe67fc..8b4181980 100644 --- a/hive/gateway/ec2/key/DeleteKeyPair.go +++ b/hive/gateway/ec2/key/DeleteKeyPair.go @@ -1,15 +1,13 @@ package gateway_ec2_key import ( - "bytes" "encoding/json" "errors" "fmt" "log/slog" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" - "github.com/mulgadc/hive/hive/awserrors" + "github.com/mulgadc/hive/hive/handlers/ec2/key" "github.com/mulgadc/hive/hive/utils" ) @@ -85,8 +83,9 @@ func DeleteKeyPair(input *ec2.DeleteKeyPairInput) (output ec2.DeleteKeyPairOutpu return output, fmt.Errorf("failed to marshal input to JSON: %w", err) } - // Run the simulated JSON request via NATS, which will return a JSON response - jsonResp := EC2_Process_DeleteKeyPair(jsonData) + // Run the simulated JSON request via handler, which will return a JSON response + handler := handlers_ec2_key.NewDeleteKeyPairHandler(handlers_ec2_key.NewMockKeyService()) + jsonResp := handler.Process(jsonData) // Validate if the response is successful or an error responseError, err := utils.ValidateErrorPayload(jsonResp) @@ -107,40 +106,3 @@ func DeleteKeyPair(input *ec2.DeleteKeyPairInput) (output ec2.DeleteKeyPairOutpu return output, nil } - -func EC2_Process_DeleteKeyPair(jsonData []byte) (output []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) - } - - // Ensure the payload provided the fields that EC2 expects before proceeding. - err = ValidateDeleteKeyPairInput(&input) - - if err != nil { - return utils.GenerateErrorPayload(awserrors.ErrorValidationError) - } - - // TODO: Add the logic to actually delete the key pair. - // This is a placeholder response for testing the framework. - - result := &ec2.DeleteKeyPairOutput{ - Return: aws.Bool(true), - KeyPairId: input.KeyPairId, - } - - // Return as JSON, to simulate the NATS response - jsonResponse, err := json.Marshal(result) - if err != nil { - slog.Error("EC2_Process_DeleteKeyPair could not marshal output", "err", err) - return nil - } - - return jsonResponse -} diff --git a/hive/gateway/ec2/key/DescribeKeyPairs.go b/hive/gateway/ec2/key/DescribeKeyPairs.go index d023c5c2d..0a2898272 100644 --- a/hive/gateway/ec2/key/DescribeKeyPairs.go +++ b/hive/gateway/ec2/key/DescribeKeyPairs.go @@ -1,15 +1,13 @@ package gateway_ec2_key import ( - "bytes" "encoding/json" "errors" "fmt" "log/slog" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" - "github.com/mulgadc/hive/hive/awserrors" + "github.com/mulgadc/hive/hive/handlers/ec2/key" "github.com/mulgadc/hive/hive/utils" ) @@ -77,8 +75,9 @@ func DescribeKeyPairs(input *ec2.DescribeKeyPairsInput) (output ec2.DescribeKeyP return output, fmt.Errorf("failed to marshal input to JSON: %w", err) } - // Run the simulated JSON request via NATS, which will return a JSON response - jsonResp := EC2_Process_DescribeKeyPairs(jsonData) + // Run the simulated JSON request via handler, which will return a JSON response + handler := handlers_ec2_key.NewDescribeKeyPairsHandler(handlers_ec2_key.NewMockKeyService()) + jsonResp := handler.Process(jsonData) // Validate if the response is successful or an error responseError, err := utils.ValidateErrorPayload(jsonResp) @@ -99,46 +98,3 @@ func DescribeKeyPairs(input *ec2.DescribeKeyPairsInput) (output ec2.DescribeKeyP return output, nil } - -func EC2_Process_DescribeKeyPairs(jsonData []byte) (output []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) - } - - // Ensure the payload provided the fields that EC2 expects before proceeding. - err = ValidateDescribeKeyPairsInput(&input) - - if err != nil { - return utils.GenerateErrorPayload(awserrors.ErrorValidationError) - } - - // TODO: Add the logic to actually query key pairs from the system. - // This is a placeholder response for testing the framework. - - result := &ec2.DescribeKeyPairsOutput{ - KeyPairs: []*ec2.KeyPairInfo{ - { - KeyPairId: aws.String("key-0123456789abcdef0"), - KeyFingerprint: aws.String("1f:51:ae:28:bf:89:e9:d8:1f:25:5d:37:2d:7d:b8:ca:9f:f5:f1:6f"), - KeyName: aws.String("test-key"), - KeyType: aws.String("rsa"), - }, - }, - } - - // Return as JSON, to simulate the NATS response - jsonResponse, err := json.Marshal(result) - if err != nil { - slog.Error("EC2_Process_DescribeKeyPairs could not marshal output", "err", err) - return nil - } - - return jsonResponse -} diff --git a/hive/gateway/ec2/key/ImportKeyPair.go b/hive/gateway/ec2/key/ImportKeyPair.go index d5750b1b3..c58bcd371 100644 --- a/hive/gateway/ec2/key/ImportKeyPair.go +++ b/hive/gateway/ec2/key/ImportKeyPair.go @@ -1,15 +1,13 @@ package gateway_ec2_key import ( - "bytes" "encoding/json" "errors" "fmt" "log/slog" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" - "github.com/mulgadc/hive/hive/awserrors" + "github.com/mulgadc/hive/hive/handlers/ec2/key" "github.com/mulgadc/hive/hive/utils" ) @@ -91,8 +89,9 @@ func ImportKeyPair(input *ec2.ImportKeyPairInput) (output ec2.ImportKeyPairOutpu return output, fmt.Errorf("failed to marshal input to JSON: %w", err) } - // Run the simulated JSON request via NATS, which will return a JSON response - jsonResp := EC2_Process_ImportKeyPair(jsonData) + // Run the simulated JSON request via handler, which will return a JSON response + handler := handlers_ec2_key.NewImportKeyPairHandler(handlers_ec2_key.NewMockKeyService()) + jsonResp := handler.Process(jsonData) // Validate if the response is successful or an error responseError, err := utils.ValidateErrorPayload(jsonResp) @@ -113,41 +112,3 @@ func ImportKeyPair(input *ec2.ImportKeyPairInput) (output ec2.ImportKeyPairOutpu return output, nil } - -func EC2_Process_ImportKeyPair(jsonData []byte) (output []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) - } - - // Ensure the payload provided the fields that EC2 expects before proceeding. - err = ValidateImportKeyPairInput(&input) - - if err != nil { - return utils.GenerateErrorPayload(awserrors.ErrorValidationError) - } - - // TODO: Add the logic to actually import the key pair. - // This is a placeholder response for testing the framework. - - result := &ec2.ImportKeyPairOutput{ - KeyFingerprint: aws.String("1f:51:ae:28:bf:89:e9:d8:1f:25:5d:37:2d:7d:b8:ca:9f:f5:f1:6f"), - KeyName: input.KeyName, - KeyPairId: aws.String("key-0987654321fedcba0"), - } - - // Return as JSON, to simulate the NATS response - jsonResponse, err := json.Marshal(result) - if err != nil { - slog.Error("EC2_Process_ImportKeyPair could not marshal output", "err", err) - return nil - } - - return jsonResponse -} diff --git a/hive/utils/utils.go b/hive/utils/utils.go index 157887a1a..c5bf858d5 100644 --- a/hive/utils/utils.go +++ b/hive/utils/utils.go @@ -264,11 +264,13 @@ func ValidateErrorPayload(payload []byte) (responseError ec2.ResponseError, err err = decoder.Decode(&responseError) - if err == nil { - // TODO: Move error codes with vars to errors.go + if err == nil && responseError.Code != nil { + // Successfully decoded as ResponseError AND has a non-nil Code field + // This is a real error response return responseError, errors.New("ResponseError detected") } + // Either failed to decode (not an error structure) or Code is nil (empty valid response) return responseError, nil }