github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/networking/v2/extensions/subnetpools/requests.go (about)

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