diff --git a/hive/gateway/ec2/instance/StartInstances.go b/hive/gateway/ec2/instance/StartInstances.go index 4741ba1ac..c6a75a548 100644 --- a/hive/gateway/ec2/instance/StartInstances.go +++ b/hive/gateway/ec2/instance/StartInstances.go @@ -57,45 +57,13 @@ func StartInstances(input *ec2.StartInstancesInput, natsConn *nats.Conn) (*ec2.S msg, err := natsConn.Request(subject, jsonData, 30*time.Second) if err != nil { slog.Error("StartInstances: Failed to send command", "instance_id", instanceID, "err", err) - // Add failed state change - stateChange := &ec2.InstanceStateChange{ - InstanceId: &instanceID, - CurrentState: &ec2.InstanceState{ - Code: new(int64), - Name: new(string), - }, - PreviousState: &ec2.InstanceState{ - Code: new(int64), - Name: new(string), - }, - } - *stateChange.CurrentState.Code = 80 // stopped - *stateChange.CurrentState.Name = "stopped" - *stateChange.PreviousState.Code = 80 - *stateChange.PreviousState.Name = "stopped" - stateChanges = append(stateChanges, stateChange) + stateChanges = append(stateChanges, newStateChange(instanceID, 80, "stopped", 80, "stopped")) continue } slog.Info("StartInstances: Command sent successfully", "instance_id", instanceID, "response", string(msg.Data)) - // Build state change response (stopped -> pending) - stateChange := &ec2.InstanceStateChange{ - InstanceId: &instanceID, - CurrentState: &ec2.InstanceState{ - Code: new(int64), - Name: new(string), - }, - PreviousState: &ec2.InstanceState{ - Code: new(int64), - Name: new(string), - }, - } - *stateChange.CurrentState.Code = 0 // pending - *stateChange.CurrentState.Name = "pending" - *stateChange.PreviousState.Code = 80 // stopped - *stateChange.PreviousState.Name = "stopped" - stateChanges = append(stateChanges, stateChange) + stateChanges = append(stateChanges, newStateChange(instanceID, 0, "pending", 80, "stopped")) } output := &ec2.StartInstancesOutput{ diff --git a/hive/gateway/ec2/instance/StopInstances.go b/hive/gateway/ec2/instance/StopInstances.go index 799db7643..c6491686f 100644 --- a/hive/gateway/ec2/instance/StopInstances.go +++ b/hive/gateway/ec2/instance/StopInstances.go @@ -55,45 +55,13 @@ func StopInstances(input *ec2.StopInstancesInput, natsConn *nats.Conn) (*ec2.Sto msg, err := natsConn.Request(subject, jsonData, 5*time.Second) if err != nil { slog.Error("StopInstances: Failed to send command", "instance_id", instanceID, "err", err) - // Add failed state change - stateChange := &ec2.InstanceStateChange{ - InstanceId: &instanceID, - CurrentState: &ec2.InstanceState{ - Code: new(int64), - Name: new(string), - }, - PreviousState: &ec2.InstanceState{ - Code: new(int64), - Name: new(string), - }, - } - *stateChange.CurrentState.Code = 16 // running - *stateChange.CurrentState.Name = "running" - *stateChange.PreviousState.Code = 16 - *stateChange.PreviousState.Name = "running" - stateChanges = append(stateChanges, stateChange) + stateChanges = append(stateChanges, newStateChange(instanceID, 16, "running", 16, "running")) continue } slog.Info("StopInstances: Command sent successfully", "instance_id", instanceID, "response", string(msg.Data)) - // Build state change response (running -> stopping) - stateChange := &ec2.InstanceStateChange{ - InstanceId: &instanceID, - CurrentState: &ec2.InstanceState{ - Code: new(int64), - Name: new(string), - }, - PreviousState: &ec2.InstanceState{ - Code: new(int64), - Name: new(string), - }, - } - *stateChange.CurrentState.Code = 64 // stopping - *stateChange.CurrentState.Name = "stopping" - *stateChange.PreviousState.Code = 16 // running - *stateChange.PreviousState.Name = "running" - stateChanges = append(stateChanges, stateChange) + stateChanges = append(stateChanges, newStateChange(instanceID, 64, "stopping", 16, "running")) } output := &ec2.StopInstancesOutput{ diff --git a/hive/gateway/ec2/instance/TerminateInstances.go b/hive/gateway/ec2/instance/TerminateInstances.go index fc7871274..94ea14b1a 100644 --- a/hive/gateway/ec2/instance/TerminateInstances.go +++ b/hive/gateway/ec2/instance/TerminateInstances.go @@ -55,45 +55,13 @@ func TerminateInstances(input *ec2.TerminateInstancesInput, natsConn *nats.Conn) msg, err := natsConn.Request(subject, jsonData, 5*time.Second) if err != nil { slog.Error("TerminateInstances: Failed to send command", "instance_id", instanceID, "err", err) - // Add failed state change - stateChange := &ec2.InstanceStateChange{ - InstanceId: &instanceID, - CurrentState: &ec2.InstanceState{ - Code: new(int64), - Name: new(string), - }, - PreviousState: &ec2.InstanceState{ - Code: new(int64), - Name: new(string), - }, - } - *stateChange.CurrentState.Code = 16 // running - *stateChange.CurrentState.Name = "running" - *stateChange.PreviousState.Code = 16 - *stateChange.PreviousState.Name = "running" - stateChanges = append(stateChanges, stateChange) + stateChanges = append(stateChanges, newStateChange(instanceID, 16, "running", 16, "running")) continue } slog.Info("TerminateInstances: Command sent successfully", "instance_id", instanceID, "response", string(msg.Data)) - // Build state change response (running -> shutting-down) - stateChange := &ec2.InstanceStateChange{ - InstanceId: &instanceID, - CurrentState: &ec2.InstanceState{ - Code: new(int64), - Name: new(string), - }, - PreviousState: &ec2.InstanceState{ - Code: new(int64), - Name: new(string), - }, - } - *stateChange.CurrentState.Code = 32 // shutting-down - *stateChange.CurrentState.Name = "shutting-down" - *stateChange.PreviousState.Code = 16 // running - *stateChange.PreviousState.Name = "running" - stateChanges = append(stateChanges, stateChange) + stateChanges = append(stateChanges, newStateChange(instanceID, 32, "shutting-down", 16, "running")) } output := &ec2.TerminateInstancesOutput{ diff --git a/hive/gateway/ec2/instance/state.go b/hive/gateway/ec2/instance/state.go new file mode 100644 index 000000000..1bd09ce0c --- /dev/null +++ b/hive/gateway/ec2/instance/state.go @@ -0,0 +1,11 @@ +package gateway_ec2_instance + +import "github.com/aws/aws-sdk-go/service/ec2" + +func newStateChange(instanceID string, currentCode int64, currentName string, prevCode int64, prevName string) *ec2.InstanceStateChange { + return &ec2.InstanceStateChange{ + InstanceId: &instanceID, + CurrentState: &ec2.InstanceState{Code: ¤tCode, Name: ¤tName}, + PreviousState: &ec2.InstanceState{Code: &prevCode, Name: &prevName}, + } +}