github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/container/containerv2/workers.go (about)

     1  package containerv2
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/IBM-Cloud/bluemix-go/client"
     7  )
     8  
     9  //Worker ...
    10  type Worker struct {
    11  	Billing           string `json:"billing,omitempty"`
    12  	HostID            string `json:"dedicatedHostId,omitempty"`
    13  	HostPoolID        string `json:"dedicatedHostPoolId,omitempty"`
    14  	Flavor            string `json:"flavor"`
    15  	ID                string `json:"id"`
    16  	KubeVersion       KubeDetails
    17  	Location          string          `json:"location"`
    18  	PoolID            string          `json:"poolid"`
    19  	PoolName          string          `json:"poolName"`
    20  	LifeCycle         WorkerLifeCycle `json:"lifecycle"`
    21  	Health            HealthStatus    `json:"health"`
    22  	NetworkInterfaces []Network       `json:"networkInterfaces"`
    23  }
    24  
    25  type KubeDetails struct {
    26  	Actual    string `json:"actual"`
    27  	Desired   string `json:"desired"`
    28  	Eos       string `json:"eos"`
    29  	MasterEOS string `json:"masterEos"`
    30  	Target    string `json:"target"`
    31  }
    32  type HealthStatus struct {
    33  	Message string `json:"message"`
    34  	State   string `json:"state"`
    35  }
    36  type WorkerLifeCycle struct {
    37  	ReasonForDelete    string `json:"reasonForDelete"`
    38  	ActualState        string `json:"actualState"`
    39  	DesiredState       string `json:"desiredState"`
    40  	Message            string `json:"message"`
    41  	MessageDate        string `json:"messageDate"`
    42  	MessageDetails     string `json:"messageDetails"`
    43  	MessageDetailsDate string `json:"messageDetailsDate"`
    44  	PendingOperation   string `json:"pendingOperation"`
    45  }
    46  
    47  type Network struct {
    48  	Cidr      string `json:"cidr"`
    49  	IpAddress string `json:"ipAddress"`
    50  	Primary   bool   `json:"primary"`
    51  	SubnetID  string `json:"subnetID"`
    52  }
    53  
    54  type ReplaceWorker struct {
    55  	ClusterIDOrName string `json:"cluster"`
    56  	Update          bool   `json:"update"`
    57  	WorkerID        string `json:"workerID"`
    58  }
    59  
    60  type VoulemeAttachments struct {
    61  	VolumeAttachments []VoulemeAttachment `json:"volume_attachments"`
    62  }
    63  
    64  type VoulemeAttachment struct {
    65  	Id     string     `json:"id"`
    66  	Volume Volume     `json:"volume"`
    67  	Device DeviceInfo `json:"device"`
    68  	Name   string     `json:"name"`
    69  	Status string     `json:"status"`
    70  	Type   string     `json:"type"`
    71  }
    72  
    73  type Volume struct {
    74  	Name string `json:"name"`
    75  	Id   string `json:"id"`
    76  }
    77  
    78  type DeviceInfo struct {
    79  	Id string `json:"id"`
    80  }
    81  
    82  type VolumeRequest struct {
    83  	Cluster            string `json:"cluster"`
    84  	VolumeAttachmentID string `json:"volumeAttachmentID,omitempty"`
    85  	VolumeID           string `json:"volumeID,omitempty"`
    86  	Worker             string `json:"worker"`
    87  }
    88  
    89  //Workers ...
    90  type Workers interface {
    91  	ListByWorkerPool(clusterIDOrName, workerPoolIDOrName string, showDeleted bool, target ClusterTargetHeader) ([]Worker, error)
    92  	ListWorkers(clusterIDOrName string, showDeleted bool, target ClusterTargetHeader) ([]Worker, error)
    93  	Get(clusterIDOrName, workerID string, target ClusterTargetHeader) (Worker, error)
    94  	ReplaceWokerNode(clusterIDOrName, workerID string, target ClusterTargetHeader) (string, error)
    95  	ListStorageAttachemnts(clusterIDOrName, workerID string, target ClusterTargetHeader) (VoulemeAttachments, error)
    96  	GetStorageAttachment(clusterIDOrName, workerID, volumeAttachmentID string, target ClusterTargetHeader) (VoulemeAttachment, error)
    97  	CreateStorageAttachment(payload VolumeRequest, target ClusterTargetHeader) (VoulemeAttachment, error)
    98  	DeleteStorageAttachment(payload VolumeRequest, target ClusterTargetHeader) (string, error)
    99  }
   100  
   101  type worker struct {
   102  	client *client.Client
   103  }
   104  
   105  func newWorkerAPI(c *client.Client) Workers {
   106  	return &worker{
   107  		client: c,
   108  	}
   109  }
   110  
   111  //ListByWorkerPool ...
   112  func (r *worker) ListByWorkerPool(clusterIDOrName, workerPoolIDOrName string, showDeleted bool, target ClusterTargetHeader) ([]Worker, error) {
   113  	rawURL := fmt.Sprintf("/v2/vpc/getWorkers?cluster=%s&showDeleted=%t", clusterIDOrName, showDeleted)
   114  	if len(workerPoolIDOrName) > 0 {
   115  		rawURL += "&pool=" + workerPoolIDOrName
   116  	}
   117  	workers := []Worker{}
   118  	_, err := r.client.Get(rawURL, &workers, target.ToMap())
   119  	if err != nil {
   120  		return nil, err
   121  	}
   122  	return workers, err
   123  }
   124  
   125  //ListWorkers ...
   126  func (r *worker) ListWorkers(clusterIDOrName string, showDeleted bool, target ClusterTargetHeader) ([]Worker, error) {
   127  	rawURL := fmt.Sprintf("/v2/vpc/getWorkers?cluster=%s&showDeleted=%t", clusterIDOrName, showDeleted)
   128  	workers := []Worker{}
   129  	_, err := r.client.Get(rawURL, &workers, target.ToMap())
   130  	if err != nil {
   131  		return nil, err
   132  	}
   133  	return workers, err
   134  }
   135  
   136  //Get ...
   137  func (r *worker) Get(clusterIDOrName, workerID string, target ClusterTargetHeader) (Worker, error) {
   138  	rawURL := fmt.Sprintf("/v2/vpc/getWorker?cluster=%s&worker=%s", clusterIDOrName, workerID)
   139  	worker := Worker{}
   140  	_, err := r.client.Get(rawURL, &worker, target.ToMap())
   141  	if err != nil {
   142  		return worker, err
   143  	}
   144  	return worker, err
   145  }
   146  
   147  func (r *worker) ReplaceWokerNode(clusterIDOrName, workerID string, target ClusterTargetHeader) (string, error) {
   148  	payload := ReplaceWorker{
   149  		ClusterIDOrName: clusterIDOrName,
   150  		WorkerID:        workerID,
   151  		Update:          true,
   152  	}
   153  	var response string
   154  	_, err := r.client.Post("/v2/vpc/replaceWorker", payload, &response, target.ToMap())
   155  	if err != nil {
   156  		return response, err
   157  	}
   158  	return response, err
   159  }
   160  
   161  // ListStorageAttachemnts returns list of attached storage blaocks to a worker node
   162  func (r *worker) ListStorageAttachemnts(clusterIDOrName, workerID string, target ClusterTargetHeader) (VoulemeAttachments, error) {
   163  	rawURL := fmt.Sprintf("/v2/storage/getAttachments?cluster=%s&worker=%s", clusterIDOrName, workerID)
   164  	workerAttachements := VoulemeAttachments{}
   165  	_, err := r.client.Get(rawURL, &workerAttachements, target.ToMap())
   166  	if err != nil {
   167  		return workerAttachements, err
   168  	}
   169  	return workerAttachements, err
   170  
   171  }
   172  
   173  func (r *worker) GetStorageAttachment(clusterIDOrName, workerID, volumeAttachmentID string, target ClusterTargetHeader) (VoulemeAttachment, error) {
   174  	rawURL := fmt.Sprintf("/v2/storage/getAttachment?cluster=%s&worker=%s&volumeAttachmentID=%s", clusterIDOrName, workerID, volumeAttachmentID)
   175  	workerAttachement := VoulemeAttachment{}
   176  	_, err := r.client.Get(rawURL, &workerAttachement, target.ToMap())
   177  	if err != nil {
   178  		return workerAttachement, err
   179  	}
   180  	return workerAttachement, err
   181  
   182  }
   183  
   184  func (r *worker) CreateStorageAttachment(payload VolumeRequest, target ClusterTargetHeader) (VoulemeAttachment, error) {
   185  	response := VoulemeAttachment{}
   186  	_, err := r.client.Post("/v2/storage/createAttachment", payload, &response, target.ToMap())
   187  	if err != nil {
   188  		return response, err
   189  	}
   190  	return response, err
   191  }
   192  
   193  func (r *worker) DeleteStorageAttachment(payload VolumeRequest, target ClusterTargetHeader) (string, error) {
   194  	var response string
   195  	_, err := r.client.Post("/v2/storage/deleteAttachment", payload, &response, target.ToMap())
   196  	if err != nil {
   197  		return response, err
   198  	}
   199  	return response, err
   200  }