Joe Topjian | 7c8dd02 | 2016-09-01 12:02:04 -0600 | [diff] [blame^] | 1 | package extensions |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/gophercloud/gophercloud" |
| 7 | "github.com/gophercloud/gophercloud/acceptance/tools" |
| 8 | "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/external" |
| 9 | "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/provider" |
| 10 | "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/groups" |
| 11 | "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/rules" |
| 12 | "github.com/gophercloud/gophercloud/openstack/networking/v2/networks" |
| 13 | "github.com/gophercloud/gophercloud/openstack/networking/v2/ports" |
| 14 | ) |
| 15 | |
| 16 | // CreateExternalNetwork will create an external network. An error will be |
| 17 | // returned if the creation failed. |
| 18 | func CreateExternalNetwork(t *testing.T, client *gophercloud.ServiceClient) (*networks.Network, error) { |
| 19 | networkName := tools.RandomString("TESTACC-", 8) |
| 20 | |
| 21 | t.Logf("Attempting to create external network: %s", networkName) |
| 22 | |
| 23 | adminStateUp := true |
| 24 | isExternal := true |
| 25 | createOpts := external.CreateOpts{ |
| 26 | External: &isExternal, |
| 27 | } |
| 28 | |
| 29 | createOpts.Name = networkName |
| 30 | createOpts.AdminStateUp = &adminStateUp |
| 31 | |
| 32 | network, err := networks.Create(client, createOpts).Extract() |
| 33 | if err != nil { |
| 34 | return network, err |
| 35 | } |
| 36 | |
| 37 | t.Logf("Created external network: %s", networkName) |
| 38 | |
| 39 | return network, nil |
| 40 | } |
| 41 | |
| 42 | // CreatePortWithSecurityGroup will create a port with a security group |
| 43 | // attached. An error will be returned if the port could not be created. |
| 44 | func CreatePortWithSecurityGroup(t *testing.T, client *gophercloud.ServiceClient, networkID, subnetID, secGroupID string) (*ports.Port, error) { |
| 45 | portName := tools.RandomString("TESTACC-", 8) |
| 46 | iFalse := false |
| 47 | |
| 48 | t.Logf("Attempting to create port: %s", portName) |
| 49 | |
| 50 | createOpts := ports.CreateOpts{ |
| 51 | NetworkID: networkID, |
| 52 | Name: portName, |
| 53 | AdminStateUp: &iFalse, |
| 54 | FixedIPs: []ports.IP{ports.IP{SubnetID: subnetID}}, |
| 55 | SecurityGroups: []string{secGroupID}, |
| 56 | } |
| 57 | |
| 58 | port, err := ports.Create(client, createOpts).Extract() |
| 59 | if err != nil { |
| 60 | return port, err |
| 61 | } |
| 62 | |
| 63 | t.Logf("Successfully created port: %s", portName) |
| 64 | |
| 65 | return port, nil |
| 66 | } |
| 67 | |
| 68 | // CreateSecurityGroup will create a security group with a random name. |
| 69 | // An error will be returned if one was failed to be created. |
| 70 | func CreateSecurityGroup(t *testing.T, client *gophercloud.ServiceClient) (*groups.SecGroup, error) { |
| 71 | secGroupName := tools.RandomString("TESTACC-", 8) |
| 72 | |
| 73 | t.Logf("Attempting to create security group: %s", secGroupName) |
| 74 | |
| 75 | createOpts := groups.CreateOpts{ |
| 76 | Name: secGroupName, |
| 77 | } |
| 78 | |
| 79 | secGroup, err := groups.Create(client, createOpts).Extract() |
| 80 | if err != nil { |
| 81 | return secGroup, err |
| 82 | } |
| 83 | |
| 84 | t.Logf("Created security group: %s", secGroup.ID) |
| 85 | |
| 86 | return secGroup, nil |
| 87 | } |
| 88 | |
| 89 | // CreateSecurityGroupRule will create a security group rule with a random name |
| 90 | // and random port between 80 and 99. |
| 91 | // An error will be returned if one was failed to be created. |
| 92 | func CreateSecurityGroupRule(t *testing.T, client *gophercloud.ServiceClient, secGroupID string) (*rules.SecGroupRule, error) { |
| 93 | t.Logf("Attempting to create security group rule in group: %s", secGroupID) |
| 94 | |
| 95 | fromPort := tools.RandomInt(80, 89) |
| 96 | toPort := tools.RandomInt(90, 99) |
| 97 | |
| 98 | createOpts := rules.CreateOpts{ |
| 99 | Direction: "ingress", |
| 100 | EtherType: "IPv4", |
| 101 | SecGroupID: secGroupID, |
| 102 | PortRangeMin: fromPort, |
| 103 | PortRangeMax: toPort, |
| 104 | Protocol: rules.ProtocolTCP, |
| 105 | } |
| 106 | |
| 107 | rule, err := rules.Create(client, createOpts).Extract() |
| 108 | if err != nil { |
| 109 | return rule, err |
| 110 | } |
| 111 | |
| 112 | t.Logf("Created security group rule: %s", rule.ID) |
| 113 | |
| 114 | return rule, nil |
| 115 | } |
| 116 | |
| 117 | // DeleteSecurityGroup will delete a security group of a specified ID. |
| 118 | // A fatal error will occur if the deletion failed. This works best as a |
| 119 | // deferred function |
| 120 | func DeleteSecurityGroup(t *testing.T, client *gophercloud.ServiceClient, secGroupID string) { |
| 121 | t.Logf("Attempting to delete security group: %s", secGroupID) |
| 122 | |
| 123 | err := groups.Delete(client, secGroupID).ExtractErr() |
| 124 | if err != nil { |
| 125 | t.Fatalf("Unable to delete security group: %v", err) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // DeleteSecurityGroupRule will delete a security group rule of a specified ID. |
| 130 | // A fatal error will occur if the deletion failed. This works best as a |
| 131 | // deferred function |
| 132 | func DeleteSecurityGroupRule(t *testing.T, client *gophercloud.ServiceClient, ruleID string) { |
| 133 | t.Logf("Attempting to delete security group rule: %s", ruleID) |
| 134 | |
| 135 | err := rules.Delete(client, ruleID).ExtractErr() |
| 136 | if err != nil { |
| 137 | t.Fatalf("Unable to delete security group rule: %v", err) |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // PrintNetworkExtAttrs prints a network and all of its extra attributes. |
| 142 | func PrintNetworkExtAttrs(t *testing.T, network *provider.NetworkExtAttrs) { |
| 143 | t.Logf("ID: %s", network.ID) |
| 144 | t.Logf("Name: %s", network.Name) |
| 145 | t.Logf("AdminStateUp: %t", network.AdminStateUp) |
| 146 | t.Logf("Status: %s", network.Status) |
| 147 | t.Logf("Subnets: %s", network.Subnets) |
| 148 | t.Logf("TenantID: %s", network.TenantID) |
| 149 | t.Logf("Shared: %t", network.Shared) |
| 150 | t.Logf("NetworkType: %s", network.NetworkType) |
| 151 | t.Logf("PhysicalNetwork: %s", network.PhysicalNetwork) |
| 152 | t.Logf("SegmentationID: %d", network.SegmentationID) |
| 153 | } |
| 154 | |
| 155 | // PrintSecurityGroup will print a security group and all of its attributes. |
| 156 | func PrintSecurityGroup(t *testing.T, secGroup *groups.SecGroup) { |
| 157 | t.Logf("ID: %s", secGroup.ID) |
| 158 | t.Logf("Name: %s", secGroup.Name) |
| 159 | t.Logf("Description: %s", secGroup.Description) |
| 160 | t.Logf("TenantID: %s", secGroup.TenantID) |
| 161 | t.Logf("Rules:") |
| 162 | |
| 163 | for _, rule := range secGroup.Rules { |
| 164 | PrintSecurityGroupRule(t, &rule) |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // PrintSecurityGroupRule will print a security group rule and all of its attributes. |
| 169 | func PrintSecurityGroupRule(t *testing.T, rule *rules.SecGroupRule) { |
| 170 | t.Logf("ID: %s", rule.ID) |
| 171 | t.Logf("Direction: %s", rule.Direction) |
| 172 | t.Logf("EtherType: %s", rule.EtherType) |
| 173 | t.Logf("SecGroupID: %s", rule.SecGroupID) |
| 174 | t.Logf("PortRangeMin: %d", rule.PortRangeMin) |
| 175 | t.Logf("PortRangeMax: %d", rule.PortRangeMax) |
| 176 | t.Logf("Protocol: %s", rule.Protocol) |
| 177 | t.Logf("RemoteGroupID: %s", rule.RemoteGroupID) |
| 178 | t.Logf("RemoteIPPrefix: %s", rule.RemoteIPPrefix) |
| 179 | t.Logf("TenantID: %s", rule.TenantID) |
| 180 | } |