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

     1  package containerv1
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/IBM-Cloud/bluemix-go/client"
     7  )
     8  
     9  // WorkerPoolConfig common worker pool data
    10  type WorkerPoolConfig struct {
    11  	Name            string            `json:"name" binding:"required"`
    12  	Size            int               `json:"sizePerZone" binding:"required"`
    13  	MachineType     string            `json:"machineType" binding:"required"`
    14  	Isolation       string            `json:"isolation"`
    15  	Labels          map[string]string `json:"labels"`
    16  	OperatingSystem string            `json:"operatingSystem,omitempty"`
    17  	Entitlement     string            `json:"entitlement"`
    18  }
    19  
    20  // WorkerPoolRequest provides worker pool data
    21  // swagger:model
    22  type WorkerPoolRequest struct {
    23  	WorkerPoolConfig
    24  	DiskEncryption bool             `json:"diskEncryption" description:"true or false to use encryption for the secondary disk"`
    25  	Zones          []WorkerPoolZone `json:"zones"`
    26  }
    27  
    28  // WorkerPoolPatchRequest provides attributes to patch update worker pool
    29  // swagger:model
    30  type WorkerPoolPatchRequest struct {
    31  	Size            int               `json:"sizePerZone"`
    32  	Labels          map[string]string `json:"labels"`
    33  	ReasonForResize string            `json:"reasonForResize"`
    34  	State           string            `json:"state"`
    35  }
    36  
    37  // WorkerPoolResponse provides worker pool data
    38  // swagger:model
    39  type WorkerPoolResponse struct {
    40  	WorkerPoolConfig
    41  	ID               string                  `json:"id" binding:"required"`
    42  	Region           string                  `json:"region" binding:"required"`
    43  	State            string                  `json:"state"`
    44  	ReasonForDelete  string                  `json:"reasonForDelete"`
    45  	IsBalanced       bool                    `json:"isBalanced"`
    46  	AutoscaleEnabled bool                    `json:"autoscaleEnabled,omitempty"`
    47  	Zones            WorkerPoolZoneResponses `json:"zones"`
    48  }
    49  
    50  // WorkerPoolResponses sorts WorkerPoolResponse by ID.
    51  // swagger:model
    52  type WorkerPoolResponses []WorkerPoolResponse
    53  
    54  // WorkerPoolZoneNetwork holds network configuration for a zone
    55  type WorkerPoolZoneNetwork struct {
    56  	PrivateVLAN string `json:"privateVlan" binding:"required"`
    57  	PublicVLAN  string `json:"publicVlan"`
    58  }
    59  
    60  // WorkerPoolZone provides zone data
    61  // swagger:model
    62  type WorkerPoolZone struct {
    63  	WorkerPoolZoneNetwork
    64  	ID string `json:"id" binding:"required"`
    65  }
    66  
    67  // WorkerPoolZonePatchRequest updates worker pool zone data
    68  // swagger:model
    69  type WorkerPoolZonePatchRequest struct {
    70  	WorkerPoolZoneNetwork
    71  }
    72  
    73  // WorkerPoolZoneResponse response contents for zone
    74  // swagger:model
    75  type WorkerPoolZoneResponse struct {
    76  	WorkerPoolZone
    77  	WorkerCount int `json:"workerCount"`
    78  }
    79  
    80  // WorkerPoolZoneResponses sorts WorkerPoolZoneResponse by ID.
    81  // swagger:model
    82  type WorkerPoolZoneResponses []WorkerPoolZoneResponse
    83  
    84  //Workers ...
    85  type WorkerPool interface {
    86  	CreateWorkerPool(clusterNameOrID string, workerPoolReq WorkerPoolRequest, target ClusterTargetHeader) (WorkerPoolResponse, error)
    87  	ResizeWorkerPool(clusterNameOrID, workerPoolNameOrID string, size int, target ClusterTargetHeader) error
    88  	UpdateLabelsWorkerPool(clusterNameOrID, workerPoolNameOrID string, labels map[string]string, target ClusterTargetHeader) error
    89  	PatchWorkerPool(clusterNameOrID, workerPoolNameOrID, state string, target ClusterTargetHeader) error
    90  	DeleteWorkerPool(clusterNameOrID string, workerPoolNameOrID string, target ClusterTargetHeader) error
    91  	ListWorkerPools(clusterNameOrID string, target ClusterTargetHeader) ([]WorkerPoolResponse, error)
    92  	GetWorkerPool(clusterNameOrID, workerPoolNameOrID string, target ClusterTargetHeader) (WorkerPoolResponse, error)
    93  	AddZone(clusterNameOrID string, poolID string, workerPoolZone WorkerPoolZone, target ClusterTargetHeader) error
    94  	RemoveZone(clusterNameOrID, zone, poolID string, target ClusterTargetHeader) error
    95  	UpdateZoneNetwork(clusterNameOrID, zone, poolID, privateVlan, publicVlan string, target ClusterTargetHeader) error
    96  }
    97  
    98  type workerpool struct {
    99  	client *client.Client
   100  }
   101  
   102  func newWorkerPoolAPI(c *client.Client) WorkerPool {
   103  	return &workerpool{
   104  		client: c,
   105  	}
   106  }
   107  
   108  // CreateWorkerPool calls the API to create a worker pool
   109  func (w *workerpool) CreateWorkerPool(clusterNameOrID string, workerPoolReq WorkerPoolRequest, target ClusterTargetHeader) (WorkerPoolResponse, error) {
   110  	var successV WorkerPoolResponse
   111  	_, err := w.client.Post(fmt.Sprintf("/v1/clusters/%s/workerpools", clusterNameOrID), workerPoolReq, &successV, target.ToMap())
   112  	return successV, err
   113  }
   114  
   115  // ResizeWorkerPool calls the API to resize a worker
   116  func (w *workerpool) PatchWorkerPool(clusterNameOrID, workerPoolNameOrID, state string, target ClusterTargetHeader) error {
   117  	requestBody := WorkerPoolPatchRequest{
   118  		State: state,
   119  	}
   120  	_, err := w.client.Patch(fmt.Sprintf("/v1/clusters/%s/workerpools/%s", clusterNameOrID, workerPoolNameOrID), requestBody, nil, target.ToMap())
   121  	return err
   122  }
   123  
   124  // ResizeWorkerPool calls the API to resize a worker
   125  func (w *workerpool) ResizeWorkerPool(clusterNameOrID, workerPoolNameOrID string, size int, target ClusterTargetHeader) error {
   126  	requestBody := WorkerPoolPatchRequest{
   127  		State: "resizing",
   128  		Size:  size,
   129  	}
   130  	_, err := w.client.Patch(fmt.Sprintf("/v1/clusters/%s/workerpools/%s", clusterNameOrID, workerPoolNameOrID), requestBody, nil, target.ToMap())
   131  	return err
   132  }
   133  
   134  // UpdateLabelsWorkerPool calls the API to resize a worker with the labels option
   135  func (w *workerpool) UpdateLabelsWorkerPool(clusterNameOrID, workerPoolNameOrID string, labels map[string]string, target ClusterTargetHeader) error {
   136  	requestBody := WorkerPoolPatchRequest{
   137  		State:  "labels",
   138  		Labels: labels,
   139  	}
   140  	_, err := w.client.Patch(fmt.Sprintf("/v1/clusters/%s/workerpools/%s", clusterNameOrID, workerPoolNameOrID), requestBody, nil, target.ToMap())
   141  	return err
   142  }
   143  
   144  // DeleteWorkerPool calls the API to remove a worker pool
   145  func (w *workerpool) DeleteWorkerPool(clusterNameOrID string, workerPoolNameOrID string, target ClusterTargetHeader) error {
   146  	// Make the request, don't care about return value
   147  	_, err := w.client.Delete(fmt.Sprintf("/v1/clusters/%s/workerpools/%s", clusterNameOrID, workerPoolNameOrID), target.ToMap())
   148  	return err
   149  }
   150  
   151  // ListWorkerPools calls the API to list all worker pools for a cluster
   152  func (w *workerpool) ListWorkerPools(clusterNameOrID string, target ClusterTargetHeader) ([]WorkerPoolResponse, error) {
   153  	var successV []WorkerPoolResponse
   154  	_, err := w.client.Get(fmt.Sprintf("/v1/clusters/%s/workerpools", clusterNameOrID), &successV, target.ToMap())
   155  	return successV, err
   156  }
   157  
   158  // GetWorkerPool calls the API to get a worker pool
   159  func (w *workerpool) GetWorkerPool(clusterNameOrID, workerPoolNameOrID string, target ClusterTargetHeader) (WorkerPoolResponse, error) {
   160  	var successV WorkerPoolResponse
   161  	_, err := w.client.Get(fmt.Sprintf("/v1/clusters/%s/workerpools/%s", clusterNameOrID, workerPoolNameOrID), &successV, target.ToMap())
   162  	return successV, err
   163  }
   164  
   165  // AddZone calls the API to add a zone to a cluster and worker pool
   166  func (w *workerpool) AddZone(clusterNameOrID string, poolID string, workerPoolZone WorkerPoolZone, target ClusterTargetHeader) error {
   167  	// Make the request, don't care about return value
   168  	_, err := w.client.Post(fmt.Sprintf("/v1/clusters/%s/workerpools/%s/zones", clusterNameOrID, poolID), workerPoolZone, nil, target.ToMap())
   169  	return err
   170  }
   171  
   172  // RemoveZone calls the API to remove a zone from a worker pool in a cluster
   173  func (w *workerpool) RemoveZone(clusterNameOrID, zone, poolID string, target ClusterTargetHeader) error {
   174  	_, err := w.client.Delete(fmt.Sprintf("/v1/clusters/%s/workerpools/%s/zones/%s", clusterNameOrID, poolID, zone), target.ToMap())
   175  	return err
   176  }
   177  
   178  // UpdateZoneNetwork calls the API to update a zone's network
   179  func (w *workerpool) UpdateZoneNetwork(clusterNameOrID, zone, poolID, privateVlan, publicVlan string, target ClusterTargetHeader) error {
   180  	body := WorkerPoolZoneNetwork{
   181  		PrivateVLAN: privateVlan,
   182  		PublicVLAN:  publicVlan,
   183  	}
   184  	_, err := w.client.Patch(fmt.Sprintf("/v1/clusters/%s/workerpools/%s/zones/%s", clusterNameOrID, poolID, zone), body, nil, target.ToMap())
   185  	return err
   186  }