Joe Topjian | 7c8dd02 | 2016-09-01 12:02:04 -0600 | [diff] [blame^] | 1 | package v2 |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/gophercloud/gophercloud" |
| 8 | "github.com/gophercloud/gophercloud/acceptance/tools" |
| 9 | "github.com/gophercloud/gophercloud/openstack/networking/v2/apiversions" |
| 10 | "github.com/gophercloud/gophercloud/openstack/networking/v2/networks" |
| 11 | "github.com/gophercloud/gophercloud/openstack/networking/v2/ports" |
| 12 | "github.com/gophercloud/gophercloud/openstack/networking/v2/subnets" |
| 13 | ) |
| 14 | |
| 15 | // CreateNetwork will create basic network. An error will be returned if the |
| 16 | // network could not be created. |
| 17 | func CreateNetwork(t *testing.T, client *gophercloud.ServiceClient) (*networks.Network, error) { |
| 18 | networkName := tools.RandomString("TESTACC-", 8) |
| 19 | createOpts := networks.CreateOpts{ |
| 20 | Name: networkName, |
| 21 | AdminStateUp: gophercloud.Enabled, |
| 22 | } |
| 23 | |
| 24 | t.Logf("Attempting to create network: %s", networkName) |
| 25 | |
| 26 | network, err := networks.Create(client, createOpts).Extract() |
| 27 | if err != nil { |
| 28 | return network, err |
| 29 | } |
| 30 | |
| 31 | t.Logf("Successfully created network.") |
| 32 | return network, nil |
| 33 | } |
| 34 | |
| 35 | // CreatePort will create a port on the specified subnet. An error will be |
| 36 | // returned if the port could not be created. |
| 37 | func CreatePort(t *testing.T, client *gophercloud.ServiceClient, networkID, subnetID string) (*ports.Port, error) { |
| 38 | portName := tools.RandomString("TESTACC-", 8) |
| 39 | |
| 40 | t.Logf("Attempting to create port: %s", portName) |
| 41 | |
| 42 | createOpts := ports.CreateOpts{ |
| 43 | NetworkID: networkID, |
| 44 | Name: portName, |
| 45 | AdminStateUp: gophercloud.Disabled, |
| 46 | FixedIPs: []ports.IP{ports.IP{SubnetID: subnetID}}, |
| 47 | } |
| 48 | |
| 49 | port, err := ports.Create(client, createOpts).Extract() |
| 50 | if err != nil { |
| 51 | return port, err |
| 52 | } |
| 53 | |
| 54 | t.Logf("Successfully created port: %s", portName) |
| 55 | |
| 56 | return port, nil |
| 57 | } |
| 58 | |
| 59 | // CreateSubnet will create a subnet on the specified Network ID. An error |
| 60 | // will be returned if the subnet could not be created. |
| 61 | func CreateSubnet(t *testing.T, client *gophercloud.ServiceClient, networkID string) (*subnets.Subnet, error) { |
| 62 | subnetName := tools.RandomString("TESTACC-", 8) |
| 63 | subnetOctet := tools.RandomInt(1, 250) |
| 64 | subnetCIDR := fmt.Sprintf("192.168.%d.0/24", subnetOctet) |
| 65 | subnetGateway := fmt.Sprintf("192.168.%d.1", subnetOctet) |
| 66 | iFalse := false |
| 67 | createOpts := subnets.CreateOpts{ |
| 68 | NetworkID: networkID, |
| 69 | CIDR: subnetCIDR, |
| 70 | IPVersion: 4, |
| 71 | Name: subnetName, |
| 72 | EnableDHCP: &iFalse, |
| 73 | GatewayIP: &subnetGateway, |
| 74 | } |
| 75 | |
| 76 | t.Logf("Attempting to create subnet: %s", subnetName) |
| 77 | |
| 78 | subnet, err := subnets.Create(client, createOpts).Extract() |
| 79 | if err != nil { |
| 80 | return subnet, err |
| 81 | } |
| 82 | |
| 83 | t.Logf("Successfully created subnet.") |
| 84 | return subnet, nil |
| 85 | } |
| 86 | |
| 87 | // DeleteNetwork will delete a network with a specified ID. A fatal error will |
| 88 | // occur if the delete was not successful. This works best when used as a |
| 89 | // deferred function. |
| 90 | func DeleteNetwork(t *testing.T, client *gophercloud.ServiceClient, networkID string) { |
| 91 | t.Logf("Attempting to delete network: %s", networkID) |
| 92 | |
| 93 | err := networks.Delete(client, networkID).ExtractErr() |
| 94 | if err != nil { |
| 95 | t.Fatalf("Unable to delete network %s: %v", networkID, err) |
| 96 | } |
| 97 | |
| 98 | t.Logf("Deleted network: %s", networkID) |
| 99 | } |
| 100 | |
| 101 | // DeletePort will delete a port with a specified ID. A fatal error will |
| 102 | // occur if the delete was not successful. This works best when used as a |
| 103 | // deferred function. |
| 104 | func DeletePort(t *testing.T, client *gophercloud.ServiceClient, portID string) { |
| 105 | t.Logf("Attempting to delete port: %s", portID) |
| 106 | |
| 107 | err := ports.Delete(client, portID).ExtractErr() |
| 108 | if err != nil { |
| 109 | t.Fatalf("Unable to delete port %s: %v", portID, err) |
| 110 | } |
| 111 | |
| 112 | t.Logf("Deleted port: %s", portID) |
| 113 | } |
| 114 | |
| 115 | // DeleteSubnet will delete a subnet with a specified ID. A fatal error will |
| 116 | // occur if the delete was not successful. This works best when used as a |
| 117 | // deferred function. |
| 118 | func DeleteSubnet(t *testing.T, client *gophercloud.ServiceClient, subnetID string) { |
| 119 | t.Logf("Attempting to delete subnet: %s", subnetID) |
| 120 | |
| 121 | err := subnets.Delete(client, subnetID).ExtractErr() |
| 122 | if err != nil { |
| 123 | t.Fatalf("Unable to delete subnet %s: %v", subnetID, err) |
| 124 | } |
| 125 | |
| 126 | t.Logf("Deleted subnet: %s", subnetID) |
| 127 | } |
| 128 | |
| 129 | // PrintAPIVersion will print an API version and all of its attributes. |
| 130 | func PrintAPIVersion(t *testing.T, apiVersion *apiversions.APIVersion) { |
| 131 | t.Logf("ID: %s", apiVersion.ID) |
| 132 | t.Logf("Status: %s", apiVersion.Status) |
| 133 | } |
| 134 | |
| 135 | // PrintNetwork will print a network and all of its attributes. |
| 136 | func PrintNetwork(t *testing.T, network *networks.Network) { |
| 137 | t.Logf("ID: %s", network.ID) |
| 138 | t.Logf("Name: %s", network.Name) |
| 139 | t.Logf("AdminStateUp: %t", network.AdminStateUp) |
| 140 | t.Logf("Status: %s", network.Status) |
| 141 | t.Logf("TenantID: %s", network.TenantID) |
| 142 | t.Logf("Shared: %t", network.Shared) |
| 143 | t.Logf("Subnets: %s", network.Subnets) |
| 144 | } |
| 145 | |
| 146 | // PrintPort will print a port and all of its attributes. |
| 147 | func PrintPort(t *testing.T, port *ports.Port) { |
| 148 | t.Logf("ID: %s", port.ID) |
| 149 | t.Logf("NetworkID: %s", port.NetworkID) |
| 150 | t.Logf("Name: %s", port.Name) |
| 151 | t.Logf("AdminStateUp: %t", port.AdminStateUp) |
| 152 | t.Logf("Status: %s", port.Status) |
| 153 | t.Logf("MACAddress: %s", port.MACAddress) |
| 154 | t.Logf("FixedIPs: %s", port.FixedIPs) |
| 155 | t.Logf("TenantID: %s", port.TenantID) |
| 156 | t.Logf("DeviceOwner: %s", port.DeviceOwner) |
| 157 | t.Logf("SecurityGroups: %s", port.SecurityGroups) |
| 158 | t.Logf("DeviceID: %s", port.DeviceID) |
| 159 | t.Logf("DeviceOwner: %s", port.DeviceOwner) |
| 160 | t.Logf("AllowedAddressPairs: %s", port.AllowedAddressPairs) |
| 161 | } |
| 162 | |
| 163 | // PrintSubnet will print a subnet and all of its attributes. |
| 164 | func PrintSubnet(t *testing.T, subnet *subnets.Subnet) { |
| 165 | t.Logf("ID: %s", subnet.ID) |
| 166 | t.Logf("NetworkID: %s", subnet.NetworkID) |
| 167 | t.Logf("Name: %s", subnet.Name) |
| 168 | t.Logf("IPVersion: %d", subnet.IPVersion) |
| 169 | t.Logf("CIDR: %s", subnet.CIDR) |
| 170 | t.Logf("GatewayIP: %s", subnet.GatewayIP) |
| 171 | t.Logf("DNSNameservers: %s", subnet.DNSNameservers) |
| 172 | t.Logf("AllocationPools: %s", subnet.AllocationPools) |
| 173 | t.Logf("HostRoutes: %s", subnet.HostRoutes) |
| 174 | t.Logf("EnableDHCP: %t", subnet.EnableDHCP) |
| 175 | t.Logf("TenantID: %s", subnet.TenantID) |
| 176 | } |
| 177 | |
| 178 | // PrintVersionResource will print an API version resource and all of its attributes. |
| 179 | func PrintVersionResource(t *testing.T, versionResource *apiversions.APIVersionResource) { |
| 180 | t.Logf("Name: %s", versionResource.Name) |
| 181 | t.Logf("Collection: %s", versionResource.Collection) |
| 182 | } |