placement_groups.go raw

   1  package linodego
   2  
   3  import "context"
   4  
   5  // PlacementGroupType is an enum that determines the affinity policy
   6  // for Linodes in a placement group.
   7  type PlacementGroupType string
   8  
   9  const (
  10  	PlacementGroupTypeAntiAffinityLocal PlacementGroupType = "anti_affinity:local"
  11  )
  12  
  13  // PlacementGroupPolicy is an enum for the policy that determines whether a
  14  // Linode can be assigned to a Placement Group.
  15  type PlacementGroupPolicy string
  16  
  17  const (
  18  	PlacementGroupPolicyStrict   PlacementGroupPolicy = "strict"
  19  	PlacementGroupPolicyFlexible PlacementGroupPolicy = "flexible"
  20  )
  21  
  22  // PlacementGroupMember represents a single Linode assigned to a
  23  // placement group.
  24  type PlacementGroupMember struct {
  25  	LinodeID    int  `json:"linode_id"`
  26  	IsCompliant bool `json:"is_compliant"`
  27  }
  28  
  29  // PlacementGroup represents a Linode placement group.
  30  type PlacementGroup struct {
  31  	ID                   int                       `json:"id"`
  32  	Label                string                    `json:"label"`
  33  	Region               string                    `json:"region"`
  34  	PlacementGroupType   PlacementGroupType        `json:"placement_group_type"`
  35  	PlacementGroupPolicy PlacementGroupPolicy      `json:"placement_group_policy"`
  36  	IsCompliant          bool                      `json:"is_compliant"`
  37  	Members              []PlacementGroupMember    `json:"members"`
  38  	Migrations           *PlacementGroupMigrations `json:"migrations"`
  39  }
  40  
  41  // PlacementGroupMigrations represent the instances that are being migrated to or from the placement group.
  42  type PlacementGroupMigrations struct {
  43  	Inbound  []PlacementGroupMigrationInstance `json:"inbound"`
  44  	Outbound []PlacementGroupMigrationInstance `json:"outbound"`
  45  }
  46  
  47  // PlacementGroupMigrationInstance represents the unique identifier for a compute instance being migrated to/from the placement group.
  48  type PlacementGroupMigrationInstance struct {
  49  	LinodeID int `json:"linode_id"`
  50  }
  51  
  52  // PlacementGroupCreateOptions represents the options to use
  53  // when creating a placement group.
  54  type PlacementGroupCreateOptions struct {
  55  	Label                string               `json:"label"`
  56  	Region               string               `json:"region"`
  57  	PlacementGroupType   PlacementGroupType   `json:"placement_group_type"`
  58  	PlacementGroupPolicy PlacementGroupPolicy `json:"placement_group_policy"`
  59  }
  60  
  61  // PlacementGroupUpdateOptions represents the options to use
  62  // when updating a placement group.
  63  type PlacementGroupUpdateOptions struct {
  64  	Label string `json:"label,omitempty"`
  65  }
  66  
  67  // PlacementGroupAssignOptions represents options used when
  68  // assigning Linodes to a placement group.
  69  type PlacementGroupAssignOptions struct {
  70  	Linodes       []int `json:"linodes"`
  71  	CompliantOnly *bool `json:"compliant_only,omitempty"`
  72  }
  73  
  74  // PlacementGroupUnAssignOptions represents options used when
  75  // unassigning Linodes from a placement group.
  76  type PlacementGroupUnAssignOptions struct {
  77  	Linodes []int `json:"linodes"`
  78  }
  79  
  80  // ListPlacementGroups lists placement groups under the current account
  81  // matching the given list options.
  82  func (c *Client) ListPlacementGroups(
  83  	ctx context.Context,
  84  	options *ListOptions,
  85  ) ([]PlacementGroup, error) {
  86  	return getPaginatedResults[PlacementGroup](
  87  		ctx,
  88  		c,
  89  		"placement/groups",
  90  		options,
  91  	)
  92  }
  93  
  94  // GetPlacementGroup gets a placement group with the specified ID.
  95  func (c *Client) GetPlacementGroup(
  96  	ctx context.Context,
  97  	id int,
  98  ) (*PlacementGroup, error) {
  99  	return doGETRequest[PlacementGroup](
 100  		ctx,
 101  		c,
 102  		formatAPIPath("placement/groups/%d", id),
 103  	)
 104  }
 105  
 106  // CreatePlacementGroup creates a placement group with the specified options.
 107  func (c *Client) CreatePlacementGroup(
 108  	ctx context.Context,
 109  	options PlacementGroupCreateOptions,
 110  ) (*PlacementGroup, error) {
 111  	return doPOSTRequest[PlacementGroup](
 112  		ctx,
 113  		c,
 114  		"placement/groups",
 115  		options,
 116  	)
 117  }
 118  
 119  // UpdatePlacementGroup updates a placement group with the specified ID using the provided options.
 120  func (c *Client) UpdatePlacementGroup(
 121  	ctx context.Context,
 122  	id int,
 123  	options PlacementGroupUpdateOptions,
 124  ) (*PlacementGroup, error) {
 125  	return doPUTRequest[PlacementGroup](
 126  		ctx,
 127  		c,
 128  		formatAPIPath("placement/groups/%d", id),
 129  		options,
 130  	)
 131  }
 132  
 133  // AssignPlacementGroupLinodes assigns the specified Linodes to the given
 134  // placement group.
 135  func (c *Client) AssignPlacementGroupLinodes(
 136  	ctx context.Context,
 137  	id int,
 138  	options PlacementGroupAssignOptions,
 139  ) (*PlacementGroup, error) {
 140  	return doPOSTRequest[PlacementGroup](
 141  		ctx,
 142  		c,
 143  		formatAPIPath("placement/groups/%d/assign", id),
 144  		options,
 145  	)
 146  }
 147  
 148  // UnassignPlacementGroupLinodes un-assigns the specified Linodes from the given
 149  // placement group.
 150  func (c *Client) UnassignPlacementGroupLinodes(
 151  	ctx context.Context,
 152  	id int,
 153  	options PlacementGroupUnAssignOptions,
 154  ) (*PlacementGroup, error) {
 155  	return doPOSTRequest[PlacementGroup](
 156  		ctx,
 157  		c,
 158  		formatAPIPath("placement/groups/%d/unassign", id),
 159  		options,
 160  	)
 161  }
 162  
 163  // DeletePlacementGroup deletes a placement group with the specified ID.
 164  func (c *Client) DeletePlacementGroup(
 165  	ctx context.Context,
 166  	id int,
 167  ) error {
 168  	return doDELETERequest(
 169  		ctx,
 170  		c,
 171  		formatAPIPath("placement/groups/%d", id),
 172  	)
 173  }
 174