github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cluster/fake/fake.go (about)

     1  package fake
     2  
     3  import (
     4  	"github.com/jenkins-x/jx-logging/pkg/log"
     5  	"github.com/olli-ai/jx/v2/pkg/cluster"
     6  	"github.com/olli-ai/jx/v2/pkg/util"
     7  )
     8  
     9  // Client a fake implementation of the cluster client
    10  type Client struct {
    11  	Clusters []*cluster.Cluster
    12  }
    13  
    14  // verify we implement the interface
    15  var _ cluster.Client = &Client{}
    16  
    17  // NewClient create a new fake client for testing
    18  func NewClient(clusters []*cluster.Cluster) *Client {
    19  	return &Client{
    20  		Clusters: clusters,
    21  	}
    22  }
    23  
    24  // List lists the clusters
    25  func (c *Client) List() ([]*cluster.Cluster, error) {
    26  	return c.Clusters, nil
    27  }
    28  
    29  // ListFilter lists the clusters with a filter
    30  func (c *Client) ListFilter(labels map[string]string) ([]*cluster.Cluster, error) {
    31  	return cluster.ListFilter(c, labels)
    32  }
    33  
    34  // Connect connects to a cluster
    35  func (c *Client) Connect(cluster *cluster.Cluster) error {
    36  	log.Logger().Infof("fake cluster connecting to cluster: %s", util.ColorInfo(cluster.Name))
    37  	return nil
    38  }
    39  
    40  // String return the string representation
    41  func (c *Client) String() string {
    42  	return "Client"
    43  }
    44  
    45  // Get looks up a cluster by name
    46  func (c *Client) Get(name string) (*cluster.Cluster, error) {
    47  	return cluster.GetCluster(c, name)
    48  }
    49  
    50  // Delete should delete the cluster from the clusters list
    51  func (c *Client) Delete(cluster *cluster.Cluster) error {
    52  	for i, v := range c.Clusters {
    53  		if v.Name == cluster.Name {
    54  			c.Clusters = append(c.Clusters[:i], c.Clusters[i+1:]...)
    55  		}
    56  	}
    57  	return nil
    58  }
    59  
    60  // SetClusterLabels labels the given cluster
    61  func (c *Client) SetClusterLabels(cluster *cluster.Cluster, labels map[string]string) error {
    62  	cluster.Labels = labels
    63  	return nil
    64  }