|
| 1 | +package mock |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + |
| 7 | + "github.com/stackitcloud/machine-controller-manager-provider-stackit/pkg/client" |
| 8 | + api "github.com/stackitcloud/machine-controller-manager-provider-stackit/pkg/provider/apis" |
| 9 | +) |
| 10 | + |
| 11 | +// StackitClient is a mock implementation of StackitClient for testing |
| 12 | +// Note: Single-tenant design - each client is bound to one set of credentials |
| 13 | +type StackitClient struct { |
| 14 | + CreateServerFunc func(ctx context.Context, projectID, region string, req *client.CreateServerRequest) (*client.Server, error) |
| 15 | + GetServerFunc func(ctx context.Context, projectID, region, serverID string) (*client.Server, error) |
| 16 | + DeleteServerFunc func(ctx context.Context, projectID, region, serverID string) error |
| 17 | + ListServersFunc func(ctx context.Context, projectID, region string, labelSelector map[string]string) ([]*client.Server, error) |
| 18 | + GetNICsFunc func(ctx context.Context, projectID, region, serverID string) ([]*client.NIC, error) |
| 19 | + UpdateNICFunc func(ctx context.Context, projectID, region, networkID, nicID string, allowedAddresses []string) (*client.NIC, error) |
| 20 | +} |
| 21 | + |
| 22 | +func (m *StackitClient) CreateServer(ctx context.Context, projectID, region string, req *client.CreateServerRequest) (*client.Server, error) { |
| 23 | + if m.CreateServerFunc != nil { |
| 24 | + return m.CreateServerFunc(ctx, projectID, region, req) |
| 25 | + } |
| 26 | + return &client.Server{ |
| 27 | + ID: "550e8400-e29b-41d4-a716-446655440000", |
| 28 | + Name: req.Name, |
| 29 | + Status: "CREATING", |
| 30 | + }, nil |
| 31 | +} |
| 32 | + |
| 33 | +func (m *StackitClient) GetServer(ctx context.Context, projectID, region, serverID string) (*client.Server, error) { |
| 34 | + if m.GetServerFunc != nil { |
| 35 | + return m.GetServerFunc(ctx, projectID, region, serverID) |
| 36 | + } |
| 37 | + return &client.Server{ |
| 38 | + ID: serverID, |
| 39 | + Name: "test-machine", |
| 40 | + Status: "ACTIVE", |
| 41 | + }, nil |
| 42 | +} |
| 43 | + |
| 44 | +func (m *StackitClient) DeleteServer(ctx context.Context, projectID, region, serverID string) error { |
| 45 | + if m.DeleteServerFunc != nil { |
| 46 | + return m.DeleteServerFunc(ctx, projectID, region, serverID) |
| 47 | + } |
| 48 | + return nil |
| 49 | +} |
| 50 | + |
| 51 | +func (m *StackitClient) ListServers(ctx context.Context, projectID, region string, labelSelector map[string]string) ([]*client.Server, error) { |
| 52 | + if m.ListServersFunc != nil { |
| 53 | + return m.ListServersFunc(ctx, projectID, region, labelSelector) |
| 54 | + } |
| 55 | + return []*client.Server{}, nil |
| 56 | +} |
| 57 | + |
| 58 | +func (m *StackitClient) GetNICsForServer(ctx context.Context, projectID, region, serverID string) ([]*client.NIC, error) { |
| 59 | + if m.GetNICsFunc != nil { |
| 60 | + return m.GetNICsFunc(ctx, projectID, region, serverID) |
| 61 | + } |
| 62 | + return []*client.NIC{}, nil |
| 63 | +} |
| 64 | + |
| 65 | +func (m *StackitClient) UpdateNIC(ctx context.Context, projectID, region, networkID, nicID string, allowedAddresses []string) (*client.NIC, error) { |
| 66 | + if m.UpdateNICFunc != nil { |
| 67 | + return m.UpdateNICFunc(ctx, projectID, region, networkID, nicID, allowedAddresses) |
| 68 | + } |
| 69 | + return &client.NIC{}, nil |
| 70 | +} |
| 71 | + |
| 72 | +// UpdateNIC updates a network interface |
| 73 | + |
| 74 | +// encodeProviderSpec is a helper function to encode ProviderSpec for tests |
| 75 | +func EncodeProviderSpec(spec *api.ProviderSpec) ([]byte, error) { |
| 76 | + return json.Marshal(spec) |
| 77 | +} |
0 commit comments