mirror of
https://github.com/mulgadc/spinifex.git
synced 2026-10-15 17:11:30 +00:00
fix: delete dead code (unused handlers)
This commit is contained in:
@@ -1,77 +0,0 @@
|
||||
package gateway_ec2_image
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strings"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
handlers_ec2_image "github.com/mulgadc/hive/hive/handlers/ec2/image"
|
||||
"github.com/mulgadc/hive/hive/utils"
|
||||
)
|
||||
|
||||
func ValidateCopyImageInput(input *ec2.CopyImageInput) (err error) {
|
||||
if input == nil {
|
||||
return errors.New("MissingParameter")
|
||||
}
|
||||
|
||||
if input.Name == nil || *input.Name == "" {
|
||||
return errors.New("MissingParameter")
|
||||
}
|
||||
|
||||
if input.SourceImageId == nil || *input.SourceImageId == "" {
|
||||
return errors.New("MissingParameter")
|
||||
}
|
||||
|
||||
if input.SourceRegion == nil || *input.SourceRegion == "" {
|
||||
return errors.New("MissingParameter")
|
||||
}
|
||||
|
||||
// Validate SourceImageId format
|
||||
if !strings.HasPrefix(*input.SourceImageId, "ami-") {
|
||||
return errors.New("InvalidAMIID.Malformed")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func CopyImage(input *ec2.CopyImageInput) (output ec2.CopyImageOutput, err error) {
|
||||
|
||||
// Validate input
|
||||
err = ValidateCopyImageInput(input)
|
||||
|
||||
if err != nil {
|
||||
return output, err
|
||||
}
|
||||
|
||||
// Marshal to JSON, to send over the wire for processing (NATS)
|
||||
jsonData, err := json.Marshal(input)
|
||||
if err != nil {
|
||||
return output, fmt.Errorf("failed to marshal input to JSON: %w", err)
|
||||
}
|
||||
|
||||
// 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)
|
||||
|
||||
if err != nil {
|
||||
if responseError.Code != nil {
|
||||
slog.Error("CopyImage failed", "err", responseError.Code)
|
||||
return output, errors.New(*responseError.Code)
|
||||
}
|
||||
return output, err
|
||||
}
|
||||
|
||||
// Unmarshal the JSON response back into output struct
|
||||
err = json.Unmarshal(jsonResp, &output)
|
||||
if err != nil {
|
||||
return output, fmt.Errorf("failed to unmarshal JSON response to CopyImageOutput: %w", err)
|
||||
}
|
||||
|
||||
return output, nil
|
||||
}
|
||||
@@ -1,78 +0,0 @@
|
||||
package gateway_ec2_image
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestValidateCopyImageInput(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input *ec2.CopyImageInput
|
||||
want error
|
||||
}{
|
||||
{
|
||||
name: "NilInput",
|
||||
input: nil,
|
||||
want: errors.New("MissingParameter"),
|
||||
},
|
||||
{
|
||||
name: "MissingName",
|
||||
input: &ec2.CopyImageInput{
|
||||
SourceImageId: aws.String("ami-0123456789abcdef0"),
|
||||
SourceRegion: aws.String("us-east-1"),
|
||||
},
|
||||
want: errors.New("MissingParameter"),
|
||||
},
|
||||
{
|
||||
name: "MissingSourceImageId",
|
||||
input: &ec2.CopyImageInput{
|
||||
Name: aws.String("test-copy"),
|
||||
SourceRegion: aws.String("us-east-1"),
|
||||
},
|
||||
want: errors.New("MissingParameter"),
|
||||
},
|
||||
{
|
||||
name: "MissingSourceRegion",
|
||||
input: &ec2.CopyImageInput{
|
||||
Name: aws.String("test-copy"),
|
||||
SourceImageId: aws.String("ami-0123456789abcdef0"),
|
||||
},
|
||||
want: errors.New("MissingParameter"),
|
||||
},
|
||||
{
|
||||
name: "InvalidSourceImageId",
|
||||
input: &ec2.CopyImageInput{
|
||||
Name: aws.String("test-copy"),
|
||||
SourceImageId: aws.String("invalid-id"),
|
||||
SourceRegion: aws.String("us-east-1"),
|
||||
},
|
||||
want: errors.New("InvalidAMIID.Malformed"),
|
||||
},
|
||||
{
|
||||
name: "ValidInput",
|
||||
input: &ec2.CopyImageInput{
|
||||
Name: aws.String("test-copy"),
|
||||
SourceImageId: aws.String("ami-0123456789abcdef0"),
|
||||
SourceRegion: aws.String("us-east-1"),
|
||||
},
|
||||
want: nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result, err := CopyImage(tt.input)
|
||||
assert.Equal(t, tt.want, err)
|
||||
if err == nil {
|
||||
assert.NotEmpty(t, result.ImageId)
|
||||
assert.True(t, strings.HasPrefix(*result.ImageId, "ami-"))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
package gateway_ec2_image
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strings"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
handlers_ec2_image "github.com/mulgadc/hive/hive/handlers/ec2/image"
|
||||
"github.com/mulgadc/hive/hive/utils"
|
||||
)
|
||||
|
||||
func ValidateCreateImageInput(input *ec2.CreateImageInput) (err error) {
|
||||
if input == nil {
|
||||
return errors.New("MissingParameter")
|
||||
}
|
||||
|
||||
if input.InstanceId == nil || *input.InstanceId == "" {
|
||||
return errors.New("MissingParameter")
|
||||
}
|
||||
|
||||
if input.Name == nil || *input.Name == "" {
|
||||
return errors.New("MissingParameter")
|
||||
}
|
||||
|
||||
// Validate InstanceId format
|
||||
if !strings.HasPrefix(*input.InstanceId, "i-") {
|
||||
return errors.New("InvalidInstanceID.Malformed")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func CreateImage(input *ec2.CreateImageInput) (output ec2.CreateImageOutput, err error) {
|
||||
|
||||
// Validate input
|
||||
err = ValidateCreateImageInput(input)
|
||||
|
||||
if err != nil {
|
||||
return output, err
|
||||
}
|
||||
|
||||
// Marshal to JSON, to send over the wire for processing (NATS)
|
||||
jsonData, err := json.Marshal(input)
|
||||
if err != nil {
|
||||
return output, fmt.Errorf("failed to marshal input to JSON: %w", err)
|
||||
}
|
||||
|
||||
// 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)
|
||||
|
||||
if err != nil {
|
||||
if responseError.Code != nil {
|
||||
slog.Error("CreateImage failed", "err", responseError.Code)
|
||||
return output, errors.New(*responseError.Code)
|
||||
}
|
||||
return output, err
|
||||
}
|
||||
|
||||
// Unmarshal the JSON response back into output struct
|
||||
err = json.Unmarshal(jsonResp, &output)
|
||||
if err != nil {
|
||||
return output, fmt.Errorf("failed to unmarshal JSON response to CreateImageOutput: %w", err)
|
||||
}
|
||||
|
||||
return output, nil
|
||||
}
|
||||
@@ -1,133 +0,0 @@
|
||||
package gateway_ec2_image
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
func TestValidateCreateImageInput(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input *ec2.CreateImageInput
|
||||
want error
|
||||
}{
|
||||
{
|
||||
name: "NilInput",
|
||||
input: nil,
|
||||
want: errors.New("MissingParameter"),
|
||||
},
|
||||
{
|
||||
name: "MissingInstanceId",
|
||||
input: &ec2.CreateImageInput{
|
||||
Name: aws.String("test-image"),
|
||||
},
|
||||
want: errors.New("MissingParameter"),
|
||||
},
|
||||
{
|
||||
name: "MissingName",
|
||||
input: &ec2.CreateImageInput{
|
||||
InstanceId: aws.String("i-0123456789abcdef0"),
|
||||
},
|
||||
want: errors.New("MissingParameter"),
|
||||
},
|
||||
{
|
||||
name: "InvalidInstanceId",
|
||||
input: &ec2.CreateImageInput{
|
||||
InstanceId: aws.String("invalid-id"),
|
||||
Name: aws.String("test-image"),
|
||||
},
|
||||
want: errors.New("InvalidInstanceID.Malformed"),
|
||||
},
|
||||
{
|
||||
name: "ValidInput",
|
||||
input: &ec2.CreateImageInput{
|
||||
InstanceId: aws.String("i-0123456789abcdef0"),
|
||||
Name: aws.String("test-image"),
|
||||
},
|
||||
want: nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result, err := CreateImage(tt.input)
|
||||
assert.Equal(t, tt.want, err)
|
||||
if err != nil {
|
||||
assert.Empty(t, result.ImageId)
|
||||
} else {
|
||||
assert.NotEmpty(t, result.ImageId)
|
||||
assert.True(t, strings.HasPrefix(*result.ImageId, "ami-"))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEC2ProcessCreateImage(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
payload any
|
||||
rawJSON []byte
|
||||
wantValidationErr bool
|
||||
wantErrCode string
|
||||
assertFn func(t *testing.T, output ec2.CreateImageOutput)
|
||||
}{
|
||||
{
|
||||
name: "InvalidPayload",
|
||||
payload: &ec2.DescribeImagesInput{},
|
||||
wantValidationErr: true,
|
||||
wantErrCode: "ValidationError",
|
||||
},
|
||||
{
|
||||
name: "ValidCreateImage",
|
||||
payload: &ec2.CreateImageInput{
|
||||
InstanceId: aws.String("i-0123456789abcdef0"),
|
||||
Name: aws.String("test-image"),
|
||||
},
|
||||
assertFn: func(t *testing.T, output ec2.CreateImageOutput) {
|
||||
assert.NotNil(t, output.ImageId)
|
||||
assert.True(t, strings.HasPrefix(*output.ImageId, "ami-"))
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var jsonData []byte
|
||||
var err error
|
||||
|
||||
if tt.rawJSON != nil {
|
||||
jsonData = tt.rawJSON
|
||||
} else {
|
||||
jsonData, err = json.Marshal(tt.payload)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
handler := handlers_ec2_image.NewCreateImageHandler(handlers_ec2_image.NewMockImageService())
|
||||
jsonResp := handler.Process(jsonData)
|
||||
responseError, err := utils.ValidateErrorPayload(jsonResp)
|
||||
|
||||
if tt.wantValidationErr {
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, tt.wantErrCode, aws.StringValue(responseError.Code))
|
||||
return
|
||||
}
|
||||
|
||||
assert.NoError(t, err)
|
||||
|
||||
var output ec2.CreateImageOutput
|
||||
assert.NoError(t, json.Unmarshal(jsonResp, &output))
|
||||
|
||||
if tt.assertFn != nil {
|
||||
tt.assertFn(t, output)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
package gateway_ec2_image
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strings"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
handlers_ec2_image "github.com/mulgadc/hive/hive/handlers/ec2/image"
|
||||
"github.com/mulgadc/hive/hive/utils"
|
||||
)
|
||||
|
||||
func ValidateDeregisterImageInput(input *ec2.DeregisterImageInput) (err error) {
|
||||
if input == nil {
|
||||
return errors.New("MissingParameter")
|
||||
}
|
||||
|
||||
if input.ImageId == nil || *input.ImageId == "" {
|
||||
return errors.New("MissingParameter")
|
||||
}
|
||||
|
||||
// Validate ImageId format
|
||||
if !strings.HasPrefix(*input.ImageId, "ami-") {
|
||||
return errors.New("InvalidAMIID.Malformed")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func DeregisterImage(input *ec2.DeregisterImageInput) (output ec2.DeregisterImageOutput, err error) {
|
||||
|
||||
// Validate input
|
||||
err = ValidateDeregisterImageInput(input)
|
||||
|
||||
if err != nil {
|
||||
return output, err
|
||||
}
|
||||
|
||||
// Marshal to JSON, to send over the wire for processing (NATS)
|
||||
jsonData, err := json.Marshal(input)
|
||||
if err != nil {
|
||||
return output, fmt.Errorf("failed to marshal input to JSON: %w", err)
|
||||
}
|
||||
|
||||
// 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)
|
||||
|
||||
if err != nil {
|
||||
if responseError.Code != nil {
|
||||
slog.Error("DeregisterImage failed", "err", responseError.Code)
|
||||
return output, errors.New(*responseError.Code)
|
||||
}
|
||||
return output, err
|
||||
}
|
||||
|
||||
// Unmarshal the JSON response back into output struct
|
||||
err = json.Unmarshal(jsonResp, &output)
|
||||
if err != nil {
|
||||
return output, fmt.Errorf("failed to unmarshal JSON response to DeregisterImageOutput: %w", err)
|
||||
}
|
||||
|
||||
return output, nil
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
package gateway_ec2_image
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestValidateDeregisterImageInput(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input *ec2.DeregisterImageInput
|
||||
want error
|
||||
}{
|
||||
{
|
||||
name: "NilInput",
|
||||
input: nil,
|
||||
want: errors.New("MissingParameter"),
|
||||
},
|
||||
{
|
||||
name: "MissingImageId",
|
||||
input: &ec2.DeregisterImageInput{
|
||||
ImageId: aws.String(""),
|
||||
},
|
||||
want: errors.New("MissingParameter"),
|
||||
},
|
||||
{
|
||||
name: "InvalidImageId",
|
||||
input: &ec2.DeregisterImageInput{
|
||||
ImageId: aws.String("invalid-id"),
|
||||
},
|
||||
want: errors.New("InvalidAMIID.Malformed"),
|
||||
},
|
||||
{
|
||||
name: "ValidInput",
|
||||
input: &ec2.DeregisterImageInput{
|
||||
ImageId: aws.String("ami-0123456789abcdef0"),
|
||||
},
|
||||
want: nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, err := DeregisterImage(tt.input)
|
||||
assert.Equal(t, tt.want, err)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
package gateway_ec2_image
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strings"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
handlers_ec2_image "github.com/mulgadc/hive/hive/handlers/ec2/image"
|
||||
"github.com/mulgadc/hive/hive/utils"
|
||||
)
|
||||
|
||||
func ValidateDescribeImageAttributeInput(input *ec2.DescribeImageAttributeInput) (err error) {
|
||||
if input == nil {
|
||||
return errors.New("MissingParameter")
|
||||
}
|
||||
|
||||
if input.Attribute == nil || *input.Attribute == "" {
|
||||
return errors.New("MissingParameter")
|
||||
}
|
||||
|
||||
if input.ImageId == nil || *input.ImageId == "" {
|
||||
return errors.New("MissingParameter")
|
||||
}
|
||||
|
||||
// Validate ImageId format
|
||||
if !strings.HasPrefix(*input.ImageId, "ami-") {
|
||||
return errors.New("InvalidAMIID.Malformed")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func DescribeImageAttribute(input *ec2.DescribeImageAttributeInput) (output ec2.DescribeImageAttributeOutput, err error) {
|
||||
|
||||
// Validate input
|
||||
err = ValidateDescribeImageAttributeInput(input)
|
||||
|
||||
if err != nil {
|
||||
return output, err
|
||||
}
|
||||
|
||||
// Marshal to JSON, to send over the wire for processing (NATS)
|
||||
jsonData, err := json.Marshal(input)
|
||||
if err != nil {
|
||||
return output, fmt.Errorf("failed to marshal input to JSON: %w", err)
|
||||
}
|
||||
|
||||
// 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)
|
||||
|
||||
if err != nil {
|
||||
if responseError.Code != nil {
|
||||
slog.Error("DescribeImageAttribute failed", "err", responseError.Code)
|
||||
return output, errors.New(*responseError.Code)
|
||||
}
|
||||
return output, err
|
||||
}
|
||||
|
||||
// Unmarshal the JSON response back into output struct
|
||||
err = json.Unmarshal(jsonResp, &output)
|
||||
if err != nil {
|
||||
return output, fmt.Errorf("failed to unmarshal JSON response to DescribeImageAttributeOutput: %w", err)
|
||||
}
|
||||
|
||||
return output, nil
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
package gateway_ec2_image
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestValidateDescribeImageAttributeInput(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input *ec2.DescribeImageAttributeInput
|
||||
want error
|
||||
}{
|
||||
{
|
||||
name: "NilInput",
|
||||
input: nil,
|
||||
want: errors.New("MissingParameter"),
|
||||
},
|
||||
{
|
||||
name: "MissingAttribute",
|
||||
input: &ec2.DescribeImageAttributeInput{
|
||||
ImageId: aws.String("ami-0123456789abcdef0"),
|
||||
},
|
||||
want: errors.New("MissingParameter"),
|
||||
},
|
||||
{
|
||||
name: "MissingImageId",
|
||||
input: &ec2.DescribeImageAttributeInput{
|
||||
Attribute: aws.String("description"),
|
||||
},
|
||||
want: errors.New("MissingParameter"),
|
||||
},
|
||||
{
|
||||
name: "InvalidImageId",
|
||||
input: &ec2.DescribeImageAttributeInput{
|
||||
ImageId: aws.String("invalid-id"),
|
||||
Attribute: aws.String("description"),
|
||||
},
|
||||
want: errors.New("InvalidAMIID.Malformed"),
|
||||
},
|
||||
{
|
||||
name: "ValidInput",
|
||||
input: &ec2.DescribeImageAttributeInput{
|
||||
ImageId: aws.String("ami-0123456789abcdef0"),
|
||||
Attribute: aws.String("description"),
|
||||
},
|
||||
want: nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result, err := DescribeImageAttribute(tt.input)
|
||||
assert.Equal(t, tt.want, err)
|
||||
if err == nil {
|
||||
assert.NotEmpty(t, result.ImageId)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
package gateway_ec2_image
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
)
|
||||
|
||||
func ImportImage() {
|
||||
|
||||
var test = ec2.ImportImageInput{}
|
||||
|
||||
fmt.Println(test)
|
||||
|
||||
var t2 = ec2.ImportImageOutput{}
|
||||
|
||||
fmt.Println(t2)
|
||||
|
||||
var t3 = ec2.DescribeImagesInput{}
|
||||
|
||||
fmt.Println(t3)
|
||||
|
||||
var t4 = ec2.DescribeImagesOutput{}
|
||||
|
||||
fmt.Println(t4)
|
||||
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
package gateway_ec2_image
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strings"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
handlers_ec2_image "github.com/mulgadc/hive/hive/handlers/ec2/image"
|
||||
"github.com/mulgadc/hive/hive/utils"
|
||||
)
|
||||
|
||||
func ValidateModifyImageAttributeInput(input *ec2.ModifyImageAttributeInput) (err error) {
|
||||
if input == nil {
|
||||
return errors.New("MissingParameter")
|
||||
}
|
||||
|
||||
if input.ImageId == nil || *input.ImageId == "" {
|
||||
return errors.New("MissingParameter")
|
||||
}
|
||||
|
||||
// Validate ImageId format
|
||||
if !strings.HasPrefix(*input.ImageId, "ami-") {
|
||||
return errors.New("InvalidAMIID.Malformed")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func ModifyImageAttribute(input *ec2.ModifyImageAttributeInput) (output ec2.ModifyImageAttributeOutput, err error) {
|
||||
|
||||
// Validate input
|
||||
err = ValidateModifyImageAttributeInput(input)
|
||||
|
||||
if err != nil {
|
||||
return output, err
|
||||
}
|
||||
|
||||
// Marshal to JSON, to send over the wire for processing (NATS)
|
||||
jsonData, err := json.Marshal(input)
|
||||
if err != nil {
|
||||
return output, fmt.Errorf("failed to marshal input to JSON: %w", err)
|
||||
}
|
||||
|
||||
// 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)
|
||||
|
||||
if err != nil {
|
||||
if responseError.Code != nil {
|
||||
slog.Error("ModifyImageAttribute failed", "err", responseError.Code)
|
||||
return output, errors.New(*responseError.Code)
|
||||
}
|
||||
return output, err
|
||||
}
|
||||
|
||||
// Unmarshal the JSON response back into output struct
|
||||
err = json.Unmarshal(jsonResp, &output)
|
||||
if err != nil {
|
||||
return output, fmt.Errorf("failed to unmarshal JSON response to ModifyImageAttributeOutput: %w", err)
|
||||
}
|
||||
|
||||
return output, nil
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
package gateway_ec2_image
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestValidateModifyImageAttributeInput(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input *ec2.ModifyImageAttributeInput
|
||||
want error
|
||||
}{
|
||||
{
|
||||
name: "NilInput",
|
||||
input: nil,
|
||||
want: errors.New("MissingParameter"),
|
||||
},
|
||||
{
|
||||
name: "MissingImageId",
|
||||
input: &ec2.ModifyImageAttributeInput{
|
||||
Description: &ec2.AttributeValue{Value: aws.String("test")},
|
||||
},
|
||||
want: errors.New("MissingParameter"),
|
||||
},
|
||||
{
|
||||
name: "InvalidImageId",
|
||||
input: &ec2.ModifyImageAttributeInput{
|
||||
ImageId: aws.String("invalid-id"),
|
||||
},
|
||||
want: errors.New("InvalidAMIID.Malformed"),
|
||||
},
|
||||
{
|
||||
name: "ValidInput",
|
||||
input: &ec2.ModifyImageAttributeInput{
|
||||
ImageId: aws.String("ami-0123456789abcdef0"),
|
||||
Description: &ec2.AttributeValue{Value: aws.String("test")},
|
||||
},
|
||||
want: nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, err := ModifyImageAttribute(tt.input)
|
||||
assert.Equal(t, tt.want, err)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
package gateway_ec2_image
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
handlers_ec2_image "github.com/mulgadc/hive/hive/handlers/ec2/image"
|
||||
"github.com/mulgadc/hive/hive/utils"
|
||||
)
|
||||
|
||||
func ValidateRegisterImageInput(input *ec2.RegisterImageInput) (err error) {
|
||||
if input == nil {
|
||||
return errors.New("MissingParameter")
|
||||
}
|
||||
|
||||
if input.Name == nil || *input.Name == "" {
|
||||
return errors.New("MissingParameter")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func RegisterImage(input *ec2.RegisterImageInput) (output ec2.RegisterImageOutput, err error) {
|
||||
|
||||
// Validate input
|
||||
err = ValidateRegisterImageInput(input)
|
||||
|
||||
if err != nil {
|
||||
return output, err
|
||||
}
|
||||
|
||||
// Marshal to JSON, to send over the wire for processing (NATS)
|
||||
jsonData, err := json.Marshal(input)
|
||||
if err != nil {
|
||||
return output, fmt.Errorf("failed to marshal input to JSON: %w", err)
|
||||
}
|
||||
|
||||
// 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)
|
||||
|
||||
if err != nil {
|
||||
if responseError.Code != nil {
|
||||
slog.Error("RegisterImage failed", "err", responseError.Code)
|
||||
return output, errors.New(*responseError.Code)
|
||||
}
|
||||
return output, err
|
||||
}
|
||||
|
||||
// Unmarshal the JSON response back into output struct
|
||||
err = json.Unmarshal(jsonResp, &output)
|
||||
if err != nil {
|
||||
return output, fmt.Errorf("failed to unmarshal JSON response to RegisterImageOutput: %w", err)
|
||||
}
|
||||
|
||||
return output, nil
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package gateway_ec2_image
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestValidateRegisterImageInput(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input *ec2.RegisterImageInput
|
||||
want error
|
||||
}{
|
||||
{
|
||||
name: "NilInput",
|
||||
input: nil,
|
||||
want: errors.New("MissingParameter"),
|
||||
},
|
||||
{
|
||||
name: "MissingName",
|
||||
input: &ec2.RegisterImageInput{
|
||||
ImageLocation: aws.String("s3://bucket/image"),
|
||||
},
|
||||
want: errors.New("MissingParameter"),
|
||||
},
|
||||
{
|
||||
name: "ValidInput",
|
||||
input: &ec2.RegisterImageInput{
|
||||
Name: aws.String("test-image"),
|
||||
ImageLocation: aws.String("s3://bucket/image"),
|
||||
},
|
||||
want: nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result, err := RegisterImage(tt.input)
|
||||
assert.Equal(t, tt.want, err)
|
||||
if err == nil {
|
||||
assert.NotEmpty(t, result.ImageId)
|
||||
assert.True(t, strings.HasPrefix(*result.ImageId, "ami-"))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
package gateway_ec2_image
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strings"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
handlers_ec2_image "github.com/mulgadc/hive/hive/handlers/ec2/image"
|
||||
"github.com/mulgadc/hive/hive/utils"
|
||||
)
|
||||
|
||||
func ValidateResetImageAttributeInput(input *ec2.ResetImageAttributeInput) (err error) {
|
||||
if input == nil {
|
||||
return errors.New("MissingParameter")
|
||||
}
|
||||
|
||||
if input.Attribute == nil || *input.Attribute == "" {
|
||||
return errors.New("MissingParameter")
|
||||
}
|
||||
|
||||
if input.ImageId == nil || *input.ImageId == "" {
|
||||
return errors.New("MissingParameter")
|
||||
}
|
||||
|
||||
// Validate ImageId format
|
||||
if !strings.HasPrefix(*input.ImageId, "ami-") {
|
||||
return errors.New("InvalidAMIID.Malformed")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func ResetImageAttribute(input *ec2.ResetImageAttributeInput) (output ec2.ResetImageAttributeOutput, err error) {
|
||||
|
||||
// Validate input
|
||||
err = ValidateResetImageAttributeInput(input)
|
||||
|
||||
if err != nil {
|
||||
return output, err
|
||||
}
|
||||
|
||||
// Marshal to JSON, to send over the wire for processing (NATS)
|
||||
jsonData, err := json.Marshal(input)
|
||||
if err != nil {
|
||||
return output, fmt.Errorf("failed to marshal input to JSON: %w", err)
|
||||
}
|
||||
|
||||
// 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)
|
||||
|
||||
if err != nil {
|
||||
if responseError.Code != nil {
|
||||
slog.Error("ResetImageAttribute failed", "err", responseError.Code)
|
||||
return output, errors.New(*responseError.Code)
|
||||
}
|
||||
return output, err
|
||||
}
|
||||
|
||||
// Unmarshal the JSON response back into output struct
|
||||
err = json.Unmarshal(jsonResp, &output)
|
||||
if err != nil {
|
||||
return output, fmt.Errorf("failed to unmarshal JSON response to ResetImageAttributeOutput: %w", err)
|
||||
}
|
||||
|
||||
return output, nil
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
package gateway_ec2_image
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestValidateResetImageAttributeInput(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input *ec2.ResetImageAttributeInput
|
||||
want error
|
||||
}{
|
||||
{
|
||||
name: "NilInput",
|
||||
input: nil,
|
||||
want: errors.New("MissingParameter"),
|
||||
},
|
||||
{
|
||||
name: "MissingAttribute",
|
||||
input: &ec2.ResetImageAttributeInput{
|
||||
ImageId: aws.String("ami-0123456789abcdef0"),
|
||||
},
|
||||
want: errors.New("MissingParameter"),
|
||||
},
|
||||
{
|
||||
name: "MissingImageId",
|
||||
input: &ec2.ResetImageAttributeInput{
|
||||
Attribute: aws.String("launchPermission"),
|
||||
},
|
||||
want: errors.New("MissingParameter"),
|
||||
},
|
||||
{
|
||||
name: "InvalidImageId",
|
||||
input: &ec2.ResetImageAttributeInput{
|
||||
ImageId: aws.String("invalid-id"),
|
||||
Attribute: aws.String("launchPermission"),
|
||||
},
|
||||
want: errors.New("InvalidAMIID.Malformed"),
|
||||
},
|
||||
{
|
||||
name: "ValidInput",
|
||||
input: &ec2.ResetImageAttributeInput{
|
||||
ImageId: aws.String("ami-0123456789abcdef0"),
|
||||
Attribute: aws.String("launchPermission"),
|
||||
},
|
||||
want: nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, err := ResetImageAttribute(tt.input)
|
||||
assert.Equal(t, tt.want, err)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,11 @@
|
||||
package gateway_ec2_instance
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
handlers_ec2_instance "github.com/mulgadc/hive/hive/handlers/ec2/instance"
|
||||
"github.com/mulgadc/hive/hive/utils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@@ -157,97 +154,3 @@ func TestParseRunInstances(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
func TestEC2ProcessRunInstances(t *testing.T) {
|
||||
makeValidInput := func() *ec2.RunInstancesInput {
|
||||
return &ec2.RunInstancesInput{
|
||||
ImageId: aws.String(*defaults.ImageId),
|
||||
InstanceType: aws.String(*defaults.InstanceType),
|
||||
MinCount: aws.Int64(1),
|
||||
MaxCount: aws.Int64(1),
|
||||
KeyName: aws.String(*defaults.KeyName),
|
||||
SecurityGroupIds: []*string{aws.String(*defaults.SecurityGroupIds[0])},
|
||||
SubnetId: aws.String(*defaults.SubnetId),
|
||||
}
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
payload any
|
||||
rawJSON []byte
|
||||
wantValidationErr bool
|
||||
wantErrCode string
|
||||
assertFn func(t *testing.T, reservation ec2.Reservation)
|
||||
}{
|
||||
{
|
||||
name: "MismatchedShape",
|
||||
payload: &ec2.DescribeInstancesInput{DryRun: aws.Bool(true)},
|
||||
wantValidationErr: true,
|
||||
wantErrCode: "ValidationError",
|
||||
},
|
||||
{
|
||||
name: "UnknownFieldInPayload",
|
||||
rawJSON: []byte(`{
|
||||
"ImageId":"ami-0abcdef1234567890",
|
||||
"InstanceType":"t2.micro",
|
||||
"MinCount":1,
|
||||
"MaxCount":1,
|
||||
"Unexpected":true
|
||||
}`),
|
||||
wantValidationErr: true,
|
||||
wantErrCode: "ValidationError",
|
||||
},
|
||||
{
|
||||
name: "ValidRunInstances",
|
||||
payload: makeValidInput(),
|
||||
assertFn: func(t *testing.T, reservation ec2.Reservation) {
|
||||
assert.Len(t, reservation.Instances, 1)
|
||||
if len(reservation.Instances) == 0 {
|
||||
return
|
||||
}
|
||||
instance := reservation.Instances[0]
|
||||
assert.Equal(t, defaults.ImageId, instance.ImageId)
|
||||
assert.Equal(t, defaults.InstanceType, instance.InstanceType)
|
||||
assert.Equal(t, defaults.KeyName, instance.KeyName)
|
||||
assert.Equal(t, aws.Int64(16), instance.State.Code)
|
||||
assert.Equal(t, aws.String("running"), instance.State.Name)
|
||||
assert.Equal(t, defaults.SubnetId, instance.SubnetId)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var (
|
||||
jsonData []byte
|
||||
err error
|
||||
)
|
||||
|
||||
if tt.rawJSON != nil {
|
||||
jsonData = tt.rawJSON
|
||||
} else {
|
||||
jsonData, err = json.Marshal(tt.payload)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
handler := handlers_ec2_instance.NewRunInstancesHandler(handlers_ec2_instance.NewMockInstanceService())
|
||||
jsonResp := handler.Process(jsonData)
|
||||
|
||||
responseError, err := utils.ValidateErrorPayload(jsonResp)
|
||||
|
||||
if tt.wantValidationErr {
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, tt.wantErrCode, aws.StringValue(responseError.Code))
|
||||
return
|
||||
}
|
||||
|
||||
assert.NoError(t, err)
|
||||
|
||||
var reservation ec2.Reservation
|
||||
assert.NoError(t, json.Unmarshal(jsonResp, &reservation))
|
||||
|
||||
if tt.assertFn != nil {
|
||||
tt.assertFn(t, reservation)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,11 @@
|
||||
package gateway_ec2_key
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
func TestValidateCreateKeyPairInput(t *testing.T) {
|
||||
@@ -56,66 +52,3 @@ func TestValidateCreateKeyPairInput(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestEC2ProcessCreateKeyPair(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
payload any
|
||||
rawJSON []byte
|
||||
wantValidationErr bool
|
||||
wantErrCode string
|
||||
assertFn func(t *testing.T, output ec2.CreateKeyPairOutput)
|
||||
}{
|
||||
{
|
||||
name: "InvalidPayload",
|
||||
payload: &ec2.DeleteKeyPairInput{},
|
||||
wantValidationErr: true,
|
||||
wantErrCode: "ValidationError",
|
||||
},
|
||||
{
|
||||
name: "ValidCreateKeyPair",
|
||||
payload: &ec2.CreateKeyPairInput{
|
||||
KeyName: aws.String("test-key"),
|
||||
},
|
||||
assertFn: func(t *testing.T, output ec2.CreateKeyPairOutput) {
|
||||
assert.NotNil(t, output.KeyName)
|
||||
assert.Equal(t, "test-key", *output.KeyName)
|
||||
assert.NotEmpty(t, output.KeyFingerprint)
|
||||
assert.NotEmpty(t, output.KeyMaterial)
|
||||
assert.NotEmpty(t, output.KeyPairId)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var jsonData []byte
|
||||
var err error
|
||||
|
||||
if tt.rawJSON != nil {
|
||||
jsonData = tt.rawJSON
|
||||
} else {
|
||||
jsonData, err = json.Marshal(tt.payload)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
handler := handlers_ec2_key.NewCreateKeyPairHandler(handlers_ec2_key.NewMockKeyService())
|
||||
jsonResp := handler.Process(jsonData)
|
||||
responseError, err := utils.ValidateErrorPayload(jsonResp)
|
||||
|
||||
if tt.wantValidationErr {
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, tt.wantErrCode, aws.StringValue(responseError.Code))
|
||||
return
|
||||
}
|
||||
|
||||
assert.NoError(t, err)
|
||||
|
||||
var output ec2.CreateKeyPairOutput
|
||||
assert.NoError(t, json.Unmarshal(jsonResp, &output))
|
||||
|
||||
if tt.assertFn != nil {
|
||||
tt.assertFn(t, output)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
package handlers_ec2
|
||||
|
||||
// EC2Handler defines the interface for EC2 operation handlers
|
||||
type EC2Handler interface {
|
||||
// Topic returns the NATS topic this handler subscribes to
|
||||
Topic() string
|
||||
|
||||
// Process handles the business logic for the operation
|
||||
// Takes JSON input and returns JSON output or error payload
|
||||
Process(jsonData []byte) []byte
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package handlers_ec2_image
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
"github.com/mulgadc/hive/hive/awserrors"
|
||||
"github.com/mulgadc/hive/hive/utils"
|
||||
)
|
||||
|
||||
// CopyImageHandler handles CopyImage operations
|
||||
type CopyImageHandler struct {
|
||||
service ImageService
|
||||
}
|
||||
|
||||
// NewCopyImageHandler creates a new CopyImage handler with the given service
|
||||
func NewCopyImageHandler(service ImageService) *CopyImageHandler {
|
||||
return &CopyImageHandler{service: service}
|
||||
}
|
||||
|
||||
// Topic returns the NATS topic for this handler (matches AWS Action name)
|
||||
func (h *CopyImageHandler) Topic() string {
|
||||
return "ec2.CopyImage"
|
||||
}
|
||||
|
||||
// Process handles the business logic for CopyImage
|
||||
// Note: Input validation is performed by the gateway before calling this handler
|
||||
func (h *CopyImageHandler) Process(jsonData []byte) []byte {
|
||||
var input ec2.CopyImageInput
|
||||
|
||||
if errPayload := utils.UnmarshalJsonPayload(&input, jsonData); errPayload != nil {
|
||||
return errPayload
|
||||
}
|
||||
|
||||
// Delegate to the service implementation
|
||||
result, err := h.service.CopyImage(&input)
|
||||
if err != nil {
|
||||
slog.Error("CopyImage service failed", "err", err)
|
||||
return utils.GenerateErrorPayload(awserrors.ErrorInternalError)
|
||||
}
|
||||
|
||||
// Return as JSON
|
||||
jsonResponse, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
slog.Error("CopyImageHandler could not marshal output", "err", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
return jsonResponse
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package handlers_ec2_image
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
"github.com/mulgadc/hive/hive/awserrors"
|
||||
"github.com/mulgadc/hive/hive/utils"
|
||||
)
|
||||
|
||||
// CreateImageHandler handles CreateImage operations
|
||||
type CreateImageHandler struct {
|
||||
service ImageService
|
||||
}
|
||||
|
||||
// NewCreateImageHandler creates a new CreateImage handler with the given service
|
||||
func NewCreateImageHandler(service ImageService) *CreateImageHandler {
|
||||
return &CreateImageHandler{service: service}
|
||||
}
|
||||
|
||||
// Topic returns the NATS topic for this handler (matches AWS Action name)
|
||||
func (h *CreateImageHandler) Topic() string {
|
||||
return "ec2.CreateImage"
|
||||
}
|
||||
|
||||
// Process handles the business logic for CreateImage
|
||||
// Note: Input validation is performed by the gateway before calling this handler
|
||||
func (h *CreateImageHandler) Process(jsonData []byte) []byte {
|
||||
var input ec2.CreateImageInput
|
||||
|
||||
if errPayload := utils.UnmarshalJsonPayload(&input, jsonData); errPayload != nil {
|
||||
return errPayload
|
||||
}
|
||||
|
||||
// Delegate to the service implementation
|
||||
result, err := h.service.CreateImage(&input)
|
||||
if err != nil {
|
||||
slog.Error("CreateImage service failed", "err", err)
|
||||
return utils.GenerateErrorPayload(awserrors.ErrorInternalError)
|
||||
}
|
||||
|
||||
// Return as JSON
|
||||
jsonResponse, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
slog.Error("CreateImageHandler could not marshal output", "err", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
return jsonResponse
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package handlers_ec2_image
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
"github.com/mulgadc/hive/hive/awserrors"
|
||||
"github.com/mulgadc/hive/hive/utils"
|
||||
)
|
||||
|
||||
// DeregisterImageHandler handles DeregisterImage operations
|
||||
type DeregisterImageHandler struct {
|
||||
service ImageService
|
||||
}
|
||||
|
||||
// NewDeregisterImageHandler creates a new DeregisterImage handler with the given service
|
||||
func NewDeregisterImageHandler(service ImageService) *DeregisterImageHandler {
|
||||
return &DeregisterImageHandler{service: service}
|
||||
}
|
||||
|
||||
// Topic returns the NATS topic for this handler (matches AWS Action name)
|
||||
func (h *DeregisterImageHandler) Topic() string {
|
||||
return "ec2.DeregisterImage"
|
||||
}
|
||||
|
||||
// Process handles the business logic for DeregisterImage
|
||||
// Note: Input validation is performed by the gateway before calling this handler
|
||||
func (h *DeregisterImageHandler) Process(jsonData []byte) []byte {
|
||||
var input ec2.DeregisterImageInput
|
||||
|
||||
if errPayload := utils.UnmarshalJsonPayload(&input, jsonData); errPayload != nil {
|
||||
return errPayload
|
||||
}
|
||||
|
||||
// Delegate to the service implementation
|
||||
result, err := h.service.DeregisterImage(&input)
|
||||
if err != nil {
|
||||
slog.Error("DeregisterImage service failed", "err", err)
|
||||
return utils.GenerateErrorPayload(awserrors.ErrorInternalError)
|
||||
}
|
||||
|
||||
// Return as JSON
|
||||
jsonResponse, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
slog.Error("DeregisterImageHandler could not marshal output", "err", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
return jsonResponse
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package handlers_ec2_image
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
"github.com/mulgadc/hive/hive/awserrors"
|
||||
"github.com/mulgadc/hive/hive/utils"
|
||||
)
|
||||
|
||||
// DescribeImageAttributeHandler handles DescribeImageAttribute operations
|
||||
type DescribeImageAttributeHandler struct {
|
||||
service ImageService
|
||||
}
|
||||
|
||||
// NewDescribeImageAttributeHandler creates a new DescribeImageAttribute handler with the given service
|
||||
func NewDescribeImageAttributeHandler(service ImageService) *DescribeImageAttributeHandler {
|
||||
return &DescribeImageAttributeHandler{service: service}
|
||||
}
|
||||
|
||||
// Topic returns the NATS topic for this handler (matches AWS Action name)
|
||||
func (h *DescribeImageAttributeHandler) Topic() string {
|
||||
return "ec2.DescribeImageAttribute"
|
||||
}
|
||||
|
||||
// Process handles the business logic for DescribeImageAttribute
|
||||
// Note: Input validation is performed by the gateway before calling this handler
|
||||
func (h *DescribeImageAttributeHandler) Process(jsonData []byte) []byte {
|
||||
var input ec2.DescribeImageAttributeInput
|
||||
|
||||
if errPayload := utils.UnmarshalJsonPayload(&input, jsonData); errPayload != nil {
|
||||
return errPayload
|
||||
}
|
||||
|
||||
// Delegate to the service implementation
|
||||
result, err := h.service.DescribeImageAttribute(&input)
|
||||
if err != nil {
|
||||
slog.Error("DescribeImageAttribute service failed", "err", err)
|
||||
return utils.GenerateErrorPayload(awserrors.ErrorInternalError)
|
||||
}
|
||||
|
||||
// Return as JSON
|
||||
jsonResponse, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
slog.Error("DescribeImageAttributeHandler could not marshal output", "err", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
return jsonResponse
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package handlers_ec2_image
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
"github.com/mulgadc/hive/hive/awserrors"
|
||||
"github.com/mulgadc/hive/hive/utils"
|
||||
)
|
||||
|
||||
// DescribeImagesHandler handles DescribeImages operations
|
||||
type DescribeImagesHandler struct {
|
||||
service ImageService
|
||||
}
|
||||
|
||||
// NewDescribeImagesHandler creates a new DescribeImages handler with the given service
|
||||
func NewDescribeImagesHandler(service ImageService) *DescribeImagesHandler {
|
||||
return &DescribeImagesHandler{service: service}
|
||||
}
|
||||
|
||||
// Topic returns the NATS topic for this handler (matches AWS Action name)
|
||||
func (h *DescribeImagesHandler) Topic() string {
|
||||
return "ec2.DescribeImages"
|
||||
}
|
||||
|
||||
// Process handles the business logic for DescribeImages
|
||||
// Note: Input validation is performed by the gateway before calling this handler
|
||||
func (h *DescribeImagesHandler) Process(jsonData []byte) []byte {
|
||||
var input ec2.DescribeImagesInput
|
||||
|
||||
if errPayload := utils.UnmarshalJsonPayload(&input, jsonData); errPayload != nil {
|
||||
return errPayload
|
||||
}
|
||||
|
||||
// Delegate to the service implementation
|
||||
result, err := h.service.DescribeImages(&input)
|
||||
if err != nil {
|
||||
slog.Error("DescribeImages service failed", "err", err)
|
||||
return utils.GenerateErrorPayload(awserrors.ErrorInternalError)
|
||||
}
|
||||
|
||||
// Return as JSON
|
||||
jsonResponse, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
slog.Error("DescribeImagesHandler could not marshal output", "err", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
return jsonResponse
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package handlers_ec2_image
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
"github.com/mulgadc/hive/hive/awserrors"
|
||||
"github.com/mulgadc/hive/hive/utils"
|
||||
)
|
||||
|
||||
// ModifyImageAttributeHandler handles ModifyImageAttribute operations
|
||||
type ModifyImageAttributeHandler struct {
|
||||
service ImageService
|
||||
}
|
||||
|
||||
// NewModifyImageAttributeHandler creates a new ModifyImageAttribute handler with the given service
|
||||
func NewModifyImageAttributeHandler(service ImageService) *ModifyImageAttributeHandler {
|
||||
return &ModifyImageAttributeHandler{service: service}
|
||||
}
|
||||
|
||||
// Topic returns the NATS topic for this handler (matches AWS Action name)
|
||||
func (h *ModifyImageAttributeHandler) Topic() string {
|
||||
return "ec2.ModifyImageAttribute"
|
||||
}
|
||||
|
||||
// Process handles the business logic for ModifyImageAttribute
|
||||
// Note: Input validation is performed by the gateway before calling this handler
|
||||
func (h *ModifyImageAttributeHandler) Process(jsonData []byte) []byte {
|
||||
var input ec2.ModifyImageAttributeInput
|
||||
|
||||
if errPayload := utils.UnmarshalJsonPayload(&input, jsonData); errPayload != nil {
|
||||
return errPayload
|
||||
}
|
||||
|
||||
// Delegate to the service implementation
|
||||
result, err := h.service.ModifyImageAttribute(&input)
|
||||
if err != nil {
|
||||
slog.Error("ModifyImageAttribute service failed", "err", err)
|
||||
return utils.GenerateErrorPayload(awserrors.ErrorInternalError)
|
||||
}
|
||||
|
||||
// Return as JSON
|
||||
jsonResponse, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
slog.Error("ModifyImageAttributeHandler could not marshal output", "err", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
return jsonResponse
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package handlers_ec2_image
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
"github.com/mulgadc/hive/hive/awserrors"
|
||||
"github.com/mulgadc/hive/hive/utils"
|
||||
)
|
||||
|
||||
// RegisterImageHandler handles RegisterImage operations
|
||||
type RegisterImageHandler struct {
|
||||
service ImageService
|
||||
}
|
||||
|
||||
// NewRegisterImageHandler creates a new RegisterImage handler with the given service
|
||||
func NewRegisterImageHandler(service ImageService) *RegisterImageHandler {
|
||||
return &RegisterImageHandler{service: service}
|
||||
}
|
||||
|
||||
// Topic returns the NATS topic for this handler (matches AWS Action name)
|
||||
func (h *RegisterImageHandler) Topic() string {
|
||||
return "ec2.RegisterImage"
|
||||
}
|
||||
|
||||
// Process handles the business logic for RegisterImage
|
||||
// Note: Input validation is performed by the gateway before calling this handler
|
||||
func (h *RegisterImageHandler) Process(jsonData []byte) []byte {
|
||||
var input ec2.RegisterImageInput
|
||||
|
||||
if errPayload := utils.UnmarshalJsonPayload(&input, jsonData); errPayload != nil {
|
||||
return errPayload
|
||||
}
|
||||
|
||||
// Delegate to the service implementation
|
||||
result, err := h.service.RegisterImage(&input)
|
||||
if err != nil {
|
||||
slog.Error("RegisterImage service failed", "err", err)
|
||||
return utils.GenerateErrorPayload(awserrors.ErrorInternalError)
|
||||
}
|
||||
|
||||
// Return as JSON
|
||||
jsonResponse, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
slog.Error("RegisterImageHandler could not marshal output", "err", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
return jsonResponse
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package handlers_ec2_image
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
"github.com/mulgadc/hive/hive/awserrors"
|
||||
"github.com/mulgadc/hive/hive/utils"
|
||||
)
|
||||
|
||||
// ResetImageAttributeHandler handles ResetImageAttribute operations
|
||||
type ResetImageAttributeHandler struct {
|
||||
service ImageService
|
||||
}
|
||||
|
||||
// NewResetImageAttributeHandler creates a new ResetImageAttribute handler with the given service
|
||||
func NewResetImageAttributeHandler(service ImageService) *ResetImageAttributeHandler {
|
||||
return &ResetImageAttributeHandler{service: service}
|
||||
}
|
||||
|
||||
// Topic returns the NATS topic for this handler (matches AWS Action name)
|
||||
func (h *ResetImageAttributeHandler) Topic() string {
|
||||
return "ec2.ResetImageAttribute"
|
||||
}
|
||||
|
||||
// Process handles the business logic for ResetImageAttribute
|
||||
// Note: Input validation is performed by the gateway before calling this handler
|
||||
func (h *ResetImageAttributeHandler) Process(jsonData []byte) []byte {
|
||||
var input ec2.ResetImageAttributeInput
|
||||
|
||||
if errPayload := utils.UnmarshalJsonPayload(&input, jsonData); errPayload != nil {
|
||||
return errPayload
|
||||
}
|
||||
|
||||
// Delegate to the service implementation
|
||||
result, err := h.service.ResetImageAttribute(&input)
|
||||
if err != nil {
|
||||
slog.Error("ResetImageAttribute service failed", "err", err)
|
||||
return utils.GenerateErrorPayload(awserrors.ErrorInternalError)
|
||||
}
|
||||
|
||||
// Return as JSON
|
||||
jsonResponse, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
slog.Error("ResetImageAttributeHandler could not marshal output", "err", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
return jsonResponse
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
package handlers_ec2_image
|
||||
|
||||
import (
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
)
|
||||
|
||||
// MockImageService provides mock responses for testing
|
||||
type MockImageService struct{}
|
||||
|
||||
// NewMockImageService creates a new mock image service
|
||||
func NewMockImageService() ImageService {
|
||||
return &MockImageService{}
|
||||
}
|
||||
|
||||
func (s *MockImageService) CreateImage(input *ec2.CreateImageInput) (*ec2.CreateImageOutput, error) {
|
||||
return &ec2.CreateImageOutput{
|
||||
ImageId: aws.String("ami-0123456789abcdef0"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *MockImageService) CopyImage(input *ec2.CopyImageInput) (*ec2.CopyImageOutput, error) {
|
||||
return &ec2.CopyImageOutput{
|
||||
ImageId: aws.String("ami-0987654321fedcba0"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *MockImageService) DescribeImages(input *ec2.DescribeImagesInput) (*ec2.DescribeImagesOutput, error) {
|
||||
return &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),
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *MockImageService) DescribeImageAttribute(input *ec2.DescribeImageAttributeInput) (*ec2.DescribeImageAttributeOutput, error) {
|
||||
return &ec2.DescribeImageAttributeOutput{
|
||||
ImageId: input.ImageId,
|
||||
Description: &ec2.AttributeValue{
|
||||
Value: aws.String("Test image description"),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *MockImageService) RegisterImage(input *ec2.RegisterImageInput) (*ec2.RegisterImageOutput, error) {
|
||||
return &ec2.RegisterImageOutput{
|
||||
ImageId: aws.String("ami-0123456789abcdef0"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *MockImageService) DeregisterImage(input *ec2.DeregisterImageInput) (*ec2.DeregisterImageOutput, error) {
|
||||
return &ec2.DeregisterImageOutput{}, nil
|
||||
}
|
||||
|
||||
func (s *MockImageService) ModifyImageAttribute(input *ec2.ModifyImageAttributeInput) (*ec2.ModifyImageAttributeOutput, error) {
|
||||
return &ec2.ModifyImageAttributeOutput{}, nil
|
||||
}
|
||||
|
||||
func (s *MockImageService) ResetImageAttribute(input *ec2.ResetImageAttributeInput) (*ec2.ResetImageAttributeOutput, error) {
|
||||
return &ec2.ResetImageAttributeOutput{}, nil
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package handlers_ec2_instance
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
"github.com/mulgadc/hive/hive/awserrors"
|
||||
"github.com/mulgadc/hive/hive/utils"
|
||||
)
|
||||
|
||||
// RunInstancesHandler handles RunInstances operations
|
||||
type RunInstancesHandler struct {
|
||||
service InstanceService
|
||||
}
|
||||
|
||||
// NewRunInstancesHandler creates a new RunInstancesHandler with the given service
|
||||
func NewRunInstancesHandler(service InstanceService) *RunInstancesHandler {
|
||||
return &RunInstancesHandler{service: service}
|
||||
}
|
||||
|
||||
// Topic returns the NATS topic for this handler
|
||||
func (h *RunInstancesHandler) Topic() string {
|
||||
return "ec2.RunInstances"
|
||||
}
|
||||
|
||||
// Process handles the business logic for RunInstances
|
||||
// Note: Input validation is performed by the gateway before calling this handler
|
||||
func (h *RunInstancesHandler) Process(jsonData []byte) []byte {
|
||||
var input ec2.RunInstancesInput
|
||||
|
||||
if errPayload := utils.UnmarshalJsonPayload(&input, jsonData); errPayload != nil {
|
||||
return errPayload
|
||||
}
|
||||
|
||||
// Call the service to perform the actual operation
|
||||
reservation, err := h.service.RunInstances(&input)
|
||||
if err != nil {
|
||||
slog.Error("RunInstances service failed", "err", err)
|
||||
return utils.GenerateErrorPayload(awserrors.ErrorInternalError)
|
||||
}
|
||||
|
||||
// Return as JSON, to simulate the NATS response
|
||||
jsonResponse, err := json.Marshal(reservation)
|
||||
if err != nil {
|
||||
slog.Error("RunInstancesHandler could not marshal reservation", "err", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
return jsonResponse
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package handlers_ec2_key
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
"github.com/mulgadc/hive/hive/awserrors"
|
||||
"github.com/mulgadc/hive/hive/utils"
|
||||
)
|
||||
|
||||
// CreateKeyPairHandler handles CreateKeyPair operations
|
||||
type CreateKeyPairHandler struct {
|
||||
service KeyService
|
||||
}
|
||||
|
||||
// NewCreateKeyPairHandler creates a new CreateKeyPairHandler with the given service
|
||||
func NewCreateKeyPairHandler(service KeyService) *CreateKeyPairHandler {
|
||||
return &CreateKeyPairHandler{service: service}
|
||||
}
|
||||
|
||||
// Topic returns the NATS topic for this handler
|
||||
func (h *CreateKeyPairHandler) Topic() string {
|
||||
return "ec2.CreateKeyPair"
|
||||
}
|
||||
|
||||
// Process handles the business logic for CreateKeyPair
|
||||
// Note: Input validation is performed by the gateway before calling this handler
|
||||
func (h *CreateKeyPairHandler) Process(jsonData []byte) []byte {
|
||||
var input ec2.CreateKeyPairInput
|
||||
|
||||
if errPayload := utils.UnmarshalJsonPayload(&input, jsonData); errPayload != nil {
|
||||
return errPayload
|
||||
}
|
||||
|
||||
// Call the service to perform the actual operation
|
||||
result, err := h.service.CreateKeyPair(&input)
|
||||
if err != nil {
|
||||
slog.Error("CreateKeyPair service failed", "err", err)
|
||||
return utils.GenerateErrorPayload(awserrors.ErrorInternalError)
|
||||
}
|
||||
|
||||
// Return as JSON, to simulate the NATS response
|
||||
jsonResponse, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
slog.Error("CreateKeyPairHandler could not marshal output", "err", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
return jsonResponse
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package handlers_ec2_key
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
"github.com/mulgadc/hive/hive/awserrors"
|
||||
"github.com/mulgadc/hive/hive/utils"
|
||||
)
|
||||
|
||||
// DeleteKeyPairHandler handles DeleteKeyPair operations
|
||||
type DeleteKeyPairHandler struct {
|
||||
service KeyService
|
||||
}
|
||||
|
||||
// NewDeleteKeyPairHandler creates a new DeleteKeyPairHandler with the given service
|
||||
func NewDeleteKeyPairHandler(service KeyService) *DeleteKeyPairHandler {
|
||||
return &DeleteKeyPairHandler{service: service}
|
||||
}
|
||||
|
||||
// Topic returns the NATS topic for this handler
|
||||
func (h *DeleteKeyPairHandler) Topic() string {
|
||||
return "ec2.DeleteKeyPair"
|
||||
}
|
||||
|
||||
// Process handles the business logic for DeleteKeyPair
|
||||
// Note: Input validation is performed by the gateway before calling this handler
|
||||
func (h *DeleteKeyPairHandler) Process(jsonData []byte) []byte {
|
||||
var input ec2.DeleteKeyPairInput
|
||||
|
||||
if errPayload := utils.UnmarshalJsonPayload(&input, jsonData); errPayload != nil {
|
||||
return errPayload
|
||||
}
|
||||
|
||||
// Call the service to perform the actual operation
|
||||
result, err := h.service.DeleteKeyPair(&input)
|
||||
if err != nil {
|
||||
slog.Error("DeleteKeyPair service failed", "err", err)
|
||||
return utils.GenerateErrorPayload(awserrors.ErrorInternalError)
|
||||
}
|
||||
|
||||
// Return as JSON, to simulate the NATS response
|
||||
jsonResponse, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
slog.Error("DeleteKeyPairHandler could not marshal output", "err", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
return jsonResponse
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package handlers_ec2_key
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
"github.com/mulgadc/hive/hive/awserrors"
|
||||
"github.com/mulgadc/hive/hive/utils"
|
||||
)
|
||||
|
||||
// DescribeKeyPairsHandler handles DescribeKeyPairs operations
|
||||
type DescribeKeyPairsHandler struct {
|
||||
service KeyService
|
||||
}
|
||||
|
||||
// NewDescribeKeyPairsHandler creates a new DescribeKeyPairsHandler with the given service
|
||||
func NewDescribeKeyPairsHandler(service KeyService) *DescribeKeyPairsHandler {
|
||||
return &DescribeKeyPairsHandler{service: service}
|
||||
}
|
||||
|
||||
// Topic returns the NATS topic for this handler
|
||||
func (h *DescribeKeyPairsHandler) Topic() string {
|
||||
return "ec2.DescribeKeyPairs"
|
||||
}
|
||||
|
||||
// Process handles the business logic for DescribeKeyPairs
|
||||
// Note: Input validation is performed by the gateway before calling this handler
|
||||
func (h *DescribeKeyPairsHandler) Process(jsonData []byte) []byte {
|
||||
var input ec2.DescribeKeyPairsInput
|
||||
|
||||
if errPayload := utils.UnmarshalJsonPayload(&input, jsonData); errPayload != nil {
|
||||
return errPayload
|
||||
}
|
||||
|
||||
// Call the service to perform the actual operation
|
||||
result, err := h.service.DescribeKeyPairs(&input)
|
||||
if err != nil {
|
||||
slog.Error("DescribeKeyPairs service failed", "err", err)
|
||||
return utils.GenerateErrorPayload(awserrors.ErrorInternalError)
|
||||
}
|
||||
|
||||
// Return as JSON, to simulate the NATS response
|
||||
jsonResponse, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
slog.Error("DescribeKeyPairsHandler could not marshal output", "err", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
return jsonResponse
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package handlers_ec2_key
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
"github.com/mulgadc/hive/hive/awserrors"
|
||||
"github.com/mulgadc/hive/hive/utils"
|
||||
)
|
||||
|
||||
// ImportKeyPairHandler handles ImportKeyPair operations
|
||||
type ImportKeyPairHandler struct {
|
||||
service KeyService
|
||||
}
|
||||
|
||||
// NewImportKeyPairHandler creates a new ImportKeyPairHandler with the given service
|
||||
func NewImportKeyPairHandler(service KeyService) *ImportKeyPairHandler {
|
||||
return &ImportKeyPairHandler{service: service}
|
||||
}
|
||||
|
||||
// Topic returns the NATS topic for this handler
|
||||
func (h *ImportKeyPairHandler) Topic() string {
|
||||
return "ec2.ImportKeyPair"
|
||||
}
|
||||
|
||||
// Process handles the business logic for ImportKeyPair
|
||||
// Note: Input validation is performed by the gateway before calling this handler
|
||||
func (h *ImportKeyPairHandler) Process(jsonData []byte) []byte {
|
||||
var input ec2.ImportKeyPairInput
|
||||
|
||||
if errPayload := utils.UnmarshalJsonPayload(&input, jsonData); errPayload != nil {
|
||||
return errPayload
|
||||
}
|
||||
|
||||
// Call the service to perform the actual operation
|
||||
result, err := h.service.ImportKeyPair(&input)
|
||||
if err != nil {
|
||||
slog.Error("ImportKeyPair service failed", "err", err)
|
||||
return utils.GenerateErrorPayload(awserrors.ErrorInternalError)
|
||||
}
|
||||
|
||||
// Return as JSON, to simulate the NATS response
|
||||
jsonResponse, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
slog.Error("ImportKeyPairHandler could not marshal output", "err", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
return jsonResponse
|
||||
}
|
||||
Reference in New Issue
Block a user