github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/acceptance/openstack/networking/v1/vpc_test.go (about) 1 package v1 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 ) 11 12 func TestVpcList(t *testing.T) { 13 client, err := clients.NewNetworkV1Client() 14 if err != nil { 15 t.Fatalf("Unable to create a vpc client: %v", err) 16 } 17 18 listOpts := vpcs.ListOpts{} 19 allVpcs, err := vpcs.List(client, listOpts) 20 if err != nil { 21 t.Fatalf("Unable to list vpcs: %v", err) 22 } 23 for _, vpc := range allVpcs { 24 tools.PrintResource(t, vpc) 25 } 26 } 27 28 func TestVpcsCRUD(t *testing.T) { 29 client, err := clients.NewNetworkV1Client() 30 if err != nil { 31 t.Fatalf("Unable to create a vpc client: %v", err) 32 } 33 34 // Create a vpc 35 vpc, err := createVpc(t, client) 36 if err != nil { 37 t.Fatalf("Unable to create create: %v", err) 38 } 39 defer deleteVpc(t, client, vpc.ID) 40 41 tools.PrintResource(t, vpc) 42 43 newName := tools.RandomString("TESTACC-", 8) 44 updateOpts := &vpcs.UpdateOpts{ 45 Name: newName, 46 } 47 48 _, err = vpcs.Update(client, vpc.ID, updateOpts).Extract() 49 if err != nil { 50 t.Fatalf("Unable to update vpc: %v", err) 51 } 52 53 newVpc, err := vpcs.Get(client, vpc.ID).Extract() 54 if err != nil { 55 t.Fatalf("Unable to retrieve vpc: %v", err) 56 } 57 58 tools.PrintResource(t, newVpc) 59 } 60 61 func createVpc(t *testing.T, client *golangsdk.ServiceClient) (*vpcs.Vpc, error) { 62 63 vpcName := tools.RandomString("TESTACC-", 8) 64 65 createOpts := vpcs.CreateOpts{ 66 Name: vpcName, 67 CIDR: "192.168.20.0/24", 68 } 69 70 t.Logf("Attempting to create vpc: %s", vpcName) 71 72 vpc, err := vpcs.Create(client, createOpts).Extract() 73 if err != nil { 74 return vpc, err 75 } 76 t.Logf("Created vpc: %s", vpcName) 77 78 return vpc, nil 79 } 80 81 func deleteVpc(t *testing.T, client *golangsdk.ServiceClient, vpcID string) { 82 t.Logf("Attempting to delete vpc: %s", vpcID) 83 84 err := vpcs.Delete(client, vpcID).ExtractErr() 85 if err != nil { 86 t.Fatalf("Error deleting vpc: %v", err) 87 } 88 89 t.Logf("Deleted vpc: %s", vpcID) 90 }