github.com/gophercloud/gophercloud@v1.11.0/openstack/loadbalancer/v2/flavorprofiles/requests.go (about)

     1  package flavorprofiles
     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  	ToFlavorProfileListQuery() (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  // ToFlavorProfileListQuery formats a ListOpts into a query string.
    21  func (opts ListOpts) ToFlavorProfileListQuery() (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  // FlavorProfiles. 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.ToFlavorProfileListQuery()
    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 FlavorProfilePage{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  	ToFlavorProfileCreateMap() (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  	// Providing the name of the provider supported by the Octavia installation.
    56  	ProviderName string `json:"provider_name" required:"true"`
    57  
    58  	// Providing the json string containing the flavor metadata.
    59  	FlavorData string `json:"flavor_data" required:"true"`
    60  }
    61  
    62  // ToFlavorProfileCreateMap builds a request body from CreateOpts.
    63  func (opts CreateOpts) ToFlavorProfileCreateMap() (map[string]interface{}, error) {
    64  	return gophercloud.BuildRequestBody(opts, "flavorprofile")
    65  }
    66  
    67  // Create is and operation which add a new FlavorProfile into the database.
    68  // CreateResult will be returned.
    69  func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    70  	b, err := opts.ToFlavorProfileCreateMap()
    71  	if err != nil {
    72  		r.Err = err
    73  		return
    74  	}
    75  	resp, err := c.Post(rootURL(c), b, &r.Body, nil)
    76  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    77  	return
    78  }
    79  
    80  // Get retrieves a particular FlavorProfile based on its unique ID.
    81  func Get(c *gophercloud.ServiceClient, id string) (r GetResult) {
    82  	resp, err := c.Get(resourceURL(c, id), &r.Body, nil)
    83  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    84  	return
    85  }
    86  
    87  // UpdateOptsBuilder allows extensions to add additional parameters to the
    88  // Update request.
    89  type UpdateOptsBuilder interface {
    90  	ToFlavorProfileUpdateMap() (map[string]interface{}, error)
    91  }
    92  
    93  // UpdateOpts is the common options struct used in this package's Update
    94  // operation.
    95  type UpdateOpts struct {
    96  	// Human-readable name for the Loadbalancer. Does not have to be unique.
    97  	Name string `json:"name,omitempty"`
    98  
    99  	// Providing the name of the provider supported by the Octavia installation.
   100  	ProviderName string `json:"provider_name,omitempty"`
   101  
   102  	// Providing the json string containing the flavor metadata.
   103  	FlavorData string `json:"flavor_data,omitempty"`
   104  }
   105  
   106  // ToFlavorProfileUpdateMap builds a request body from UpdateOpts.
   107  func (opts UpdateOpts) ToFlavorProfileUpdateMap() (map[string]interface{}, error) {
   108  	b, err := gophercloud.BuildRequestBody(opts, "flavorprofile")
   109  	if err != nil {
   110  		return nil, err
   111  	}
   112  
   113  	return b, nil
   114  }
   115  
   116  // Update is an operation which modifies the attributes of the specified
   117  // FlavorProfile.
   118  func Update(c *gophercloud.ServiceClient, id string, opts UpdateOpts) (r UpdateResult) {
   119  	b, err := opts.ToFlavorProfileUpdateMap()
   120  	if err != nil {
   121  		r.Err = err
   122  		return
   123  	}
   124  	resp, err := c.Put(resourceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
   125  		OkCodes: []int{200, 202},
   126  	})
   127  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   128  	return
   129  }
   130  
   131  // Delete will permanently delete a particular FlavorProfile based on its
   132  // unique ID.
   133  func Delete(c *gophercloud.ServiceClient, id string) (r DeleteResult) {
   134  	resp, err := c.Delete(resourceURL(c, id), nil)
   135  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   136  	return
   137  }