fix: state change

This commit is contained in:
Julian Sommer
2026-02-09 10:51:21 +10:00
parent e15c077d3a
commit a554b9b884
4 changed files with 17 additions and 102 deletions
+2 -34
View File
@@ -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{
+2 -34
View File
@@ -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{
@@ -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{
+11
View File
@@ -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: &currentCode, Name: &currentName},
PreviousState: &ec2.InstanceState{Code: &prevCode, Name: &prevName},
}
}