github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/networking/v2/extensions/layer3/addressscopes/requests.go (about)

     1  package addressscopes
     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  	ToAddressScopeListQuery() (string, error)
    14  }
    15  
    16  // ListOpts allows the filtering and sorting of paginated collections through
    17  // the Neutron API. Filtering is achieved by passing in struct field values
    18  // that map to the address-scope attributes you want to see returned.
    19  // SortKey allows you to sort by a particular address-scope attribute.
    20  // SortDir sets the direction, and is either `asc' or `desc'.
    21  // Marker and Limit are used for the pagination.
    22  type ListOpts struct {
    23  	ID          string `q:"id"`
    24  	Name        string `q:"name"`
    25  	TenantID    string `q:"tenant_id"`
    26  	ProjectID   string `q:"project_id"`
    27  	IPVersion   int    `q:"ip_version"`
    28  	Shared      *bool  `q:"shared"`
    29  	Description string `q:"description"`
    30  	Limit       int    `q:"limit"`
    31  	Marker      string `q:"marker"`
    32  	SortKey     string `q:"sort_key"`
    33  	SortDir     string `q:"sort_dir"`
    34  }
    35  
    36  // ToAddressScopeListQuery formats a ListOpts into a query string.
    37  func (opts ListOpts) ToAddressScopeListQuery() (string, error) {
    38  	q, err := gophercloud.BuildQueryString(opts)
    39  	return q.String(), err
    40  }
    41  
    42  // List returns a Pager which allows you to iterate over a collection of
    43  // address-scopes. It accepts a ListOpts struct, which allows you to filter and
    44  // sort the returned collection for greater efficiency.
    45  //
    46  // Default policy settings return only the address-scopes owned by the project
    47  // of the user submitting the request, unless the user has the administrative
    48  // role.
    49  func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    50  	url := listURL(c)
    51  	if opts != nil {
    52  		query, err := opts.ToAddressScopeListQuery()
    53  		if err != nil {
    54  			return pagination.Pager{Err: err}
    55  		}
    56  		url += query
    57  	}
    58  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    59  		return AddressScopePage{pagination.LinkedPageBase{PageResult: r}}
    60  	})
    61  }
    62  
    63  // Get retrieves a specific address-scope based on its ID.
    64  func Get(ctx context.Context, c *gophercloud.ServiceClient, id string) (r GetResult) {
    65  	resp, err := c.Get(ctx, getURL(c, id), &r.Body, nil)
    66  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    67  	return
    68  }
    69  
    70  // CreateOptsBuilder allows to add additional parameters to the
    71  // Create request.
    72  type CreateOptsBuilder interface {
    73  	ToAddressScopeCreateMap() (map[string]any, error)
    74  }
    75  
    76  // CreateOpts specifies parameters of a new address-scope.
    77  type CreateOpts struct {
    78  	// Name is the human-readable name of the address-scope.
    79  	Name string `json:"name"`
    80  
    81  	// TenantID is the id of the Identity project.
    82  	TenantID string `json:"tenant_id,omitempty"`
    83  
    84  	// ProjectID is the id of the Identity project.
    85  	ProjectID string `json:"project_id,omitempty"`
    86  
    87  	// IPVersion is the IP protocol version.
    88  	IPVersion int `json:"ip_version"`
    89  
    90  	// Shared indicates whether this address-scope is shared across all projects.
    91  	Shared bool `json:"shared,omitempty"`
    92  }
    93  
    94  // ToAddressScopeCreateMap constructs a request body from CreateOpts.
    95  func (opts CreateOpts) ToAddressScopeCreateMap() (map[string]any, error) {
    96  	return gophercloud.BuildRequestBody(opts, "address_scope")
    97  }
    98  
    99  // Create requests the creation of a new address-scope on the server.
   100  func Create(ctx context.Context, client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   101  	b, err := opts.ToAddressScopeCreateMap()
   102  	if err != nil {
   103  		r.Err = err
   104  		return
   105  	}
   106  	resp, err := client.Post(ctx, createURL(client), b, &r.Body, &gophercloud.RequestOpts{
   107  		OkCodes: []int{201},
   108  	})
   109  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   110  	return
   111  }
   112  
   113  // UpdateOptsBuilder allows extensions to add additional parameters to the
   114  // Update request.
   115  type UpdateOptsBuilder interface {
   116  	ToAddressScopeUpdateMap() (map[string]any, error)
   117  }
   118  
   119  // UpdateOpts represents options used to update an address-scope.
   120  type UpdateOpts struct {
   121  	// Name is the human-readable name of the address-scope.
   122  	Name *string `json:"name,omitempty"`
   123  
   124  	// Shared indicates whether this address-scope is shared across all projects.
   125  	Shared *bool `json:"shared,omitempty"`
   126  }
   127  
   128  // ToAddressScopeUpdateMap builds a request body from UpdateOpts.
   129  func (opts UpdateOpts) ToAddressScopeUpdateMap() (map[string]any, error) {
   130  	return gophercloud.BuildRequestBody(opts, "address_scope")
   131  }
   132  
   133  // Update accepts a UpdateOpts struct and updates an existing address-scope
   134  // using the values provided.
   135  func Update(ctx context.Context, c *gophercloud.ServiceClient, addressScopeID string, opts UpdateOptsBuilder) (r UpdateResult) {
   136  	b, err := opts.ToAddressScopeUpdateMap()
   137  	if err != nil {
   138  		r.Err = err
   139  		return
   140  	}
   141  	resp, err := c.Put(ctx, updateURL(c, addressScopeID), b, &r.Body, &gophercloud.RequestOpts{
   142  		OkCodes: []int{200},
   143  	})
   144  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   145  	return
   146  }
   147  
   148  // Delete accepts a unique ID and deletes the address-scope associated with it.
   149  func Delete(ctx context.Context, c *gophercloud.ServiceClient, id string) (r DeleteResult) {
   150  	resp, err := c.Delete(ctx, deleteURL(c, id), nil)
   151  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   152  	return
   153  }