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

     1  /*
     2  Package subnets contains functionality for working with Neutron subnet
     3  resources. A subnet represents an IP address block that can be used to
     4  assign IP addresses to virtual instances. Each subnet must have a CIDR and
     5  must be associated with a network. IPs can either be selected from the whole
     6  subnet CIDR or from allocation pools specified by the user.
     7  
     8  A subnet can also have a gateway, a list of DNS name servers, and host routes.
     9  This information is pushed to instances whose interfaces are associated with
    10  the subnet.
    11  
    12  Example to List Subnets
    13  
    14  	listOpts := subnets.ListOpts{
    15  		IPVersion: 4,
    16  	}
    17  
    18  	allPages, err := subnets.List(networkClient, listOpts).AllPages()
    19  	if err != nil {
    20  		panic(err)
    21  	}
    22  
    23  	allSubnets, err := subnets.ExtractSubnets(allPages)
    24  	if err != nil {
    25  		panic(err)
    26  	}
    27  
    28  	for _, subnet := range allSubnets {
    29  		fmt.Printf("%+v\n", subnet)
    30  	}
    31  
    32  Example to Create a Subnet With Specified Gateway
    33  
    34  	var gatewayIP = "192.168.199.1"
    35  	createOpts := subnets.CreateOpts{
    36  		NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a22",
    37  		IPVersion: 4,
    38  		CIDR:      "192.168.199.0/24",
    39  		GatewayIP: &gatewayIP,
    40  		AllocationPools: []subnets.AllocationPool{
    41  		  {
    42  		    Start: "192.168.199.2",
    43  		    End:   "192.168.199.254",
    44  		  },
    45  		},
    46  		DNSNameservers: []string{"foo"},
    47  		ServiceTypes: []string{"network:floatingip"},
    48  	}
    49  
    50  	subnet, err := subnets.Create(networkClient, createOpts).Extract()
    51  	if err != nil {
    52  		panic(err)
    53  	}
    54  
    55  Example to Create a Subnet With No Gateway
    56  
    57  	var noGateway = ""
    58  
    59  	createOpts := subnets.CreateOpts{
    60  		NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a23",
    61  		IPVersion: 4,
    62  		CIDR:      "192.168.1.0/24",
    63  		GatewayIP: &noGateway,
    64  		AllocationPools: []subnets.AllocationPool{
    65  			{
    66  				Start: "192.168.1.2",
    67  				End:   "192.168.1.254",
    68  			},
    69  		},
    70  		DNSNameservers: []string{},
    71  	}
    72  
    73  	subnet, err := subnets.Create(networkClient, createOpts).Extract()
    74  	if err != nil {
    75  		panic(err)
    76  	}
    77  
    78  Example to Create a Subnet With a Default Gateway
    79  
    80  	createOpts := subnets.CreateOpts{
    81  		NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a23",
    82  		IPVersion: 4,
    83  		CIDR:      "192.168.1.0/24",
    84  		AllocationPools: []subnets.AllocationPool{
    85  			{
    86  				Start: "192.168.1.2",
    87  				End:   "192.168.1.254",
    88  			},
    89  		},
    90  		DNSNameservers: []string{},
    91  	}
    92  
    93  	subnet, err := subnets.Create(networkClient, createOpts).Extract()
    94  	if err != nil {
    95  		panic(err)
    96  	}
    97  
    98  Example to Update a Subnet
    99  
   100  	subnetID := "db77d064-e34f-4d06-b060-f21e28a61c23"
   101  	dnsNameservers := []string{"8.8.8.8"}
   102  	serviceTypes := []string{"network:floatingip", "network:routed"}
   103  	name := "new_name"
   104  
   105  	updateOpts := subnets.UpdateOpts{
   106  		Name:           &name,
   107  		DNSNameservers: &dnsNameservers,
   108  		ServiceTypes:   &serviceTypes,
   109  	}
   110  
   111  	subnet, err := subnets.Update(networkClient, subnetID, updateOpts).Extract()
   112  	if err != nil {
   113  		panic(err)
   114  	}
   115  
   116  Example to Remove a Gateway From a Subnet
   117  
   118  	var noGateway = ""
   119  	subnetID := "db77d064-e34f-4d06-b060-f21e28a61c23"
   120  
   121  	updateOpts := subnets.UpdateOpts{
   122  		GatewayIP: &noGateway,
   123  	}
   124  
   125  	subnet, err := subnets.Update(networkClient, subnetID, updateOpts).Extract()
   126  	if err != nil {
   127  		panic(err)
   128  	}
   129  
   130  Example to Delete a Subnet
   131  
   132  	subnetID := "db77d064-e34f-4d06-b060-f21e28a61c23"
   133  	err := subnets.Delete(networkClient, subnetID).ExtractErr()
   134  	if err != nil {
   135  		panic(err)
   136  	}
   137  */
   138  package subnets