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