github.com/gophercloud/gophercloud@v1.11.0/openstack/loadbalancer/v2/flavors/requests.go (about) 1 package flavors 2 3 import ( 4 "github.com/gophercloud/gophercloud" 5 "github.com/gophercloud/gophercloud/pagination" 6 ) 7 8 // ListOptsBuilder allows extensions to add additional parameters to the 9 // List request. 10 type ListOptsBuilder interface { 11 ToFlavorListQuery() (string, error) 12 } 13 14 // ListOpts allows to manage the output of the request. 15 type ListOpts struct { 16 // The fields that you want the server to return 17 Fields []string `q:"fields"` 18 } 19 20 // ToFlavorListQuery formats a ListOpts into a query string. 21 func (opts ListOpts) ToFlavorListQuery() (string, error) { 22 q, err := gophercloud.BuildQueryString(opts) 23 return q.String(), err 24 } 25 26 // List returns a Pager which allows you to iterate over a collection of 27 // Flavor. It accepts a ListOpts struct, which allows you to filter 28 // and sort the returned collection for greater efficiency. 29 func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { 30 url := rootURL(c) 31 if opts != nil { 32 query, err := opts.ToFlavorListQuery() 33 if err != nil { 34 return pagination.Pager{Err: err} 35 } 36 url += query 37 } 38 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { 39 return FlavorPage{pagination.LinkedPageBase{PageResult: r}} 40 }) 41 } 42 43 // CreateOptsBuilder allows extensions to add additional parameters to the 44 // Create request. 45 type CreateOptsBuilder interface { 46 ToFlavorCreateMap() (map[string]interface{}, error) 47 } 48 49 // CreateOpts is the common options struct used in this package's Create 50 // operation. 51 type CreateOpts struct { 52 // Human-readable name for the Loadbalancer. Does not have to be unique. 53 Name string `json:"name" required:"true"` 54 55 // Human-readable description for the Flavor. 56 Description string `json:"description,omitempty"` 57 58 // The ID of the FlavorProfile which give the metadata for the creation of 59 // a LoadBalancer. 60 FlavorProfileId string `json:"flavor_profile_id" required:"true"` 61 62 // If the resource is available for use. The default is True. 63 Enabled bool `json:"enabled,omitempty"` 64 } 65 66 // ToFlavorCreateMap builds a request body from CreateOpts. 67 func (opts CreateOpts) ToFlavorCreateMap() (map[string]interface{}, error) { 68 return gophercloud.BuildRequestBody(opts, "flavor") 69 } 70 71 // Create is and operation which add a new Flavor into the database. 72 // CreateResult will be returned. 73 func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 74 b, err := opts.ToFlavorCreateMap() 75 if err != nil { 76 r.Err = err 77 return 78 } 79 resp, err := c.Post(rootURL(c), b, &r.Body, nil) 80 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 81 return 82 } 83 84 // Get retrieves a particular Flavor based on its unique ID. 85 func Get(c *gophercloud.ServiceClient, id string) (r GetResult) { 86 resp, err := c.Get(resourceURL(c, id), &r.Body, nil) 87 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 88 return 89 } 90 91 // UpdateOptsBuilder allows extensions to add additional parameters to the 92 // Update request. 93 type UpdateOptsBuilder interface { 94 ToFlavorUpdateMap() (map[string]interface{}, error) 95 } 96 97 // UpdateOpts is the common options struct used in this package's Update 98 // operation. 99 type UpdateOpts struct { 100 // Human-readable name for the Loadbalancer. Does not have to be unique. 101 Name string `json:"name,omitempty"` 102 103 // Human-readable description for the Flavor. 104 Description string `json:"description,omitempty"` 105 106 // If the resource is available for use. 107 Enabled bool `json:"enabled,omitempty"` 108 } 109 110 // ToFlavorUpdateMap builds a request body from UpdateOpts. 111 func (opts UpdateOpts) ToFlavorUpdateMap() (map[string]interface{}, error) { 112 b, err := gophercloud.BuildRequestBody(opts, "flavor") 113 if err != nil { 114 return nil, err 115 } 116 117 return b, nil 118 } 119 120 // Update is an operation which modifies the attributes of the specified 121 // Flavor. 122 func Update(c *gophercloud.ServiceClient, id string, opts UpdateOpts) (r UpdateResult) { 123 b, err := opts.ToFlavorUpdateMap() 124 if err != nil { 125 r.Err = err 126 return 127 } 128 resp, err := c.Put(resourceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{ 129 OkCodes: []int{200}, 130 }) 131 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 132 return 133 } 134 135 // Delete will permanently delete a particular Flavor based on its 136 // unique ID. 137 func Delete(c *gophercloud.ServiceClient, id string) (r DeleteResult) { 138 resp, err := c.Delete(resourceURL(c, id), nil) 139 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 140 return 141 }