| 1 |
// A REST client to interact with LINSTOR's REST API |
| 2 |
// Copyright (C) LINBIT HA-Solutions GmbH |
| 3 |
// All Rights Reserved. |
| 4 |
// Author: Roland Kammerer <[email protected]> |
| 5 |
// |
| 6 |
// Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 |
// not use this file except in compliance with the License. You may obtain |
| 8 |
// a copy of the License at |
| 9 |
// |
| 10 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
// |
| 12 |
// Unless required by applicable law or agreed to in writing, software |
| 13 |
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 |
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 |
// License for the specific language governing permissions and limitations |
| 16 |
// under the License. |
| 17 |
|
| 18 |
package client |
| 19 |
|
| 20 |
import ( |
| 21 |
"fmt" |
| 22 |
"net/url" |
| 23 |
"reflect" |
| 24 |
|
| 25 |
"github.com/google/go-querystring/query" |
| 26 |
) |
| 27 |
|
| 28 |
// ListOpts is a struct primarily used to define parameters used for pagination. It is also used for filtering (e.g., the /view/ calls) |
| 29 |
type ListOpts struct { |
| 30 |
// Number of items to skip. Only used if Limit is a positive value |
| 31 |
Offset int `url:"offset"` |
| 32 |
// Maximum number of items to retrieve |
| 33 |
Limit int `url:"limit"` |
| 34 |
// Some responses can be cached controller side, such as snapshot lists |
| 35 |
Cached *bool `url:"cached,omitempty"` |
| 36 |
|
| 37 |
StoragePool []string `url:"storage_pools"` |
| 38 |
Resource []string `url:"resources"` |
| 39 |
Node []string `url:"nodes"` |
| 40 |
Prop []string `url:"props"` |
| 41 |
Snapshots []string `url:"snapshots"` |
| 42 |
Status string `url:"status,omitempty"` |
| 43 |
|
| 44 |
// Content is used in the files API. If true, fetching files will include the content. |
| 45 |
Content bool `url:"content,omitempty"` |
| 46 |
} |
| 47 |
|
| 48 |
func Optional[T any](objs ...*T) (*T, error) { |
| 49 |
switch len(objs) { |
| 50 |
case 0: |
| 51 |
return nil, nil |
| 52 |
case 1: |
| 53 |
return objs[0], nil |
| 54 |
default: |
| 55 |
// Safe to dereference 0 here, we are in the len(objs) > 1 case. |
| 56 |
return nil, fmt.Errorf("expected exactly zero or one arguments %T, got %d", objs[0], len(objs)) |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
func addOptions(s string, opt interface{}) (string, error) { |
| 61 |
v := reflect.ValueOf(opt) |
| 62 |
if v.Kind() == reflect.Pointer && v.IsNil() { |
| 63 |
return s, nil |
| 64 |
} |
| 65 |
|
| 66 |
u, err := url.Parse(s) |
| 67 |
if err != nil { |
| 68 |
return s, err |
| 69 |
} |
| 70 |
|
| 71 |
vs, err := query.Values(opt) |
| 72 |
if err != nil { |
| 73 |
return s, err |
| 74 |
} |
| 75 |
|
| 76 |
u.RawQuery = vs.Encode() |
| 77 |
return u.String(), nil |
| 78 |
} |