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

     1  package subnetpools
     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  	ToSubnetPoolListQuery() (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 subnetpool attributes you want to see returned.
    17  // SortKey allows you to sort by a particular subnetpool 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  	DefaultQuota     int    `q:"default_quota"`
    24  	TenantID         string `q:"tenant_id"`
    25  	ProjectID        string `q:"project_id"`
    26  	DefaultPrefixLen int    `q:"default_prefixlen"`
    27  	MinPrefixLen     int    `q:"min_prefixlen"`
    28  	MaxPrefixLen     int    `q:"max_prefixlen"`
    29  	AddressScopeID   string `q:"address_scope_id"`
    30  	IPVersion        int    `q:"ip_version"`
    31  	Shared           *bool  `q:"shared"`
    32  	Description      string `q:"description"`
    33  	IsDefault        *bool  `q:"is_default"`
    34  	RevisionNumber   int    `q:"revision_number"`
    35  	Limit            int    `q:"limit"`
    36  	Marker           string `q:"marker"`
    37  	SortKey          string `q:"sort_key"`
    38  	SortDir          string `q:"sort_dir"`
    39  	Tags             string `q:"tags"`
    40  	TagsAny          string `q:"tags-any"`
    41  	NotTags          string `q:"not-tags"`
    42  	NotTagsAny       string `q:"not-tags-any"`
    43  }
    44  
    45  // ToSubnetPoolListQuery formats a ListOpts into a query string.
    46  func (opts ListOpts) ToSubnetPoolListQuery() (string, error) {
    47  	q, err := gophercloud.BuildQueryString(opts)
    48  	return q.String(), err
    49  }
    50  
    51  // List returns a Pager which allows you to iterate over a collection of
    52  // subnetpools. It accepts a ListOpts struct, which allows you to filter and sort
    53  // the returned collection for greater efficiency.
    54  //
    55  // Default policy settings return only the subnetpools owned by the project
    56  // of the user submitting the request, unless the user has the administrative role.
    57  func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    58  	url := listURL(c)
    59  	if opts != nil {
    60  		query, err := opts.ToSubnetPoolListQuery()
    61  		if err != nil {
    62  			return pagination.Pager{Err: err}
    63  		}
    64  		url += query
    65  	}
    66  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    67  		return SubnetPoolPage{pagination.LinkedPageBase{PageResult: r}}
    68  	})
    69  }
    70  
    71  // Get retrieves a specific subnetpool based on its ID.
    72  func Get(c *gophercloud.ServiceClient, id string) (r GetResult) {
    73  	resp, err := c.Get(getURL(c, id), &r.Body, nil)
    74  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    75  	return
    76  }
    77  
    78  // CreateOptsBuilder allows to add additional parameters to the
    79  // Create request.
    80  type CreateOptsBuilder interface {
    81  	ToSubnetPoolCreateMap() (map[string]interface{}, error)
    82  }
    83  
    84  // CreateOpts specifies parameters of a new subnetpool.
    85  type CreateOpts struct {
    86  	// Name is the human-readable name of the subnetpool.
    87  	Name string `json:"name"`
    88  
    89  	// DefaultQuota is the per-project quota on the prefix space
    90  	// that can be allocated from the subnetpool for project subnets.
    91  	DefaultQuota int `json:"default_quota,omitempty"`
    92  
    93  	// TenantID is the id of the Identity project.
    94  	TenantID string `json:"tenant_id,omitempty"`
    95  
    96  	// ProjectID is the id of the Identity project.
    97  	ProjectID string `json:"project_id,omitempty"`
    98  
    99  	// Prefixes is the list of subnet prefixes to assign to the subnetpool.
   100  	// Neutron API merges adjacent prefixes and treats them as a single prefix.
   101  	// Each subnet prefix must be unique among all subnet prefixes in all subnetpools
   102  	// that are associated with the address scope.
   103  	Prefixes []string `json:"prefixes"`
   104  
   105  	// DefaultPrefixLen is the size of the prefix to allocate when the cidr
   106  	// or prefixlen attributes are omitted when you create the subnet.
   107  	// Defaults to the MinPrefixLen.
   108  	DefaultPrefixLen int `json:"default_prefixlen,omitempty"`
   109  
   110  	// MinPrefixLen is the smallest prefix that can be allocated from a subnetpool.
   111  	// For IPv4 subnetpools, default is 8.
   112  	// For IPv6 subnetpools, default is 64.
   113  	MinPrefixLen int `json:"min_prefixlen,omitempty"`
   114  
   115  	// MaxPrefixLen is the maximum prefix size that can be allocated from the subnetpool.
   116  	// For IPv4 subnetpools, default is 32.
   117  	// For IPv6 subnetpools, default is 128.
   118  	MaxPrefixLen int `json:"max_prefixlen,omitempty"`
   119  
   120  	// AddressScopeID is the Neutron address scope to assign to the subnetpool.
   121  	AddressScopeID string `json:"address_scope_id,omitempty"`
   122  
   123  	// Shared indicates whether this network is shared across all projects.
   124  	Shared bool `json:"shared,omitempty"`
   125  
   126  	// Description is the human-readable description for the resource.
   127  	Description string `json:"description,omitempty"`
   128  
   129  	// IsDefault indicates if the subnetpool is default pool or not.
   130  	IsDefault bool `json:"is_default,omitempty"`
   131  }
   132  
   133  // ToSubnetPoolCreateMap constructs a request body from CreateOpts.
   134  func (opts CreateOpts) ToSubnetPoolCreateMap() (map[string]interface{}, error) {
   135  	return gophercloud.BuildRequestBody(opts, "subnetpool")
   136  }
   137  
   138  // Create requests the creation of a new subnetpool on the server.
   139  func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   140  	b, err := opts.ToSubnetPoolCreateMap()
   141  	if err != nil {
   142  		r.Err = err
   143  		return
   144  	}
   145  	resp, err := client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{
   146  		OkCodes: []int{201},
   147  	})
   148  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   149  	return
   150  }
   151  
   152  // UpdateOptsBuilder allows extensions to add additional parameters to the
   153  // Update request.
   154  type UpdateOptsBuilder interface {
   155  	ToSubnetPoolUpdateMap() (map[string]interface{}, error)
   156  }
   157  
   158  // UpdateOpts represents options used to update a network.
   159  type UpdateOpts struct {
   160  	// Name is the human-readable name of the subnetpool.
   161  	Name string `json:"name,omitempty"`
   162  
   163  	// DefaultQuota is the per-project quota on the prefix space
   164  	// that can be allocated from the subnetpool for project subnets.
   165  	DefaultQuota *int `json:"default_quota,omitempty"`
   166  
   167  	// TenantID is the id of the Identity project.
   168  	TenantID string `json:"tenant_id,omitempty"`
   169  
   170  	// ProjectID is the id of the Identity project.
   171  	ProjectID string `json:"project_id,omitempty"`
   172  
   173  	// Prefixes is the list of subnet prefixes to assign to the subnetpool.
   174  	// Neutron API merges adjacent prefixes and treats them as a single prefix.
   175  	// Each subnet prefix must be unique among all subnet prefixes in all subnetpools
   176  	// that are associated with the address scope.
   177  	Prefixes []string `json:"prefixes,omitempty"`
   178  
   179  	// DefaultPrefixLen is yhe size of the prefix to allocate when the cidr
   180  	// or prefixlen attributes are omitted when you create the subnet.
   181  	// Defaults to the MinPrefixLen.
   182  	DefaultPrefixLen int `json:"default_prefixlen,omitempty"`
   183  
   184  	// MinPrefixLen is the smallest prefix that can be allocated from a subnetpool.
   185  	// For IPv4 subnetpools, default is 8.
   186  	// For IPv6 subnetpools, default is 64.
   187  	MinPrefixLen int `json:"min_prefixlen,omitempty"`
   188  
   189  	// MaxPrefixLen is the maximum prefix size that can be allocated from the subnetpool.
   190  	// For IPv4 subnetpools, default is 32.
   191  	// For IPv6 subnetpools, default is 128.
   192  	MaxPrefixLen int `json:"max_prefixlen,omitempty"`
   193  
   194  	// AddressScopeID is the Neutron address scope to assign to the subnetpool.
   195  	AddressScopeID *string `json:"address_scope_id,omitempty"`
   196  
   197  	// Description is thehuman-readable description for the resource.
   198  	Description *string `json:"description,omitempty"`
   199  
   200  	// IsDefault indicates if the subnetpool is default pool or not.
   201  	IsDefault *bool `json:"is_default,omitempty"`
   202  }
   203  
   204  // ToSubnetPoolUpdateMap builds a request body from UpdateOpts.
   205  func (opts UpdateOpts) ToSubnetPoolUpdateMap() (map[string]interface{}, error) {
   206  	return gophercloud.BuildRequestBody(opts, "subnetpool")
   207  }
   208  
   209  // Update accepts a UpdateOpts struct and updates an existing subnetpool using the
   210  // values provided.
   211  func Update(c *gophercloud.ServiceClient, subnetPoolID string, opts UpdateOptsBuilder) (r UpdateResult) {
   212  	b, err := opts.ToSubnetPoolUpdateMap()
   213  	if err != nil {
   214  		r.Err = err
   215  		return
   216  	}
   217  	resp, err := c.Put(updateURL(c, subnetPoolID), b, &r.Body, &gophercloud.RequestOpts{
   218  		OkCodes: []int{200},
   219  	})
   220  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   221  	return
   222  }
   223  
   224  // Delete accepts a unique ID and deletes the subnetpool associated with it.
   225  func Delete(c *gophercloud.ServiceClient, id string) (r DeleteResult) {
   226  	resp, err := c.Delete(deleteURL(c, id), nil)
   227  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   228  	return
   229  }