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