|
| 1 | +//go:build acceptance || networking || bgp || bgpvpns |
| 2 | + |
| 3 | +package bgpvpns |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/gophercloud/gophercloud/v2/internal/acceptance/clients" |
| 10 | + networking "github.com/gophercloud/gophercloud/v2/internal/acceptance/openstack/networking/v2" |
| 11 | + "github.com/gophercloud/gophercloud/v2/internal/acceptance/openstack/networking/v2/extensions/layer3" |
| 12 | + "github.com/gophercloud/gophercloud/v2/internal/acceptance/tools" |
| 13 | + "github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/bgpvpns" |
| 14 | + th "github.com/gophercloud/gophercloud/v2/testhelper" |
| 15 | +) |
| 16 | + |
| 17 | +func TestBGPVPNCRUD(t *testing.T) { |
| 18 | + clients.RequireAdmin(t) |
| 19 | + |
| 20 | + client, err := clients.NewNetworkV2Client() |
| 21 | + th.AssertNoErr(t, err) |
| 22 | + |
| 23 | + // Create a BGP VPN |
| 24 | + bgpVpnCreated, err := CreateBGPVPN(t, client) |
| 25 | + th.AssertNoErr(t, err) |
| 26 | + |
| 27 | + // Get a BGP VPN |
| 28 | + bgpVpnGot, err := bgpvpns.Get(context.TODO(), client, bgpVpnCreated.ID).Extract() |
| 29 | + th.AssertNoErr(t, err) |
| 30 | + th.AssertEquals(t, bgpVpnCreated.ID, bgpVpnGot.ID) |
| 31 | + th.AssertEquals(t, bgpVpnCreated.Name, bgpVpnGot.Name) |
| 32 | + |
| 33 | + // Update a BGP VPN |
| 34 | + newBGPVPNName := tools.RandomString("TESTACC-BGPVPN-", 10) |
| 35 | + updateBGPOpts := bgpvpns.UpdateOpts{ |
| 36 | + Name: &newBGPVPNName, |
| 37 | + } |
| 38 | + bgpVpnUpdated, err := bgpvpns.Update(context.TODO(), client, bgpVpnGot.ID, updateBGPOpts).Extract() |
| 39 | + th.AssertNoErr(t, err) |
| 40 | + th.AssertEquals(t, bgpVpnUpdated.Name, newBGPVPNName) |
| 41 | + t.Logf("Update BGP VPN, renamed from %s to %s", bgpVpnGot.Name, bgpVpnUpdated.Name) |
| 42 | + |
| 43 | + // List all BGP VPNs |
| 44 | + allPages, err := bgpvpns.List(client, bgpvpns.ListOpts{}).AllPages(context.TODO()) |
| 45 | + th.AssertNoErr(t, err) |
| 46 | + allVPNs, err := bgpvpns.ExtractBGPVPNs(allPages) |
| 47 | + th.AssertNoErr(t, err) |
| 48 | + |
| 49 | + t.Logf("Retrieved BGP VPNs") |
| 50 | + tools.PrintResource(t, allVPNs) |
| 51 | + th.AssertIntGreaterOrEqual(t, len(allVPNs), 1) |
| 52 | + |
| 53 | + // Delete a BGP VPN |
| 54 | + t.Logf("Attempting to delete BGP VPN: %s", bgpVpnUpdated.Name) |
| 55 | + err = bgpvpns.Delete(context.TODO(), client, bgpVpnUpdated.ID).ExtractErr() |
| 56 | + th.AssertNoErr(t, err) |
| 57 | + |
| 58 | + _, err = bgpvpns.Get(context.TODO(), client, bgpVpnGot.ID).Extract() |
| 59 | + th.AssertErr(t, err) |
| 60 | + t.Logf("BGP VPN %s deleted", bgpVpnUpdated.Name) |
| 61 | +} |
| 62 | + |
| 63 | +func TestBGPVPNNetworkAssociationCRD(t *testing.T) { |
| 64 | + clients.RequireAdmin(t) |
| 65 | + |
| 66 | + client, err := clients.NewNetworkV2Client() |
| 67 | + th.AssertNoErr(t, err) |
| 68 | + |
| 69 | + // Create a BGP VPN |
| 70 | + bgpVpnCreated, err := CreateBGPVPN(t, client) |
| 71 | + th.AssertNoErr(t, err) |
| 72 | + defer func() { |
| 73 | + err = bgpvpns.Delete(context.TODO(), client, bgpVpnCreated.ID).ExtractErr() |
| 74 | + th.AssertNoErr(t, err) |
| 75 | + }() |
| 76 | + |
| 77 | + // Create a Network |
| 78 | + network, err := networking.CreateNetwork(t, client) |
| 79 | + th.AssertNoErr(t, err) |
| 80 | + defer networking.DeleteNetwork(t, client, network.ID) |
| 81 | + |
| 82 | + // Associate a Network with a BGP VPN |
| 83 | + assocOpts := bgpvpns.CreateNetworkAssociationOpts{ |
| 84 | + NetworkID: network.ID, |
| 85 | + } |
| 86 | + assoc, err := bgpvpns.CreateNetworkAssociation(context.TODO(), client, bgpVpnCreated.ID, assocOpts).Extract() |
| 87 | + th.AssertNoErr(t, err) |
| 88 | + defer func() { |
| 89 | + err = bgpvpns.DeleteNetworkAssociation(context.TODO(), client, bgpVpnCreated.ID, assoc.ID).ExtractErr() |
| 90 | + th.AssertNoErr(t, err) |
| 91 | + }() |
| 92 | + th.AssertEquals(t, assoc.NetworkID, network.ID) |
| 93 | + |
| 94 | + // Get a Network Association |
| 95 | + assocGot, err := bgpvpns.GetNetworkAssociation(context.TODO(), client, bgpVpnCreated.ID, assoc.ID).Extract() |
| 96 | + th.AssertNoErr(t, err) |
| 97 | + th.AssertEquals(t, assocGot.NetworkID, network.ID) |
| 98 | + |
| 99 | + // List all Network Associations |
| 100 | + allPages, err := bgpvpns.ListNetworkAssociations(client, bgpVpnCreated.ID, bgpvpns.ListNetworkAssociationsOpts{}).AllPages(context.TODO()) |
| 101 | + th.AssertNoErr(t, err) |
| 102 | + allAssocs, err := bgpvpns.ExtractNetworkAssociations(allPages) |
| 103 | + th.AssertNoErr(t, err) |
| 104 | + t.Logf("Retrieved Network Associations") |
| 105 | + tools.PrintResource(t, allAssocs) |
| 106 | + th.AssertIntGreaterOrEqual(t, len(allAssocs), 1) |
| 107 | + |
| 108 | + // Get BGP VPN with associations |
| 109 | + getBgpVpn, err := bgpvpns.Get(context.TODO(), client, bgpVpnCreated.ID).Extract() |
| 110 | + th.AssertNoErr(t, err) |
| 111 | + tools.PrintResource(t, getBgpVpn) |
| 112 | +} |
| 113 | + |
| 114 | +func TestBGPVPNRouterAssociationCRUD(t *testing.T) { |
| 115 | + clients.RequireAdmin(t) |
| 116 | + |
| 117 | + client, err := clients.NewNetworkV2Client() |
| 118 | + th.AssertNoErr(t, err) |
| 119 | + |
| 120 | + // Create a BGP VPN |
| 121 | + bgpVpnCreated, err := CreateBGPVPN(t, client) |
| 122 | + th.AssertNoErr(t, err) |
| 123 | + defer func() { |
| 124 | + err = bgpvpns.Delete(context.TODO(), client, bgpVpnCreated.ID).ExtractErr() |
| 125 | + th.AssertNoErr(t, err) |
| 126 | + }() |
| 127 | + |
| 128 | + // Create a Network |
| 129 | + network, err := networking.CreateNetwork(t, client) |
| 130 | + th.AssertNoErr(t, err) |
| 131 | + defer networking.DeleteNetwork(t, client, network.ID) |
| 132 | + |
| 133 | + // Create a Router |
| 134 | + routerCreated, err := layer3.CreateRouter(t, client, network.ID) |
| 135 | + th.AssertNoErr(t, err) |
| 136 | + defer layer3.DeleteRouter(t, client, routerCreated.ID) |
| 137 | + |
| 138 | + // Associate a Router with a BGP VPN |
| 139 | + assocOpts := bgpvpns.CreateRouterAssociationOpts{ |
| 140 | + RouterID: routerCreated.ID, |
| 141 | + } |
| 142 | + assoc, err := bgpvpns.CreateRouterAssociation(context.TODO(), client, bgpVpnCreated.ID, assocOpts).Extract() |
| 143 | + th.AssertNoErr(t, err) |
| 144 | + defer func() { |
| 145 | + err = bgpvpns.DeleteRouterAssociation(context.TODO(), client, bgpVpnCreated.ID, assoc.ID).ExtractErr() |
| 146 | + th.AssertNoErr(t, err) |
| 147 | + }() |
| 148 | + th.AssertEquals(t, assoc.RouterID, routerCreated.ID) |
| 149 | + |
| 150 | + // Get a Router Association |
| 151 | + assocGot, err := bgpvpns.GetRouterAssociation(context.TODO(), client, bgpVpnCreated.ID, assoc.ID).Extract() |
| 152 | + th.AssertNoErr(t, err) |
| 153 | + th.AssertEquals(t, assocGot.RouterID, routerCreated.ID) |
| 154 | + |
| 155 | + // Update a Router Association |
| 156 | + assocUpdOpts := bgpvpns.UpdateRouterAssociationOpts{ |
| 157 | + AdvertiseExtraRoutes: new(bool), |
| 158 | + } |
| 159 | + assocUpdate, err := bgpvpns.UpdateRouterAssociation(context.TODO(), client, bgpVpnCreated.ID, assoc.ID, assocUpdOpts).Extract() |
| 160 | + th.AssertNoErr(t, err) |
| 161 | + th.AssertEquals(t, assocUpdate.RouterID, routerCreated.ID) |
| 162 | + th.AssertEquals(t, assocUpdate.AdvertiseExtraRoutes, false) |
| 163 | + |
| 164 | + // List all Router Associations |
| 165 | + allPages, err := bgpvpns.ListRouterAssociations(client, bgpVpnCreated.ID, bgpvpns.ListRouterAssociationsOpts{}).AllPages(context.TODO()) |
| 166 | + th.AssertNoErr(t, err) |
| 167 | + allAssocs, err := bgpvpns.ExtractRouterAssociations(allPages) |
| 168 | + th.AssertNoErr(t, err) |
| 169 | + t.Logf("Retrieved Router Associations") |
| 170 | + tools.PrintResource(t, allAssocs) |
| 171 | + th.AssertIntGreaterOrEqual(t, len(allAssocs), 1) |
| 172 | + |
| 173 | + // Get BGP VPN with associations |
| 174 | + getBgpVpn, err := bgpvpns.Get(context.TODO(), client, bgpVpnCreated.ID).Extract() |
| 175 | + th.AssertNoErr(t, err) |
| 176 | + tools.PrintResource(t, getBgpVpn) |
| 177 | +} |
| 178 | + |
| 179 | +func TestBGPVPNPortAssociationCRUD(t *testing.T) { |
| 180 | + clients.RequireAdmin(t) |
| 181 | + |
| 182 | + client, err := clients.NewNetworkV2Client() |
| 183 | + th.AssertNoErr(t, err) |
| 184 | + |
| 185 | + // Create a BGP VPN |
| 186 | + bgpVpnCreated, err := CreateBGPVPN(t, client) |
| 187 | + th.AssertNoErr(t, err) |
| 188 | + defer func() { |
| 189 | + err = bgpvpns.Delete(context.TODO(), client, bgpVpnCreated.ID).ExtractErr() |
| 190 | + th.AssertNoErr(t, err) |
| 191 | + }() |
| 192 | + |
| 193 | + // Create a Network |
| 194 | + network, err := networking.CreateNetwork(t, client) |
| 195 | + th.AssertNoErr(t, err) |
| 196 | + defer networking.DeleteNetwork(t, client, network.ID) |
| 197 | + |
| 198 | + // Create Subnet |
| 199 | + subnet, err := networking.CreateSubnet(t, client, network.ID) |
| 200 | + th.AssertNoErr(t, err) |
| 201 | + defer networking.DeleteSubnet(t, client, subnet.ID) |
| 202 | + |
| 203 | + // Create port |
| 204 | + port, err := networking.CreatePort(t, client, network.ID, subnet.ID) |
| 205 | + th.AssertNoErr(t, err) |
| 206 | + defer networking.DeletePort(t, client, port.ID) |
| 207 | + |
| 208 | + // Associate a Port with a BGP VPN |
| 209 | + assocOpts := bgpvpns.CreatePortAssociationOpts{ |
| 210 | + PortID: port.ID, |
| 211 | + } |
| 212 | + assoc, err := bgpvpns.CreatePortAssociation(context.TODO(), client, bgpVpnCreated.ID, assocOpts).Extract() |
| 213 | + th.AssertNoErr(t, err) |
| 214 | + defer func() { |
| 215 | + err = bgpvpns.DeletePortAssociation(context.TODO(), client, bgpVpnCreated.ID, assoc.ID).ExtractErr() |
| 216 | + th.AssertNoErr(t, err) |
| 217 | + }() |
| 218 | + th.AssertEquals(t, assoc.PortID, port.ID) |
| 219 | + |
| 220 | + // Get a Port Association |
| 221 | + assocGot, err := bgpvpns.GetPortAssociation(context.TODO(), client, bgpVpnCreated.ID, assoc.ID).Extract() |
| 222 | + th.AssertNoErr(t, err) |
| 223 | + th.AssertEquals(t, assocGot.PortID, port.ID) |
| 224 | + |
| 225 | + // Update a Port Association |
| 226 | + assocUpdOpts := bgpvpns.UpdatePortAssociationOpts{ |
| 227 | + AdvertiseFixedIPs: new(bool), |
| 228 | + } |
| 229 | + assocUpdate, err := bgpvpns.UpdatePortAssociation(context.TODO(), client, bgpVpnCreated.ID, assoc.ID, assocUpdOpts).Extract() |
| 230 | + th.AssertNoErr(t, err) |
| 231 | + th.AssertEquals(t, assocUpdate.PortID, port.ID) |
| 232 | + th.AssertEquals(t, assocUpdate.AdvertiseFixedIPs, false) |
| 233 | + |
| 234 | + // List all Port Associations |
| 235 | + allPages, err := bgpvpns.ListPortAssociations(client, bgpVpnCreated.ID, bgpvpns.ListPortAssociationsOpts{}).AllPages(context.TODO()) |
| 236 | + th.AssertNoErr(t, err) |
| 237 | + allAssocs, err := bgpvpns.ExtractPortAssociations(allPages) |
| 238 | + th.AssertNoErr(t, err) |
| 239 | + t.Logf("Retrieved Port Associations") |
| 240 | + tools.PrintResource(t, allAssocs) |
| 241 | + th.AssertIntGreaterOrEqual(t, len(allAssocs), 1) |
| 242 | + |
| 243 | + // Get BGP VPN with associations |
| 244 | + getBgpVpn, err := bgpvpns.Get(context.TODO(), client, bgpVpnCreated.ID).Extract() |
| 245 | + th.AssertNoErr(t, err) |
| 246 | + tools.PrintResource(t, getBgpVpn) |
| 247 | +} |
0 commit comments