• CLI

sdk
  1. Help Center
  2. CLI
  3. Developer Guide
  4. SDK
  5. Go
  6. VPC Go SDK User Guide

VPC Go SDK User Guide

Service Client

To interact with OpenStack APIs, transfer the identity credentials to the provider. After that, if you want to retrieve any information about the network service, you need to make a call to NewNetworkV2 to create a network service client and then use required SDKs.

client, err := openstack.NewNetworkV2(provider, gophercloud.EndpointOpts{
 Name:   "neutron",
 Region: "RegionName",
})

Networks

A network is the core resource of OpenStack Neutron APIs. Compared with the physical network, the Neutron network is similar to a VLAN and is an independent broadcast domain on a large layer-2 network. As a virtualized partition, a virtual network can share data packets with other networks only through one or more routers.

Create a network.

You can use a specified name to create a network.

import(
"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
)
func TestNetworksCreate(t *testing.T) {
createOpts := networks.CreateOpts{
    Name               : "Sample",
}

// Execute the operation and get back a networks.Network struct
network, err := networks.Create(client, createOpts).Extract()
}

List networks.

To retrieve information about a specified network, use the network name. The returned networks.Network structure contains ID, Name, Status, Subnets, Shared, and TenandID fields.

func TestNetworksListByName(t *testing.T) {

// We have the option of filtering the network list. If we want the full
// collection, leave it as an empty struct
opts := networks.ListOpts{Name: "Sample"}

// Retrieve a pager (i.e. a paginated collection)
allPages, err := networks.List(client, listOpts).AllPages()

// Define an anonymous function to be executed on each page's iteration
var allNetworks []networks.ListOpts
err = networks.ExtractNetworksInto(allPages, &allNetworks)

for _, network := range allNetworks {
    // " network " will be a networks.Network
}
}

Get details about an existing network.

You can use the UUID to obtain information about a specified network.

func TestNetworksShow(t *testing.T) {
// We need to know what the UUID of our network is and pass it in as a string.
network, err := networks.Get(client, "network_id").Extract()
}

Update an existing network.

You can update the network name.

func TestNetworksUpdate(t *testing.T) {

updateOpts := &networks.UpdateOpts{
    Name    : "Sample",
     Shared  : true,
}

// Like Get(), we need the UUID in string form
network, err := networks.Update(client, "network_id", updateOpts).Extract()
}

Delete a network.

You can use the UUID to delete a specified network.

func TestNetworksDelete(t *testing.T) {

// We need to know what the UUID of our network is and pass it in as a string.
err := networks.Delete(client, "network_id").ExtractErr()
}

Subnets

A subnet is an IP address block. An IP address is assigned to a device on the network. In Neutron, a device refers to a VM (computing instance). Each subnet must have a CIDR and be associated with a network.

Create a subnet.

You can use the network name and CIDR to create a subnet.

import(
"github.com/gophercloud/gophercloud/openstack/networking/v2/subnets"
"github.com/gophercloud/gophercloud/pagination"
)

func TestSubnetCreate(t *testing.T) {

// You must associate a new subnet with an existing network - to do this you
// need its UUID. You must also provide a well-formed CID
createOpts := subnets.CreateOpts{
     Name           :  "Sample",
     NetworkID      :  "network_id",
     CIDR           :  subnetCIDR,
}

// Execute the operation and get back a subnets.Subnet struct
subnet, err := subnets.Create(client, createOpts).Extract()
}

List subnets.

To retrieve the subnet information, you need to use the network name (a character string) associated with the subnet. The returned subnets.Subnet structure contains ID, Name, Cidr, and TenandID fields.

func TestSubnetsListByNetID(t *testing.T) {

// We have the option of filtering subnets. For example, we may want to return
// every subnet that belongs to a specific network. Or filter again by name.
opts := subnets.ListOpts{NetworkID: "some_uuid"}

// Retrieve a pager (i.e. a paginated collection)
pager := subnets.List(client, opts)

// Define an anonymous function to be executed on each page's iteration
err := pager.EachPage(func(page pagination.Page) (bool, error) {
    subnetList, err := subnets.ExtractSubnets(page)

    for _, subnet := range subnetList {
           // "subnet" will be a subnets.Subnet
    }
})
}

Get details about an existing subnet.

You can use the UUID to obtain information about a specific subnet.

func TestSubnetShow(t *testing.T) {
// We need to know what the UUID of our subnet is and pass it in as a string
subnet, err := subnets.Get(client, "subnet_id").Extract()
}

Update an existing subnet.

You can update the subnet name, gateway IP address, DNS domain name server, host route, and enable DHCP status.

func TestSubnetUpdate(t *testing.T) {

opts := subnets.UpdateOpts{Name: "Sample"}
subnet, err = subnets.Update(client, "subnet_id", opts).Extract() 
}

Delete a subnet.

You can use the UUID to delete a specified subnet.

func TestSubnetDelete(t *testing.T) {

// We need to know what the UUID of our network is and pass it in as a string
result := subnets.Delete(client, "subnet_id") 
}

Ports

Before ascertaining what a port is, you need to know what is a network switch (including virtual and physical switches). A network switch connects two different network segments. A port is used to connect a device and the switch. In this section, a device refers to a VM. You can refer to the relevant literature for knowledge of these terms.

Create a port.

import(
"github.com/gophercloud/gophercloud/openstack/networking/v2/ports"
"github.com/gophercloud/gophercloud/pagination"
)

func TestPortsCreate(t *testing.T) {

// You must associate a new port with an existing network - to do this you
// need its UUID. Also notice the "FixedIPs" field; this allows you to specify
// either a specific IP to use for this port, or the subnet ID from which a
// random free IP is selected.
opts := ports.CreateOpts{
    NetworkID:    "network_id",
    Name:         "my_port",
    AdminStateUp: ports.Up,
    FixedIPs:     []ports.IP{ports.IP{SubnetID: "subnet_id"}},
}

// Execute the operation and get back a subnets.Subnet struct
port, err := ports.Create(client, opts).Extract()
}

List ports.

func TestPortsListByNetID(t *testing.T) {

// We have the option of filtering ports. For example, we may want to return
// every port that belongs to a specific network. Or filter again by MAC address.
opts := ports.ListOpts{NetworkID: "some_uuid", MACAddress: "some_addr"}

// Retrieve a pager (i.e. a paginated collection)
pager := ports.List(client, opts)

// Define an anonymous function to be executed on each page's iteration
err := pager.EachPage(func(page pagination.Page) (bool, error) {
    portList, err := ports.ExtractPorts(page)

    for _, port := range portList {
           // "port" will be a ports.Port
    }
})
}

Get details about an existing port.

You can use the UUID to obtain information about a specific port.

func TestPortShow(t *testing.T) {
// We need to know what the UUID of our port is and pass it in as a string.
port, err := ports.Get(client, "port_id").Extract()
}

Update an existing port.

You can update the port name, management status, fixed IP address, device ID, and security group.

func TestPortUpdate(t *testing.T) {

opts := ports.UpdateOpts{Name: "new_port_name"}
port, err = ports.Update(client, "port_id", opts).Extract()
}

Delete a port.

You can use the UUID to delete a specified port.

func TestPortDelete(t *testing.T) {

// We need to know what the UUID of our network is and pass it in as a string.
result := ports.Delete(client, "port_id").ExtractErr()
}

Routers

Create a router.

import(
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/routers"
networking "github.com/gophercloud/gophercloud/openstack/networking/v2"
"github.com/gophercloud/gophercloud/pagination"
)

func TestLayer3ExternalRouterCreate(t *testing.T) {
// You can specify router pamameter
createOpts := routers.CreateOpts{
    Name                 : "Sample",
}
// Execute the operation and get back a routers.Router struct
router, err = routers.Create(client, createOpts).Extract()
}

Add an interface to a router.

func TestLayer3RouterAddInterfaceByPortID(t *testing.T) {
    // You can specify router pamameter
    createOpts := routers.CreateOpts{
      Name               : "Sample",
    }
     // Execute the operation and get back a routers.Router struct
     router, err = routers.Create(client, createOpts).Extract()

     // Create port
     port, err := networking.CreatePort(t, client, network.ID, subnet.ID)
    
    // Add an interface to a router
    aiOpts := routers.AddInterfaceOpts{
           PortID: "port_id",
    }
     iface, err := routers.AddInterface(client, router.ID, aiOpts).Extract()
}

Remove an interface from a router.

func TestLayer3RouterAddInterfaceByPortID(t *testing.T) {
    // Remove an interface from a router
    aiOpts := routers.RemoveInterfaceOpts{
           PortID: "port_id",
    }
     iface, err := routers.RemoveInterface(client, "router_id", aiOpts).Extract()
}

List routers.

func TestLayer3RouterListByName(t *testing.T) {

// We have the option of filtering routers. For example, we may want to 
// filter by name.
listOpts := routers.ListOpts{
    Name: router.Name,
}

// Retrieve a pager (i.e. a paginated collection)
pager := routers.List(client, opts)

// Define an anonymous function to be executed on each page's iteration
err := pager.EachPage(func(page pagination.Page) (bool, error) {
    routerList, err := routers.ExtractRouters(page)

    for _, router := range routerList {
           // " router " will be a routers.Router
    }
})
}

Get details about an existing router.

You can use the UUID to obtain information about a specific router.

func TestLayer3RouterShow(t *testing.T) {
// We need to know what the UUID of our routers is and pass it in as a string
subnet, err := routers.Get(client, "router_id").Extract()
}

Update an existing router.

func TestLayer3ExternalRouterUpdate(t *testing.T) {

opts := routers.UpdateOpts{Name: "Sample"}
subnet, err = routers.Update(client, "router_id", opts).Extract() 
}

Delete a router.

You can use the UUID to delete a specified router.

func TestLayer3RouterDelete (t *testing.T) {

// We need to know what the UUID of our routers is and pass it in as a string
result := routers.Delete(client, "router_id").ExtractErr()
}

Floating IP Addresses

Create a floating IP address.

import(
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips"
"github.com/gophercloud/gophercloud/pagination"
)

func TestLayer3FloatingIPsCreate(t *testing.T) {

createOpts := &floatingips.CreateOpts{
    FloatingNetworkID: "external_network_id",
}
// Execute the operation and get back a floatingips.Floatingip struct
floatingIP, err := floatingips.Create(client, createOpts).Extract()
}

List floating IP addresses.

func TestLayer3FloatingIPsListByID(t *testing.T) {

listOpts := floatingips.ListOpts{
    ID: "floatingip_id",
}

// Retrieve a pager (i.e. a paginated collection)
pager := floatingips.List(client, opts)

// Define an anonymous function to be executed on each page's iteration
err := pager.EachPage(func(page pagination.Page) (bool, error) {
    floatingipList, err := floatingips.ExtractFloatingIPs(page)

    for _, floatingip:= range floatingipList {
           // "floatingip" will be a floatingips.Floatingip
    }
})
}

Get details about an existing floating IP address.

You can use the UUID to obtain information about a specified floating IP address.

func TestLayer3FloatingIPShow(t *testing.T) {
// We need to know what the UUID of our floatingip is and pass it in as a string
floatingip, err := floatingips.Get(client, "floatingip_id").Extract()
}

Update an existing floating IP address.

func TestLayer3FloatingIPsUpdate(t *testing.T) {
opts := floatingips.UpdateOpts{Name: "Sample"}
floatingip, err = floatingips.Update(client, "floatingip_id", opts).Extract() 
}

Delete a floating IP address.

You can use the UUID to delete a specified floating IP address.

func TestLayer3FloatingIPsDelete(t *testing.T) {

// We need to know what the UUID of our floatingip is and pass it in as a string
result := floatingip.Delete(client, "floatingip_id").ExtractErr()
}

Security Groups

Create a security group.

import (   "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/groups"   ) 

// Create is an operation which provisions a new security group with default
// security group rules for the IPv4 and IPv6 ether types.
func TestSecGroupsCreate(t *testing.T) {
createOpts:= groups. CreateOpts{Name: "Security", Description: "Create Group"} 
secgroup, err := groups.Create(client, createOpts).Extract()
}

List security groups.

func TestCreateSecGroupsList(t *testing.T) {
// We have the option of filtering the Security Groups list. If we want the full       
// collection, leave it as an empty struct       
opts := groups.ListOpts{TenantID: "tenant_id", limit: 2}         
// Retrieve a pager (i.e. a paginated collection)      allPages,err:= groups.List(client, opts).AllPages()    // Define an anonymous function to be executed on each page's iteration      
securityGroupsList, err := groups.ExtractGroups(allPages)         
for _, secgroup := range securityGroupsList {      
// " secgroup " will be a groups.SecGroup      
} 
}

Get details about a specific security group.

You can use Get to search for a specific security group based on its unique ID.

func TestCreateSecGroupsShow(t *testing.T) {
// Get back a groups.Secgroup struct   
group, err := groups.Get(client, "secgroup_id").Extract()
}

Update a security group.

func TestSecGroupsUpdate(t *testing.T) {

// Update is an operation which updates an existing security group.  
ops:= groups.UpdateOpts{Name:"UpdateGroup", Description:"Update a Group"}
updatedSecurityGroup, err := groups.Update(client, "secgroup_id", updateOpts).Extract()
}

Security Group Rules

Create a security group rule.

You can use Create to add a security group rule and associate it with an existing security group. The ID of the current security group is specified by CreateOpts.

import (  
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/rules"   ) 

func TestCreateSecurityGroupRule(t *testing.T, client *gophercloud.ServiceClient) {
    createOpts := rules.CreateOpts{
           Direction:    "ingress",
           EtherType:    "IPv4",
           SecGroupID:   "secGroup_id",
           PortRangeMin: 10,
           PortRangeMax: 20,
           Protocol:     rules.ProtocolTCP,
    }
    rule, err := rules.Create(client, createOpts).Extract()
}

List security group rules.

You can use List to return the list of security group rules. You can use ListOpts to configure filtering rules to improve efficiency.

func TestSecGroupsRuleListBySgID(t *testing.T){
    ListOpts := rules.ListOpts{
           SecGroupID : "secgroup_id",
    }
    // Retrieve a pager (i.e. a paginated collection)
    allPages, err := rules.List(client, ListOpts).AllPages()

    // Define an anonymous function to be executed on each page's iteration
    allRules, err := rules.ExtractRules(allPages)

                                for _, rule := range allRules {
    // " rule " will be a rules.SecGroupRule
    }
}

Get details about a specific security group rule.

You can use Get to retrieve a specific security group rule based on its unique ID.

func TestSecGroupsRuleShow(t *testing.T) {
    SecurityGroupRule, err := rules.Get(client, "securitygrouprule_id").Extract()
      }

Delete a security group rule.

You can use Delete to permanently delete a specific security group rule based on its unique ID.

func TestDeleteSecurityGroupRule(t *testing.T, client *gophercloud.ServiceClient{
err = rules.Delete(client,"rule_id").ExtractErr()
}