github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/loadbalancer/v2/flavorprofiles/requests.go (about)

     1  package flavorprofiles
     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  	ToFlavorProfileListQuery() (string, error)
    14  }
    15  
    16  // ListOpts allows to manage the output of the request.
    17  type ListOpts struct {
    18  	// The name of the flavor profile to filter by.
    19  	Name string `q:"name"`
    20  	// The provider name of the flavor profile to filter by.
    21  	ProviderName string `q:"provider_name"`
    22  	// The fields that you want the server to return
    23  	Fields []string `q:"fields"`
    24  }
    25  
    26  // ToFlavorProfileListQuery formats a ListOpts into a query string.
    27  func (opts ListOpts) ToFlavorProfileListQuery() (string, error) {
    28  	q, err := gophercloud.BuildQueryString(opts)
    29  	return q.String(), err
    30  }
    31  
    32  // List returns a Pager which allows you to iterate over a collection of
    33  // FlavorProfiles. It accepts a ListOpts struct, which allows you to filter
    34  // and sort the returned collection for greater efficiency.
    35  func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    36  	url := rootURL(c)
    37  	if opts != nil {
    38  		query, err := opts.ToFlavorProfileListQuery()
    39  		if err != nil {
    40  			return pagination.Pager{Err: err}
    41  		}
    42  		url += query
    43  	}
    44  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    45  		return FlavorProfilePage{pagination.LinkedPageBase{PageResult: r}}
    46  	})
    47  }
    48  
    49  // CreateOptsBuilder allows extensions to add additional parameters to the
    50  // Create request.
    51  type CreateOptsBuilder interface {
    52  	ToFlavorProfileCreateMap() (map[string]any, error)
    53  }
    54  
    55  // CreateOpts is the common options struct used in this package's Create
    56  // operation.
    57  type CreateOpts struct {
    58  	// Human-readable name for the Loadbalancer. Does not have to be unique.
    59  	Name string `json:"name" required:"true"`
    60  
    61  	// Providing the name of the provider supported by the Octavia installation.
    62  	ProviderName string `json:"provider_name" required:"true"`
    63  
    64  	// Providing the json string containing the flavor metadata.
    65  	FlavorData string `json:"flavor_data" required:"true"`
    66  }
    67  
    68  // ToFlavorProfileCreateMap builds a request body from CreateOpts.
    69  func (opts CreateOpts) ToFlavorProfileCreateMap() (map[string]any, error) {
    70  	return gophercloud.BuildRequestBody(opts, "flavorprofile")
    71  }
    72  
    73  // Create is and operation which add a new FlavorProfile into the database.
    74  // CreateResult will be returned.
    75  func Create(ctx context.Context, c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    76  	b, err := opts.ToFlavorProfileCreateMap()
    77  	if err != nil {
    78  		r.Err = err
    79  		return
    80  	}
    81  	resp, err := c.Post(ctx, rootURL(c), b, &r.Body, nil)
    82  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    83  	return
    84  }
    85  
    86  // Get retrieves a particular FlavorProfile based on its unique ID.
    87  func Get(ctx context.Context, c *gophercloud.ServiceClient, id string) (r GetResult) {
    88  	resp, err := c.Get(ctx, resourceURL(c, id), &r.Body, nil)
    89  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    90  	return
    91  }
    92  
    93  // UpdateOptsBuilder allows extensions to add additional parameters to the
    94  // Update request.
    95  type UpdateOptsBuilder interface {
    96  	ToFlavorProfileUpdateMap() (map[string]any, error)
    97  }
    98  
    99  // UpdateOpts is the common options struct used in this package's Update
   100  // operation.
   101  type UpdateOpts struct {
   102  	// Human-readable name for the Loadbalancer. Does not have to be unique.
   103  	Name string `json:"name,omitempty"`
   104  
   105  	// Providing the name of the provider supported by the Octavia installation.
   106  	ProviderName string `json:"provider_name,omitempty"`
   107  
   108  	// Providing the json string containing the flavor metadata.
   109  	FlavorData string `json:"flavor_data,omitempty"`
   110  }
   111  
   112  // ToFlavorProfileUpdateMap builds a request body from UpdateOpts.
   113  func (opts UpdateOpts) ToFlavorProfileUpdateMap() (map[string]any, error) {
   114  	b, err := gophercloud.BuildRequestBody(opts, "flavorprofile")
   115  	if err != nil {
   116  		return nil, err
   117  	}
   118  
   119  	return b, nil
   120  }
   121  
   122  // Update is an operation which modifies the attributes of the specified
   123  // FlavorProfile.
   124  func Update(ctx context.Context, c *gophercloud.ServiceClient, id string, opts UpdateOpts) (r UpdateResult) {
   125  	b, err := opts.ToFlavorProfileUpdateMap()
   126  	if err != nil {
   127  		r.Err = err
   128  		return
   129  	}
   130  	resp, err := c.Put(ctx, resourceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
   131  		OkCodes: []int{200, 202},
   132  	})
   133  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   134  	return
   135  }
   136  
   137  // Delete will permanently delete a particular FlavorProfile based on its
   138  // unique ID.
   139  func Delete(ctx context.Context, c *gophercloud.ServiceClient, id string) (r DeleteResult) {
   140  	resp, err := c.Delete(ctx, resourceURL(c, id), nil)
   141  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   142  	return
   143  }