11package subnets
22
33import (
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-
1616type 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
7069func 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+
126102func 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.
140116type 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.
186163type 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.
192168type 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