1package nodegroups 2 3import ( 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. 12func 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 18type 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. 24type 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 37func (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. 55func 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 69type 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. 76type 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} 92 93func (opts CreateOpts) ToNodeGroupCreateMap() (map[string]interface{}, error) { 94 return gophercloud.BuildRequestBody(opts, "") 95} 96 97// Create makes a request to the Magnum API to create a node group 98// for the the given cluster. 99// Use the Extract method of the returned CreateResult to extract the 100// returned node group. 101func Create(client *gophercloud.ServiceClient, clusterID string, opts CreateOptsBuilder) (r CreateResult) { 102 b, err := opts.ToNodeGroupCreateMap() 103 if err != nil { 104 r.Err = err 105 return 106 } 107 resp, err := client.Post(createURL(client, clusterID), b, &r.Body, &gophercloud.RequestOpts{OkCodes: []int{202}}) 108 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 109 return 110} 111 112type UpdateOptsBuilder interface { 113 ToResourceUpdateMap() (map[string]interface{}, error) 114} 115 116type UpdateOp string 117 118const ( 119 AddOp UpdateOp = "add" 120 RemoveOp UpdateOp = "remove" 121 ReplaceOp UpdateOp = "replace" 122) 123 124// UpdateOpts is used to define the action taken when updating a node group. 125// 126// Valid Ops are "add", "remove", "replace" 127// Valid Paths are "/min_node_count" and "/max_node_count" 128type UpdateOpts struct { 129 Op UpdateOp `json:"op" required:"true"` 130 Path string `json:"path" required:"true"` 131 Value interface{} `json:"value,omitempty"` 132} 133 134func (opts UpdateOpts) ToResourceUpdateMap() (map[string]interface{}, error) { 135 return gophercloud.BuildRequestBody(opts, "") 136} 137 138// Update makes a request to the Magnum API to update a field of 139// the given node group belonging to the given cluster. More than 140// one UpdateOpts can be passed at a time. 141// Use the Extract method of the returned UpdateResult to extract the 142// updated node group from the result. 143func Update(client *gophercloud.ServiceClient, clusterID string, nodeGroupID string, opts []UpdateOptsBuilder) (r UpdateResult) { 144 var o []map[string]interface{} 145 for _, opt := range opts { 146 b, err := opt.ToResourceUpdateMap() 147 if err != nil { 148 r.Err = err 149 return 150 } 151 o = append(o, b) 152 } 153 resp, err := client.Patch(updateURL(client, clusterID, nodeGroupID), o, &r.Body, &gophercloud.RequestOpts{OkCodes: []int{202}}) 154 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 155 return 156} 157 158// Delete makes a request to the Magnum API to delete a node group. 159func Delete(client *gophercloud.ServiceClient, clusterID, nodeGroupID string) (r DeleteResult) { 160 resp, err := client.Delete(deleteURL(client, clusterID, nodeGroupID), nil) 161 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 162 return 163} 164