github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/acceptance/openstack/networking/v1/subnet_test.go (about) 1 package v1 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/huaweicloud/golangsdk" 8 "github.com/huaweicloud/golangsdk/acceptance/clients" 9 "github.com/huaweicloud/golangsdk/acceptance/tools" 10 "github.com/huaweicloud/golangsdk/openstack/networking/v1/subnets" 11 "github.com/huaweicloud/golangsdk/openstack/networking/v1/vpcs" 12 ) 13 14 func TestSubnetList(t *testing.T) { 15 client, err := clients.NewNetworkV1Client() 16 if err != nil { 17 t.Fatalf("Unable to create a subnet : %v", err) 18 } 19 allPages, err := subnets.List(client, subnets.ListOpts{}) 20 tools.PrintResource(t, allPages) 21 22 } 23 24 func TestSubnetsCRUD(t *testing.T) { 25 client, err := clients.NewNetworkV1Client() 26 if err != nil { 27 t.Fatalf("Unable to create a subnet : %v", err) 28 } 29 30 // Create a subnet 31 subnet, err := createSubnetNResources(t, client) 32 if err != nil { 33 t.Fatalf("Unable to create subnet: %v", err) 34 } 35 // Delete a subnet 36 defer deleteSubnetNResources(t, client, subnet.VPC_ID, subnet.ID) 37 38 tools.PrintResource(t, subnet) 39 40 // wait to be active 41 t.Logf("waitting for subnet %s to be active", subnet.ID) 42 if err := waitForSubnetToActive(client, subnet.ID, 60); err != nil { 43 t.Fatalf("Error deleting subnet: %v", err) 44 } 45 46 // Update a subnet 47 newName := tools.RandomString("ACPTTEST-", 8) 48 updateOpts := &subnets.UpdateOpts{ 49 Name: newName, 50 } 51 t.Logf("Attempting to update name of subnet to %s", newName) 52 _, err = subnets.Update(client, subnet.VPC_ID, subnet.ID, updateOpts).Extract() 53 if err != nil { 54 t.Fatalf("Unable to update subnet: %v", err) 55 } 56 57 // Query a subnet 58 newSubnet, err := subnets.Get(client, subnet.ID).Extract() 59 if err != nil { 60 t.Fatalf("Unable to retrieve subnet: %v", err) 61 } 62 63 tools.PrintResource(t, newSubnet) 64 } 65 66 func createSubnetNResources(t *testing.T, client *golangsdk.ServiceClient) (*subnets.Subnet, error) { 67 68 vpcName := tools.RandomString("TESTACC-", 8) 69 70 createOpts := vpcs.CreateOpts{ 71 Name: vpcName, 72 CIDR: "192.168.20.0/24", 73 } 74 75 t.Logf("Attempting to create vpc: %s", vpcName) 76 77 vpc, err := vpcs.Create(client, createOpts).Extract() 78 if err != nil { 79 return nil, err 80 } 81 t.Logf("Created vpc: %s", vpcName) 82 83 subnetName := tools.RandomString("ACPTTEST-", 8) 84 85 createSubnetOpts := subnets.CreateOpts{ 86 Name: subnetName, 87 CIDR: "192.168.20.0/24", 88 GatewayIP: "192.168.20.1", 89 EnableDHCP: true, 90 VPC_ID: vpc.ID, 91 } 92 93 t.Logf("Attempting to create subnet: %s", subnetName) 94 95 subnet, err := subnets.Create(client, createSubnetOpts).Extract() 96 if err != nil { 97 return subnet, err 98 } 99 t.Logf("Created subnet: %v", subnet) 100 101 return subnet, nil 102 } 103 104 func deleteSubnetNResources(t *testing.T, client *golangsdk.ServiceClient, vpcID string, id string) { 105 t.Logf("Attempting to delete subnet: %s", id) 106 107 err := subnets.Delete(client, vpcID, id).ExtractErr() 108 if err != nil { 109 t.Fatalf("Error deleting subnet: %v", err) 110 } 111 112 t.Logf("waitting for subnet %s to delete", id) 113 if err := waitForSubnetToDelete(client, id, 60); err != nil { 114 t.Fatalf("Error deleting subnet: %v", err) 115 } 116 117 t.Logf("Deleted subnet: %s", id) 118 t.Logf("Attempting to delete vpc: %s", vpcID) 119 120 err = vpcs.Delete(client, vpcID).ExtractErr() 121 if err != nil { 122 t.Fatalf("Error deleting vpc: %v", err) 123 } 124 125 t.Logf("Deleted vpc: %s", vpcID) 126 } 127 128 func waitForSubnetToDelete(client *golangsdk.ServiceClient, subnetID string, secs int) error { 129 return golangsdk.WaitFor(secs, func() (bool, error) { 130 _, err := subnets.Get(client, subnetID).Extract() 131 if err != nil { 132 if _, ok := err.(golangsdk.ErrDefault404); ok { 133 return true, nil 134 } 135 } 136 137 return false, nil 138 }) 139 } 140 141 func waitForSubnetToActive(client *golangsdk.ServiceClient, subnetID string, secs int) error { 142 return golangsdk.WaitFor(secs, func() (bool, error) { 143 n, err := subnets.Get(client, subnetID).Extract() 144 if err != nil { 145 return false, err 146 } 147 148 if n.Status == "ACTIVE" { 149 return true, nil 150 } 151 152 //If subnet status is other than Active, send error 153 if n.Status == "DOWN" || n.Status == "ERROR" { 154 return false, fmt.Errorf("Subnet status: '%s'", n.Status) 155 } 156 157 return false, nil 158 }) 159 }