This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
requests.go
84 lines (68 loc) · 1.93 KB
/
requests.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package nics
import (
"reflect"
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/pagination"
)
// ListOpts allows the filtering and sorting of paginated collections through
// the API.
type ListOpts struct {
// ID is the unique identifier for the nic.
ID string `json:"port_id"`
// Status indicates whether or not a nic is currently operational.
Status string `json:"port_state"`
}
// List returns collection of nics. It accepts a ListOpts struct, which allows you to filter and sort
// the returned collection for greater efficiency.
func List(c *golangsdk.ServiceClient, serverId string, opts ListOpts) ([]Nic, error) {
u := listURL(c, serverId)
pages, err := pagination.NewPager(c, u, func(r pagination.PageResult) pagination.Page {
return NicPage{pagination.LinkedPageBase{PageResult: r}}
}).AllPages()
if err != nil {
return nil, err
}
allNICs, err := ExtractNics(pages)
if err != nil {
return nil, err
}
return FilterNICs(allNICs, opts)
}
//FilterNICs used to filter nics using id and status.
func FilterNICs(nics []Nic, opts ListOpts) ([]Nic, error) {
var refinedNICs []Nic
var matched bool
m := map[string]interface{}{}
if opts.ID != "" {
m["ID"] = opts.ID
}
if opts.Status != "" {
m["Status"] = opts.Status
}
if len(m) > 0 && len(nics) > 0 {
for _, nic := range nics {
matched = true
for key, value := range m {
if sVal := getStructField(&nic, key); !(sVal == value) {
matched = false
}
}
if matched {
refinedNICs = append(refinedNICs, nic)
}
}
} else {
refinedNICs = nics
}
return refinedNICs, nil
}
func getStructField(v *Nic, field string) string {
r := reflect.ValueOf(v)
f := reflect.Indirect(r).FieldByName(field)
return string(f.String())
}
// Get retrieves a particular nic based on its unique ID.
func Get(c *golangsdk.ServiceClient, serverId string, id string) (r GetResult) {
_, r.Err = c.Get(getURL(c, serverId, id), &r.Body, nil)
return
}