|
| 1 | +package query |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/url" |
| 6 | +) |
| 7 | + |
| 8 | +func New() *ListOpts { |
| 9 | + return &ListOpts{} |
| 10 | +} |
| 11 | + |
| 12 | +// ListOpts can be used to list multiple resources. |
| 13 | +type ListOpts struct { |
| 14 | + query url.Values |
| 15 | +} |
| 16 | + |
| 17 | +// And adds an arbitrary number of permutations of a single property to filter |
| 18 | +// in. When a single ListOpts is called multiple times with the same property |
| 19 | +// name, the resulting query contains the resulting intersection (AND). Note |
| 20 | +// that how these properties are combined in OpenStack depend on the property. |
| 21 | +// For example: passing multiple "id" behaves like an OR. Instead, passing |
| 22 | +// multiple "tags" will only return resources that have ALL those tags. This |
| 23 | +// helper function only combines the parameters in the most straightforward |
| 24 | +// way; please refer to the OpenStack documented behaviour to know how these |
| 25 | +// parameters are treated. |
| 26 | +// |
| 27 | +// ListOpts is currently implemented for three Network resources: |
| 28 | +// |
| 29 | +// * ports |
| 30 | +// * networks |
| 31 | +// * subnets |
| 32 | +func (o *ListOpts) And(property string, values ...interface{}) *ListOpts { |
| 33 | + if o.query == nil { |
| 34 | + o.query = url.Values{} |
| 35 | + } |
| 36 | + if existingValues, ok := o.query[property]; ok { |
| 37 | + // There already are values of the same property: we AND them |
| 38 | + // with the new ones. We only keep the values that exist in |
| 39 | + // both `o.query` AND in `values`. |
| 40 | + |
| 41 | + // First, to avoid nested loops, we build a hashmap with the |
| 42 | + // new values. |
| 43 | + newValuesSet := make(map[string]struct{}) |
| 44 | + for _, newValue := range values { |
| 45 | + newValuesSet[fmt.Sprint(newValue)] = struct{}{} |
| 46 | + } |
| 47 | + |
| 48 | + // intersectedValues is a slice which will contain the values |
| 49 | + // that we want to keep. They will be at most as many as what |
| 50 | + // we already have; that's what we set the slice capacity to. |
| 51 | + intersectedValues := make([]string, 0, len(existingValues)) |
| 52 | + |
| 53 | + // We add each existing value to intersectedValues if and only |
| 54 | + // if it's also present in the new set. |
| 55 | + for _, existingValue := range existingValues { |
| 56 | + if _, ok := newValuesSet[existingValue]; ok { |
| 57 | + intersectedValues = append(intersectedValues, existingValue) |
| 58 | + } |
| 59 | + } |
| 60 | + o.query[property] = intersectedValues |
| 61 | + return o |
| 62 | + } |
| 63 | + |
| 64 | + for _, v := range values { |
| 65 | + o.query.Add(property, fmt.Sprint(v)) |
| 66 | + } |
| 67 | + |
| 68 | + return o |
| 69 | +} |
| 70 | + |
| 71 | +func (o ListOpts) String() string { |
| 72 | + return "?" + o.query.Encode() |
| 73 | +} |
| 74 | + |
| 75 | +func (o ListOpts) ToPortListQuery() (string, error) { |
| 76 | + return o.String(), nil |
| 77 | +} |
| 78 | + |
| 79 | +func (o ListOpts) ToNetworkListQuery() (string, error) { |
| 80 | + return o.String(), nil |
| 81 | +} |
| 82 | + |
| 83 | +func (o ListOpts) ToSubnetListQuery() (string, error) { |
| 84 | + return o.String(), nil |
| 85 | +} |
0 commit comments