Skip to content

Commit d7b2355

Browse files
authored
[VPC] Update subnets pkg (#141)
[VPC] Update subnets pkg What this PR does / why we need it Update subnets pkg Which issue this PR fixes Resolves: #140 unit === RUN TestListSubnet --- PASS: TestListSubnet (0.00s) === RUN TestGetSubnet --- PASS: TestGetSubnet (0.00s) === RUN TestCreateSubnet --- PASS: TestCreateSubnet (0.00s) === RUN TestUpdateSubnet --- PASS: TestUpdateSubnet (0.00s) === RUN TestDeleteSubnet --- PASS: TestDeleteSubnet (0.00s) PASS Process finished with the exit code 0 acceptance === RUN TestSubnetList --- PASS: TestSubnetList (3.37s) === RUN TestSubnetsLifecycle helpers.go:206: Attempting to create vpc: acc-vpc-Tox helpers.go:210: Created vpc: 642adede-8f37-49a4-9f36-cbfcd93eedf0 helpers.go:139: Attempting to create subnet: acc-subnet-u70 helpers.go:145: Waitting for subnet bc3977fe-d8fe-474a-83d0-46fde40cb4fd to be active helpers.go:148: Created subnet: bc3977fe-d8fe-474a-83d0-46fde40cb4fd subnet_test.go:38: Attempting to update name of subnet to acc-subnet-orD helpers.go:154: Attempting to delete subnet: bc3977fe-d8fe-474a-83d0-46fde40cb4fd helpers.go:159: Waiting for subnet bc3977fe-d8fe-474a-83d0-46fde40cb4fd to be deleted helpers.go:163: Deleted subnet: bc3977fe-d8fe-474a-83d0-46fde40cb4fd helpers.go:216: Attempting to delete vpc: 642adede-8f37-49a4-9f36-cbfcd93eedf0 helpers.go:221: Deleted vpc: 642adede-8f37-49a4-9f36-cbfcd93eedf0 --- PASS: TestSubnetsLifecycle (21.15s) PASS Process finished with the exit code 0 Reviewed-by: None <None> Reviewed-by: Anton Sidelnikov <None> Reviewed-by: Anton Kachurin <katchuring@gmail.com>
1 parent 26282ae commit d7b2355

File tree

8 files changed

+165
-177
lines changed

8 files changed

+165
-177
lines changed

acceptance/openstack/networking/v1/helpers.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ func CreateNetwork(t *testing.T, prefix, az string) *subnets.Subnet {
2323
CIDR: "192.168.0.0/16",
2424
}).Extract()
2525
th.AssertNoErr(t, err)
26+
enableDHCP := true
2627
subnet, err := subnets.Create(client, subnets.CreateOpts{
2728
Name: tools.RandomString(prefix, 4),
2829
CIDR: "192.168.0.0/24",
29-
DnsList: []string{"1.1.1.1", "8.8.8.8"},
30+
DNSList: []string{"1.1.1.1", "8.8.8.8"},
3031
GatewayIP: "192.168.0.1",
31-
EnableDHCP: true,
32+
EnableDHCP: &enableDHCP,
3233
AvailabilityZone: az,
33-
VPC_ID: vpc.ID,
34+
VpcID: vpc.ID,
3435
}).Extract()
3536
th.AssertNoErr(t, err)
3637

@@ -45,12 +46,12 @@ func DeleteNetwork(t *testing.T, subnet *subnets.Subnet) {
4546
client, err := clients.NewNetworkV1Client()
4647
th.AssertNoErr(t, err)
4748

48-
err = subnets.Delete(client, subnet.VPC_ID, subnet.ID).ExtractErr()
49+
err = subnets.Delete(client, subnet.VpcID, subnet.ID).ExtractErr()
4950
th.AssertNoErr(t, err)
5051
err = waitForSubnetToBeDeleted(client, subnet.ID, 300)
5152
th.AssertNoErr(t, err)
5253

53-
err = vpcs.Delete(client, subnet.VPC_ID).ExtractErr()
54+
err = vpcs.Delete(client, subnet.VpcID).ExtractErr()
5455
th.AssertNoErr(t, err)
5556
}
5657

@@ -127,12 +128,13 @@ func waitForEipToDelete(client *golangsdk.ServiceClient, eipID string, secs int)
127128
}
128129

129130
func createSubnet(t *testing.T, client *golangsdk.ServiceClient, vpcID string) *subnets.Subnet {
131+
enableDHCP := true
130132
createSubnetOpts := subnets.CreateOpts{
131133
Name: tools.RandomString("acc-subnet-", 3),
132134
CIDR: "192.168.20.0/24",
133135
GatewayIP: "192.168.20.1",
134-
EnableDHCP: true,
135-
VPC_ID: vpcID,
136+
EnableDHCP: &enableDHCP,
137+
VpcID: vpcID,
136138
}
137139
t.Logf("Attempting to create subnet: %s", createSubnetOpts.Name)
138140

@@ -143,7 +145,7 @@ func createSubnet(t *testing.T, client *golangsdk.ServiceClient, vpcID string) *
143145
t.Logf("Waitting for subnet %s to be active", subnet.ID)
144146
err = waitForSubnetToBeActive(client, subnet.ID, 600)
145147
th.AssertNoErr(t, err)
146-
t.Logf("Created subnet: %v", subnet)
148+
t.Logf("Created subnet: %v", subnet.ID)
147149

148150
return subnet
149151
}

acceptance/openstack/networking/v1/subnet_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestSubnetsLifecycle(t *testing.T) {
2727
defer deleteVpc(t, client, vpc.ID)
2828

2929
subnet := createSubnet(t, client, vpc.ID)
30-
defer deleteSubnet(t, client, subnet.VPC_ID, subnet.ID)
30+
defer deleteSubnet(t, client, subnet.VpcID, subnet.ID)
3131

3232
tools.PrintResource(t, subnet)
3333

@@ -36,12 +36,11 @@ func TestSubnetsLifecycle(t *testing.T) {
3636
Name: tools.RandomString("acc-subnet-", 3),
3737
}
3838
t.Logf("Attempting to update name of subnet to %s", updateOpts.Name)
39-
_, err = subnets.Update(client, subnet.VPC_ID, subnet.ID, updateOpts).Extract()
39+
_, err = subnets.Update(client, subnet.VpcID, subnet.ID, updateOpts).Extract()
4040
th.AssertNoErr(t, err)
4141

4242
// Query a subnet
4343
newSubnet, err := subnets.Get(client, subnet.ID).Extract()
4444
th.AssertNoErr(t, err)
45-
46-
tools.PrintResource(t, newSubnet)
45+
th.AssertEquals(t, updateOpts.Name, newSubnet.Name)
4746
}

openstack/networking/v1/subnets/doc.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
Package Subnets enables management and retrieval of Subnets
33
4-
Example to List Vpcs
4+
Example to List VPCs
55
66
listOpts := subnets.ListOpts{}
77
allSubnets, err := subnets.List(subnetClient, listOpts)
@@ -16,14 +16,13 @@ Example to List Vpcs
1616
Example to Create a Vpc
1717
1818
createOpts := subnets.CreateOpts{
19-
Name: "test_subnets",
20-
CIDR: "192.168.0.0/16"
21-
GatewayIP: "192.168.0.1"
22-
PRIMARY_DNS: "8.8.8.8"
23-
SECONDARY_DNS: "8.8.4.4"
24-
AvailabilityZone:"eu-de-02"
25-
VPC_ID:"3b9740a0-b44d-48f0-84ee-42eb166e54f7"
26-
19+
Name: "test_subnets",
20+
CIDR: "192.168.0.0/16"
21+
GatewayIP: "192.168.0.1"
22+
PrimaryDNS: "8.8.8.8"
23+
SecondaryDNS: "8.8.4.4"
24+
AvailabilityZone: "eu-de-02"
25+
VpcID: "3b9740a0-b44d-48f0-84ee-42eb166e54f7"
2726
}
2827
vpc, err := subnets.Create(subnetClient, createOpts).Extract()
2928
@@ -36,7 +35,7 @@ Example to Update a Vpc
3635
subnetID := "4e8e5957-649f-477b-9e5b-f1f75b21c03c"
3736
3837
updateOpts := subnets.UpdateOpts{
39-
Name: "testsubnet",
38+
Name: "testsubnet",
4039
}
4140
4241
subnet, err := subnets.Update(subnetClient, subnetID, updateOpts).Extract()
@@ -54,4 +53,5 @@ Example to Delete a Vpc
5453
panic(err)
5554
}
5655
*/
56+
5757
package subnets
Lines changed: 60 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package subnets
22

33
import (
4+
"encoding/json"
45
"reflect"
56

67
"github.com/opentelekomcloud/gophertelekomcloud"
@@ -12,35 +13,34 @@ import (
1213
// the floating IP attributes you want to see returned. SortKey allows you to
1314
// sort by a particular network attribute. SortDir sets the direction, and is
1415
// either `asc' or `desc'. Marker and Limit are used for pagination.
15-
1616
type ListOpts struct {
1717
// ID is the unique identifier for the subnet.
18-
ID string `json:"id"`
18+
ID string `json:"id,omitempty"`
1919

2020
// Name is the human readable name for the subnet. It does not have to be
2121
// unique.
22-
Name string `json:"name"`
22+
Name string `json:"name,omitempty"`
2323

2424
// Specifies the network segment on which the subnet resides.
25-
CIDR string `json:"cidr"`
25+
CIDR string `json:"cidr,omitempty"`
2626

2727
// Status indicates whether or not a subnet is currently operational.
28-
Status string `json:"status"`
28+
Status string `json:"status,omitempty"`
2929

3030
// Specifies the gateway of the subnet.
31-
GatewayIP string `json:"gateway_ip"`
31+
GatewayIP string `json:"gateway_ip,omitempty"`
3232

3333
// Specifies the IP address of DNS server 1 on the subnet.
34-
PRIMARY_DNS string `json:"primary_dns"`
34+
PrimaryDNS string `json:"primary_dns,omitempty"`
3535

3636
// Specifies the IP address of DNS server 2 on the subnet.
37-
SECONDARY_DNS string `json:"secondary_dns"`
37+
SecondaryDNS string `json:"secondary_dns,omitempty"`
3838

3939
// Identifies the availability zone (AZ) to which the subnet belongs.
40-
AvailabilityZone string `json:"availability_zone"`
40+
AvailabilityZone string `json:"availability_zone,omitempty"`
4141

4242
// Specifies the ID of the VPC to which the subnet belongs.
43-
VPC_ID string `json:"vpc_id"`
43+
VpcID string `json:"vpc_id,omitempty"`
4444
}
4545

4646
// List returns collection of
@@ -49,10 +49,9 @@ type ListOpts struct {
4949
//
5050
// Default policy settings return only those subnets that are owned by the
5151
// tenant who submits the request, unless an admin user submits the request.
52-
53-
func List(c *golangsdk.ServiceClient, opts ListOpts) ([]Subnet, error) {
54-
u := rootURL(c)
55-
pages, err := pagination.NewPager(c, u, func(r pagination.PageResult) pagination.Page {
52+
func List(client *golangsdk.ServiceClient, opts ListOpts) ([]Subnet, error) {
53+
url := rootURL(client)
54+
pages, err := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
5655
return SubnetPage{pagination.LinkedPageBase{PageResult: r}}
5756
}).AllPages()
5857
if err != nil {
@@ -68,61 +67,38 @@ func List(c *golangsdk.ServiceClient, opts ListOpts) ([]Subnet, error) {
6867
}
6968

7069
func FilterSubnets(subnets []Subnet, opts ListOpts) ([]Subnet, error) {
71-
72-
var refinedSubnets []Subnet
73-
var matched bool
74-
m := map[string]interface{}{}
75-
76-
if opts.ID != "" {
77-
m["ID"] = opts.ID
78-
}
79-
if opts.Name != "" {
80-
m["Name"] = opts.Name
81-
}
82-
if opts.CIDR != "" {
83-
m["CIDR"] = opts.CIDR
84-
}
85-
if opts.Status != "" {
86-
m["Status"] = opts.Status
87-
}
88-
if opts.GatewayIP != "" {
89-
m["GatewayIP"] = opts.GatewayIP
90-
}
91-
if opts.PRIMARY_DNS != "" {
92-
m["PRIMARY_DNS"] = opts.PRIMARY_DNS
93-
}
94-
if opts.SECONDARY_DNS != "" {
95-
m["SECONDARY_DNS"] = opts.SECONDARY_DNS
96-
}
97-
if opts.AvailabilityZone != "" {
98-
m["AvailabilityZone"] = opts.AvailabilityZone
70+
matchOptsByte, err := json.Marshal(opts)
71+
if err != nil {
72+
return nil, err
9973
}
100-
if opts.VPC_ID != "" {
101-
m["VPC_ID"] = opts.VPC_ID
74+
var matchOpts map[string]interface{}
75+
err = json.Unmarshal(matchOptsByte, &matchOpts)
76+
if err != nil {
77+
return nil, err
10278
}
10379

104-
if len(m) > 0 && len(subnets) > 0 {
105-
for _, subnet := range subnets {
106-
matched = true
107-
108-
for key, value := range m {
109-
if sVal := getStructField(&subnet, key); !(sVal == value) {
110-
matched = false
111-
}
112-
}
80+
if len(matchOpts) == 0 {
81+
return subnets, nil
82+
}
11383

114-
if matched {
115-
refinedSubnets = append(refinedSubnets, subnet)
116-
}
84+
var refinedSubnets []Subnet
85+
for _, subnet := range subnets {
86+
if subnetMatchesFilter(&subnet, matchOpts) {
87+
refinedSubnets = append(refinedSubnets, subnet)
11788
}
118-
119-
} else {
120-
refinedSubnets = subnets
12189
}
122-
12390
return refinedSubnets, nil
12491
}
12592

93+
func subnetMatchesFilter(subnet *Subnet, filter map[string]interface{}) bool {
94+
for key, expectedValue := range filter {
95+
if getStructField(subnet, key) != expectedValue {
96+
return false
97+
}
98+
}
99+
return true
100+
}
101+
126102
func getStructField(v *Subnet, field string) string {
127103
r := reflect.ValueOf(v)
128104
f := reflect.Indirect(r).FieldByName(field)
@@ -139,18 +115,19 @@ type CreateOptsBuilder interface {
139115
// no required values.
140116
type CreateOpts struct {
141117
Name string `json:"name" required:"true"`
118+
Description string `json:"description,omitempty"`
142119
CIDR string `json:"cidr" required:"true"`
143-
DnsList []string `json:"dnsList,omitempty"`
120+
DNSList []string `json:"dnsList,omitempty"`
144121
GatewayIP string `json:"gateway_ip" required:"true"`
145-
EnableDHCP bool `json:"dhcp_enable" no_default:"y"`
146-
PRIMARY_DNS string `json:"primary_dns,omitempty"`
147-
SECONDARY_DNS string `json:"secondary_dns,omitempty"`
122+
EnableDHCP *bool `json:"dhcp_enable,omitempty"`
123+
PrimaryDNS string `json:"primary_dns,omitempty"`
124+
SecondaryDNS string `json:"secondary_dns,omitempty"`
148125
AvailabilityZone string `json:"availability_zone,omitempty"`
149-
VPC_ID string `json:"vpc_id" required:"true"`
150-
ExtraDhcpOpts []ExtraDhcpOpt `json:"extra_dhcp_opts,omitempty"`
126+
VpcID string `json:"vpc_id" required:"true"`
127+
ExtraDHCPOpts []ExtraDHCPOpt `json:"extra_dhcp_opts,omitempty"`
151128
}
152129

153-
type ExtraDhcpOpt struct {
130+
type ExtraDHCPOpt struct {
154131
OptName string `json:"opt_name" required:"true"`
155132
OptValue string `json:"opt_value,omitempty"`
156133
}
@@ -163,39 +140,39 @@ func (opts CreateOpts) ToSubnetCreateMap() (map[string]interface{}, error) {
163140
// Create accepts a CreateOpts struct and uses the values to create a new
164141
// logical subnets. When it is created, the subnets does not have an internal
165142
// interface - it is not associated to any subnet.
166-
//
167-
func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
143+
func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
168144
b, err := opts.ToSubnetCreateMap()
169145
if err != nil {
170146
r.Err = err
171147
return
172148
}
173-
reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
174-
_, r.Err = c.Post(rootURL(c), b, &r.Body, reqOpt)
149+
_, r.Err = client.Post(rootURL(client), b, &r.Body, &golangsdk.RequestOpts{
150+
OkCodes: []int{200},
151+
})
175152
return
176153
}
177154

178155
// Get retrieves a particular subnets based on its unique ID.
179-
func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
180-
_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
156+
func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
157+
_, r.Err = client.Get(resourceURL(client, id), &r.Body, nil)
181158
return
182159
}
183160

184161
// UpdateOptsBuilder allows extensions to add additional parameters to the
185162
// Update request.
186163
type UpdateOptsBuilder interface {
187-
// ToSubnetUpdateMap() (map[string]interface{}, error)
188164
ToSubnetUpdateMap() (map[string]interface{}, error)
189165
}
190166

191167
// UpdateOpts contains the values used when updating a subnets.
192168
type UpdateOpts struct {
193169
Name string `json:"name,omitempty"`
194-
EnableDHCP bool `json:"dhcp_enable"`
195-
PRIMARY_DNS string `json:"primary_dns,omitempty"`
196-
SECONDARY_DNS string `json:"secondary_dns,omitempty"`
197-
DnsList []string `json:"dnsList,omitempty"`
198-
ExtraDhcpOpts []ExtraDhcpOpt `json:"extra_dhcp_opts,omitempty"`
170+
Description string `json:"description,omitempty"`
171+
EnableDHCP *bool `json:"dhcp_enable,omitempty"`
172+
PrimaryDNS string `json:"primary_dns,omitempty"`
173+
SecondaryDNS string `json:"secondary_dns,omitempty"`
174+
DNSList []string `json:"dnsList,omitempty"`
175+
ExtraDhcpOpts []ExtraDHCPOpt `json:"extra_dhcp_opts,omitempty"`
199176
}
200177

201178
// ToSubnetUpdateMap builds an update body based on UpdateOpts.
@@ -205,20 +182,20 @@ func (opts UpdateOpts) ToSubnetUpdateMap() (map[string]interface{}, error) {
205182

206183
// Update allows subnets to be updated. You can update the name, administrative
207184
// state, and the external gateway.
208-
func Update(c *golangsdk.ServiceClient, vpcid string, id string, opts UpdateOptsBuilder) (r UpdateResult) {
185+
func Update(client *golangsdk.ServiceClient, vpcID string, id string, opts UpdateOptsBuilder) (r UpdateResult) {
209186
b, err := opts.ToSubnetUpdateMap()
210187
if err != nil {
211188
r.Err = err
212189
return
213190
}
214-
_, r.Err = c.Put(updateURL(c, vpcid, id), b, &r.Body, &golangsdk.RequestOpts{
191+
_, r.Err = client.Put(updateURL(client, vpcID, id), b, &r.Body, &golangsdk.RequestOpts{
215192
OkCodes: []int{200},
216193
})
217194
return
218195
}
219196

220197
// Delete will permanently delete a particular subnets based on its unique ID.
221-
func Delete(c *golangsdk.ServiceClient, vpcid string, id string) (r DeleteResult) {
222-
_, r.Err = c.Delete(updateURL(c, vpcid, id), nil)
198+
func Delete(client *golangsdk.ServiceClient, vpcID string, id string) (r DeleteResult) {
199+
_, r.Err = client.Delete(updateURL(client, vpcID, id), nil)
223200
return
224201
}

0 commit comments

Comments
 (0)