github.com/gophercloud/gophercloud@v1.11.0/openstack/networking/v2/extensions/layer3/addressscopes/requests.go (about)

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