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" |
Joe Topjian | 7c8dd02 | 2016-09-01 12:02:04 -0600 | [diff] [blame] | 9 | "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/groups" |
| 10 | "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/rules" |
| 11 | "github.com/gophercloud/gophercloud/openstack/networking/v2/networks" |
| 12 | "github.com/gophercloud/gophercloud/openstack/networking/v2/ports" |
| 13 | ) |
| 14 | |
| 15 | // CreateExternalNetwork will create an external network. An error will be |
| 16 | // returned if the creation failed. |
| 17 | func CreateExternalNetwork(t *testing.T, client *gophercloud.ServiceClient) (*networks.Network, error) { |
| 18 | networkName := tools.RandomString("TESTACC-", 8) |
| 19 | |
| 20 | t.Logf("Attempting to create external network: %s", networkName) |
| 21 | |
| 22 | adminStateUp := true |
| 23 | isExternal := true |
| 24 | createOpts := external.CreateOpts{ |
| 25 | External: &isExternal, |
| 26 | } |
| 27 | |
| 28 | createOpts.Name = networkName |
| 29 | createOpts.AdminStateUp = &adminStateUp |
| 30 | |
| 31 | network, err := networks.Create(client, createOpts).Extract() |
| 32 | if err != nil { |
| 33 | return network, err |
| 34 | } |
| 35 | |
| 36 | t.Logf("Created external network: %s", networkName) |
| 37 | |
| 38 | return network, nil |
| 39 | } |
| 40 | |
| 41 | // CreatePortWithSecurityGroup will create a port with a security group |
| 42 | // attached. An error will be returned if the port could not be created. |
| 43 | func CreatePortWithSecurityGroup(t *testing.T, client *gophercloud.ServiceClient, networkID, subnetID, secGroupID string) (*ports.Port, error) { |
| 44 | portName := tools.RandomString("TESTACC-", 8) |
| 45 | iFalse := false |
| 46 | |
| 47 | t.Logf("Attempting to create port: %s", portName) |
| 48 | |
| 49 | createOpts := ports.CreateOpts{ |
| 50 | NetworkID: networkID, |
| 51 | Name: portName, |
| 52 | AdminStateUp: &iFalse, |
| 53 | FixedIPs: []ports.IP{ports.IP{SubnetID: subnetID}}, |
| 54 | SecurityGroups: []string{secGroupID}, |
| 55 | } |
| 56 | |
| 57 | port, err := ports.Create(client, createOpts).Extract() |
| 58 | if err != nil { |
| 59 | return port, err |
| 60 | } |
| 61 | |
| 62 | t.Logf("Successfully created port: %s", portName) |
| 63 | |
| 64 | return port, nil |
| 65 | } |
| 66 | |
| 67 | // CreateSecurityGroup will create a security group with a random name. |
| 68 | // An error will be returned if one was failed to be created. |
| 69 | func CreateSecurityGroup(t *testing.T, client *gophercloud.ServiceClient) (*groups.SecGroup, error) { |
| 70 | secGroupName := tools.RandomString("TESTACC-", 8) |
| 71 | |
| 72 | t.Logf("Attempting to create security group: %s", secGroupName) |
| 73 | |
| 74 | createOpts := groups.CreateOpts{ |
| 75 | Name: secGroupName, |
| 76 | } |
| 77 | |
| 78 | secGroup, err := groups.Create(client, createOpts).Extract() |
| 79 | if err != nil { |
| 80 | return secGroup, err |
| 81 | } |
| 82 | |
| 83 | t.Logf("Created security group: %s", secGroup.ID) |
| 84 | |
| 85 | return secGroup, nil |
| 86 | } |
| 87 | |
| 88 | // CreateSecurityGroupRule will create a security group rule with a random name |
| 89 | // and random port between 80 and 99. |
| 90 | // An error will be returned if one was failed to be created. |
| 91 | func CreateSecurityGroupRule(t *testing.T, client *gophercloud.ServiceClient, secGroupID string) (*rules.SecGroupRule, error) { |
| 92 | t.Logf("Attempting to create security group rule in group: %s", secGroupID) |
| 93 | |
| 94 | fromPort := tools.RandomInt(80, 89) |
| 95 | toPort := tools.RandomInt(90, 99) |
| 96 | |
| 97 | createOpts := rules.CreateOpts{ |
| 98 | Direction: "ingress", |
| 99 | EtherType: "IPv4", |
| 100 | SecGroupID: secGroupID, |
| 101 | PortRangeMin: fromPort, |
| 102 | PortRangeMax: toPort, |
| 103 | Protocol: rules.ProtocolTCP, |
| 104 | } |
| 105 | |
| 106 | rule, err := rules.Create(client, createOpts).Extract() |
| 107 | if err != nil { |
| 108 | return rule, err |
| 109 | } |
| 110 | |
| 111 | t.Logf("Created security group rule: %s", rule.ID) |
| 112 | |
| 113 | return rule, nil |
| 114 | } |
| 115 | |
| 116 | // DeleteSecurityGroup will delete a security group of a specified ID. |
| 117 | // A fatal error will occur if the deletion failed. This works best as a |
| 118 | // deferred function |
| 119 | func DeleteSecurityGroup(t *testing.T, client *gophercloud.ServiceClient, secGroupID string) { |
| 120 | t.Logf("Attempting to delete security group: %s", secGroupID) |
| 121 | |
| 122 | err := groups.Delete(client, secGroupID).ExtractErr() |
| 123 | if err != nil { |
| 124 | t.Fatalf("Unable to delete security group: %v", err) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // DeleteSecurityGroupRule will delete a security group rule of a specified ID. |
| 129 | // A fatal error will occur if the deletion failed. This works best as a |
| 130 | // deferred function |
| 131 | func DeleteSecurityGroupRule(t *testing.T, client *gophercloud.ServiceClient, ruleID string) { |
| 132 | t.Logf("Attempting to delete security group rule: %s", ruleID) |
| 133 | |
| 134 | err := rules.Delete(client, ruleID).ExtractErr() |
| 135 | if err != nil { |
| 136 | t.Fatalf("Unable to delete security group rule: %v", err) |
| 137 | } |
| 138 | } |