Skip to content

Commit c5d096d

Browse files
committed
Fix stop / start instance with network
1 parent 9edbbfa commit c5d096d

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

cmd/api/api/instances_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func TestInstanceLifecycle_StopStart(t *testing.T) {
148148

149149
// 1. Create instance
150150
t.Log("Creating instance...")
151-
networkEnabled := false
151+
networkEnabled := true
152152
createResp, err := svc.CreateInstance(ctx(), oapi.CreateInstanceRequestObject{
153153
Body: &oapi.CreateInstanceRequest{
154154
Name: "test-lifecycle",

lib/network/allocate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ func (m *manager) CreateAllocation(ctx context.Context, req AllocateRequest) (*N
2727
return nil, fmt.Errorf("get default network: %w", err)
2828
}
2929

30-
// 2. Check name uniqueness
31-
exists, err := m.NameExists(ctx, req.InstanceName)
30+
// 2. Check name uniqueness (exclude current instance to allow restarts)
31+
exists, err := m.NameExists(ctx, req.InstanceName, req.InstanceID)
3232
if err != nil {
3333
return nil, fmt.Errorf("check name exists: %w", err)
3434
}

lib/network/derive.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,20 @@ func (m *manager) ListAllocations(ctx context.Context) ([]Allocation, error) {
113113
return allocations, nil
114114
}
115115

116-
// NameExists checks if instance name is already used in the default network
117-
func (m *manager) NameExists(ctx context.Context, name string) (bool, error) {
116+
// NameExists checks if instance name is already used in the default network.
117+
// excludeInstanceID allows excluding a specific instance from the check (used when
118+
// starting an existing instance to avoid it conflicting with itself).
119+
func (m *manager) NameExists(ctx context.Context, name string, excludeInstanceID string) (bool, error) {
118120
allocations, err := m.ListAllocations(ctx)
119121
if err != nil {
120122
return false, err
121123
}
122124

123125
for _, alloc := range allocations {
126+
// Skip the excluded instance (e.g., when restarting an instance)
127+
if excludeInstanceID != "" && alloc.InstanceID == excludeInstanceID {
128+
continue
129+
}
124130
if alloc.InstanceName == name {
125131
return true, nil
126132
}

lib/network/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type Manager interface {
2929
// Queries (derive from CH/snapshots)
3030
GetAllocation(ctx context.Context, instanceID string) (*Allocation, error)
3131
ListAllocations(ctx context.Context) ([]Allocation, error)
32-
NameExists(ctx context.Context, name string) (bool, error)
32+
NameExists(ctx context.Context, name string, excludeInstanceID string) (bool, error)
3333

3434
// GetUploadBurstMultiplier returns the configured multiplier for upload burst ceiling.
3535
GetUploadBurstMultiplier() int

0 commit comments

Comments
 (0)