subnets

package
v1.0.20 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 19, 2019 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package subnets contains functionality for working with Neutron subnet resources. A subnet represents an IP address block that can be used to assign IP addresses to virtual instances. Each subnet must have a CIDR and must be associated with a network. IPs can either be selected from the whole subnet CIDR or from allocation pools specified by the user.

A subnet can also have a gateway, a list of DNS name servers, and host routes. This information is pushed to instances whose interfaces are associated with the subnet.

Example to List Subnets

listOpts := subnets.ListOpts{
	IPVersion: 4,
}

allPages, err := subnets.List(networkClient, listOpts).AllPages()
if err != nil {
	panic(err)
}

allSubnets, err := subnets.ExtractSubnets(allPages)
if err != nil {
	panic(err)
}

for _, subnet := range allSubnets {
	fmt.Printf("%+v\n", subnet)
}

Example to Create a Subnet With Specified Gateway

var gatewayIP = "192.168.199.1"
createOpts := subnets.CreateOpts{
	NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a22",
	IPVersion: 4,
	CIDR:      "192.168.199.0/24",
	GatewayIP: &gatewayIP,
	AllocationPools: []subnets.AllocationPool{
	  {
	    Start: "192.168.199.2",
	    End:   "192.168.199.254",
	  },
	},
	DNSNameservers: []string{"foo"},
}

subnet, err := subnets.Create(networkClient, createOpts).Extract()
if err != nil {
	panic(err)
}

Example to Create a Subnet With No Gateway

var noGateway = ""

createOpts := subnets.CreateOpts{
	NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a23",
	IPVersion: 4,
	CIDR:      "192.168.1.0/24",
	GatewayIP: &noGateway,
	AllocationPools: []subnets.AllocationPool{
		{
			Start: "192.168.1.2",
			End:   "192.168.1.254",
		},
	},
	DNSNameservers: []string{},
}

subnet, err := subnets.Create(networkClient, createOpts).Extract()
if err != nil {
	panic(err)
}

Example to Create a Subnet With a Default Gateway

createOpts := subnets.CreateOpts{
	NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a23",
	IPVersion: 4,
	CIDR:      "192.168.1.0/24",
	AllocationPools: []subnets.AllocationPool{
		{
			Start: "192.168.1.2",
			End:   "192.168.1.254",
		},
	},
	DNSNameservers: []string{},
}

subnet, err := subnets.Create(networkClient, createOpts).Extract()
if err != nil {
	panic(err)
}

Example to Update a Subnet

subnetID := "db77d064-e34f-4d06-b060-f21e28a61c23"

updateOpts := subnets.UpdateOpts{
	Name:           "new_name",
	DNSNameservers: []string{"8.8.8.8},
}

subnet, err := subnets.Update(networkClient, subnetID, updateOpts).Extract()
if err != nil {
	panic(err)
}

Example to Remove a Gateway From a Subnet

var noGateway = ""
subnetID := "db77d064-e34f-4d06-b060-f21e28a61c23"

updateOpts := subnets.UpdateOpts{
	GatewayIP: &noGateway,
}

subnet, err := subnets.Update(networkClient, subnetID, updateOpts).Extract()
if err != nil {
	panic(err)
}

Example to Delete a Subnet

subnetID := "db77d064-e34f-4d06-b060-f21e28a61c23"
err := subnets.Delete(networkClient, subnetID).ExtractErr()
if err != nil {
	panic(err)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IDFromName

func IDFromName(client *gophercloud.ServiceClient, name string) (string, error)

IDFromName is a convenience function that returns a subnet's ID, given its name.

func List

List returns a Pager which allows you to iterate over a collection of subnets. It accepts a ListOpts struct, which allows you to filter and sort the returned collection for greater efficiency.

Default policy settings return only those subnets that are owned by the tenant who submits the request, unless the request is submitted by a user with administrative rights.

Types

type AllocationPool

type AllocationPool struct {
	Start string `json:"start"`
	End   string `json:"end"`
}

AllocationPool represents a sub-range of cidr available for dynamic allocation to ports, e.g. {Start: "10.0.0.2", End: "10.0.0.254"}

type CreateOpts

type CreateOpts struct {
	// NetworkID is the UUID of the network the subnet will be associated with.
	NetworkID string `json:"network_id" required:"true"`

	// CIDR is the address CIDR of the subnet.
	CIDR string `json:"cidr" required:"true"`

	// Name is a human-readable name of the subnet.
	Name string `json:"name,omitempty"`

	// The UUID of the tenant who owns the Subnet. Only administrative users
	// can specify a tenant UUID other than their own.
	TenantID string `json:"tenant_id,omitempty"`

	// AllocationPools are IP Address pools that will be available for DHCP.
	AllocationPools []AllocationPool `json:"allocation_pools,omitempty"`

	// GatewayIP sets gateway information for the subnet. Setting to nil will
	// cause a default gateway to automatically be created. Setting to an empty
	// string will cause the subnet to be created with no gateway. Setting to
	// an explicit address will set that address as the gateway.
	GatewayIP *string `json:"gateway_ip,omitempty"`

	// IPVersion is the IP version for the subnet.
	IPVersion gophercloud.IPVersion `json:"ip_version,omitempty"`

	// EnableDHCP will either enable to disable the DHCP service.
	EnableDHCP *bool `json:"enable_dhcp,omitempty"`

	// DNSNameservers are the nameservers to be set via DHCP.
	DNSNameservers []string `json:"dns_nameservers,omitempty"`

	// HostRoutes are any static host routes to be set via DHCP.
	HostRoutes []HostRoute `json:"host_routes,omitempty"`

	// The IPv6 address modes specifies mechanisms for assigning IPv6 IP addresses.
	IPv6AddressMode string `json:"ipv6_address_mode,omitempty"`

	// The IPv6 router advertisement specifies whether the networking service
	// should transmit ICMPv6 packets.
	IPv6RAMode string `json:"ipv6_ra_mode,omitempty"`
}

CreateOpts represents the attributes used when creating a new subnet.

func (CreateOpts) ToSubnetCreateMap

func (opts CreateOpts) ToSubnetCreateMap() (map[string]interface{}, error)

ToSubnetCreateMap builds a request body from CreateOpts.

type CreateOptsBuilder

type CreateOptsBuilder interface {
	ToSubnetCreateMap() (map[string]interface{}, error)
}

CreateOptsBuilder allows extensions to add additional parameters to the List request.

type CreateResult

type CreateResult struct {
	// contains filtered or unexported fields
}

CreateResult represents the result of a create operation. Call its Extract method to interpret it as a Subnet.

func Create

Create accepts a CreateOpts struct and creates a new subnet using the values provided. You must remember to provide a valid NetworkID, CIDR and IP version.

func (CreateResult) Extract

func (r CreateResult) Extract() (*Subnet, error)

Extract is a function that accepts a result and extracts a subnet resource.

type DeleteResult

type DeleteResult struct {
	gophercloud.ErrResult
}

DeleteResult represents the result of a delete operation. Call its ExtractErr method to determine if the request succeeded or failed.

func Delete

func Delete(c *gophercloud.ServiceClient, id string) (r DeleteResult)

Delete accepts a unique ID and deletes the subnet associated with it.

type GetResult

type GetResult struct {
	// contains filtered or unexported fields
}

GetResult represents the result of a get operation. Call its Extract method to interpret it as a Subnet.

func Get

func Get(c *gophercloud.ServiceClient, id string) (r GetResult)

Get retrieves a specific subnet based on its unique ID.

func (GetResult) Extract

func (r GetResult) Extract() (*Subnet, error)

Extract is a function that accepts a result and extracts a subnet resource.

type HostRoute

type HostRoute struct {
	DestinationCIDR string `json:"destination"`
	NextHop         string `json:"nexthop"`
}

HostRoute represents a route that should be used by devices with IPs from a subnet (not including local subnet route).

type ListOpts

type ListOpts struct {
	Name            string `q:"name"`
	EnableDHCP      *bool  `q:"enable_dhcp"`
	NetworkID       string `q:"network_id"`
	TenantID        string `q:"tenant_id"`
	IPVersion       int    `q:"ip_version"`
	GatewayIP       string `q:"gateway_ip"`
	CIDR            string `q:"cidr"`
	IPv6AddressMode string `q:"ipv6_address_mode"`
	IPv6RAMode      string `q:"ipv6_ra_mode"`
	ID              string `q:"id"`
	Limit           int    `q:"limit"`
	Marker          string `q:"marker"`
	SortKey         string `q:"sort_key"`
	SortDir         string `q:"sort_dir"`
}

ListOpts allows the filtering and sorting of paginated collections through the API. Filtering is achieved by passing in struct field values that map to the subnet attributes you want to see returned. SortKey allows you to sort by a particular subnet attribute. SortDir sets the direction, and is either `asc' or `desc'. Marker and Limit are used for pagination.

func (ListOpts) ToSubnetListQuery

func (opts ListOpts) ToSubnetListQuery() (string, error)

ToSubnetListQuery formats a ListOpts into a query string.

type ListOptsBuilder

type ListOptsBuilder interface {
	ToSubnetListQuery() (string, error)
}

ListOptsBuilder allows extensions to add additional parameters to the List request.

type Subnet

type Subnet struct {
	// UUID representing the subnet.
	ID string `json:"id"`

	// UUID of the parent network.
	NetworkID string `json:"network_id"`

	// Human-readable name for the subnet. Might not be unique.
	Name string `json:"name"`

	// IP version, either `4' or `6'.
	IPVersion int `json:"ip_version"`

	// CIDR representing IP range for this subnet, based on IP version.
	CIDR string `json:"cidr"`

	// Default gateway used by devices in this subnet.
	GatewayIP string `json:"gateway_ip"`

	// DNS name servers used by hosts in this subnet.
	DNSNameservers []string `json:"dns_nameservers"`

	// Sub-ranges of CIDR available for dynamic allocation to ports.
	// See AllocationPool.
	AllocationPools []AllocationPool `json:"allocation_pools"`

	// Routes that should be used by devices with IPs from this subnet
	// (not including local subnet route).
	HostRoutes []HostRoute `json:"host_routes"`

	// Specifies whether DHCP is enabled for this subnet or not.
	EnableDHCP bool `json:"enable_dhcp"`

	// Owner of network.
	TenantID string `json:"tenant_id"`

	// The IPv6 address modes specifies mechanisms for assigning IPv6 IP addresses.
	IPv6AddressMode string `json:"ipv6_address_mode"`

	// The IPv6 router advertisement specifies whether the networking service
	// should transmit ICMPv6 packets.
	IPv6RAMode string `json:"ipv6_ra_mode"`
}

Subnet represents a subnet. See package documentation for a top-level description of what this is.

func ExtractSubnets

func ExtractSubnets(r pagination.Page) ([]Subnet, error)

ExtractSubnets accepts a Page struct, specifically a SubnetPage struct, and extracts the elements into a slice of Subnet structs. In other words, a generic collection is mapped into a relevant slice.

type SubnetPage

type SubnetPage struct {
	pagination.LinkedPageBase
}

SubnetPage is the page returned by a pager when traversing over a collection of subnets.

func (SubnetPage) IsEmpty

func (r SubnetPage) IsEmpty() (bool, error)

IsEmpty checks whether a SubnetPage struct is empty.

func (SubnetPage) NextPageURL

func (r SubnetPage) NextPageURL() (string, error)

NextPageURL is invoked when a paginated collection of subnets has reached the end of a page and the pager seeks to traverse over a new one. In order to do this, it needs to construct the next page's URL.

type UpdateOpts

type UpdateOpts struct {
	// Name is a human-readable name of the subnet.
	Name string `json:"name,omitempty"`

	// AllocationPools are IP Address pools that will be available for DHCP.
	AllocationPools []AllocationPool `json:"allocation_pools,omitempty"`

	// GatewayIP sets gateway information for the subnet. Setting to nil will
	// cause a default gateway to automatically be created. Setting to an empty
	// string will cause the subnet to be created with no gateway. Setting to
	// an explicit address will set that address as the gateway.
	GatewayIP *string `json:"gateway_ip,omitempty"`

	// DNSNameservers are the nameservers to be set via DHCP.
	DNSNameservers []string `json:"dns_nameservers,omitempty"`

	// HostRoutes are any static host routes to be set via DHCP.
	HostRoutes []HostRoute `json:"host_routes,omitempty"`

	// EnableDHCP will either enable to disable the DHCP service.
	EnableDHCP *bool `json:"enable_dhcp,omitempty"`
}

UpdateOpts represents the attributes used when updating an existing subnet.

func (UpdateOpts) ToSubnetUpdateMap

func (opts UpdateOpts) ToSubnetUpdateMap() (map[string]interface{}, error)

ToSubnetUpdateMap builds a request body from UpdateOpts.

type UpdateOptsBuilder

type UpdateOptsBuilder interface {
	ToSubnetUpdateMap() (map[string]interface{}, error)
}

UpdateOptsBuilder allows extensions to add additional parameters to the Update request.

type UpdateResult

type UpdateResult struct {
	// contains filtered or unexported fields
}

UpdateResult represents the result of an update operation. Call its Extract method to interpret it as a Subnet.

func Update

Update accepts a UpdateOpts struct and updates an existing subnet using the values provided.

func (UpdateResult) Extract

func (r UpdateResult) Extract() (*Subnet, error)

Extract is a function that accepts a result and extracts a subnet resource.

Directories

Path Synopsis
subnets unit tests
subnets unit tests

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL