github.com/gophercloud/gophercloud@v1.11.0/openstack/containerinfra/v1/nodegroups/requests.go (about)

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