123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- package clusters
- import (
- "net/http"
- "devel.mephi.ru/iacherepanov/openstack-gophercloud"
- "devel.mephi.ru/iacherepanov/openstack-gophercloud/pagination"
- )
- // CreateOptsBuilder Builder.
- type CreateOptsBuilder interface {
- ToClusterCreateMap() (map[string]interface{}, error)
- }
- // CreateOpts params
- type CreateOpts struct {
- Name string `json:"name" required:"true"`
- DesiredCapacity int `json:"desired_capacity"`
- ProfileID string `json:"profile_id" required:"true"`
- MinSize *int `json:"min_size,omitempty"`
- Timeout int `json:"timeout,omitempty"`
- MaxSize int `json:"max_size,omitempty"`
- Metadata map[string]interface{} `json:"metadata,omitempty"`
- Config map[string]interface{} `json:"config,omitempty"`
- }
- // ToClusterCreateMap constructs a request body from CreateOpts.
- func (opts CreateOpts) ToClusterCreateMap() (map[string]interface{}, error) {
- return gophercloud.BuildRequestBody(opts, "cluster")
- }
- // Create requests the creation of a new cluster.
- func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
- b, err := opts.ToClusterCreateMap()
- if err != nil {
- r.Err = err
- return
- }
- var result *http.Response
- result, r.Err = client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{
- OkCodes: []int{200, 201, 202},
- })
- if r.Err == nil {
- r.Header = result.Header
- }
- return
- }
- // Get retrieves details of a single cluster. Use Extract to convert its
- // result into a Cluster.
- func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
- var result *http.Response
- result, r.Err = client.Get(getURL(client, id), &r.Body, &gophercloud.RequestOpts{OkCodes: []int{200}})
- if r.Err == nil {
- r.Header = result.Header
- }
- return
- }
- // ListOptsBuilder Builder.
- type ListOptsBuilder interface {
- ToClusterListQuery() (string, error)
- }
- // ListOpts params
- type ListOpts struct {
- Limit int `q:"limit"`
- Marker string `q:"marker"`
- Sort string `q:"sort"`
- GlobalProject *bool `q:"global_project"`
- Name string `q:"name,omitempty"`
- Status string `q:"status,omitempty"`
- }
- // ToClusterListQuery formats a ListOpts into a query string.
- func (opts ListOpts) ToClusterListQuery() (string, error) {
- q, err := gophercloud.BuildQueryString(opts)
- return q.String(), err
- }
- // List instructs OpenStack to provide a list of clusters.
- func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
- url := listURL(client)
- if opts != nil {
- query, err := opts.ToClusterListQuery()
- if err != nil {
- return pagination.Pager{Err: err}
- }
- url += query
- }
- return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
- return ClusterPage{pagination.LinkedPageBase{PageResult: r}}
- })
- }
- // UpdateOpts implements UpdateOpts
- type UpdateOpts struct {
- Config string `json:"config,omitempty"`
- Name string `json:"name,omitempty"`
- ProfileID string `json:"profile_id,omitempty"`
- Timeout *int `json:"timeout,omitempty"`
- Metadata map[string]interface{} `json:"metadata,omitempty"`
- ProfileOnly *bool `json:"profile_only,omitempty"`
- }
- // UpdateOptsBuilder allows extensions to add additional parameters to the
- // Update request.
- type UpdateOptsBuilder interface {
- ToClusterUpdateMap() (map[string]interface{}, error)
- }
- // ToClusterUpdateMap assembles a request body based on the contents of
- // UpdateOpts.
- func (opts UpdateOpts) ToClusterUpdateMap() (map[string]interface{}, error) {
- b, err := gophercloud.BuildRequestBody(opts, "cluster")
- if err != nil {
- return nil, err
- }
- return b, nil
- }
- // Update implements profile updated request.
- func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
- b, err := opts.ToClusterUpdateMap()
- if err != nil {
- r.Err = err
- return r
- }
- var result *http.Response
- result, r.Err = client.Patch(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
- OkCodes: []int{200, 202},
- })
- if r.Err == nil {
- r.Header = result.Header
- }
- return
- }
- // Delete deletes the specified cluster ID.
- func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) {
- var result *http.Response
- result, r.Err = client.Delete(deleteURL(client, id), nil)
- if r.Err == nil {
- r.Header = result.Header
- }
- return
- }
|