|
| 1 | +package mock |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "math/rand" |
| 8 | + "sync" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/UpCloudLtd/upcloud-go-api/v6/upcloud" |
| 12 | + "github.com/UpCloudLtd/upcloud-go-api/v6/upcloud/request" |
| 13 | + upsvc "github.com/UpCloudLtd/upcloud-go-api/v6/upcloud/service" |
| 14 | +) |
| 15 | + |
| 16 | +type UpCloudClient struct { |
| 17 | + upsvc.Storage |
| 18 | + |
| 19 | + servers sync.Map |
| 20 | +} |
| 21 | + |
| 22 | +func (u *UpCloudClient) StoreServer(s *upcloud.ServerDetails) { |
| 23 | + u.servers.LoadOrStore(s.UUID, s) |
| 24 | +} |
| 25 | + |
| 26 | +func (u *UpCloudClient) getServer(id string) *upcloud.ServerDetails { |
| 27 | + if s, ok := u.servers.Load(id); ok { |
| 28 | + return s.(*upcloud.ServerDetails) |
| 29 | + } |
| 30 | + return nil |
| 31 | +} |
| 32 | + |
| 33 | +func (u *UpCloudClient) WaitForServerState(ctx context.Context, r *request.WaitForServerStateRequest) (*upcloud.ServerDetails, error) { |
| 34 | + s, _ := u.GetServerDetails(ctx, &request.GetServerDetailsRequest{ |
| 35 | + UUID: r.UUID, |
| 36 | + }) |
| 37 | + return s, nil |
| 38 | +} |
| 39 | + |
| 40 | +func (u *UpCloudClient) GetServers(ctx context.Context) (*upcloud.Servers, error) { |
| 41 | + s := []upcloud.Server{} |
| 42 | + u.servers.Range(func(key, value any) bool { |
| 43 | + if d, ok := value.(*upcloud.ServerDetails); ok { |
| 44 | + s = append(s, d.Server) |
| 45 | + } |
| 46 | + return true |
| 47 | + }) |
| 48 | + return &upcloud.Servers{Servers: s}, nil |
| 49 | +} |
| 50 | + |
| 51 | +func (u *UpCloudClient) GetServerDetails(ctx context.Context, r *request.GetServerDetailsRequest) (*upcloud.ServerDetails, error) { |
| 52 | + if s := u.getServer(r.UUID); s != nil { |
| 53 | + return s, nil |
| 54 | + } |
| 55 | + return nil, fmt.Errorf("server '%s' not found", r.UUID) |
| 56 | +} |
| 57 | + |
| 58 | +func (u *UpCloudClient) AttachStorage(ctx context.Context, r *request.AttachStorageRequest) (*upcloud.ServerDetails, error) { |
| 59 | + server := u.getServer(r.ServerUUID) |
| 60 | + if server == nil { |
| 61 | + return server, errors.New("server not found") |
| 62 | + } |
| 63 | + if server.State != upcloud.ServerStateStarted { |
| 64 | + return nil, fmt.Errorf("server %s state is %s", r.ServerUUID, server.State) |
| 65 | + } |
| 66 | + server.State = upcloud.ServerStateMaintenance |
| 67 | + u.StoreServer(server) |
| 68 | + time.Sleep(time.Duration(rand.Intn(200)+100) * time.Millisecond) //nolint:gosec // using weak random number doesn't affect the result. |
| 69 | + server.State = upcloud.ServerStateStarted |
| 70 | + if server.StorageDevices == nil { |
| 71 | + server.StorageDevices = make(upcloud.ServerStorageDeviceSlice, 0) |
| 72 | + } |
| 73 | + server.StorageDevices = append(server.StorageDevices, upcloud.ServerStorageDevice{ |
| 74 | + Address: fmt.Sprintf("%s:%d", r.Address, len(server.StorageDevices)+1), |
| 75 | + UUID: r.StorageUUID, |
| 76 | + Size: 10, |
| 77 | + }) |
| 78 | + u.StoreServer(server) |
| 79 | + |
| 80 | + return u.getServer(r.ServerUUID), nil |
| 81 | +} |
| 82 | + |
| 83 | +func (u *UpCloudClient) DetachStorage(ctx context.Context, r *request.DetachStorageRequest) (*upcloud.ServerDetails, error) { |
| 84 | + server := u.getServer(r.ServerUUID) |
| 85 | + if server == nil { |
| 86 | + return server, fmt.Errorf("server %s not found", r.ServerUUID) |
| 87 | + } |
| 88 | + if server.State != upcloud.ServerStateStarted { |
| 89 | + return nil, fmt.Errorf("server %s state is %s", r.ServerUUID, server.State) |
| 90 | + } |
| 91 | + server.State = upcloud.ServerStateMaintenance |
| 92 | + u.StoreServer(server) |
| 93 | + time.Sleep(time.Duration(rand.Intn(200)+100) * time.Millisecond) //nolint:gosec // using weak random number doesn't affect the result. |
| 94 | + server = u.getServer(r.ServerUUID) |
| 95 | + server.State = upcloud.ServerStateStarted |
| 96 | + if len(server.StorageDevices) > 0 { |
| 97 | + storage := make([]upcloud.ServerStorageDevice, 0) |
| 98 | + for i := range server.StorageDevices { |
| 99 | + if server.StorageDevices[i].Address != r.Address { |
| 100 | + storage = append(storage, server.StorageDevices[i]) |
| 101 | + } |
| 102 | + } |
| 103 | + server.StorageDevices = storage |
| 104 | + } |
| 105 | + u.StoreServer(server) |
| 106 | + |
| 107 | + return server, nil |
| 108 | +} |
0 commit comments