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

     1  package subnets
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/gophercloud/gophercloud"
     7  	"github.com/gophercloud/gophercloud/pagination"
     8  )
     9  
    10  // ListOptsBuilder allows extensions to add additional parameters to the
    11  // List request.
    12  type ListOptsBuilder interface {
    13  	ToSubnetListQuery() (string, error)
    14  }
    15  
    16  // ListOpts allows the filtering and sorting of paginated collections through
    17  // the API. Filtering is achieved by passing in struct field values that map to
    18  // the subnet attributes you want to see returned. SortKey allows you to sort
    19  // by a particular subnet attribute. SortDir sets the direction, and is either
    20  // `asc' or `desc'. Marker and Limit are used for pagination.
    21  type ListOpts struct {
    22  	Name            string `q:"name"`
    23  	Description     string `q:"description"`
    24  	EnableDHCP      *bool  `q:"enable_dhcp"`
    25  	NetworkID       string `q:"network_id"`
    26  	TenantID        string `q:"tenant_id"`
    27  	ProjectID       string `q:"project_id"`
    28  	IPVersion       int    `q:"ip_version"`
    29  	GatewayIP       string `q:"gateway_ip"`
    30  	CIDR            string `q:"cidr"`
    31  	IPv6AddressMode string `q:"ipv6_address_mode"`
    32  	IPv6RAMode      string `q:"ipv6_ra_mode"`
    33  	ID              string `q:"id"`
    34  	SubnetPoolID    string `q:"subnetpool_id"`
    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  // ToSubnetListQuery formats a ListOpts into a query string.
    46  func (opts ListOpts) ToSubnetListQuery() (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  // subnets. 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 those subnets that are owned by the tenant
    56  // who submits the request, unless the request is submitted by a user with
    57  // administrative rights.
    58  func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    59  	url := listURL(c)
    60  	if opts != nil {
    61  		query, err := opts.ToSubnetListQuery()
    62  		if err != nil {
    63  			return pagination.Pager{Err: err}
    64  		}
    65  		url += query
    66  	}
    67  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    68  		return SubnetPage{pagination.LinkedPageBase{PageResult: r}}
    69  	})
    70  }
    71  
    72  // Get retrieves a specific subnet based on its unique ID.
    73  func Get(c *gophercloud.ServiceClient, id string) (r GetResult) {
    74  	resp, err := c.Get(getURL(c, id), &r.Body, nil)
    75  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    76  	return
    77  }
    78  
    79  // CreateOptsBuilder allows extensions to add additional parameters to the
    80  // List request.
    81  type CreateOptsBuilder interface {
    82  	ToSubnetCreateMap() (map[string]interface{}, error)
    83  }
    84  
    85  // CreateOpts represents the attributes used when creating a new subnet.
    86  type CreateOpts struct {
    87  	// NetworkID is the UUID of the network the subnet will be associated with.
    88  	NetworkID string `json:"network_id" required:"true"`
    89  
    90  	// CIDR is the address CIDR of the subnet.
    91  	CIDR string `json:"cidr,omitempty"`
    92  
    93  	// Name is a human-readable name of the subnet.
    94  	Name string `json:"name,omitempty"`
    95  
    96  	// Description of the subnet.
    97  	Description string `json:"description,omitempty"`
    98  
    99  	// The UUID of the project who owns the Subnet. Only administrative users
   100  	// can specify a project UUID other than their own.
   101  	TenantID string `json:"tenant_id,omitempty"`
   102  
   103  	// The UUID of the project who owns the Subnet. Only administrative users
   104  	// can specify a project UUID other than their own.
   105  	ProjectID string `json:"project_id,omitempty"`
   106  
   107  	// AllocationPools are IP Address pools that will be available for DHCP.
   108  	AllocationPools []AllocationPool `json:"allocation_pools,omitempty"`
   109  
   110  	// GatewayIP sets gateway information for the subnet. Setting to nil will
   111  	// cause a default gateway to automatically be created. Setting to an empty
   112  	// string will cause the subnet to be created with no gateway. Setting to
   113  	// an explicit address will set that address as the gateway.
   114  	GatewayIP *string `json:"gateway_ip,omitempty"`
   115  
   116  	// IPVersion is the IP version for the subnet.
   117  	IPVersion gophercloud.IPVersion `json:"ip_version,omitempty"`
   118  
   119  	// EnableDHCP will either enable to disable the DHCP service.
   120  	EnableDHCP *bool `json:"enable_dhcp,omitempty"`
   121  
   122  	// DNSNameservers are the nameservers to be set via DHCP.
   123  	DNSNameservers []string `json:"dns_nameservers,omitempty"`
   124  
   125  	// ServiceTypes are the service types associated with the subnet.
   126  	ServiceTypes []string `json:"service_types,omitempty"`
   127  
   128  	// HostRoutes are any static host routes to be set via DHCP.
   129  	HostRoutes []HostRoute `json:"host_routes,omitempty"`
   130  
   131  	// The IPv6 address modes specifies mechanisms for assigning IPv6 IP addresses.
   132  	IPv6AddressMode string `json:"ipv6_address_mode,omitempty"`
   133  
   134  	// The IPv6 router advertisement specifies whether the networking service
   135  	// should transmit ICMPv6 packets.
   136  	IPv6RAMode string `json:"ipv6_ra_mode,omitempty"`
   137  
   138  	// SubnetPoolID is the id of the subnet pool that subnet should be associated to.
   139  	SubnetPoolID string `json:"subnetpool_id,omitempty"`
   140  
   141  	// Prefixlen is used when user creates a subnet from the subnetpool. It will
   142  	// overwrite the "default_prefixlen" value of the referenced subnetpool.
   143  	Prefixlen int `json:"prefixlen,omitempty"`
   144  }
   145  
   146  // ToSubnetCreateMap builds a request body from CreateOpts.
   147  func (opts CreateOpts) ToSubnetCreateMap() (map[string]interface{}, error) {
   148  	b, err := gophercloud.BuildRequestBody(opts, "subnet")
   149  	if err != nil {
   150  		return nil, err
   151  	}
   152  
   153  	if m := b["subnet"].(map[string]interface{}); m["gateway_ip"] == "" {
   154  		m["gateway_ip"] = nil
   155  	}
   156  
   157  	return b, nil
   158  }
   159  
   160  // Create accepts a CreateOpts struct and creates a new subnet using the values
   161  // provided. You must remember to provide a valid NetworkID, CIDR and IP
   162  // version.
   163  func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   164  	b, err := opts.ToSubnetCreateMap()
   165  	if err != nil {
   166  		r.Err = err
   167  		return
   168  	}
   169  	resp, err := c.Post(createURL(c), b, &r.Body, nil)
   170  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   171  	return
   172  }
   173  
   174  // UpdateOptsBuilder allows extensions to add additional parameters to the
   175  // Update request.
   176  type UpdateOptsBuilder interface {
   177  	ToSubnetUpdateMap() (map[string]interface{}, error)
   178  }
   179  
   180  // UpdateOpts represents the attributes used when updating an existing subnet.
   181  type UpdateOpts struct {
   182  	// Name is a human-readable name of the subnet.
   183  	Name *string `json:"name,omitempty"`
   184  
   185  	// Description of the subnet.
   186  	Description *string `json:"description,omitempty"`
   187  
   188  	// AllocationPools are IP Address pools that will be available for DHCP.
   189  	AllocationPools []AllocationPool `json:"allocation_pools,omitempty"`
   190  
   191  	// GatewayIP sets gateway information for the subnet. Setting to nil will
   192  	// cause a default gateway to automatically be created. Setting to an empty
   193  	// string will cause the subnet to be created with no gateway. Setting to
   194  	// an explicit address will set that address as the gateway.
   195  	GatewayIP *string `json:"gateway_ip,omitempty"`
   196  
   197  	// DNSNameservers are the nameservers to be set via DHCP.
   198  	DNSNameservers *[]string `json:"dns_nameservers,omitempty"`
   199  
   200  	// ServiceTypes are the service types associated with the subnet.
   201  	ServiceTypes *[]string `json:"service_types,omitempty"`
   202  
   203  	// HostRoutes are any static host routes to be set via DHCP.
   204  	HostRoutes *[]HostRoute `json:"host_routes,omitempty"`
   205  
   206  	// EnableDHCP will either enable to disable the DHCP service.
   207  	EnableDHCP *bool `json:"enable_dhcp,omitempty"`
   208  
   209  	// RevisionNumber implements extension:standard-attr-revisions. If != "" it
   210  	// will set revision_number=%s. If the revision number does not match, the
   211  	// update will fail.
   212  	RevisionNumber *int `json:"-" h:"If-Match"`
   213  }
   214  
   215  // ToSubnetUpdateMap builds a request body from UpdateOpts.
   216  func (opts UpdateOpts) ToSubnetUpdateMap() (map[string]interface{}, error) {
   217  	b, err := gophercloud.BuildRequestBody(opts, "subnet")
   218  	if err != nil {
   219  		return nil, err
   220  	}
   221  
   222  	if m := b["subnet"].(map[string]interface{}); m["gateway_ip"] == "" {
   223  		m["gateway_ip"] = nil
   224  	}
   225  
   226  	return b, nil
   227  }
   228  
   229  // Update accepts a UpdateOpts struct and updates an existing subnet using the
   230  // values provided.
   231  func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   232  	b, err := opts.ToSubnetUpdateMap()
   233  	if err != nil {
   234  		r.Err = err
   235  		return
   236  	}
   237  	h, err := gophercloud.BuildHeaders(opts)
   238  	if err != nil {
   239  		r.Err = err
   240  		return
   241  	}
   242  	for k := range h {
   243  		if k == "If-Match" {
   244  			h[k] = fmt.Sprintf("revision_number=%s", h[k])
   245  		}
   246  	}
   247  
   248  	resp, err := c.Put(updateURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
   249  		MoreHeaders: h,
   250  		OkCodes:     []int{200, 201},
   251  	})
   252  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   253  	return
   254  }
   255  
   256  // Delete accepts a unique ID and deletes the subnet associated with it.
   257  func Delete(c *gophercloud.ServiceClient, id string) (r DeleteResult) {
   258  	resp, err := c.Delete(deleteURL(c, id), nil)
   259  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   260  	return
   261  }