github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/containerinfra/v1/nodegroups/requests.go (about)

     1  package nodegroups
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/vnpaycloud-console/gophercloud/v2"
     7  	"github.com/vnpaycloud-console/gophercloud/v2/pagination"
     8  )
     9  
    10  // Get makes a request to the Magnum API to retrieve a node group
    11  // with the given ID/name belonging to the given cluster.
    12  // Use the Extract method of the returned GetResult to extract the
    13  // node group from the result.
    14  func Get(ctx context.Context, client *gophercloud.ServiceClient, clusterID, nodeGroupID string) (r GetResult) {
    15  	resp, err := client.Get(ctx, getURL(client, clusterID, nodeGroupID), &r.Body, &gophercloud.RequestOpts{OkCodes: []int{200}})
    16  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    17  	return
    18  }
    19  
    20  type ListOptsBuilder interface {
    21  	ToNodeGroupsListQuery() (string, error)
    22  }
    23  
    24  // ListOpts is used to filter and sort the node groups of a cluster
    25  // when using List.
    26  type ListOpts struct {
    27  	// Pagination marker for large data sets. (UUID field from node group).
    28  	Marker int `q:"marker"`
    29  	// Maximum number of resources to return in a single page.
    30  	Limit int `q:"limit"`
    31  	// Column to sort results by. Default: id.
    32  	SortKey string `q:"sort_key"`
    33  	// Direction to sort. "asc" or "desc". Default: asc.
    34  	SortDir string `q:"sort_dir"`
    35  	// List all nodegroups with the specified role.
    36  	Role string `q:"role"`
    37  }
    38  
    39  func (opts ListOpts) ToNodeGroupsListQuery() (string, error) {
    40  	q, err := gophercloud.BuildQueryString(opts)
    41  	return q.String(), err
    42  }
    43  
    44  // List makes a request to the Magnum API to retrieve node groups
    45  // belonging to the given cluster. The request can be modified to
    46  // filter or sort the list using the options available in ListOpts.
    47  //
    48  // Use the AllPages method of the returned Pager to ensure that
    49  // all node groups are returned (for example when using the Limit
    50  // option to limit the number of node groups returned per page).
    51  //
    52  // Not all node group fields are returned in a list request.
    53  // Only the fields UUID, Name, FlavorID, ImageID,
    54  // NodeCount, Role, IsDefault, Status and StackID
    55  // are returned, all other fields are omitted
    56  // and will have their zero value when extracted.
    57  func List(client *gophercloud.ServiceClient, clusterID string, opts ListOptsBuilder) pagination.Pager {
    58  	url := listURL(client, clusterID)
    59  	if opts != nil {
    60  		query, err := opts.ToNodeGroupsListQuery()
    61  		if err != nil {
    62  			return pagination.Pager{Err: err}
    63  		}
    64  		url += query
    65  	}
    66  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    67  		return NodeGroupPage{pagination.LinkedPageBase{PageResult: r}}
    68  	})
    69  }
    70  
    71  type CreateOptsBuilder interface {
    72  	ToNodeGroupCreateMap() (map[string]any, error)
    73  }
    74  
    75  // CreateOpts is used to set available fields upon node group creation.
    76  //
    77  // If unset, some fields have defaults or will inherit from the cluster value.
    78  type CreateOpts struct {
    79  	Name             string `json:"name" required:"true"`
    80  	DockerVolumeSize *int   `json:"docker_volume_size,omitempty"`
    81  	// Labels will default to the cluster labels if unset.
    82  	Labels       map[string]string `json:"labels,omitempty"`
    83  	NodeCount    *int              `json:"node_count,omitempty"`
    84  	MinNodeCount int               `json:"min_node_count,omitempty"`
    85  	// MaxNodeCount can be left unset for no maximum node count.
    86  	MaxNodeCount *int `json:"max_node_count,omitempty"`
    87  	// Role defaults to "worker" if unset.
    88  	Role string `json:"role,omitempty"`
    89  	// Node image ID. Defaults to cluster template image if unset.
    90  	ImageID string `json:"image_id,omitempty"`
    91  	// Node machine flavor ID. Defaults to cluster minion flavor if unset.
    92  	FlavorID    string `json:"flavor_id,omitempty"`
    93  	MergeLabels *bool  `json:"merge_labels,omitempty"`
    94  }
    95  
    96  func (opts CreateOpts) ToNodeGroupCreateMap() (map[string]any, error) {
    97  	return gophercloud.BuildRequestBody(opts, "")
    98  }
    99  
   100  // Create makes a request to the Magnum API to create a node group
   101  // for the the given cluster.
   102  // Use the Extract method of the returned CreateResult to extract the
   103  // returned node group.
   104  func Create(ctx context.Context, client *gophercloud.ServiceClient, clusterID string, opts CreateOptsBuilder) (r CreateResult) {
   105  	b, err := opts.ToNodeGroupCreateMap()
   106  	if err != nil {
   107  		r.Err = err
   108  		return
   109  	}
   110  	resp, err := client.Post(ctx, createURL(client, clusterID), b, &r.Body, &gophercloud.RequestOpts{OkCodes: []int{202}})
   111  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   112  	return
   113  }
   114  
   115  type UpdateOptsBuilder interface {
   116  	ToResourceUpdateMap() (map[string]any, error)
   117  }
   118  
   119  type UpdateOp string
   120  
   121  const (
   122  	AddOp     UpdateOp = "add"
   123  	RemoveOp  UpdateOp = "remove"
   124  	ReplaceOp UpdateOp = "replace"
   125  )
   126  
   127  // UpdateOpts is used to define the action taken when updating a node group.
   128  //
   129  // Valid Ops are "add", "remove", "replace"
   130  // Valid Paths are "/min_node_count" and "/max_node_count"
   131  type UpdateOpts struct {
   132  	Op    UpdateOp `json:"op" required:"true"`
   133  	Path  string   `json:"path" required:"true"`
   134  	Value any      `json:"value,omitempty"`
   135  }
   136  
   137  func (opts UpdateOpts) ToResourceUpdateMap() (map[string]any, error) {
   138  	return gophercloud.BuildRequestBody(opts, "")
   139  }
   140  
   141  // Update makes a request to the Magnum API to update a field of
   142  // the given node group belonging to the given cluster. More than
   143  // one UpdateOpts can be passed at a time.
   144  // Use the Extract method of the returned UpdateResult to extract the
   145  // updated node group from the result.
   146  func Update[T UpdateOptsBuilder](ctx context.Context, client *gophercloud.ServiceClient, clusterID string, nodeGroupID string, opts []T) (r UpdateResult) {
   147  	var o []map[string]any
   148  	for _, opt := range opts {
   149  		b, err := opt.ToResourceUpdateMap()
   150  		if err != nil {
   151  			r.Err = err
   152  			return
   153  		}
   154  		o = append(o, b)
   155  	}
   156  	resp, err := client.Patch(ctx, updateURL(client, clusterID, nodeGroupID), o, &r.Body, &gophercloud.RequestOpts{OkCodes: []int{202}})
   157  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   158  	return
   159  }
   160  
   161  // Delete makes a request to the Magnum API to delete a node group.
   162  func Delete(ctx context.Context, client *gophercloud.ServiceClient, clusterID, nodeGroupID string) (r DeleteResult) {
   163  	resp, err := client.Delete(ctx, deleteURL(client, clusterID, nodeGroupID), nil)
   164  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   165  	return
   166  }