117 lines
3.0 KiB
Go
117 lines
3.0 KiB
Go
package virtigolib
|
|
|
|
// makes a Cluster interface which is:
|
|
|
|
// a number of hypervisors (the physical machines)
|
|
// a list of the virtual machines
|
|
// functions to start, stop, etc virtual machines
|
|
|
|
// virtual machines are often called droplets to make it easier to understand
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"go.wit.com/lib/protobuf/virtbuf"
|
|
)
|
|
|
|
var ctx context.Context
|
|
var myClient cloudAPI
|
|
|
|
// CloudManager is a concrete implementation of the Manager interface.
|
|
type CloudManager struct {
|
|
// client represents a hypothetical API client for interacting with the cloud.
|
|
client cloudAPI
|
|
cluster *virtbuf.Cluster
|
|
}
|
|
|
|
// cloudAPIt defines the methods required from the API client.
|
|
// This is useful if you want to mock this client for testing.
|
|
type cloudAPI interface {
|
|
GetDropletByName(name string) *virtbuf.Droplet
|
|
|
|
StartCluster(clusterID string) error
|
|
StopCluster(clusterID string) error
|
|
ListDroplets() ([]*virtbuf.Droplet, error)
|
|
|
|
GetClusterStatus(clusterID string) (string, error)
|
|
}
|
|
|
|
func NewCloud() *CloudManager {
|
|
// client := virtbuf.NewRealCloudAPIClient()
|
|
// clusterManager := NewCloudManager(myClient)
|
|
newCloudManager := &CloudManager{client: myClient}
|
|
|
|
ctx = context.Background()
|
|
|
|
return newCloudManager
|
|
}
|
|
|
|
// NewCloudManager creates a new CloudManager with the provided API client.
|
|
// func NewCloudManager(client cloudAPI) *CloudManager {
|
|
// return &CloudManager{client: client}
|
|
// }
|
|
|
|
// FindByName retrieves a cluster by name.
|
|
func (m *CloudManager) FindDropletByName(name string) (*virtbuf.Droplet, error) {
|
|
if m.cluster == nil {
|
|
return nil, nil
|
|
}
|
|
d := m.cluster.FindDropletByName(name)
|
|
return d, nil
|
|
}
|
|
|
|
// Start initiates the startup process for the specified cluster.
|
|
func (m *CloudManager) Start(cluster *virtbuf.Cluster) error {
|
|
if cluster == nil {
|
|
return errors.New("cluster cannot be nil")
|
|
}
|
|
/*
|
|
err := m.client.StartCluster(ctx, cluster.Id)
|
|
if err != nil {
|
|
return fmt.Errorf("error starting cluster %q: %w", cluster.Id, err)
|
|
}
|
|
*/
|
|
return nil
|
|
}
|
|
|
|
// Stop halts the specified cluster.
|
|
func (m *CloudManager) Stop(cluster *virtbuf.Cluster) error {
|
|
if cluster == nil {
|
|
return errors.New("cluster cannot be nil")
|
|
}
|
|
/*
|
|
err := m.client.StopCluster(ctx, cluster.Id)
|
|
if err != nil {
|
|
return fmt.Errorf("error stopping cluster %q: %w", cluster.Id, err)
|
|
}
|
|
*/
|
|
return nil
|
|
}
|
|
|
|
// List retrieves all available clusters.
|
|
func (m *CloudManager) List() ([]*virtbuf.Cluster, error) {
|
|
/*
|
|
clusters, err := m.client.ListClusters(ctx)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error listing clusters: %w", err)
|
|
}
|
|
return clusters, nil
|
|
*/
|
|
return nil, errors.New("List not done yet")
|
|
}
|
|
|
|
// Status checks the current status of a specified cluster.
|
|
func (m *CloudManager) Status(cluster *virtbuf.Cluster) (string, error) {
|
|
if cluster == nil {
|
|
return "", errors.New("cluster cannot be nil")
|
|
}
|
|
/*
|
|
status, err := m.client.GetClusterStatus(ctx, cluster.Id)
|
|
if err != nil {
|
|
return "", fmt.Errorf("error getting status of cluster %q: %w", cluster.Id, err)
|
|
}
|
|
*/
|
|
return "", nil
|
|
}
|