github.com/gophercloud/gophercloud@v1.11.0/openstack/containerinfra/v1/clusters/requests.go (about) 1 package clusters 2 3 import ( 4 "github.com/gophercloud/gophercloud" 5 "github.com/gophercloud/gophercloud/pagination" 6 ) 7 8 // CreateOptsBuilder Builder. 9 type CreateOptsBuilder interface { 10 ToClusterCreateMap() (map[string]interface{}, error) 11 } 12 13 // CreateOpts params 14 type CreateOpts struct { 15 ClusterTemplateID string `json:"cluster_template_id" required:"true"` 16 CreateTimeout *int `json:"create_timeout"` 17 DiscoveryURL string `json:"discovery_url,omitempty"` 18 DockerVolumeSize *int `json:"docker_volume_size,omitempty"` 19 FlavorID string `json:"flavor_id,omitempty"` 20 Keypair string `json:"keypair,omitempty"` 21 Labels map[string]string `json:"labels,omitempty"` 22 MasterCount *int `json:"master_count,omitempty"` 23 MasterFlavorID string `json:"master_flavor_id,omitempty"` 24 Name string `json:"name"` 25 NodeCount *int `json:"node_count,omitempty"` 26 FloatingIPEnabled *bool `json:"floating_ip_enabled,omitempty"` 27 MasterLBEnabled *bool `json:"master_lb_enabled,omitempty"` 28 FixedNetwork string `json:"fixed_network,omitempty"` 29 FixedSubnet string `json:"fixed_subnet,omitempty"` 30 MergeLabels *bool `json:"merge_labels,omitempty"` 31 } 32 33 // ToClusterCreateMap constructs a request body from CreateOpts. 34 func (opts CreateOpts) ToClusterCreateMap() (map[string]interface{}, error) { 35 return gophercloud.BuildRequestBody(opts, "") 36 } 37 38 // Create requests the creation of a new cluster. 39 func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 40 b, err := opts.ToClusterCreateMap() 41 if err != nil { 42 r.Err = err 43 return 44 } 45 resp, err := client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{ 46 OkCodes: []int{202}, 47 }) 48 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 49 return 50 } 51 52 // Get retrieves a specific clusters based on its unique ID. 53 func Get(client *gophercloud.ServiceClient, id string) (r GetResult) { 54 resp, err := client.Get(getURL(client, id), &r.Body, &gophercloud.RequestOpts{OkCodes: []int{200}}) 55 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 56 return 57 } 58 59 // Delete deletes the specified cluster ID. 60 func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) { 61 resp, err := client.Delete(deleteURL(client, id), nil) 62 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 63 return 64 } 65 66 // ListOptsBuilder allows extensions to add additional parameters to the 67 // List request. 68 type ListOptsBuilder interface { 69 ToClustersListQuery() (string, error) 70 } 71 72 // ListOpts allows the sorting of paginated collections through 73 // the API. SortKey allows you to sort by a particular cluster attribute. 74 // SortDir sets the direction, and is either `asc' or `desc'. 75 // Marker and Limit are used for pagination. 76 type ListOpts struct { 77 Marker string `q:"marker"` 78 Limit int `q:"limit"` 79 SortKey string `q:"sort_key"` 80 SortDir string `q:"sort_dir"` 81 } 82 83 // ToClustersListQuery formats a ListOpts into a query string. 84 func (opts ListOpts) ToClustersListQuery() (string, error) { 85 q, err := gophercloud.BuildQueryString(opts) 86 return q.String(), err 87 } 88 89 // List returns a Pager which allows you to iterate over a collection of 90 // clusters. It accepts a ListOptsBuilder, which allows you to sort 91 // the returned collection for greater efficiency. 92 func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { 93 url := listURL(c) 94 if opts != nil { 95 query, err := opts.ToClustersListQuery() 96 if err != nil { 97 return pagination.Pager{Err: err} 98 } 99 url += query 100 } 101 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { 102 return ClusterPage{pagination.LinkedPageBase{PageResult: r}} 103 }) 104 } 105 106 // ListDetail returns a Pager which allows you to iterate over a collection of 107 // clusters with detailed information. 108 // It accepts a ListOptsBuilder, which allows you to sort the returned 109 // collection for greater efficiency. 110 func ListDetail(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { 111 url := listDetailURL(c) 112 if opts != nil { 113 query, err := opts.ToClustersListQuery() 114 if err != nil { 115 return pagination.Pager{Err: err} 116 } 117 url += query 118 } 119 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { 120 return ClusterPage{pagination.LinkedPageBase{PageResult: r}} 121 }) 122 } 123 124 type UpdateOp string 125 126 const ( 127 AddOp UpdateOp = "add" 128 RemoveOp UpdateOp = "remove" 129 ReplaceOp UpdateOp = "replace" 130 ) 131 132 type UpdateOpts struct { 133 Op UpdateOp `json:"op" required:"true"` 134 Path string `json:"path" required:"true"` 135 Value interface{} `json:"value,omitempty"` 136 } 137 138 // UpdateOptsBuilder allows extensions to add additional parameters to the 139 // Update request. 140 type UpdateOptsBuilder interface { 141 ToClustersUpdateMap() (map[string]interface{}, error) 142 } 143 144 // ToClusterUpdateMap assembles a request body based on the contents of 145 // UpdateOpts. 146 func (opts UpdateOpts) ToClustersUpdateMap() (map[string]interface{}, error) { 147 return gophercloud.BuildRequestBody(opts, "") 148 } 149 150 // Update implements cluster updated request. 151 func Update(client *gophercloud.ServiceClient, id string, opts []UpdateOptsBuilder) (r UpdateResult) { 152 var o []map[string]interface{} 153 for _, opt := range opts { 154 b, err := opt.ToClustersUpdateMap() 155 if err != nil { 156 r.Err = err 157 return r 158 } 159 o = append(o, b) 160 } 161 resp, err := client.Patch(updateURL(client, id), o, &r.Body, &gophercloud.RequestOpts{ 162 OkCodes: []int{200, 202}, 163 }) 164 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 165 return 166 } 167 168 type UpgradeOpts struct { 169 ClusterTemplate string `json:"cluster_template" required:"true"` 170 MaxBatchSize *int `json:"max_batch_size,omitempty"` 171 NodeGroup string `json:"nodegroup,omitempty"` 172 } 173 174 // UpgradeOptsBuilder allows extensions to add additional parameters to the 175 // Upgrade request. 176 type UpgradeOptsBuilder interface { 177 ToClustersUpgradeMap() (map[string]interface{}, error) 178 } 179 180 // ToClustersUpgradeMap constructs a request body from UpgradeOpts. 181 func (opts UpgradeOpts) ToClustersUpgradeMap() (map[string]interface{}, error) { 182 if opts.MaxBatchSize == nil { 183 defaultMaxBatchSize := 1 184 opts.MaxBatchSize = &defaultMaxBatchSize 185 } 186 return gophercloud.BuildRequestBody(opts, "") 187 } 188 189 // Upgrade implements cluster upgrade request. 190 func Upgrade(client *gophercloud.ServiceClient, id string, opts UpgradeOptsBuilder) (r UpgradeResult) { 191 b, err := opts.ToClustersUpgradeMap() 192 if err != nil { 193 r.Err = err 194 return 195 } 196 197 resp, err := client.Post(upgradeURL(client, id), b, &r.Body, &gophercloud.RequestOpts{ 198 OkCodes: []int{202}, 199 }) 200 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 201 return 202 } 203 204 // ResizeOptsBuilder allows extensions to add additional parameters to the 205 // Resize request. 206 type ResizeOptsBuilder interface { 207 ToClusterResizeMap() (map[string]interface{}, error) 208 } 209 210 // ResizeOpts params 211 type ResizeOpts struct { 212 NodeCount *int `json:"node_count" required:"true"` 213 NodesToRemove []string `json:"nodes_to_remove,omitempty"` 214 NodeGroup string `json:"nodegroup,omitempty"` 215 } 216 217 // ToClusterResizeMap constructs a request body from ResizeOpts. 218 func (opts ResizeOpts) ToClusterResizeMap() (map[string]interface{}, error) { 219 return gophercloud.BuildRequestBody(opts, "") 220 } 221 222 // Resize an existing cluster node count. 223 func Resize(client *gophercloud.ServiceClient, id string, opts ResizeOptsBuilder) (r ResizeResult) { 224 b, err := opts.ToClusterResizeMap() 225 if err != nil { 226 r.Err = err 227 return 228 } 229 resp, err := client.Post(resizeURL(client, id), b, &r.Body, &gophercloud.RequestOpts{ 230 OkCodes: []int{200, 202}, 231 }) 232 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 233 return 234 }