github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/acceptance/openstack/networking/v2/peering/peerings.go (about) 1 package peering 2 3 import ( 4 "testing" 5 6 "github.com/huaweicloud/golangsdk" 7 "github.com/huaweicloud/golangsdk/acceptance/clients" 8 "github.com/huaweicloud/golangsdk/acceptance/tools" 9 "github.com/huaweicloud/golangsdk/openstack/networking/v1/vpcs" 10 "github.com/huaweicloud/golangsdk/openstack/networking/v2/peerings" 11 ) 12 13 func CreatePeeringResourcesNConn(t *testing.T, clientV2 *golangsdk.ServiceClient, peerClientV2 *golangsdk.ServiceClient, 14 clientV1 *golangsdk.ServiceClient, peerClientV1 *golangsdk.ServiceClient) (*peerings.Peering, error) { 15 16 vpcName := tools.RandomString("TESTACC-vpc", 8) 17 peerVpcName := tools.RandomString("TESTACC-peervpc", 8) 18 19 createOpts := vpcs.CreateOpts{ 20 Name: vpcName, 21 CIDR: "192.168.20.0/24", 22 } 23 24 t.Logf("Attempting to create vpc: %s and peer vpc: %s", vpcName, peerVpcName) 25 26 vpc, err := vpcs.Create(clientV1, createOpts).Extract() 27 if err != nil { 28 return nil, err 29 } 30 31 peerVpc, err := vpcs.Create(peerClientV1, createOpts).Extract() 32 if err != nil { 33 return nil, err 34 } 35 36 t.Logf("Created vpcs: %s %s", vpcName, peerVpcName) 37 38 peeringConnName := tools.RandomString("TESTACC-", 8) 39 40 peerCreateOpts := peerings.CreateOpts{ 41 Name: peeringConnName, 42 RequestVpcInfo: peerings.VpcInfo{VpcId: vpc.ID}, 43 AcceptVpcInfo: peerings.VpcInfo{VpcId: peerVpc.ID, TenantId: peerClientV2.ProjectID}, 44 } 45 46 t.Logf("Attempting to create vpc peering connection: %s", peeringConnName) 47 48 peeringConns, err := peerings.Create(clientV2, peerCreateOpts).Extract() 49 if err != nil { 50 return peeringConns, err 51 } 52 53 if err := WaitForPeeringConnToCreate(clientV2, peeringConns.ID, 60); err != nil { 54 return peeringConns, err 55 } 56 57 t.Logf("Created vpc peering connection: %s", peeringConnName) 58 59 return peeringConns, nil 60 } 61 62 func DeletePeeringConnNResources(t *testing.T, clientV2 *golangsdk.ServiceClient, clientV1 *golangsdk.ServiceClient, 63 peerClientV1 *golangsdk.ServiceClient, peeringConn *peerings.Peering) { 64 t.Logf("Attempting to delete vpc peering connection: %s", peeringConn.ID) 65 66 err := peerings.Delete(clientV2, peeringConn.ID).ExtractErr() 67 if err != nil { 68 t.Fatalf("Error deleting vpc peering connection: %v", err) 69 } 70 71 t.Logf("Deleted vpc peering connection: %s", peeringConn.ID) 72 73 t.Logf("Attempting to delete vpc: %s", peeringConn.RequestVpcInfo.VpcId) 74 75 err = vpcs.Delete(clientV1, peeringConn.RequestVpcInfo.VpcId).ExtractErr() 76 if err != nil { 77 t.Fatalf("Error deleting vpc: %v", err) 78 } 79 80 err = vpcs.Delete(peerClientV1, peeringConn.AcceptVpcInfo.VpcId).ExtractErr() 81 if err != nil { 82 t.Fatalf("Error deleting vpc: %v", err) 83 } 84 85 t.Logf("Deleted vpcs: %s and %s", peeringConn.RequestVpcInfo.VpcId, peeringConn.AcceptVpcInfo.VpcId) 86 } 87 88 func InitiatePeeringConnCommonTasks(t *testing.T) (*golangsdk.ServiceClient, *golangsdk.ServiceClient, 89 *golangsdk.ServiceClient, *golangsdk.ServiceClient, *peerings.Peering) { 90 91 clientV2, err := clients.NewNetworkV2Client() 92 if err != nil { 93 t.Fatalf("Unable to create a network v2 client: %v", err) 94 } 95 96 clientV1, err := clients.NewNetworkV1Client() 97 if err != nil { 98 t.Fatalf("Unable to create a network v1 client: %v", err) 99 } 100 101 peerClientV2, err := clients.NewPeerNetworkV2Client() 102 if err != nil { 103 t.Fatalf("Unable to create a network v2 client for peer: %v", err) 104 } 105 106 peerClientV1, err := clients.NewPeerNetworkV1Client() 107 if err != nil { 108 t.Fatalf("Unable to create a network v1 client for peer: %v", err) 109 } 110 111 // Create a vpc peering connection 112 peeringConn, err := CreatePeeringResourcesNConn(t, clientV2, peerClientV2, clientV1, peerClientV1) 113 if err != nil { 114 t.Fatalf("Unable to create vpc peering connection: %v", err) 115 } 116 117 return clientV2, peerClientV2, clientV1, peerClientV1, peeringConn 118 } 119 120 func WaitForPeeringConnToCreate(client *golangsdk.ServiceClient, peeringConnID string, secs int) error { 121 return golangsdk.WaitFor(secs, func() (bool, error) { 122 conn, err := peerings.Get(client, peeringConnID).Extract() 123 if err != nil { 124 return false, err 125 } 126 127 if conn.Status == "PENDING_ACCEPTANCE" { 128 return true, nil 129 } 130 131 return false, nil 132 }) 133 }