github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/networking/v1/bandwidths/requests.go (about)

     1  package bandwidths
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  )
     6  
     7  //UpdateOptsBuilder is an interface by which can be able to build the request
     8  //body
     9  type UpdateOptsBuilder interface {
    10  	ToBWUpdateMap() (map[string]interface{}, error)
    11  }
    12  
    13  //UpdateOpts is a struct which represents the request body of update method
    14  type UpdateOpts struct {
    15  	Size       int    `json:"size,omitempty"`
    16  	Name       string `json:"name,omitempty"`
    17  	ChargeMode string `json:"charge_mode,omitempty"`
    18  }
    19  
    20  func (opts UpdateOpts) ToBWUpdateMap() (map[string]interface{}, error) {
    21  	return golangsdk.BuildRequestBody(opts, "bandwidth")
    22  }
    23  
    24  //Get is a method by which can get the detailed information of a bandwidth
    25  func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
    26  	_, r.Err = client.Get(resourceURL(client, id), &r.Body, nil)
    27  	return
    28  }
    29  
    30  //Update is a method which can be able to update the port of public ip
    31  func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
    32  	b, err := opts.ToBWUpdateMap()
    33  	if err != nil {
    34  		r.Err = err
    35  		return
    36  	}
    37  	_, r.Err = client.Put(resourceURL(client, id), b, &r.Body, &golangsdk.RequestOpts{
    38  		OkCodes: []int{200},
    39  	})
    40  	return
    41  }
    42  
    43  // ListOptsBuilder allows extensions to add additional parameters to the
    44  // List request.
    45  type ListOptsBuilder interface {
    46  	ToBWListQuery() (string, error)
    47  }
    48  
    49  // ListOpts allows extensions to add additional parameters to the API.
    50  type ListOpts struct {
    51  	ShareType           string `q:"share_type"`
    52  	EnterpriseProjectID string `q:"enterprise_project_id"`
    53  }
    54  
    55  // ToBWListQuery formats a ListOpts into a query string.
    56  func (opts ListOpts) ToBWListQuery() (string, error) {
    57  	q, err := golangsdk.BuildQueryString(opts)
    58  	return q.String(), err
    59  }
    60  
    61  // List is a method by which can get the detailed information of all bandwidths
    62  func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) (r ListResult) {
    63  	url := listURL(client)
    64  	query, err := opts.ToBWListQuery()
    65  	if err != nil {
    66  		r.Err = err
    67  		return
    68  	}
    69  	url += query
    70  
    71  	_, r.Err = client.Get(url, &r.Body, nil)
    72  	return
    73  }