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

     1  package bandwidths
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/chnsz/golangsdk"
     7  )
     8  
     9  func Get(client *golangsdk.ServiceClient, bandwidthId string) (r GetResult) {
    10  	url := GetURL(client, bandwidthId)
    11  	_, r.Err = client.Get(url, &r.Body, &golangsdk.RequestOpts{
    12  		OkCodes: []int{http.StatusOK},
    13  	})
    14  	return
    15  }
    16  
    17  type UpdateOpts struct {
    18  	// Specifies the bandwidth name. The value is a string of 1 to 64
    19  	// characters that can contain letters, digits, underscores (_), and hyphens (-).
    20  	Name string `json:"name,omitempty"`
    21  
    22  	// Specifies the bandwidth size. The value ranges from 1 Mbit/s to
    23  	// 300 Mbit/s.
    24  	Size int `json:"size,omitempty"`
    25  }
    26  
    27  type UpdateOptsBuilder interface {
    28  	ToBandwidthsUpdateMap() (map[string]interface{}, error)
    29  }
    30  
    31  func (opts UpdateOpts) ToBandwidthsUpdateMap() (map[string]interface{}, error) {
    32  	b, err := golangsdk.BuildRequestBody(&opts, "bandwidth")
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  	return b, nil
    37  }
    38  
    39  func Update(client *golangsdk.ServiceClient, bandwidthId string, opts UpdateOptsBuilder) (r UpdateResult) {
    40  	b, err := opts.ToBandwidthsUpdateMap()
    41  	if err != nil {
    42  		r.Err = err
    43  		return
    44  	}
    45  
    46  	_, r.Err = client.Put(UpdateURL(client, bandwidthId), b, &r.Body, &golangsdk.RequestOpts{
    47  		OkCodes: []int{http.StatusOK},
    48  	})
    49  	return
    50  }
    51  
    52  type ListOpts struct {
    53  	Limit  int    `q:"limit"`
    54  	Offset int    `q:"offset"`
    55  	SiteID string `q:"site_id"`
    56  }
    57  
    58  type ListBandwidthsOptsBuilder interface {
    59  	ToListBandwidthsQuery() (string, error)
    60  }
    61  
    62  func (opts ListOpts) ToListBandwidthsQuery() (string, error) {
    63  	b, err := golangsdk.BuildQueryString(&opts)
    64  	if err != nil {
    65  		return "", err
    66  	}
    67  	return b.String(), nil
    68  }
    69  
    70  func List(client *golangsdk.ServiceClient, opts ListBandwidthsOptsBuilder) (r ListResult) {
    71  	listURL := listURL(client)
    72  	if opts != nil {
    73  		query, err := opts.ToListBandwidthsQuery()
    74  		if err != nil {
    75  			r.Err = err
    76  			return r
    77  		}
    78  		listURL += query
    79  	}
    80  
    81  	_, r.Err = client.Get(listURL, &r.Body, nil)
    82  	return
    83  }