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

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